chore: Android auto-detect-interface plus

This commit is contained in:
gVisor bot 2022-05-18 12:00:57 +08:00
parent 109e4b25cc
commit 18ee3ceac5
8 changed files with 20 additions and 33 deletions

View file

@ -112,20 +112,18 @@ func (gb *GroupBase) onDialFailed() {
go func() { go func() {
gb.failedTestMux.Lock() gb.failedTestMux.Lock()
defer func() { defer gb.failedTestMux.Unlock()
gb.failedTestMux.Unlock()
}()
gb.failedTimes++ gb.failedTimes++
if gb.failedTimes == 1 { if gb.failedTimes == 1 {
log.Warnln("%s first failed", gb.Name()) log.Warnln("ProxyGroup: %s first failed", gb.Name())
gb.failedTime = time.Now() gb.failedTime = time.Now()
} else { } else {
if time.Since(gb.failedTime) > gb.failedTimeoutInterval() { if time.Since(gb.failedTime) > gb.failedTimeoutInterval() {
return 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() { if gb.failedTimes >= gb.maxFailedTimes() {
gb.failedTesting.Store(true) gb.failedTesting.Store(true)
log.Warnln("because %s failed multiple times, active health check", gb.Name()) log.Warnln("because %s failed multiple times, active health check", gb.Name())

View file

@ -867,7 +867,7 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
func parseTun(rawTun RawTun, general *General) (*Tun, error) { func parseTun(rawTun RawTun, general *General) (*Tun, error) {
if rawTun.Enable && rawTun.AutoDetectInterface { if rawTun.Enable && rawTun.AutoDetectInterface {
autoDetectInterfaceName, err := commons.GetAutoDetectInterface(rawTun.Device) autoDetectInterfaceName, err := commons.GetAutoDetectInterface()
if err != nil { if err != nil {
log.Warnln("Can not find auto detect interface.[%s]", err) log.Warnln("Can not find auto detect interface.[%s]", err)
} else { } else {

View file

@ -23,14 +23,14 @@ func ipv4MaskString(bits int) string {
return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]) 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) t := time.NewTicker(defaultInterfaceMonitorDuration)
defer t.Stop() defer t.Stop()
for { for {
<-t.C <-t.C
interfaceName, err := GetAutoDetectInterface(tunName) interfaceName, err := GetAutoDetectInterface()
if err != nil { if err != nil {
log.Warnln("[TUN] default interface monitor exited, cause: %v", err) log.Warnln("[TUN] default interface monitor exited, cause: %v", err)
continue continue

View file

@ -10,27 +10,16 @@ import (
"strings" "strings"
) )
func GetAutoDetectInterface(tunName string) (ifn string, err error) { func GetAutoDetectInterface() (ifn string, err error) {
cmdRes, err := cmd.ExecCmd("ip route show") cmdRes, err := cmd.ExecCmd("ip route get 1.1.1.1 uid 4294967295")
if err != nil {
return
}
for _, route := range strings.Split(cmdRes, "\n") { sps := strings.Split(cmdRes, " ")
rs := strings.Split(route, " ") if len(sps) > 4 {
if len(rs) > 2 { ifn = sps[4]
if rs[2] == tunName {
continue
}
ifn = rs[2]
if ifn == "wlan0" {
return
}
}
} }
if ifn == "" { if ifn == "" {
return "", fmt.Errorf("interface not found") err = fmt.Errorf("interface not found")
} }
return 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)) execAddRuleCmd(fmt.Sprintf("not from all iif lo lookup %d pref 9004", tableId))
if autoDetectInterface { if autoDetectInterface {
go DefaultInterfaceChangeMonitor(interfaceName) go DefaultInterfaceChangeMonitor()
} }
return nil return nil

View file

@ -8,7 +8,7 @@ import (
"github.com/Dreamacro/clash/listener/tun/device" "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") 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 { if autoDetectInterface {
go DefaultInterfaceChangeMonitor(interfaceName) go DefaultInterfaceChangeMonitor()
} }
return execRouterCmd("add", "-inet6", "2000::/3", interfaceName) return execRouterCmd("add", "-inet6", "2000::/3", interfaceName)

View file

@ -9,7 +9,7 @@ import (
"net/netip" "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") 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 { if autoDetectInterface {
go DefaultInterfaceChangeMonitor(interfaceName) go DefaultInterfaceChangeMonitor()
} }
return nil return nil

View file

@ -10,7 +10,7 @@ import (
"github.com/Dreamacro/clash/listener/tun/device" "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) return "", fmt.Errorf("can not auto detect interface-name on this OS: %s, you must be detecting interface-name by manual", runtime.GOOS)
} }

View file

@ -18,7 +18,7 @@ import (
var wintunInterfaceName string var wintunInterfaceName string
func GetAutoDetectInterface(string) (string, error) { func GetAutoDetectInterface() (string, error) {
ifname, err := getAutoDetectInterfaceByFamily(winipcfg.AddressFamily(windows.AF_INET)) ifname, err := getAutoDetectInterfaceByFamily(winipcfg.AddressFamily(windows.AF_INET))
if err == nil { if err == nil {
return ifname, err return ifname, err
@ -206,7 +206,7 @@ startOver:
wintunInterfaceName = dev.Name() wintunInterfaceName = dev.Name()
if autoDetectInterface { if autoDetectInterface {
go DefaultInterfaceChangeMonitor(dev.Name()) go DefaultInterfaceChangeMonitor()
} }
return nil return nil