From a73f1f0b37968b8e17af2453c9d0532c4dcff45c Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Fri, 7 Oct 2022 16:54:08 +0800 Subject: [PATCH] fix: don't set auto detect interface with tun name --- config/config.go | 4 ++-- listener/sing_tun/server.go | 39 ++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/config/config.go b/config/config.go index 1969bc27..1a0cbc96 100644 --- a/config/config.go +++ b/config/config.go @@ -373,8 +373,8 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { Device: "", Stack: "gvisor", DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query - AutoRoute: false, - AutoDetectInterface: false, + AutoRoute: true, + AutoDetectInterface: true, Inet6Address: []ListenPrefix{ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))}, }, EBpf: EBpf{ diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index a8c60e2e..aae29fec 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -142,28 +142,35 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P } l.networkUpdateMonitor = networkUpdateMonitor - defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, tun.DefaultInterfaceMonitorOptions{}) + defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, tun.DefaultInterfaceMonitorOptions{OverrideAndroidVPN: true}) if err != nil { err = E.Cause(err, "create DefaultInterfaceMonitor") return } - defaultInterfaceMonitor.RegisterCallback(func(event int) error { - targetInterface := dialer.DefaultInterface.Load() - autoDetectInterfaceName := defaultInterfaceMonitor.DefaultInterfaceName(netip.IPv4Unspecified()) - if autoDetectInterfaceName != "" && autoDetectInterfaceName != "" { - targetInterface = autoDetectInterfaceName - } else { - log.Warnln("Auto detect interface name is empty.") - } - if old := dialer.DefaultInterface.Load(); old != targetInterface { - log.Warnln("[TUN] default interface changed by monitor, %s => %s", old, targetInterface) + if options.AutoDetectInterface { + defaultInterfaceMonitor.RegisterCallback(func(event int) error { + targetInterface := dialer.DefaultInterface.Load() + for _, destination := range []netip.Addr{netip.IPv4Unspecified(), netip.IPv6Unspecified(), netip.MustParseAddr("1.1.1.1")} { + autoDetectInterfaceName := defaultInterfaceMonitor.DefaultInterfaceName(destination) + if autoDetectInterfaceName == tunName { + log.Warnln("Auto detect interface by %s get same name with tun", destination.String()) + } else if autoDetectInterfaceName == "" || autoDetectInterfaceName == "" { + log.Warnln("Auto detect interface by %s get empty name.", destination.String()) + } 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() - } - return nil - }) + iface.FlushCache() + } + return nil + } + } + return nil + }) + } err = defaultInterfaceMonitor.Start() if err != nil { err = E.Cause(err, "start DefaultInterfaceMonitor")