Improve interface monitor

This commit is contained in:
世界 2022-08-26 20:50:22 +08:00
parent 409136e644
commit d1c9876e60
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 64 additions and 4 deletions

View file

@ -1,6 +1,8 @@
package tun package tun
import ( import (
"net/netip"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
) )
@ -23,8 +25,8 @@ type NetworkUpdateMonitor interface {
type DefaultInterfaceMonitor interface { type DefaultInterfaceMonitor interface {
Start() error Start() error
Close() error Close() error
DefaultInterfaceName() string DefaultInterfaceName(destination netip.Addr) string
DefaultInterfaceIndex() int DefaultInterfaceIndex(destination netip.Addr) int
RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback] RegisterCallback(callback DefaultInterfaceUpdateCallback) *list.Element[DefaultInterfaceUpdateCallback]
UnregisterCallback(element *list.Element[DefaultInterfaceUpdateCallback]) UnregisterCallback(element *list.Element[DefaultInterfaceUpdateCallback])
} }

View file

@ -4,9 +4,14 @@ package tun
import ( import (
"context" "context"
"net"
"net/netip"
"sync" "sync"
"time" "time"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
) )
@ -39,6 +44,7 @@ func (m *networkUpdateMonitor) NewError(ctx context.Context, err error) {
} }
type defaultInterfaceMonitor struct { type defaultInterfaceMonitor struct {
networkAddresses []networkAddress
defaultInterfaceName string defaultInterfaceName string
defaultInterfaceIndex int defaultInterfaceIndex int
networkMonitor NetworkUpdateMonitor networkMonitor NetworkUpdateMonitor
@ -47,6 +53,12 @@ type defaultInterfaceMonitor struct {
callbacks list.List[DefaultInterfaceUpdateCallback] callbacks list.List[DefaultInterfaceUpdateCallback]
} }
type networkAddress struct {
interfaceName string
interfaceIndex int
addresses []netip.Prefix
}
func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor) (DefaultInterfaceMonitor, error) { func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor) (DefaultInterfaceMonitor, error) {
return &defaultInterfaceMonitor{ return &defaultInterfaceMonitor{
networkMonitor: networkMonitor, networkMonitor: networkMonitor,
@ -64,9 +76,41 @@ func (m *defaultInterfaceMonitor) Start() error {
func (m *defaultInterfaceMonitor) delayCheckUpdate() error { func (m *defaultInterfaceMonitor) delayCheckUpdate() error {
time.Sleep(time.Second) time.Sleep(time.Second)
err := m.updateInterfaces()
if err != nil {
m.networkMonitor.NewError(context.Background(), E.Cause(err, "update interfaces"))
}
return m.checkUpdate() return m.checkUpdate()
} }
func (m *defaultInterfaceMonitor) updateInterfaces() error {
interfaces, err := net.Interfaces()
if err != nil {
return err
}
var addresses []networkAddress
for _, iif := range interfaces {
var netAddresses []net.Addr
netAddresses, err = iif.Addrs()
if err != nil {
return err
}
var address networkAddress
address.interfaceName = iif.Name
address.interfaceIndex = iif.Index
address.addresses = common.Map(common.FilterIsInstance(netAddresses, func(it net.Addr) (*net.IPNet, bool) {
value, loaded := it.(*net.IPNet)
return value, loaded
}), func(it *net.IPNet) netip.Prefix {
bits, _ := it.Mask.Size()
return netip.PrefixFrom(M.AddrFromIP(it.IP), bits)
})
addresses = append(addresses, address)
}
m.networkAddresses = addresses
return nil
}
func (m *defaultInterfaceMonitor) Close() error { func (m *defaultInterfaceMonitor) Close() error {
if m.element != nil { if m.element != nil {
m.networkMonitor.UnregisterCallback(m.element) m.networkMonitor.UnregisterCallback(m.element)
@ -74,11 +118,25 @@ func (m *defaultInterfaceMonitor) Close() error {
return nil return nil
} }
func (m *defaultInterfaceMonitor) DefaultInterfaceName() string { func (m *defaultInterfaceMonitor) DefaultInterfaceName(destination netip.Addr) string {
for _, address := range m.networkAddresses {
for _, prefix := range address.addresses {
if prefix.Contains(destination) {
return address.interfaceName
}
}
}
return m.defaultInterfaceName return m.defaultInterfaceName
} }
func (m *defaultInterfaceMonitor) DefaultInterfaceIndex() int { func (m *defaultInterfaceMonitor) DefaultInterfaceIndex(destination netip.Addr) int {
for _, address := range m.networkAddresses {
for _, prefix := range address.addresses {
if prefix.Contains(destination) {
return address.interfaceIndex
}
}
}
return m.defaultInterfaceIndex return m.defaultInterfaceIndex
} }