diff --git a/monitor.go b/monitor.go index 9b903f6..a3b4ef6 100644 --- a/monitor.go +++ b/monitor.go @@ -10,14 +10,10 @@ var ErrNoRoute = E.New("no route to internet") type ( NetworkUpdateCallback = func() - DefaultInterfaceUpdateCallback = func(event int) + DefaultInterfaceUpdateCallback = func(defaultInterface *control.Interface, flags int) ) -const ( - EventInterfaceUpdate = 1 - EventAndroidVPNUpdate = 2 - EventNoRoute = 4 -) +const FlagAndroidVPNUpdate = 1 << iota type NetworkUpdateMonitor interface { Start() error diff --git a/monitor_android.go b/monitor_android.go index d058169..2734c85 100644 --- a/monitor_android.go +++ b/monitor_android.go @@ -57,16 +57,13 @@ func (m *defaultInterfaceMonitor) checkUpdate() error { return E.Cause(err, "find updated interface: ", link.Attrs().Name) } m.defaultInterface.Store(newInterface) - var event int - if oldInterface == nil || !oldInterface.Equals(*newInterface) { - event |= EventInterfaceUpdate + if oldInterface != nil && oldInterface.Equals(*newInterface) && oldVPNEnabled == m.androidVPNEnabled { + return nil } + var flags int if oldVPNEnabled != m.androidVPNEnabled { - event |= EventAndroidVPNUpdate + flags = FlagAndroidVPNUpdate } - if event != 0 { - m.emit(event) - } - + m.emit(newInterface, flags) return nil } diff --git a/monitor_darwin.go b/monitor_darwin.go index 7529820..88ea90c 100644 --- a/monitor_darwin.go +++ b/monitor_darwin.go @@ -174,7 +174,7 @@ func (m *defaultInterfaceMonitor) checkUpdate() error { if oldInterface != nil && oldInterface.Equals(*newInterface) { return nil } - m.emit(EventInterfaceUpdate) + m.emit(newInterface, 0) return nil } diff --git a/monitor_linux_default.go b/monitor_linux_default.go index 0cb6b19..e9cce1d 100644 --- a/monitor_linux_default.go +++ b/monitor_linux_default.go @@ -34,7 +34,7 @@ func (m *defaultInterfaceMonitor) checkUpdate() error { if oldInterface != nil && oldInterface.Equals(*newInterface) { return nil } - m.emit(EventInterfaceUpdate) + m.emit(newInterface, 0) return nil } return ErrNoRoute diff --git a/monitor_shared.go b/monitor_shared.go index d742906..a5ee4e3 100644 --- a/monitor_shared.go +++ b/monitor_shared.go @@ -84,7 +84,7 @@ func (m *defaultInterfaceMonitor) postCheckUpdate() { if !m.noRoute { m.noRoute = true m.defaultInterface.Store(nil) - m.emit(EventNoRoute) + m.emit(nil, 0) } } else if err != nil { m.logger.Error("check interface: ", err) @@ -124,11 +124,11 @@ func (m *defaultInterfaceMonitor) UnregisterCallback(element *list.Element[Defau m.callbacks.Remove(element) } -func (m *defaultInterfaceMonitor) emit(event int) { +func (m *defaultInterfaceMonitor) emit(defaultInterface *control.Interface, flags int) { m.access.Lock() callbacks := m.callbacks.Array() m.access.Unlock() for _, callback := range callbacks { - callback(event) + callback(defaultInterface, flags) } } diff --git a/monitor_windows.go b/monitor_windows.go index 38de4ab..d58e701 100644 --- a/monitor_windows.go +++ b/monitor_windows.go @@ -111,6 +111,6 @@ func (m *defaultInterfaceMonitor) checkUpdate() error { if oldInterface != nil && !oldInterface.Equals(*newInterface) { return nil } - m.emit(EventInterfaceUpdate) + m.emit(newInterface, 0) return nil } diff --git a/tun_linux.go b/tun_linux.go index a799b8d..15b6a29 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -16,6 +16,7 @@ import ( "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" + "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" N "github.com/sagernet/sing/common/network" "github.com/sagernet/sing/common/rw" @@ -881,8 +882,8 @@ func (t *NativeTun) resetRules() error { return t.setRules() } -func (t *NativeTun) routeUpdate(event int) { - if event&EventAndroidVPNUpdate == 0 { +func (t *NativeTun) routeUpdate(_ *control.Interface, flags int) { + if flags&FlagAndroidVPNUpdate == 0 { return } err := t.resetRules()