Fix monitor

This commit is contained in:
世界 2023-08-07 19:54:17 +08:00
parent 59b86002c4
commit 0a68b9f1d8
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
9 changed files with 80 additions and 87 deletions

View file

@ -10,14 +10,14 @@ import (
var ErrNoRoute = E.New("no route to internet") var ErrNoRoute = E.New("no route to internet")
type ( type (
NetworkUpdateCallback = func() error NetworkUpdateCallback = func()
DefaultInterfaceUpdateCallback = func(event int) error DefaultInterfaceUpdateCallback = func(event int)
) )
const ( const (
EventInterfaceUpdate = iota EventInterfaceUpdate = 1
EventAndroidVPNUpdate EventAndroidVPNUpdate = 2
EventNoRoute EventNoRoute = 4
) )
type NetworkUpdateMonitor interface { type NetworkUpdateMonitor interface {
@ -25,7 +25,6 @@ type NetworkUpdateMonitor interface {
Close() error Close() error
RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback] RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback]
UnregisterCallback(element *list.Element[NetworkUpdateCallback]) UnregisterCallback(element *list.Element[NetworkUpdateCallback])
E.Handler
} }
type DefaultInterfaceMonitor interface { type DefaultInterfaceMonitor interface {

View file

@ -1,16 +1,15 @@
package tun package tun
import ( import (
"context"
"net" "net"
"net/netip" "net/netip"
"os" "os"
"sync" "sync"
"syscall"
"time" "time"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
"golang.org/x/net/route" "golang.org/x/net/route"
@ -18,61 +17,63 @@ import (
) )
type networkUpdateMonitor struct { type networkUpdateMonitor struct {
errorHandler E.Handler
access sync.Mutex access sync.Mutex
callbacks list.List[NetworkUpdateCallback] callbacks list.List[NetworkUpdateCallback]
routeSocket *os.File routeSocket int
logger logger.Logger
} }
func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) { func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
return &networkUpdateMonitor{ return &networkUpdateMonitor{logger: logger}, nil
errorHandler: errorHandler,
}, nil
} }
func (m *networkUpdateMonitor) Start() error { func (m *networkUpdateMonitor) Start() error {
routeSocket, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, 0)
if err != nil {
return err
}
err = unix.SetNonblock(routeSocket, true)
if err != nil {
return err
}
m.routeSocket = os.NewFile(uintptr(routeSocket), "route")
go m.loopUpdate() go m.loopUpdate()
return nil return nil
} }
func (m *networkUpdateMonitor) loopUpdate() { func (m *networkUpdateMonitor) loopUpdate() {
rawConn, err := m.routeSocket.SyscallConn() for {
err := m.loopUpdate0()
if err != nil {
m.logger.Error("listen network update: ", err)
return
}
}
}
func (m *networkUpdateMonitor) loopUpdate0() error {
routeSocket, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, 0)
if err != nil {
return err
}
m.loopUpdate1(os.NewFile(uintptr(routeSocket), "route"))
return nil
}
func (m *networkUpdateMonitor) loopUpdate1(routeSocketFile *os.File) {
defer routeSocketFile.Close()
buffer := buf.NewPacket()
defer buffer.Release()
n, err := routeSocketFile.Read(buffer.FreeBytes())
if err != nil { if err != nil {
m.errorHandler.NewError(context.Background(), E.Cause(err, "create raw route connection"))
return return
} }
for { buffer.Truncate(n)
var innerErr error messages, err := route.ParseRIB(route.RIBTypeRoute, buffer.Bytes())
err = rawConn.Read(func(fd uintptr) (done bool) { if err != nil {
var msg [2048]byte return
_, innerErr = unix.Read(int(fd), msg[:])
return innerErr != unix.EWOULDBLOCK
})
if innerErr != nil {
err = innerErr
}
if err != nil {
break
}
m.emit()
} }
if err != syscall.EAGAIN { for _, message := range messages {
m.errorHandler.NewError(context.Background(), E.Cause(err, "read route message")) if _, isRouteMessage := message.(*route.RouteMessage); isRouteMessage {
m.emit()
return
}
} }
} }
func (m *networkUpdateMonitor) Close() error { func (m *networkUpdateMonitor) Close() error {
return common.Close(common.PtrOrNil(m.routeSocket)) return unix.Close(m.routeSocket)
} }
func (m *defaultInterfaceMonitor) checkUpdate() error { func (m *defaultInterfaceMonitor) checkUpdate() error {
@ -116,7 +117,7 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
continue continue
} }
if routeMessage.Flags&unix.RTF_IFSCOPE != 0 { if routeMessage.Flags&unix.RTF_IFSCOPE != 0 {
continue // continue
} }
defaultInterface = routeInterface defaultInterface = routeInterface
break break

View file

@ -5,26 +5,26 @@ import (
"sync" "sync"
"github.com/sagernet/netlink" "github.com/sagernet/netlink"
E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
) )
type networkUpdateMonitor struct { type networkUpdateMonitor struct {
routeUpdate chan netlink.RouteUpdate routeUpdate chan netlink.RouteUpdate
linkUpdate chan netlink.LinkUpdate linkUpdate chan netlink.LinkUpdate
close chan struct{} close chan struct{}
errorHandler E.Handler
access sync.Mutex access sync.Mutex
callbacks list.List[NetworkUpdateCallback] callbacks list.List[NetworkUpdateCallback]
logger logger.Logger
} }
func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) { func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
return &networkUpdateMonitor{ return &networkUpdateMonitor{
routeUpdate: make(chan netlink.RouteUpdate, 2), routeUpdate: make(chan netlink.RouteUpdate, 2),
linkUpdate: make(chan netlink.LinkUpdate, 2), linkUpdate: make(chan netlink.LinkUpdate, 2),
close: make(chan struct{}), close: make(chan struct{}),
errorHandler: errorHandler, logger: logger,
}, nil }, nil
} }

View file

@ -4,8 +4,6 @@ package tun
import ( import (
"github.com/sagernet/netlink" "github.com/sagernet/netlink"
E "github.com/sagernet/sing/common/exceptions"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -37,5 +35,5 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
m.emit(EventInterfaceUpdate) m.emit(EventInterfaceUpdate)
return nil return nil
} }
return E.New("no route to internet") return ErrNoRoute
} }

View file

@ -3,15 +3,14 @@
package tun package tun
import ( import (
"github.com/sagernet/sing/common/logger"
"os" "os"
E "github.com/sagernet/sing/common/exceptions"
) )
func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) { func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
return nil, os.ErrInvalid return nil, os.ErrInvalid
} }
func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error) { func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, logger logger.Logger, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error) {
return nil, os.ErrInvalid return nil, os.ErrInvalid
} }

View file

@ -3,7 +3,6 @@
package tun package tun
import ( import (
"context"
"errors" "errors"
"net" "net"
"net/netip" "net/netip"
@ -11,7 +10,7 @@ import (
"time" "time"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" "github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
) )
@ -33,17 +32,10 @@ func (m *networkUpdateMonitor) emit() {
callbacks := m.callbacks.Array() callbacks := m.callbacks.Array()
m.access.Unlock() m.access.Unlock()
for _, callback := range callbacks { for _, callback := range callbacks {
err := callback() callback()
if err != nil {
m.NewError(context.Background(), err)
}
} }
} }
func (m *networkUpdateMonitor) NewError(ctx context.Context, err error) {
m.errorHandler.NewError(ctx, err)
}
type defaultInterfaceMonitor struct { type defaultInterfaceMonitor struct {
options DefaultInterfaceMonitorOptions options DefaultInterfaceMonitorOptions
networkAddresses []networkAddress networkAddresses []networkAddress
@ -54,6 +46,7 @@ type defaultInterfaceMonitor struct {
element *list.Element[NetworkUpdateCallback] element *list.Element[NetworkUpdateCallback]
access sync.Mutex access sync.Mutex
callbacks list.List[DefaultInterfaceUpdateCallback] callbacks list.List[DefaultInterfaceUpdateCallback]
logger logger.Logger
} }
type networkAddress struct { type networkAddress struct {
@ -62,34 +55,35 @@ type networkAddress struct {
addresses []netip.Prefix addresses []netip.Prefix
} }
func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error) { func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, logger logger.Logger, options DefaultInterfaceMonitorOptions) (DefaultInterfaceMonitor, error) {
return &defaultInterfaceMonitor{ return &defaultInterfaceMonitor{
options: options, options: options,
networkMonitor: networkMonitor, networkMonitor: networkMonitor,
defaultInterfaceIndex: -1, defaultInterfaceIndex: -1,
logger: logger,
}, nil }, nil
} }
func (m *defaultInterfaceMonitor) Start() error { func (m *defaultInterfaceMonitor) Start() error {
err := m.checkUpdate() err := m.checkUpdate()
if err != nil { if err != nil {
m.networkMonitor.NewError(context.Background(), err) m.logger.Error("initialize default interface: ", err)
} }
m.element = m.networkMonitor.RegisterCallback(m.delayCheckUpdate) m.element = m.networkMonitor.RegisterCallback(m.delayCheckUpdate)
return nil return nil
} }
func (m *defaultInterfaceMonitor) delayCheckUpdate() error { func (m *defaultInterfaceMonitor) delayCheckUpdate() {
time.Sleep(time.Second) time.Sleep(time.Second)
err := m.updateInterfaces() err := m.updateInterfaces()
if err != nil { if err != nil {
m.networkMonitor.NewError(context.Background(), E.Cause(err, "update interfaces")) m.logger.Error("update interfaces: ", err)
} }
err = m.checkUpdate() err = m.checkUpdate()
if errors.Is(err, ErrNoRoute) { if errors.Is(err, ErrNoRoute) {
m.defaultInterfaceIndex = -1
m.emit(EventNoRoute) m.emit(EventNoRoute)
} }
return err
} }
func (m *defaultInterfaceMonitor) updateInterfaces() error { func (m *defaultInterfaceMonitor) updateInterfaces() error {
@ -180,9 +174,6 @@ func (m *defaultInterfaceMonitor) emit(event int) {
callbacks := m.callbacks.Array() callbacks := m.callbacks.Array()
m.access.Unlock() m.access.Unlock()
for _, callback := range callbacks { for _, callback := range callbacks {
err := callback(event) callback(event)
if err != nil {
m.networkMonitor.NewError(context.Background(), err)
}
} }
} }

View file

@ -5,6 +5,7 @@ import (
"github.com/sagernet/sing-tun/internal/winipcfg" "github.com/sagernet/sing-tun/internal/winipcfg"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@ -17,11 +18,12 @@ type networkUpdateMonitor struct {
access sync.Mutex access sync.Mutex
callbacks list.List[NetworkUpdateCallback] callbacks list.List[NetworkUpdateCallback]
logger logger.Logger
} }
func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) { func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
return &networkUpdateMonitor{ return &networkUpdateMonitor{
errorHandler: errorHandler, logger: logger,
}, nil }, nil
} }

2
tun.go
View file

@ -10,6 +10,7 @@ import (
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format" F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/logger"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/ranges" "github.com/sagernet/sing/common/ranges"
) )
@ -47,6 +48,7 @@ type Options struct {
InterfaceMonitor DefaultInterfaceMonitor InterfaceMonitor DefaultInterfaceMonitor
TableIndex int TableIndex int
FileDescriptor int FileDescriptor int
Logger logger.Logger
} }
func CalculateInterfaceName(name string) (tunName string) { func CalculateInterfaceName(name string) (tunName string) {

View file

@ -588,15 +588,16 @@ func (t *NativeTun) resetRules() error {
return t.setRules() return t.setRules()
} }
func (t *NativeTun) routeUpdate(event int) error { func (t *NativeTun) routeUpdate(event int) {
if event&EventAndroidVPNUpdate == 0 { if event&EventAndroidVPNUpdate == 0 {
return nil return
} }
err := t.resetRules() err := t.resetRules()
if err != nil { if err != nil {
return E.Cause(err, "reset route") if t.options.Logger != nil {
t.options.Logger.Error(E.Cause(err, "reset route"))
}
} }
return nil
} }
func (t *NativeTun) setSearchDomainForSystemdResolved() { func (t *NativeTun) setSearchDomainForSystemdResolved() {