From 6cfee41a56842ec023ab379d09b13770dd476e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 1 Oct 2023 01:00:51 +0800 Subject: [PATCH] Add DefaultInterface func --- monitor.go | 1 + monitor_shared.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/monitor.go b/monitor.go index b424256..2a0a419 100644 --- a/monitor.go +++ b/monitor.go @@ -32,6 +32,7 @@ type DefaultInterfaceMonitor interface { Close() error DefaultInterfaceName(destination netip.Addr) string DefaultInterfaceIndex(destination netip.Addr) int + DefaultInterface(destination netip.Addr) (string, int) OverrideAndroidVPN() bool AndroidVPNEnabled() bool RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback] diff --git a/monitor_shared.go b/monitor_shared.go index 70634ae..a1b2387 100644 --- a/monitor_shared.go +++ b/monitor_shared.go @@ -150,6 +150,20 @@ func (m *defaultInterfaceMonitor) DefaultInterfaceIndex(destination netip.Addr) return m.defaultInterfaceIndex } +func (m *defaultInterfaceMonitor) DefaultInterface(destination netip.Addr) (string, int) { + for _, address := range m.networkAddresses { + for _, prefix := range address.addresses { + if prefix.Contains(destination) { + return address.interfaceName, address.interfaceIndex + } + } + } + if m.defaultInterfaceIndex == -1 { + m.checkUpdate() + } + return m.defaultInterfaceName, m.defaultInterfaceIndex +} + func (m *defaultInterfaceMonitor) OverrideAndroidVPN() bool { return m.options.OverrideAndroidVPN }