sing-box/experimental/libbox/monitor.go

101 lines
3.2 KiB
Go
Raw Normal View History

2023-04-18 06:04:09 +00:00
package libbox
import (
2024-12-10 01:58:55 +00:00
C "github.com/sagernet/sing-box/constant"
2023-04-18 06:04:09 +00:00
"github.com/sagernet/sing-tun"
2024-11-11 08:23:45 +00:00
"github.com/sagernet/sing/common/control"
2023-04-18 06:04:09 +00:00
E "github.com/sagernet/sing/common/exceptions"
2023-07-23 06:42:19 +00:00
"github.com/sagernet/sing/common/logger"
2023-04-18 06:04:09 +00:00
"github.com/sagernet/sing/common/x/list"
)
var (
_ tun.DefaultInterfaceMonitor = (*platformDefaultInterfaceMonitor)(nil)
_ InterfaceUpdateListener = (*platformDefaultInterfaceMonitor)(nil)
)
type platformDefaultInterfaceMonitor struct {
*platformInterfaceWrapper
2024-11-11 08:23:45 +00:00
element *list.Element[tun.NetworkUpdateCallback]
callbacks list.List[tun.DefaultInterfaceUpdateCallback]
logger logger.Logger
2023-04-18 06:04:09 +00:00
}
func (m *platformDefaultInterfaceMonitor) Start() error {
return m.iif.StartDefaultInterfaceMonitor(m)
}
func (m *platformDefaultInterfaceMonitor) Close() error {
return m.iif.CloseDefaultInterfaceMonitor(m)
}
2024-11-11 08:23:45 +00:00
func (m *platformDefaultInterfaceMonitor) DefaultInterface() *control.Interface {
m.defaultInterfaceAccess.Lock()
defer m.defaultInterfaceAccess.Unlock()
return m.defaultInterface
2023-10-21 04:00:00 +00:00
}
2023-04-18 06:04:09 +00: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 08:23:45 +00:00
m.defaultInterfaceAccess.Lock()
defer m.defaultInterfaceAccess.Unlock()
2023-04-18 06:04:09 +00:00
return m.callbacks.PushBack(callback)
}
func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) {
2024-11-11 08:23:45 +00:00
m.defaultInterfaceAccess.Lock()
defer m.defaultInterfaceAccess.Unlock()
2023-04-18 06:04:09 +00:00
m.callbacks.Remove(element)
}
2024-11-11 08:23:45 +00:00
func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32, isExpensive bool, isConstrained bool) {
2024-12-10 01:58:55 +00:00
if C.FixAndroidStack {
2024-11-11 08:23:45 +00:00
go m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained)
2024-12-10 01:58:55 +00:00
} else {
2024-11-11 08:23:45 +00:00
m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained)
2024-12-10 01:58:55 +00:00
}
}
2024-11-11 08:23:45 +00: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 09:51:33 +00:00
callbacks := m.callbacks.Array()
2024-11-11 08:23:45 +00:00
m.defaultInterfaceAccess.Unlock()
2024-02-02 09:51:33 +00:00
for _, callback := range callbacks {
2024-11-12 11:37:10 +00:00
callback(nil, 0)
2024-02-02 09:51:33 +00:00
}
return
}
2024-11-11 08:23:45 +00:00
oldInterface := m.defaultInterface
newInterface, err := m.networkManager.InterfaceFinder().ByIndex(int(interfaceIndex32))
2023-04-18 06:04:09 +00:00
if err != nil {
2024-11-11 08:23:45 +00:00
m.defaultInterfaceAccess.Unlock()
m.logger.Error(E.Cause(err, "find updated interface: ", interfaceName))
return
2023-04-18 06:04:09 +00:00
}
2024-11-11 08:23:45 +00:00
m.defaultInterface = newInterface
if oldInterface != nil && oldInterface.Name == m.defaultInterface.Name && oldInterface.Index == m.defaultInterface.Index {
m.defaultInterfaceAccess.Unlock()
2023-04-18 06:04:09 +00:00
return
}
callbacks := m.callbacks.Array()
2024-11-11 08:23:45 +00:00
m.defaultInterfaceAccess.Unlock()
2023-04-18 06:04:09 +00:00
for _, callback := range callbacks {
2024-11-12 11:37:10 +00:00
callback(newInterface, 0)
2023-04-18 06:04:09 +00:00
}
}