mirror of
https://github.com/SagerNet/sing-tun.git
synced 2025-04-03 03:47:39 +03:00
Improve monitor api
This commit is contained in:
parent
2a1554dfd3
commit
7ee0d19103
8 changed files with 110 additions and 192 deletions
|
@ -1,7 +1,6 @@
|
|||
package tun
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/sagernet/sing-tun/internal/winipcfg"
|
||||
|
@ -26,30 +25,6 @@ func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, erro
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (m *networkUpdateMonitor) RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback] {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
return m.callbacks.PushBack(callback)
|
||||
}
|
||||
|
||||
func (m *networkUpdateMonitor) UnregisterCallback(element *list.Element[NetworkUpdateCallback]) {
|
||||
m.access.Lock()
|
||||
defer m.access.Unlock()
|
||||
m.callbacks.Remove(element)
|
||||
}
|
||||
|
||||
func (m *networkUpdateMonitor) emit() {
|
||||
m.access.Lock()
|
||||
callbacks := m.callbacks.Array()
|
||||
m.access.Unlock()
|
||||
for _, callback := range callbacks {
|
||||
err := callback()
|
||||
if err != nil {
|
||||
m.errorHandler.NewError(context.Background(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *networkUpdateMonitor) Start() error {
|
||||
routeListener, err := winipcfg.RegisterRouteChangeCallback(func(notificationType winipcfg.MibNotificationType, route *winipcfg.MibIPforwardRow2) {
|
||||
m.emit()
|
||||
|
@ -76,35 +51,6 @@ func (m *networkUpdateMonitor) Close() error {
|
|||
)
|
||||
}
|
||||
|
||||
type defaultInterfaceMonitor struct {
|
||||
defaultInterfaceName string
|
||||
defaultInterfaceIndex int
|
||||
networkMonitor NetworkUpdateMonitor
|
||||
element *list.Element[NetworkUpdateCallback]
|
||||
callback DefaultInterfaceUpdateCallback
|
||||
}
|
||||
|
||||
func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, callback DefaultInterfaceUpdateCallback) (DefaultInterfaceMonitor, error) {
|
||||
return &defaultInterfaceMonitor{
|
||||
networkMonitor: networkMonitor,
|
||||
callback: callback,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) Start() error {
|
||||
err := m.checkUpdate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.element = m.networkMonitor.RegisterCallback(m.checkUpdate)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) Close() error {
|
||||
m.networkMonitor.UnregisterCallback(m.element)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) checkUpdate() error {
|
||||
rows, err := winipcfg.GetIPForwardTable2(windows.AF_INET)
|
||||
if err != nil {
|
||||
|
@ -152,14 +98,6 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
m.callback()
|
||||
m.emit()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) DefaultInterfaceName() string {
|
||||
return m.defaultInterfaceName
|
||||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) DefaultInterfaceIndex() int {
|
||||
return m.defaultInterfaceIndex
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue