sing-box/experimental/libbox/monitor.go

92 lines
2.8 KiB
Go
Raw Normal View History

2023-04-18 14:04:09 +08:00
package libbox
import (
"github.com/sagernet/sing-tun"
2024-11-11 16:23:45 +08:00
"github.com/sagernet/sing/common/control"
2023-04-18 14:04:09 +08:00
E "github.com/sagernet/sing/common/exceptions"
2023-07-23 14:42:19 +08:00
"github.com/sagernet/sing/common/logger"
2023-04-18 14:04:09 +08:00
"github.com/sagernet/sing/common/x/list"
)
var (
_ tun.DefaultInterfaceMonitor = (*platformDefaultInterfaceMonitor)(nil)
_ InterfaceUpdateListener = (*platformDefaultInterfaceMonitor)(nil)
)
type platformDefaultInterfaceMonitor struct {
*platformInterfaceWrapper
2024-11-11 16:23:45 +08:00
element *list.Element[tun.NetworkUpdateCallback]
callbacks list.List[tun.DefaultInterfaceUpdateCallback]
logger logger.Logger
2023-04-18 14:04:09 +08:00
}
func (m *platformDefaultInterfaceMonitor) Start() error {
return m.iif.StartDefaultInterfaceMonitor(m)
}
func (m *platformDefaultInterfaceMonitor) Close() error {
return m.iif.CloseDefaultInterfaceMonitor(m)
}
2024-11-11 16:23:45 +08:00
func (m *platformDefaultInterfaceMonitor) DefaultInterface() *control.Interface {
m.defaultInterfaceAccess.Lock()
defer m.defaultInterfaceAccess.Unlock()
return m.defaultInterface
2023-10-21 12:00:00 +08:00
}
2023-04-18 14:04:09 +08:00
func (m *platformDefaultInterfaceMonitor) OverrideAndroidVPN() bool {
return false
}
func (m *platformDefaultInterfaceMonitor) AndroidVPNEnabled() bool {
return false
}
func (m *platformDefaultInterfaceMonitor) RegisterCallback(callback tun.DefaultInterfaceUpdateCallback) *list.Element[tun.DefaultInterfaceUpdateCallback] {
2024-11-11 16:23:45 +08:00
m.defaultInterfaceAccess.Lock()
defer m.defaultInterfaceAccess.Unlock()
2023-04-18 14:04:09 +08:00
return m.callbacks.PushBack(callback)
}
func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) {
2024-11-11 16:23:45 +08:00
m.defaultInterfaceAccess.Lock()
defer m.defaultInterfaceAccess.Unlock()
2023-04-18 14:04:09 +08:00
m.callbacks.Remove(element)
}
2024-11-11 16:23:45 +08:00
func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32, isExpensive bool, isConstrained bool) {
m.isExpensive = isExpensive
m.isConstrained = isConstrained
err := m.networkManager.UpdateInterfaces()
if err != nil {
m.logger.Error(E.Cause(err, "update interfaces"))
}
m.defaultInterfaceAccess.Lock()
if interfaceIndex32 == -1 {
m.defaultInterface = nil
2024-02-02 17:51:33 +08:00
callbacks := m.callbacks.Array()
2024-11-11 16:23:45 +08:00
m.defaultInterfaceAccess.Unlock()
2024-02-02 17:51:33 +08:00
for _, callback := range callbacks {
2024-11-11 16:23:45 +08:00
callback(tun.EventInterfaceUpdate)
2024-02-02 17:51:33 +08:00
}
return
}
2024-11-11 16:23:45 +08:00
oldInterface := m.defaultInterface
newInterface, err := m.networkManager.InterfaceFinder().ByIndex(int(interfaceIndex32))
2023-04-18 14:04:09 +08:00
if err != nil {
2024-11-11 16:23:45 +08:00
m.defaultInterfaceAccess.Unlock()
m.logger.Error(E.Cause(err, "find updated interface: ", interfaceName))
return
2023-04-18 14:04:09 +08:00
}
2024-11-11 16:23:45 +08:00
m.defaultInterface = newInterface
if oldInterface != nil && oldInterface.Name == m.defaultInterface.Name && oldInterface.Index == m.defaultInterface.Index {
m.defaultInterfaceAccess.Unlock()
2023-04-18 14:04:09 +08:00
return
}
callbacks := m.callbacks.Array()
2024-11-11 16:23:45 +08:00
m.defaultInterfaceAccess.Unlock()
2023-04-18 14:04:09 +08:00
for _, callback := range callbacks {
2023-07-23 14:42:19 +08:00
callback(tun.EventInterfaceUpdate)
2023-04-18 14:04:09 +08:00
}
}