From 8760a0d94d2255a44475f74998746cc4f679b80c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 2 Feb 2024 17:51:33 +0800 Subject: [PATCH] Fix android interface monitor --- experimental/libbox/monitor.go | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/experimental/libbox/monitor.go b/experimental/libbox/monitor.go index 575466c942..60fc2b8e75 100644 --- a/experimental/libbox/monitor.go +++ b/experimental/libbox/monitor.go @@ -97,6 +97,17 @@ func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Eleme } func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32) { + if interfaceName == "" || interfaceIndex32 == -1 { + m.defaultInterfaceName = "" + m.defaultInterfaceIndex = -1 + m.access.Lock() + callbacks := m.callbacks.Array() + m.access.Unlock() + for _, callback := range callbacks { + callback(tun.EventNoRoute) + } + return + } var err error if m.iif.UsePlatformInterfaceGetter() { err = m.updateInterfacesPlatform() @@ -110,28 +121,6 @@ func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName s m.logger.Error(E.Cause(err, "update interfaces")) } interfaceIndex := int(interfaceIndex32) - if interfaceName == "" { - for _, netIf := range m.networkAddresses { - if netIf.interfaceIndex == interfaceIndex { - interfaceName = netIf.interfaceName - break - } - } - } else if interfaceIndex == -1 { - for _, netIf := range m.networkAddresses { - if netIf.interfaceName == interfaceName { - interfaceIndex = netIf.interfaceIndex - break - } - } - } - if interfaceName == "" { - m.logger.Error(E.New("invalid interface name for ", interfaceIndex)) - return - } else if interfaceIndex == -1 { - m.logger.Error(E.New("invalid interface index for ", interfaceName)) - return - } if m.defaultInterfaceName == interfaceName && m.defaultInterfaceIndex == interfaceIndex { return }