Skip to content

Commit

Permalink
Fix router start &
Browse files Browse the repository at this point in the history
Remove `gso_max_size` &
Fix WireGuard GSO
  • Loading branch information
nekohasekai committed Dec 15, 2023
1 parent b7a3143 commit 3c4287d
Show file tree
Hide file tree
Showing 26 changed files with 427 additions and 305 deletions.
1 change: 1 addition & 0 deletions adapter/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

type Router interface {
Service
PreStarter
PostStarter

Outbounds() []Outbound
Expand Down
9 changes: 5 additions & 4 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ func (s *Box) preStart() error {
}
}
}
err = s.router.PreStart()
if err != nil {
return E.Cause(err, "pre-start router")
}
err = s.startOutbounds()
if err != nil {
return err
Expand Down Expand Up @@ -313,10 +317,7 @@ func (s *Box) postStart() error {
}
}
}
err := s.router.PostStart()
if err != nil {
return E.Cause(err, "post-start router")
}

return s.router.PostStart()
}

Expand Down
27 changes: 20 additions & 7 deletions common/dialer/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import (
N "github.com/sagernet/sing/common/network"
)

var _ WireGuardListener = (*DefaultDialer)(nil)

type DefaultDialer struct {
dialer4 tcpDialer
dialer6 tcpDialer
udpDialer4 net.Dialer
udpDialer6 net.Dialer
udpListener net.ListenConfig
udpAddr4 string
udpAddr6 string
dialer4 tcpDialer
dialer6 tcpDialer
udpDialer4 net.Dialer
udpDialer6 net.Dialer
udpListener net.ListenConfig
udpAddr4 string
udpAddr6 string
isWireGuardListener bool
}

func NewDefault(router adapter.Router, options option.DialerOptions) (*DefaultDialer, error) {
Expand Down Expand Up @@ -98,6 +101,11 @@ func NewDefault(router adapter.Router, options option.DialerOptions) (*DefaultDi
}
setMultiPathTCP(&dialer4)
}
if options.IsWireGuradListener {
for _, controlFn := range wgControlFns {
listener.Control = control.Append(listener.Control, controlFn)
}
}
tcpDialer4, err := newTCPDialer(dialer4, options.TCPFastOpen)
if err != nil {
return nil, err
Expand All @@ -114,6 +122,7 @@ func NewDefault(router adapter.Router, options option.DialerOptions) (*DefaultDi
listener,
udpAddr4,
udpAddr6,
options.IsWireGuradListener,
}, nil
}

Expand Down Expand Up @@ -146,6 +155,10 @@ func (d *DefaultDialer) ListenPacket(ctx context.Context, destination M.Socksadd
}
}

func (d *DefaultDialer) ListenPacketCompat(network, address string) (net.PacketConn, error) {
return trackPacketConn(d.udpListener.ListenPacket(context.Background(), network, address))
}

