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 @@ import (
"sync"
"github.com/sagernet/sing-tun/internal/winipcfg"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list"
@ -101,16 +102,15 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
return ErrNoRoute
}
oldInterface := m.defaultInterfaceName
oldIndex := m.defaultInterfaceIndex
m.defaultInterfaceName = alias
m.defaultInterfaceIndex = index
if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
oldInterface := m.defaultInterface.Load()
newInterface, err := m.interfaceFinder.ByIndex(index)
if err != nil {
return E.Cause(err, "find updated interface: ", alias)
}
m.defaultInterface.Store(newInterface)
if oldInterface != nil && oldInterface.Name == newInterface.Name && oldInterface.Index == newInterface.Index {
return nil
}
m.emit(EventInterfaceUpdate)
return nil
}