From f57754918d4d8010b08c5ff2221703085c3a915c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 12 Apr 2025 14:39:22 +0800 Subject: [PATCH] Add DefaultInterfaceMonitor.MyInterface --- monitor.go | 2 ++ monitor_shared.go | 15 ++++++++++++++- tun_darwin.go | 1 + tun_linux.go | 2 +- tun_windows.go | 1 + 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/monitor.go b/monitor.go index a3b4ef6..a1866c8 100644 --- a/monitor.go +++ b/monitor.go @@ -30,6 +30,8 @@ type DefaultInterfaceMonitor interface { AndroidVPNEnabled() bool RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback] UnregisterCallback(element *list.Element[DefaultInterfaceUpdateCallback]) + RegisterMyInterface(interfaceName string) + MyInterface() string } type DefaultInterfaceMonitorOptions struct { diff --git a/monitor_shared.go b/monitor_shared.go index a5ee4e3..12e3e21 100644 --- a/monitor_shared.go +++ b/monitor_shared.go @@ -42,11 +42,12 @@ type defaultInterfaceMonitor struct { androidVPNEnabled bool noRoute bool networkMonitor NetworkUpdateMonitor + logger logger.Logger checkUpdateTimer *time.Timer element *list.Element[NetworkUpdateCallback] access sync.Mutex callbacks list.List[DefaultInterfaceUpdateCallback] - logger logger.Logger + myInterface string } func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, logger logger.Logger, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error) { @@ -132,3 +133,15 @@ func (m *defaultInterfaceMonitor) emit(defaultInterface *control.Interface, flag callback(defaultInterface, flags) } } + +func (m *defaultInterfaceMonitor) RegisterMyInterface(interfaceName string) { + m.access.Lock() + defer m.access.Unlock() + m.myInterface = interfaceName +} + +func (m *defaultInterfaceMonitor) MyInterface() string { + m.access.Lock() + defer m.access.Unlock() + return m.myInterface +} diff --git a/tun_darwin.go b/tun_darwin.go index a0dd54a..2462d75 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -82,6 +82,7 @@ func New(options Options) (Tun, error) { } func (t *NativeTun) Start() error { + t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name) return t.setRoutes() } diff --git a/tun_linux.go b/tun_linux.go index 6a3549d..9c28377 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -264,7 +264,7 @@ func (t *NativeTun) Start() error { if t.options.FileDescriptor != 0 { return nil } - + t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name) tunLink, err := netlink.LinkByName(t.options.Name) if err != nil { return err diff --git a/tun_windows.go b/tun_windows.go index 9cb1e96..dde6199 100644 --- a/tun_windows.go +++ b/tun_windows.go @@ -163,6 +163,7 @@ func (t *NativeTun) Name() (string, error) { } func (t *NativeTun) Start() error { + t.options.InterfaceMonitor.RegisterMyInterface(t.options.Name) if !t.options.AutoRoute { return nil }