func trackConn(conn net.Conn, err error) (net.Conn, error) {
if !conntrack.Enabled || err != nil {
return conn, err
Expand Down
8 changes: 3 additions & 5 deletions common/dialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common"
N "github.com/sagernet/sing/common/network"
)

func MustNew(router adapter.Router, options option.DialerOptions) N.Dialer {
return common.Must1(New(router, options))
}

func New(router adapter.Router, options option.DialerOptions) (N.Dialer, error) {
if options.IsWireGuradListener {
return NewDefault(router, options)
}
var (
dialer N.Dialer
err error
Expand Down
9 changes: 9 additions & 0 deletions common/dialer/wireguard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dialer

import (
"net"
)

type WireGuardListener interface {
ListenPacketCompat(network, address string) (net.PacketConn, error)
}
11 changes: 11 additions & 0 deletions common/dialer/wireguard_control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build with_wireguard

package dialer

import (
"github.com/sagernet/wireguard-go/conn"
)

var _ WireGuardListener = (conn.Listener)(nil)

var wgControlFns = conn.ControlFns
9 changes: 9 additions & 0 deletions common/dialer/wiregurad_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !with_wireguard

package dialer

import (
"github.com/sagernet/sing/common/control"
)

var wgControlFns []control.Func
1 change: 0 additions & 1 deletion docs/clients/android/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ SFA provides an unprivileged TUN implementation through Android VpnService.
| `inet6_address` | :material-check: | / |
| `mtu` | :material-check: | / |
| `gso` | :material-close: | No permission |
| `gso_max_size` | :material-close: | No permission |
| `auto_route` | :material-check: | / |
| `strict_route` | :material-close: | Not implemented |
| `inet4_route_address` | :material-check: | / |
Expand Down
1 change: 0 additions & 1 deletion docs/clients/apple/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ SFI/SFM/SFT provides an unprivileged TUN implementation through NetworkExtension
| `inet6_address` | :material-check: | / |
| `mtu` | :material-check: | / |
| `gso` | :material-close: | Not implemented |
| `gso_max_size` | :material-close: | Not implemented |
| `auto_route` | :material-check: | / |
| `strict_route` | :material-close:️ | Not implemented |
| `inet4_route_address` | :material-check: | / |
Expand Down
14 changes: 0 additions & 14 deletions docs/configuration/inbound/tun.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ icon: material/alert-decagram
!!! quote "Changes in sing-box 1.8.0"

:material-plus: [gso](#gso)
:material-plus: [gso_max_size](#gso_max_size)
:material-alert-decagram: [stack](#stack)

!!! quote ""
Expand All @@ -23,7 +22,6 @@ icon: material/alert-decagram
"inet6_address": "fdfe:dcba:9876::1/126",
"mtu": 9000,
"gso": false,
"gso_max_size": 65536,
"auto_route": true,
"strict_route": true,
"inet4_route_address": [
Expand Down Expand Up @@ -120,18 +118,6 @@ The maximum transmission unit.

Enable generic segmentation offload.

#### gso_max_size

!!! question "Since sing-box 1.8.0"

!!! quote ""

Only supported on Linux.

Maximum GSO packet size.

`65536` is used by default.

#### auto_route

Set the default route to the Tun.
Expand Down
14 changes: 0 additions & 14 deletions docs/configuration/inbound/tun.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ icon: material/alert-decagram
!!! quote "sing-box 1.8.0 中的更改"

:material-plus: [gso](#gso)
:material-plus: [gso_max_size](#gso_max_size)
:material-alert-decagram: [stack](#stack)

!!! quote ""
Expand All @@ -23,7 +22,6 @@ icon: material/alert-decagram
"inet6_address": "fdfe:dcba:9876::1/126",
"mtu": 9000,
"gso": false,
"gso_max_size": 65536,
"auto_route": true,
"strict_route": true,
"inet4_route_address": [
Expand Down Expand Up @@ -120,18 +118,6 @@ tun 接口的 IPv6 前缀。

启用通用分段卸载。

#### gso_max_size

!!! question "自 sing-box 1.8.0 起"

!!! quote ""

仅支持 Linux。

通用分段卸载包的最大大小。

默认使用 `65536`

#### auto_route

设置到 Tun 的默认路由。
Expand Down
14 changes: 0 additions & 14 deletions docs/configuration/outbound/wireguard.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ icon: material/new-box
!!! quote "Changes in sing-box 1.8.0"

:material-plus: [gso](#gso)
:material-plus: [gso_max_size](#gso_max_size)

### Structure

Expand All @@ -18,7 +17,6 @@ icon: material/new-box
"server_port": 1080,
"system_interface": false,
"gso": false,
"gso_max_size": 65536,
"interface_name": "wg0",
"local_address": [
"10.0.0.2/32"
Expand Down Expand Up @@ -83,18 +81,6 @@ Custom interface name for system interface.

Enable generic segmentation offload for system interface.

#### gso_max_size

!!! question "Since sing-box 1.8.0"

!!! quote ""

Only supported on Linux.

Maximum GSO packet size.

`65536` is used by default.

#### local_address

==Required==
Expand Down
14 changes: 0 additions & 14 deletions docs/configuration/outbound/wireguard.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ icon: material/new-box
!!! quote "sing-box 1.8.0 中的更改"

:material-plus: [gso](#gso)
:material-plus: [gso_max_size](#gso_max_size)

### 结构

Expand All @@ -18,7 +17,6 @@ icon: material/new-box
"server_port": 1080,
"system_interface": false,
"gso": false,
"gso_max_size": 65536,
"interface_name": "wg0",
"local_address": [
"10.0.0.2/32"
Expand Down Expand Up @@ -71,18 +69,6 @@ icon: material/new-box

为系统接口启用通用分段卸载。

#### gso_max_size

!!! question "自 sing-box 1.8.0 起"

!!! quote ""

仅支持 Linux。

通用分段卸载包的最大大小。

默认使用 `65536`

#### local_address

==必填==
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ require (
github.com/sagernet/gvisor v0.0.0-20231209105102-8d27a30e436e
github.com/sagernet/quic-go v0.40.0
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
github.com/sagernet/sing v0.3.0-beta.2
github.com/sagernet/sing v0.3.0-beta.2.0.20231215035907-17dab0e7bc33
github.com/sagernet/sing-dns v0.1.11
github.com/sagernet/sing-mux v0.1.6-0.20231208180947-9053c29513a2
github.com/sagernet/sing-quic v0.1.6-0.20231207143711-eb3cbf9ed054
github.com/sagernet/sing-shadowsocks v0.2.6
github.com/sagernet/sing-shadowsocks2 v0.1.6-0.20231207143709-50439739601a
github.com/sagernet/sing-shadowtls v0.1.4
github.com/sagernet/sing-tun v0.2.0-beta.1
github.com/sagernet/sing-tun v0.2.0-beta.1.0.20231215163740-de4c486b47ab
github.com/sagernet/sing-vmess v0.1.8
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7
github.com/sagernet/tfo-go v0.0.0-20231209031829-7b5343ac1dc6
github.com/sagernet/utls v1.5.4
github.com/sagernet/wireguard-go v0.0.0-20230807125731-5d4a7ef2dc5f
github.com/sagernet/wireguard-go v0.0.0-20231215045610-50d65c2c2dd6
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/sagernet/quic-go v0.40.0/go.mod h1:VqtdhlbkeeG5Okhb3eDMb/9o0EoglReHun
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byLGkEnIYp6grlXfo1QYUfiYFGjewIdc=
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
github.com/sagernet/sing v0.2.18/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo=
github.com/sagernet/sing v0.3.0-beta.2 h1:7wqgP+cMQeHS3M/64WWvJLeX07fLctBkE4+lguAFWeU=
github.com/sagernet/sing v0.3.0-beta.2/go.mod h1:9pfuAH6mZfgnz/YjP6xu5sxx882rfyjpcrTdUpd6w3g=
github.com/sagernet/sing v0.3.0-beta.2.0.20231215035907-17dab0e7bc33 h1:U+jYyznmZDduPLObO6KxIkqJmEujbhI32HpEACAVVO4=
github.com/sagernet/sing v0.3.0-beta.2.0.20231215035907-17dab0e7bc33/go.mod h1:9pfuAH6mZfgnz/YjP6xu5sxx882rfyjpcrTdUpd6w3g=
github.com/sagernet/sing-dns v0.1.11 h1:PPrMCVVrAeR3f5X23I+cmvacXJ+kzuyAsBiWyUKhGSE=
github.com/sagernet/sing-dns v0.1.11/go.mod h1:zJ/YjnYB61SYE+ubMcMqVdpaSvsyQ2iShQGO3vuLvvE=
github.com/sagernet/sing-mux v0.1.6-0.20231208180947-9053c29513a2 h1:rRlYQPbMKmzKX+43XC04gEQvxc45/AxfteRWfcl2/rw=
Expand All @@ -123,8 +123,8 @@ github.com/sagernet/sing-shadowsocks2 v0.1.6-0.20231207143709-50439739601a h1:uY
github.com/sagernet/sing-shadowsocks2 v0.1.6-0.20231207143709-50439739601a/go.mod h1:pjeylQ4ApvpEH7B4PUBrdyJf4xmQkg8BaIzT5fI2fR0=
github.com/sagernet/sing-shadowtls v0.1.4 h1:aTgBSJEgnumzFenPvc+kbD9/W0PywzWevnVpEx6Tw3k=
github.com/sagernet/sing-shadowtls v0.1.4/go.mod h1:F8NBgsY5YN2beQavdgdm1DPlhaKQlaL6lpDdcBglGK4=
github.com/sagernet/sing-tun v0.2.0-beta.1 h1:78AopSXaMxLsay15J2rQIRszZmOXlJs/wVFqnthzGAo=
github.com/sagernet/sing-tun v0.2.0-beta.1/go.mod h1:DgXPnBqtqWrZj37Mun/W61dW0Q56eLqTZYhcuNLaCtY=
github.com/sagernet/sing-tun v0.2.0-beta.1.0.20231215163740-de4c486b47ab h1:YTKoR7kEzUTzIKRor+cEnmk2wNWwJiKHfelYfeGYtQs=
github.com/sagernet/sing-tun v0.2.0-beta.1.0.20231215163740-de4c486b47ab/go.mod h1:DgXPnBqtqWrZj37Mun/W61dW0Q56eLqTZYhcuNLaCtY=
github.com/sagernet/sing-vmess v0.1.8 h1:XVWad1RpTy9b5tPxdm5MCU8cGfrTGdR8qCq6HV2aCNc=
github.com/sagernet/sing-vmess v0.1.8/go.mod h1:vhx32UNzTDUkNwOyIjcZQohre1CaytquC5mPplId8uA=
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 h1:DImB4lELfQhplLTxeq2z31Fpv8CQqqrUwTbrIRumZqQ=
Expand All @@ -133,8 +133,8 @@ github.com/sagernet/tfo-go v0.0.0-20231209031829-7b5343ac1dc6 h1:z3SJQhVyU63FT26
github.com/sagernet/tfo-go v0.0.0-20231209031829-7b5343ac1dc6/go.mod h1:73xRZuxwkFk4aiLw28hG8W6o9cr2UPrGL9pdY2UTbvY=
github.com/sagernet/utls v1.5.4 h1:KmsEGbB2dKUtCNC+44NwAdNAqnqQ6GA4pTO0Yik56co=
github.com/sagernet/utls v1.5.4/go.mod h1:CTGxPWExIloRipK3XFpYv0OVyhO8kk3XCGW/ieyTh1s=
github.com/sagernet/wireguard-go v0.0.0-20230807125731-5d4a7ef2dc5f h1:Kvo8w8Y9lzFGB/7z09MJ3TR99TFtfI/IuY87Ygcycho=
github.com/sagernet/wireguard-go v0.0.0-20230807125731-5d4a7ef2dc5f/go.mod h1:mySs0abhpc/gLlvhoq7HP1RzOaRmIXVeZGCh++zoApk=
github.com/sagernet/wireguard-go v0.0.0-20231215045610-50d65c2c2dd6 h1:p3Jh0wMQgzuM92QHcwx/Fo43CVTk2G23wyd/qWL7H80=
github.com/sagernet/wireguard-go v0.0.0-20231215045610-50d65c2c2dd6/go.mod h1:j5/RnC1gayVOSuauklanMxssxM51iTBsrkFPVb7A4kE=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854 h1:6uUiZcDRnZSAegryaUGwPC/Fj13JSHwiTftrXhMmYOc=
github.com/sagernet/ws v0.0.0-20231204124109-acfe8907c854/go.mod h1:LtfoSK3+NG57tvnVEHgcuBW9ujgE8enPSgzgwStwCAA=
github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9 h1:rc/CcqLH3lh8n+csdOuDfP+NuykE0U6AeYSJJHKDgSg=
Expand Down
5 changes: 0 additions & 5 deletions inbound/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger
if tunMTU == 0 {
tunMTU = 9000
}
gsoMaxSize := options.GSOMaxSize
if gsoMaxSize == 0 {
gsoMaxSize = 65536
}
var udpTimeout int64
if options.UDPTimeout != 0 {
udpTimeout = options.UDPTimeout
Expand Down Expand Up @@ -79,7 +75,6 @@ func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger
Name: options.InterfaceName,
MTU: tunMTU,
GSO: options.GSO,
GSOMaxSize: gsoMaxSize,
Inet4Address: options.Inet4Address,
Inet6Address: options.Inet6Address,
AutoRoute: options.AutoRoute,
Expand Down
Loading

0 comments on commit 3c4287d

Please sign in to comment.