fix: don't set auto detect interface with tun name

This commit is contained in:
gVisor bot 2022-10-07 16:54:08 +08:00
parent 1bd6a4c4c2
commit a73f1f0b37
2 changed files with 25 additions and 18 deletions

View file

@ -373,8 +373,8 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
Device: "", Device: "",
Stack: "gvisor", Stack: "gvisor",
DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query
AutoRoute: false, AutoRoute: true,
AutoDetectInterface: false, AutoDetectInterface: true,
Inet6Address: []ListenPrefix{ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))}, Inet6Address: []ListenPrefix{ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))},
}, },
EBpf: EBpf{ EBpf: EBpf{

View file

@ -142,28 +142,35 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
} }
l.networkUpdateMonitor = networkUpdateMonitor l.networkUpdateMonitor = networkUpdateMonitor
defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, tun.DefaultInterfaceMonitorOptions{}) defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, tun.DefaultInterfaceMonitorOptions{OverrideAndroidVPN: true})
if err != nil { if err != nil {
err = E.Cause(err, "create DefaultInterfaceMonitor") err = E.Cause(err, "create DefaultInterfaceMonitor")
return return
} }
defaultInterfaceMonitor.RegisterCallback(func(event int) error { if options.AutoDetectInterface {
targetInterface := dialer.DefaultInterface.Load() defaultInterfaceMonitor.RegisterCallback(func(event int) error {
autoDetectInterfaceName := defaultInterfaceMonitor.DefaultInterfaceName(netip.IPv4Unspecified()) targetInterface := dialer.DefaultInterface.Load()
if autoDetectInterfaceName != "" && autoDetectInterfaceName != "<nil>" { for _, destination := range []netip.Addr{netip.IPv4Unspecified(), netip.IPv6Unspecified(), netip.MustParseAddr("1.1.1.1")} {
targetInterface = autoDetectInterfaceName autoDetectInterfaceName := defaultInterfaceMonitor.DefaultInterfaceName(destination)
} else { if autoDetectInterfaceName == tunName {
log.Warnln("Auto detect interface name is empty.") log.Warnln("Auto detect interface by %s get same name with tun", destination.String())
} } else if autoDetectInterfaceName == "" || autoDetectInterfaceName == "<nil>" {
if old := dialer.DefaultInterface.Load(); old != targetInterface { log.Warnln("Auto detect interface by %s get empty name.", destination.String())
log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, targetInterface) } else {
targetInterface = autoDetectInterfaceName
if old := dialer.DefaultInterface.Load(); old != targetInterface {
log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, targetInterface)
dialer.DefaultInterface.Store(targetInterface) dialer.DefaultInterface.Store(targetInterface)
iface.FlushCache() iface.FlushCache()
} }
return nil return nil
}) }
}
return nil
})
}
err = defaultInterfaceMonitor.Start() err = defaultInterfaceMonitor.Start()
if err != nil { if err != nil {
err = E.Cause(err, "start DefaultInterfaceMonitor") err = E.Cause(err, "start DefaultInterfaceMonitor")