diff --git a/listener/listener.go b/listener/listener.go index 40d2d3d3..8512c0b1 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -348,6 +348,9 @@ func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- * }() if !hasTunConfigChange(tunConf) { + if tunLister != nil { + tunLister.FlushDefaultInterface() + } return } diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index aae29fec..ea173481 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -29,6 +29,7 @@ type Listener struct { closed bool options config.Tun handler *ListenerHandler + tunName string tunIf tun.Tun tunStack tun.Stack @@ -135,48 +136,28 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P err = E.Cause(err, "create NetworkUpdateMonitor") return } + l.networkUpdateMonitor = networkUpdateMonitor err = networkUpdateMonitor.Start() if err != nil { err = E.Cause(err, "start NetworkUpdateMonitor") return } - l.networkUpdateMonitor = networkUpdateMonitor defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, tun.DefaultInterfaceMonitorOptions{OverrideAndroidVPN: true}) if err != nil { err = E.Cause(err, "create DefaultInterfaceMonitor") return } - 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) - - iface.FlushCache() - } - return nil - } - } - return nil - }) - } + l.defaultInterfaceMonitor = defaultInterfaceMonitor + defaultInterfaceMonitor.RegisterCallback(func(event int) error { + l.FlushDefaultInterface() + return nil + }) err = defaultInterfaceMonitor.Start() if err != nil { err = E.Cause(err, "start DefaultInterfaceMonitor") return } - l.defaultInterfaceMonitor = defaultInterfaceMonitor tunOptions := tun.Options{ Name: tunName, @@ -229,6 +210,30 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P return } +func (l *Listener) FlushDefaultInterface() { + if l.options.AutoDetectInterface { + targetInterface := dialer.DefaultInterface.Load() + for _, destination := range []netip.Addr{netip.IPv4Unspecified(), netip.IPv6Unspecified(), netip.MustParseAddr("1.1.1.1")} { + autoDetectInterfaceName := l.defaultInterfaceMonitor.DefaultInterfaceName(destination) + if autoDetectInterfaceName == l.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) + + iface.FlushCache() + } + return + } + } + } +} + func uidToRange(uidList []uint32) []ranges.Range[uint32] { return common.Map(uidList, func(uid uint32) ranges.Range[uint32] { return ranges.NewSingle(uid)