diff --git a/go.mod b/go.mod index eaaca5c..425d788 100644 --- a/go.mod +++ b/go.mod @@ -8,21 +8,21 @@ require ( github.com/sagernet/fswatch v0.1.1 github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a github.com/sagernet/nftables v0.3.0-beta.4 - github.com/sagernet/sing v0.5.0 + github.com/sagernet/sing v0.5.1 go4.org/netipx v0.0.0-20231129151722-fdeea329fbba golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 - golang.org/x/net v0.26.0 - golang.org/x/sys v0.21.0 + golang.org/x/net v0.31.0 + golang.org/x/sys v0.27.0 ) require ( github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/google/btree v1.1.2 // indirect + github.com/google/btree v1.1.3 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/josharian/native v1.1.0 // indirect github.com/mdlayher/netlink v1.7.2 // indirect github.com/mdlayher/socket v0.4.1 // indirect github.com/vishvananda/netns v0.0.4 // indirect golang.org/x/sync v0.7.0 // indirect - golang.org/x/time v0.5.0 // indirect + golang.org/x/time v0.7.0 // indirect ) diff --git a/go.sum b/go.sum index 25dfaee..73dd907 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= -github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU= -github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= +github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg= +github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= @@ -22,8 +22,8 @@ github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a h1:ObwtHN2VpqE0ZN github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= github.com/sagernet/nftables v0.3.0-beta.4 h1:kbULlAwAC3jvdGAC1P5Fa3GSxVwQJibNenDW2zaXr8I= github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8= -github.com/sagernet/sing v0.5.0 h1:soo2wVwLcieKWWKIksFNK6CCAojUgAppqQVwyRYGkEM= -github.com/sagernet/sing v0.5.0/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= +github.com/sagernet/sing v0.5.1 h1:mhL/MZVq0TjuvHcpYcFtmSD1BFOxZ/+8ofbNZcg1k1Y= +github.com/sagernet/sing v0.5.1/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8= github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= @@ -31,13 +31,13 @@ go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBs go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y= golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= -golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= -golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= +golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= -golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= +golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/stack_gvisor.go b/stack_gvisor.go index 80c78af..70558ca 100644 --- a/stack_gvisor.go +++ b/stack_gvisor.go @@ -153,6 +153,9 @@ func (t *GVisor) Start() error { } func (t *GVisor) Close() error { + if t.stack == nil { + return nil + } t.endpoint.Attach(nil) t.stack.Close() for _, endpoint := range t.stack.CleanupEndpoints() { diff --git a/stack_mixed.go b/stack_mixed.go index 03f887a..b0b404a 100644 --- a/stack_mixed.go +++ b/stack_mixed.go @@ -261,6 +261,9 @@ func (m *Mixed) packetLoop() { } func (m *Mixed) Close() error { + if m.stack == nil { + return nil + } m.endpoint.Attach(nil) m.stack.Close() for _, endpoint := range m.stack.CleanupEndpoints() { diff --git a/tun.go b/tun.go index f1688fa..5c0663a 100644 --- a/tun.go +++ b/tun.go @@ -89,10 +89,20 @@ func (o *Options) Inet4GatewayAddr() netip.Addr { return o.Inet4Gateway } if len(o.Inet4Address) > 0 { - if HasNextAddress(o.Inet4Address[0], 1) { - return o.Inet4Address[0].Addr().Next() - } else if runtime.GOOS != "linux" { + switch runtime.GOOS { + case "android": + case "linux": + if HasNextAddress(o.Inet4Address[0], 1) { + return o.Inet4Address[0].Addr().Next() + } + case "darwin": return o.Inet4Address[0].Addr() + default: + if HasNextAddress(o.Inet4Address[0], 1) { + return o.Inet4Address[0].Addr().Next() + } else { + return o.Inet4Address[0].Addr() + } } } return netip.IPv4Unspecified() @@ -103,10 +113,20 @@ func (o *Options) Inet6GatewayAddr() netip.Addr { return o.Inet6Gateway } if len(o.Inet6Address) > 0 { - if HasNextAddress(o.Inet6Address[0], 1) { - return o.Inet6Address[0].Addr().Next() - } else if runtime.GOOS != "linux" { + switch runtime.GOOS { + case "android": + case "linux": + if HasNextAddress(o.Inet6Address[0], 1) { + return o.Inet6Address[0].Addr().Next() + } + case "darwin": return o.Inet6Address[0].Addr() + default: + if HasNextAddress(o.Inet6Address[0], 1) { + return o.Inet6Address[0].Addr().Next() + } else { + return o.Inet6Address[0].Addr() + } } } return netip.IPv6Unspecified() diff --git a/tun_darwin_gvisor.go b/tun_darwin_gvisor.go index 06dd0b7..eaa1b5b 100644 --- a/tun_darwin_gvisor.go +++ b/tun_darwin_gvisor.go @@ -28,6 +28,9 @@ func (e *DarwinEndpoint) MTU() uint32 { return e.tun.mtu } +func (e *DarwinEndpoint) SetMTU(mtu uint32) { +} + func (e *DarwinEndpoint) MaxHeaderLength() uint16 { return 0 } @@ -36,6 +39,9 @@ func (e *DarwinEndpoint) LinkAddress() tcpip.LinkAddress { return "" } +func (e *DarwinEndpoint) SetLinkAddress(addr tcpip.LinkAddress) { +} + func (e *DarwinEndpoint) Capabilities() stack.LinkEndpointCapabilities { return stack.CapabilityRXChecksumOffload } @@ -121,3 +127,9 @@ func (e *DarwinEndpoint) WritePackets(packetBufferList stack.PacketBufferList) ( } return n, nil } + +func (e *DarwinEndpoint) Close() { +} + +func (e *DarwinEndpoint) SetOnCloseAction(f func()) { +} diff --git a/tun_linux.go b/tun_linux.go index 4ed7c5f..66cd917 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -578,6 +578,7 @@ func (t *NativeTun) rules() []*netlink.Rule { it = netlink.NewRule() if t.options.InterfaceMonitor.OverrideAndroidVPN() { it.Mark = protectedFromVPN + it.MarkSet = true } it.Mask = protectedFromVPN it.Priority = priority @@ -590,6 +591,7 @@ func (t *NativeTun) rules() []*netlink.Rule { it = netlink.NewRule() if t.options.InterfaceMonitor.OverrideAndroidVPN() { it.Mark = protectedFromVPN + it.MarkSet = true } it.Mask = protectedFromVPN it.Family = unix.AF_INET6 diff --git a/tun_windows_gvisor.go b/tun_windows_gvisor.go index ee22459..724e94b 100644 --- a/tun_windows_gvisor.go +++ b/tun_windows_gvisor.go @@ -26,6 +26,9 @@ func (e *WintunEndpoint) MTU() uint32 { return e.tun.options.MTU } +func (e *WintunEndpoint) SetMTU(mtu uint32) { +} + func (e *WintunEndpoint) MaxHeaderLength() uint16 { return 0 } @@ -34,6 +37,9 @@ func (e *WintunEndpoint) LinkAddress() tcpip.LinkAddress { return "" } +func (e *WintunEndpoint) SetLinkAddress(addr tcpip.LinkAddress) { +} + func (e *WintunEndpoint) Capabilities() stack.LinkEndpointCapabilities { return stack.CapabilityRXChecksumOffload } @@ -117,3 +123,9 @@ func (e *WintunEndpoint) WritePackets(packetBufferList stack.PacketBufferList) ( } return n, nil } + +func (e *WintunEndpoint) Close() { +} + +func (e *WintunEndpoint) SetOnCloseAction(f func()) { +}