fix: flush default interface when tun config hasn't change
This commit is contained in:
parent
66e5136ba0
commit
2c236387b7
2 changed files with 34 additions and 26 deletions
|
@ -348,6 +348,9 @@ func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if !hasTunConfigChange(tunConf) {
|
if !hasTunConfigChange(tunConf) {
|
||||||
|
if tunLister != nil {
|
||||||
|
tunLister.FlushDefaultInterface()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ type Listener struct {
|
||||||
closed bool
|
closed bool
|
||||||
options config.Tun
|
options config.Tun
|
||||||
handler *ListenerHandler
|
handler *ListenerHandler
|
||||||
|
tunName string
|
||||||
|
|
||||||
tunIf tun.Tun
|
tunIf tun.Tun
|
||||||
tunStack tun.Stack
|
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")
|
err = E.Cause(err, "create NetworkUpdateMonitor")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
l.networkUpdateMonitor = networkUpdateMonitor
|
||||||
err = networkUpdateMonitor.Start()
|
err = networkUpdateMonitor.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = E.Cause(err, "start NetworkUpdateMonitor")
|
err = E.Cause(err, "start NetworkUpdateMonitor")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
l.networkUpdateMonitor = networkUpdateMonitor
|
|
||||||
|
|
||||||
defaultInterfaceMonitor, err := tun.NewDefaultInterfaceMonitor(networkUpdateMonitor, tun.DefaultInterfaceMonitorOptions{OverrideAndroidVPN: true})
|
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
|
||||||
}
|
}
|
||||||
if options.AutoDetectInterface {
|
l.defaultInterfaceMonitor = defaultInterfaceMonitor
|
||||||
defaultInterfaceMonitor.RegisterCallback(func(event int) error {
|
defaultInterfaceMonitor.RegisterCallback(func(event int) error {
|
||||||
targetInterface := dialer.DefaultInterface.Load()
|
l.FlushDefaultInterface()
|
||||||
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
|
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")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
l.defaultInterfaceMonitor = defaultInterfaceMonitor
|
|
||||||
|
|
||||||
tunOptions := tun.Options{
|
tunOptions := tun.Options{
|
||||||
Name: tunName,
|
Name: tunName,
|
||||||
|
@ -229,6 +210,30 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
|
||||||
return
|
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] {
|
func uidToRange(uidList []uint32) []ranges.Range[uint32] {
|
||||||
return common.Map(uidList, func(uid uint32) ranges.Range[uint32] {
|
return common.Map(uidList, func(uid uint32) ranges.Range[uint32] {
|
||||||
return ranges.NewSingle(uid)
|
return ranges.NewSingle(uid)
|
||||||
|
|
Loading…
Reference in a new issue