fix: logic of auto-detect-interface
This commit is contained in:
parent
62dcfed9c2
commit
086772b721
6 changed files with 30 additions and 22 deletions
|
@ -115,6 +115,7 @@ type Tun struct {
|
||||||
Stack C.TUNStack `yaml:"stack" json:"stack"`
|
Stack C.TUNStack `yaml:"stack" json:"stack"`
|
||||||
DNSHijack []netip.AddrPort `yaml:"dns-hijack" json:"dns-hijack"`
|
DNSHijack []netip.AddrPort `yaml:"dns-hijack" json:"dns-hijack"`
|
||||||
AutoRoute bool `yaml:"auto-route" json:"auto-route"`
|
AutoRoute bool `yaml:"auto-route" json:"auto-route"`
|
||||||
|
AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPTables config
|
// IPTables config
|
||||||
|
@ -915,6 +916,7 @@ func parseTun(rawTun RawTun, general *General) (*Tun, error) {
|
||||||
Stack: rawTun.Stack,
|
Stack: rawTun.Stack,
|
||||||
DNSHijack: dnsHijack,
|
DNSHijack: dnsHijack,
|
||||||
AutoRoute: rawTun.AutoRoute,
|
AutoRoute: rawTun.AutoRoute,
|
||||||
|
AutoDetectInterface: rawTun.AutoDetectInterface,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ func GetAutoDetectInterface() (string, error) {
|
||||||
return cmd.ExecCmd("bash -c route -n get default | grep 'interface:' | awk -F ' ' 'NR==1{print $2}' | xargs echo -n")
|
return cmd.ExecCmd("bash -c route -n get default | grep 'interface:' | awk -F ' ' 'NR==1{print $2}' | xargs echo -n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute bool) error {
|
func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute, autoDetectInterface bool) error {
|
||||||
if !addr.Addr().Is4() {
|
if !addr.Addr().Is4() {
|
||||||
return fmt.Errorf("supported ipv4 only")
|
return fmt.Errorf("supported ipv4 only")
|
||||||
}
|
}
|
||||||
|
@ -37,12 +37,12 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
|
||||||
}
|
}
|
||||||
|
|
||||||
if autoRoute {
|
if autoRoute {
|
||||||
err = configInterfaceRouting(interfaceName, addr)
|
err = configInterfaceRouting(interfaceName, addr, autoDetectInterface)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
func configInterfaceRouting(interfaceName string, addr netip.Prefix, autoDetectInterface bool) error {
|
||||||
var (
|
var (
|
||||||
routes = append(defaultRoutes, addr.String())
|
routes = append(defaultRoutes, addr.String())
|
||||||
gateway = addr.Masked().Addr().Next()
|
gateway = addr.Masked().Addr().Next()
|
||||||
|
@ -54,7 +54,9 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if autoDetectInterface {
|
||||||
go DefaultInterfaceChangeMonitor()
|
go DefaultInterfaceChangeMonitor()
|
||||||
|
}
|
||||||
|
|
||||||
return execRouterCmd("add", "-inet6", "2000::/3", interfaceName)
|
return execRouterCmd("add", "-inet6", "2000::/3", interfaceName)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ func GetAutoDetectInterface() (string, error) {
|
||||||
return cmd.ExecCmd("bash -c ip route show | grep 'default via' | awk -F ' ' 'NR==1{print $5}' | xargs echo -n")
|
return cmd.ExecCmd("bash -c ip route show | grep 'default via' | awk -F ' ' 'NR==1{print $5}' | xargs echo -n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute bool) error {
|
func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute, autoDetectInterface bool) error {
|
||||||
var (
|
var (
|
||||||
interfaceName = dev.Name()
|
interfaceName = dev.Name()
|
||||||
ip = addr.Masked().Addr().Next()
|
ip = addr.Masked().Addr().Next()
|
||||||
|
@ -34,12 +34,12 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
|
||||||
}
|
}
|
||||||
|
|
||||||
if autoRoute {
|
if autoRoute {
|
||||||
err = configInterfaceRouting(interfaceName, addr)
|
err = configInterfaceRouting(interfaceName, addr, autoDetectInterface)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
func configInterfaceRouting(interfaceName string, addr netip.Prefix, autoDetectInterface bool) error {
|
||||||
linkIP := addr.Masked().Addr().Next()
|
linkIP := addr.Masked().Addr().Next()
|
||||||
if runtime.GOOS == "android" {
|
if runtime.GOOS == "android" {
|
||||||
const tableId = 1981801
|
const tableId = 1981801
|
||||||
|
@ -61,7 +61,9 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if autoDetectInterface {
|
||||||
go DefaultInterfaceChangeMonitor()
|
go DefaultInterfaceChangeMonitor()
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,6 @@ func GetAutoDetectInterface() (string, error) {
|
||||||
return "", fmt.Errorf("can not auto detect interface-name on this OS: %s, you must be detecting interface-name by manual", runtime.GOOS)
|
return "", fmt.Errorf("can not auto detect interface-name on this OS: %s, you must be detecting interface-name by manual", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigInterfaceAddress(device.Device, netip.Prefix, int, bool) error {
|
func ConfigInterfaceAddress(device.Device, netip.Prefix, int, bool, bool) error {
|
||||||
return fmt.Errorf("unsupported on this OS: %s", runtime.GOOS)
|
return fmt.Errorf("unsupported on this OS: %s", runtime.GOOS)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func GetAutoDetectInterface() (string, error) {
|
||||||
return getAutoDetectInterfaceByFamily(winipcfg.AddressFamily(windows.AF_INET6))
|
return getAutoDetectInterfaceByFamily(winipcfg.AddressFamily(windows.AF_INET6))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute bool) error {
|
func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int, autoRoute, autoDetectInterface bool) error {
|
||||||
retryOnFailure := services.StartedAtBoot()
|
retryOnFailure := services.StartedAtBoot()
|
||||||
tryTimes := 0
|
tryTimes := 0
|
||||||
var err error
|
var err error
|
||||||
|
@ -205,7 +205,9 @@ startOver:
|
||||||
|
|
||||||
wintunInterfaceName = dev.Name()
|
wintunInterfaceName = dev.Name()
|
||||||
|
|
||||||
|
if autoDetectInterface {
|
||||||
go DefaultInterfaceChangeMonitor()
|
go DefaultInterfaceChangeMonitor()
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,7 @@ func New(tunConf *config.Tun, dnsConf *config.DNS, tcpIn chan<- C.ConnContext, u
|
||||||
}
|
}
|
||||||
|
|
||||||
// setting address and routing
|
// setting address and routing
|
||||||
err = commons.ConfigInterfaceAddress(tunDevice, tunAddress, mtu, autoRoute)
|
err = commons.ConfigInterfaceAddress(tunDevice, tunAddress, mtu, autoRoute, tunConf.AutoDetectInterface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = tunDevice.Close()
|
_ = tunDevice.Close()
|
||||||
return nil, fmt.Errorf("setting interface address and routing failed: %w", err)
|
return nil, fmt.Errorf("setting interface address and routing failed: %w", err)
|
||||||
|
|
Loading…
Reference in a new issue