refactor: More interface reuse

This commit is contained in:
世界 2024-11-11 15:59:47 +08:00
parent 1015c096bb
commit 355e4e81cc
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 51 additions and 107 deletions

View file

@ -4,6 +4,7 @@ package tun
import (
"github.com/sagernet/netlink"
E "github.com/sagernet/sing/common/exceptions"
"golang.org/x/sys/unix"
)
@ -24,13 +25,13 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
return err
}
oldInterface := m.defaultInterfaceName
oldIndex := m.defaultInterfaceIndex
m.defaultInterfaceName = link.Attrs().Name
m.defaultInterfaceIndex = link.Attrs().Index
if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
oldInterface := m.defaultInterface.Load()
newInterface, err := m.interfaceFinder.ByIndex(link.Attrs().Index)
if err != nil {
return E.Cause(err, "find updated interface: ", link.Attrs().Name)
}
m.defaultInterface.Store(newInterface)
if oldInterface != nil && oldInterface.Name == newInterface.Name && oldInterface.Index == newInterface.Index {
return nil
}
m.emit(EventInterfaceUpdate)