From b5623602f548c9e6227af0b5649ce7b7f786c243 Mon Sep 17 00:00:00 2001 From: adlyq Date: Wed, 18 May 2022 12:00:57 +0800 Subject: [PATCH] chore: Android auto-detect-interface plus --- adapter/outboundgroup/groupbase.go | 8 +++--- config/config.go | 2 +- listener/tun/ipstack/commons/router.go | 4 +-- .../tun/ipstack/commons/router_android.go | 25 ++++++------------- listener/tun/ipstack/commons/router_darwin.go | 4 +-- listener/tun/ipstack/commons/router_linux.go | 4 +-- listener/tun/ipstack/commons/router_others.go | 2 +- .../tun/ipstack/commons/router_windows.go | 4 +-- 8 files changed, 20 insertions(+), 33 deletions(-) diff --git a/adapter/outboundgroup/groupbase.go b/adapter/outboundgroup/groupbase.go index 66529d88..a03b24f9 100644 --- a/adapter/outboundgroup/groupbase.go +++ b/adapter/outboundgroup/groupbase.go @@ -112,20 +112,18 @@ func (gb *GroupBase) onDialFailed() { go func() { gb.failedTestMux.Lock() - defer func() { - gb.failedTestMux.Unlock() - }() + defer gb.failedTestMux.Unlock() gb.failedTimes++ if gb.failedTimes == 1 { - log.Warnln("%s first failed", gb.Name()) + log.Warnln("ProxyGroup: %s first failed", gb.Name()) gb.failedTime = time.Now() } else { if time.Since(gb.failedTime) > gb.failedTimeoutInterval() { return } - log.Warnln("%s failed count: %d", gb.Name(), gb.failedTimes) + log.Warnln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes) if gb.failedTimes >= gb.maxFailedTimes() { gb.failedTesting.Store(true) log.Warnln("because %s failed multiple times, active health check", gb.Name()) diff --git a/config/config.go b/config/config.go index 1f9423ff..08f48863 100644 --- a/config/config.go +++ b/config/config.go @@ -867,7 +867,7 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser { func parseTun(rawTun RawTun, general *General) (*Tun, error) { if rawTun.Enable && rawTun.AutoDetectInterface { - autoDetectInterfaceName, err := commons.GetAutoDetectInterface(rawTun.Device) + autoDetectInterfaceName, err := commons.GetAutoDetectInterface() if err != nil { log.Warnln("Can not find auto detect interface.[%s]", err) } else { diff --git a/listener/tun/ipstack/commons/router.go b/listener/tun/ipstack/commons/router.go index 83a8a794..5d83b70e 100644 --- a/listener/tun/ipstack/commons/router.go +++ b/listener/tun/ipstack/commons/router.go @@ -23,14 +23,14 @@ func ipv4MaskString(bits int) string { return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]) } -func DefaultInterfaceChangeMonitor(tunName string) { +func DefaultInterfaceChangeMonitor() { t := time.NewTicker(defaultInterfaceMonitorDuration) defer t.Stop() for { <-t.C - interfaceName, err := GetAutoDetectInterface(tunName) + interfaceName, err := GetAutoDetectInterface() if err != nil { log.Warnln("[TUN] default interface monitor exited, cause: %v", err) continue diff --git a/listener/tun/ipstack/commons/router_android.go b/listener/tun/ipstack/commons/router_android.go index 8e95e327..c24ed5a9 100644 --- a/listener/tun/ipstack/commons/router_android.go +++ b/listener/tun/ipstack/commons/router_android.go @@ -10,27 +10,16 @@ import ( "strings" ) -func GetAutoDetectInterface(tunName string) (ifn string, err error) { - cmdRes, err := cmd.ExecCmd("ip route show") - if err != nil { - return - } +func GetAutoDetectInterface() (ifn string, err error) { + cmdRes, err := cmd.ExecCmd("ip route get 1.1.1.1 uid 4294967295") - for _, route := range strings.Split(cmdRes, "\n") { - rs := strings.Split(route, " ") - if len(rs) > 2 { - if rs[2] == tunName { - continue - } - ifn = rs[2] - if ifn == "wlan0" { - return - } - } + sps := strings.Split(cmdRes, " ") + if len(sps) > 4 { + ifn = sps[4] } if ifn == "" { - return "", fmt.Errorf("interface not found") + err = fmt.Errorf("interface not found") } return } @@ -77,7 +66,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix, autoDetectI execAddRuleCmd(fmt.Sprintf("not from all iif lo lookup %d pref 9004", tableId)) if autoDetectInterface { - go DefaultInterfaceChangeMonitor(interfaceName) + go DefaultInterfaceChangeMonitor() } return nil diff --git a/listener/tun/ipstack/commons/router_darwin.go b/listener/tun/ipstack/commons/router_darwin.go index da81526c..abead15a 100644 --- a/listener/tun/ipstack/commons/router_darwin.go +++ b/listener/tun/ipstack/commons/router_darwin.go @@ -8,7 +8,7 @@ import ( "github.com/Dreamacro/clash/listener/tun/device" ) -func GetAutoDetectInterface(string) (string, error) { +func GetAutoDetectInterface() (string, error) { return cmd.ExecCmd("bash -c route -n get default | grep 'interface:' | awk -F ' ' 'NR==1{print $2}' | xargs echo -n") } @@ -55,7 +55,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix, autoDetectI } if autoDetectInterface { - go DefaultInterfaceChangeMonitor(interfaceName) + go DefaultInterfaceChangeMonitor() } return execRouterCmd("add", "-inet6", "2000::/3", interfaceName) diff --git a/listener/tun/ipstack/commons/router_linux.go b/listener/tun/ipstack/commons/router_linux.go index 99787704..4a11f99f 100644 --- a/listener/tun/ipstack/commons/router_linux.go +++ b/listener/tun/ipstack/commons/router_linux.go @@ -9,7 +9,7 @@ import ( "net/netip" ) -func GetAutoDetectInterface(string) (string, error) { +func GetAutoDetectInterface() (string, error) { return cmd.ExecCmd("bash -c ip route show | grep 'default via' | awk -F ' ' 'NR==1{print $5}' | xargs echo -n") } @@ -47,7 +47,7 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix, autoDetectI } if autoDetectInterface { - go DefaultInterfaceChangeMonitor(interfaceName) + go DefaultInterfaceChangeMonitor() } return nil diff --git a/listener/tun/ipstack/commons/router_others.go b/listener/tun/ipstack/commons/router_others.go index a26952e6..cec981cc 100644 --- a/listener/tun/ipstack/commons/router_others.go +++ b/listener/tun/ipstack/commons/router_others.go @@ -10,7 +10,7 @@ import ( "github.com/Dreamacro/clash/listener/tun/device" ) -func GetAutoDetectInterface(string) (string, error) { +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) } diff --git a/listener/tun/ipstack/commons/router_windows.go b/listener/tun/ipstack/commons/router_windows.go index 4feb9db6..b9d1a68c 100644 --- a/listener/tun/ipstack/commons/router_windows.go +++ b/listener/tun/ipstack/commons/router_windows.go @@ -18,7 +18,7 @@ import ( var wintunInterfaceName string -func GetAutoDetectInterface(string) (string, error) { +func GetAutoDetectInterface() (string, error) { ifname, err := getAutoDetectInterfaceByFamily(winipcfg.AddressFamily(windows.AF_INET)) if err == nil { return ifname, err @@ -206,7 +206,7 @@ startOver: wintunInterfaceName = dev.Name() if autoDetectInterface { - go DefaultInterfaceChangeMonitor(dev.Name()) + go DefaultInterfaceChangeMonitor() } return nil