fix: flush default interface when tun config hasn't change

This commit is contained in:
wwqgtxx 2022-10-10 09:32:42 +08:00
parent 66e5136ba0
commit 2c236387b7
2 changed files with 34 additions and 26 deletions

View File

@ -348,6 +348,9 @@ func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *
}()
if !hasTunConfigChange(tunConf) {
if tunLister != nil {
tunLister.FlushDefaultInterface()
}
return
}

View File

@ -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 == "<nil>" {
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 == "<nil>" {
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)