mirror of
https://github.com/SagerNet/sing-tun.git
synced 2025-03-31 10:27:39 +03:00
Fix interface callback
This commit is contained in:
parent
64c0f526fd
commit
b4300eaa6a
7 changed files with 16 additions and 22 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue