Add darwin monitor

This commit is contained in:
世界 2022-08-04 18:23:58 +08:00
parent a8fd6450d4
commit 2a1554dfd3
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
14 changed files with 205 additions and 11 deletions

5
go.mod
View file

@ -4,8 +4,9 @@ go 1.18
require (
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805
github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698
golang.org/x/sys v0.0.0-20220731174439-a90be440212d
github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704
gvisor.dev/gvisor v0.0.0-20220711011657-cecae2f4234d
)

10
go.sum
View file

@ -2,13 +2,15 @@ github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs=
github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698 h1:wjoF4/FOwze8cN2/EvQyyuq1tzXjxNViPIoqQ7CNIb8=
github.com/sagernet/sing v0.0.0-20220730061053-a21e329a2698/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc h1:x7H64IiqyrpxPWl/KrWkknzEK4GmpqgfZeVKFVw6E/M=
github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b h1:3ogNYyK4oIQdIKzTu68hQrr4iuVxF3AxKl9Aj/eDrw0=
golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 h1:Y7NOhdqIOU8kYI7BxsgL38d0ot0raxvcW+EMQU2QrT4=
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
gvisor.dev/gvisor v0.0.0-20220711011657-cecae2f4234d h1:KjI6i6P1ib9DiNdNIN8pb2TXfBewpKHf3O58cjj9vw4=

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (

View file

@ -2,7 +2,6 @@ package tun
import (
"github.com/sagernet/netlink"
E "github.com/sagernet/sing/common/exceptions"
)
func (m *defaultInterfaceMonitor) checkUpdate() error {
@ -19,7 +18,7 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
}
if defaultTableIndex == 0 {
return E.New("no route to internet")
return ErrNoRoute
}
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Table: defaultTableIndex}, netlink.RT_FILTER_TABLE)
@ -47,5 +46,5 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
return nil
}
return E.New("no route in the system table")
return ErrNoRoute
}

171
monitor_darwin.go Normal file
View file

@ -0,0 +1,171 @@
package tun
import (
"context"
"net"
"net/netip"
"os"
"sync"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/x/list"
"golang.org/x/net/route"
"golang.org/x/sys/unix"
)
type networkUpdateMonitor struct {
errorHandler E.Handler
access sync.Mutex
callbacks list.List[NetworkUpdateCallback]
routeSocket *os.File
}
func NewNetworkUpdateMonitor(errorHandler E.Handler) (NetworkUpdateMonitor, error) {
return &networkUpdateMonitor{
errorHandler: errorHandler,
}, nil
}
func (m *networkUpdateMonitor) RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback] {
m.access.Lock()
defer m.access.Unlock()
return m.callbacks.PushBack(callback)
}
func (m *networkUpdateMonitor) UnregisterCallback(element *list.Element[NetworkUpdateCallback]) {
m.access.Lock()
defer m.access.Unlock()
m.callbacks.Remove(element)
}
func (m *networkUpdateMonitor) emit() {
m.access.Lock()
callbacks := m.callbacks.Array()
m.access.Unlock()
for _, callback := range callbacks {
err := callback()
if err != nil {
m.errorHandler.NewError(context.Background(), err)
}
}
}
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()
return nil
}
func (m *networkUpdateMonitor) loopUpdate() {
rawConn, err := m.routeSocket.SyscallConn()
if err != nil {
m.errorHandler.NewError(context.Background(), err)
return
}
for {
var innerErr error
err = rawConn.Read(func(fd uintptr) (done bool) {
var msg [2048]byte
_, innerErr = unix.Read(int(fd), msg[:])
return innerErr != unix.EWOULDBLOCK
})
if innerErr != nil {
err = innerErr
}
if err != nil {
break
}
m.emit()
}
if !E.IsClosed(err) {
m.errorHandler.NewError(context.Background(), err)
}
}
func (m *networkUpdateMonitor) Close() error {
return common.Close(common.PtrOrNil(m.routeSocket))
}
type defaultInterfaceMonitor struct {
defaultInterfaceName string
defaultInterfaceIndex int
networkMonitor NetworkUpdateMonitor
element *list.Element[NetworkUpdateCallback]
callback DefaultInterfaceUpdateCallback
}
func NewDefaultInterfaceMonitor(networkMonitor NetworkUpdateMonitor, callback DefaultInterfaceUpdateCallback) (DefaultInterfaceMonitor, error) {
return &defaultInterfaceMonitor{
networkMonitor: networkMonitor,
callback: callback,
}, nil
}
func (m *defaultInterfaceMonitor) Start() error {
err := m.checkUpdate()
if err != nil {
return err
}
m.element = m.networkMonitor.RegisterCallback(m.checkUpdate)
return nil
}
func (m *defaultInterfaceMonitor) Close() error {
m.networkMonitor.UnregisterCallback(m.element)
return nil
}
func (m *defaultInterfaceMonitor) DefaultInterfaceName() string {
return m.defaultInterfaceName
}
func (m *defaultInterfaceMonitor) DefaultInterfaceIndex() int {
return m.defaultInterfaceIndex
}
func (m *defaultInterfaceMonitor) checkUpdate() error {
ribMessage, err := route.FetchRIB(unix.AF_UNSPEC, route.RIBTypeRoute, 0)
if err != nil {
return err
}
routeMessages, err := route.ParseRIB(route.RIBTypeRoute, ribMessage)
if err != nil {
return err
}
for _, rawRouteMessage := range routeMessages {
routeMessage := rawRouteMessage.(*route.RouteMessage)
if common.Any(common.FilterIsInstance(routeMessage.Addrs, func(it route.Addr) (*route.Inet4Addr, bool) {
addr, loaded := it.(*route.Inet4Addr)
return addr, loaded
}), func(addr *route.Inet4Addr) bool {
return addr.IP == netip.IPv4Unspecified().As4()
}) {
oldInterface := m.defaultInterfaceName
oldIndex := m.defaultInterfaceIndex
m.defaultInterfaceIndex = routeMessage.Index
defaultInterface, err := net.InterfaceByIndex(routeMessage.Index)
if err != nil {
return err
}
m.defaultInterfaceName = defaultInterface.Name
if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
return nil
}
m.callback()
return nil
}
}
return ErrNoRoute
}

View file

@ -1,4 +1,4 @@
//go:build !linux && !windows
//go:build !(linux || windows || darwin)
package tun

2
tun.go
View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (

View file

@ -1,4 +1,4 @@
//go:build !linux && !windows
//go:build no_gvisor || !(linux || windows)
package tun

7
tun_stub.go Normal file
View file

@ -0,0 +1,7 @@
//go:build no_gvisor
package tun
type Tun interface {
Close() error
}

View file

@ -1,3 +1,5 @@
//go:build !no_gvisor
package tun
import (