Skip to content

Commit

Permalink
Fix interface callback
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 12, 2024
1 parent 64c0f52 commit b4300ea
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 22 deletions.
8 changes: 2 additions & 6 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions monitor_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion monitor_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion monitor_linux_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions monitor_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion monitor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions tun_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b4300ea

Please sign in to comment.