mirror of
https://github.com/SagerNet/sing-tun.git
synced 2025-04-03 20:07:40 +03:00
refactor: More interface reuse
This commit is contained in:
parent
1015c096bb
commit
355e4e81cc
8 changed files with 51 additions and 107 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
"github.com/sagernet/sing/common/x/list"
|
||||
|
@ -106,12 +107,13 @@ func (m *networkUpdateMonitor) Close() error {
|
|||
}
|
||||
|
||||
func (m *defaultInterfaceMonitor) checkUpdate() error {
|
||||
var (
|
||||
defaultInterface *net.Interface
|
||||
err error
|
||||
)
|
||||
err := m.interfaceFinder.Update()
|
||||
if err != nil {
|
||||
return E.Cause(err, "update interfaces")
|
||||
}
|
||||
var defaultInterface *control.Interface
|
||||
if m.underNetworkExtension {
|
||||
defaultInterface, err = getDefaultInterfaceBySocket()
|
||||
defaultInterface, err = m.getDefaultInterfaceBySocket()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -144,7 +146,7 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
|
|||
if ones != 0 {
|
||||
continue
|
||||
}
|
||||
routeInterface, err := net.InterfaceByIndex(routeMessage.Index)
|
||||
routeInterface, err := m.interfaceFinder.ByIndex(routeMessage.Index)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -164,18 +166,20 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
|
|||
if defaultInterface == nil {
|
||||
return ErrNoRoute
|
||||
}
|
||||
oldInterface := m.defaultInterfaceName
|
||||
oldIndex := m.defaultInterfaceIndex
|
||||
m.defaultInterfaceIndex = defaultInterface.Index
|
||||
m.defaultInterfaceName = defaultInterface.Name
|
||||
if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
|
||||
oldInterface := m.defaultInterface.Load()
|
||||
newInterface, err := m.interfaceFinder.ByIndex(defaultInterface.Index)
|
||||
if err != nil {
|
||||
return E.Cause(err, "find updated interface: ", defaultInterface.Name)
|
||||
}
|
||||
m.defaultInterface.Store(newInterface)
|
||||
if oldInterface != nil && oldInterface.Name == newInterface.Name && oldInterface.Index == newInterface.Index {
|
||||
return nil
|
||||
}
|
||||
m.emit(EventInterfaceUpdate)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getDefaultInterfaceBySocket() (*net.Interface, error) {
|
||||
func (m *defaultInterfaceMonitor) getDefaultInterfaceBySocket() (*control.Interface, error) {
|
||||
socketFd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "create file descriptor")
|
||||
|
@ -218,24 +222,5 @@ func getDefaultInterfaceBySocket() (*net.Interface, error) {
|
|||
case <-time.After(time.Second):
|
||||
return nil, nil
|
||||
}
|
||||
interfaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "net.Interfaces")
|
||||
}
|
||||
for _, netInterface := range interfaces {
|
||||
interfaceAddrs, err := netInterface.Addrs()
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "net.Interfaces.Addrs")
|
||||
}
|
||||
for _, interfaceAddr := range interfaceAddrs {
|
||||
ipNet, isIPNet := interfaceAddr.(*net.IPNet)
|
||||
if !isIPNet {
|
||||
continue
|
||||
}
|
||||
if ipNet.Contains(selectedAddr.AsSlice()) {
|
||||
return &netInterface, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, E.New("no interface found for address ", selectedAddr)
|
||||
return m.interfaceFinder.ByAddr(selectedAddr)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue