Skip to content

Commit f58ec68

Browse files
committed
Merge branch 'dev' of https://github.com/SagerNet/sing-tun into meta
2 parents 1aa1d8c + 05e6812 commit f58ec68

17 files changed

+996
-372
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ go 1.18
55
require (
66
github.com/fsnotify/fsnotify v1.7.0
77
github.com/go-ole/go-ole v1.3.0
8-
github.com/metacubex/gvisor v0.0.0-20231206145044-b6960a648d8b
9-
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61
8+
github.com/metacubex/gvisor v0.0.0-20231209122014-3e43224c7bbc
109
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97
1110
github.com/sagernet/sing v0.2.19-0.20231207034108-445cd4f41e3f
1211
github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9
@@ -18,5 +17,5 @@ require (
1817
require (
1918
github.com/google/btree v1.1.2 // indirect
2019
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect
21-
golang.org/x/time v0.4.0 // indirect
20+
golang.org/x/time v0.5.0 // indirect
2221
)

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
44
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
55
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
66
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
7-
github.com/metacubex/gvisor v0.0.0-20231206145044-b6960a648d8b h1:xJHepHYyQ7NOpUkEcz9wC3TnMRFjFQ3KVVqtHC/6G5s=
8-
github.com/metacubex/gvisor v0.0.0-20231206145044-b6960a648d8b/go.mod h1:rhBU9tD5ktoGPBtXUquhWuGJ4u+8ZZzBMi2cAdv9q8Y=
9-
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 h1:5+m7c6AkmAylhauulqN/c5dnh8/KssrE9c93TQrXldA=
10-
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61/go.mod h1:QUQ4RRHD6hGGHdFMEtR8T2P6GS6R3D/CXKdaYHKKXms=
7+
github.com/metacubex/gvisor v0.0.0-20231209122014-3e43224c7bbc h1:+yTZ6q2EeQCAJNpKNEu5j32Pm23ShD38ElIa635wTrk=
8+
github.com/metacubex/gvisor v0.0.0-20231209122014-3e43224c7bbc/go.mod h1:rhBU9tD5ktoGPBtXUquhWuGJ4u+8ZZzBMi2cAdv9q8Y=
119
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6EslacyapiRz7LLSJyr4RajF/BhMVyE=
1210
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
13-
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
1411
github.com/sagernet/sing v0.2.19-0.20231207034108-445cd4f41e3f h1:hYkBnmJjVphGc4b02b4jN46ojh05vACYZI3ciD/V3pA=
1512
github.com/sagernet/sing v0.2.19-0.20231207034108-445cd4f41e3f/go.mod h1:Ce5LNojQOgOiWhiD8pPD6E9H7e2KgtOe3Zxx4Ou5u80=
1613
github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9 h1:rc/CcqLH3lh8n+csdOuDfP+NuykE0U6AeYSJJHKDgSg=
@@ -22,9 +19,8 @@ go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/W
2219
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
2320
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
2421
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
25-
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2622
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2723
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
2824
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
29-
golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
30-
golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
25+
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
26+
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=

stack.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ type Stack interface {
1919
type StackOptions struct {
2020
Context context.Context
2121
Tun Tun
22-
Name string
23-
MTU uint32
24-
Inet4Address []netip.Prefix
25-
Inet6Address []netip.Prefix
22+
TunOptions Options
2623
EndpointIndependentNat bool
2724
UDPTimeout int64
2825
Handler Handler
@@ -31,13 +28,21 @@ type StackOptions struct {
3128
InterfaceFinder control.InterfaceFinder
3229
}
3330

31+
func (o *StackOptions) BufferSize() uint32 {
32+
if o.TunOptions.GSO {
33+
return o.TunOptions.GSOMaxSize
34+
} else {
35+
return o.TunOptions.MTU
36+
}
37+
}
38+
3439
func NewStack(
3540
stack string,
3641
options StackOptions,
3742
) (Stack, error) {
3843
switch stack {
3944
case "":
40-
if WithGVisor {
45+
if WithGVisor && !options.TunOptions.GSO {
4146
return NewMixed(options)
4247
} else {
4348
return NewSystem(options)
@@ -48,8 +53,6 @@ func NewStack(
4853
return NewMixed(options)
4954
case "system":
5055
return NewSystem(options)
51-
case "lwip":
52-
return NewLWIP(options)
5356
default:
5457
return nil, E.New("unknown stack: ", stack)
5558
}

stack_gvisor.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ const defaultNIC tcpip.NICID = 1
3232
type GVisor struct {
3333
ctx context.Context
3434
tun GVisorTun
35-
tunMtu uint32
3635
endpointIndependentNat bool
3736
udpTimeout int64
38-
broadcastAddr netip.Addr
37+
broadcastAddr netip.Addr
3938
handler Handler
4039
logger logger.Logger
4140
stack *stack.Stack
@@ -58,10 +57,9 @@ func NewGVisor(
5857
gStack := &GVisor{
5958
ctx: options.Context,
6059
tun: gTun,
61-
tunMtu: options.MTU,
6260
endpointIndependentNat: options.EndpointIndependentNat,
6361
udpTimeout: options.UDPTimeout,
64-
broadcastAddr: BroadcastAddr(options.Inet4Address),
62+
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
6563
handler: options.Handler,
6664
logger: options.Logger,
6765
}
@@ -73,7 +71,7 @@ func (t *GVisor) Start() error {
7371
if err != nil {
7472
return err
7573
}
76-
linkEndpoint = &LinkEndpointFilter{linkEndpoint, t.broadcastAddr, t.tun.CreateVectorisedWriter()}
74+
linkEndpoint = &LinkEndpointFilter{linkEndpoint, t.broadcastAddr, bufio.NewVectorisedWriter(t.tun)}
7775
ipStack, err := newGVisorStack(linkEndpoint)
7876
if err != nil {
7977
return err

stack_lwip.go

Lines changed: 0 additions & 144 deletions
This file was deleted.

stack_lwip_stub.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

stack_mixed.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewMixed(
3939
}
4040
return &Mixed{
4141
System: system.(*System),
42-
writer: options.Tun.CreateVectorisedWriter(),
42+
writer: bufio.NewVectorisedWriter(options.Tun),
4343
endpointIndependentNat: options.EndpointIndependentNat,
4444
}, nil
4545
}
@@ -96,21 +96,23 @@ func (m *Mixed) tunLoop() {
9696
m.wintunLoop(winTun)
9797
return
9898
}
99-
packetBuffer := make([]byte, m.mtu+PacketOffset)
99+
frontHeadroom := m.tun.FrontHeadroom()
100+
packetBuffer := make([]byte, m.bufferSize+frontHeadroom)
100101
for {
101-
n, err := m.tun.Read(packetBuffer)
102+
n, err := m.tun.Read(packetBuffer[frontHeadroom:])
102103
if err != nil {
103104
return
104105
}
105106
if n < clashtcpip.IPv4PacketMinLength {
106107
continue
107108
}
108-
packet := packetBuffer[PacketOffset:n]
109+
rawPacket := packetBuffer[:frontHeadroom+n]
110+
packet := packetBuffer[frontHeadroom : frontHeadroom+n]
109111
switch ipVersion := packet[0] >> 4; ipVersion {
110112
case 4:
111-
err = m.processIPv4(packet)
113+
err = m.processIPv4(rawPacket, packet)
112114
case 6:
113-
err = m.processIPv6(packet)
115+
err = m.processIPv6(rawPacket, packet)
114116
default:
115117
err = E.New("ip: unknown version: ", ipVersion)
116118
}
@@ -132,9 +134,9 @@ func (m *Mixed) wintunLoop(winTun WinTun) {
132134
}
133135
switch ipVersion := packet[0] >> 4; ipVersion {
134136
case 4:
135-
err = m.processIPv4(packet)
137+
err = m.processIPv4(packet, packet)
136138
case 6:
137-
err = m.processIPv6(packet)
139+
err = m.processIPv6(packet, packet)
138140
default:
139141
err = E.New("ip: unknown version: ", ipVersion)
140142
}
@@ -145,14 +147,14 @@ func (m *Mixed) wintunLoop(winTun WinTun) {
145147
}
146148
}
147149

148-
func (m *Mixed) processIPv4(packet clashtcpip.IPv4Packet) error {
150+
func (m *Mixed) processIPv4(rawPacket []byte, packet clashtcpip.IPv4Packet) error {
149151
destination := packet.DestinationIP()
150152
if destination == m.broadcastAddr || !destination.IsGlobalUnicast() {
151153
return common.Error(m.tun.Write(packet))
152154
}
153155
switch packet.Protocol() {
154156
case clashtcpip.TCP:
155-
return m.processIPv4TCP(packet, packet.Payload())
157+
return m.processIPv4TCP(rawPacket, packet, packet.Payload())
156158
case clashtcpip.UDP:
157159
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
158160
Payload: buffer.MakeWithData(packet),
@@ -161,19 +163,19 @@ func (m *Mixed) processIPv4(packet clashtcpip.IPv4Packet) error {
161163
pkt.DecRef()
162164
return nil
163165
case clashtcpip.ICMP:
164-
return m.processIPv4ICMP(packet, packet.Payload())
166+
return m.processIPv4ICMP(rawPacket, packet, packet.Payload())
165167
default:
166168
return common.Error(m.tun.Write(packet))
167169
}
168170
}
169171

170-
func (m *Mixed) processIPv6(packet clashtcpip.IPv6Packet) error {
172+
func (m *Mixed) processIPv6(rawPacket []byte, packet clashtcpip.IPv6Packet) error {
171173
if !packet.DestinationIP().IsGlobalUnicast() {
172174
return common.Error(m.tun.Write(packet))
173175
}
174176
switch packet.Protocol() {
175177
case clashtcpip.TCP:
176-
return m.processIPv6TCP(packet, packet.Payload())
178+
return m.processIPv6TCP(rawPacket, packet, packet.Payload())
177179
case clashtcpip.UDP:
178180
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{
179181
Payload: buffer.MakeWithData(packet),
@@ -182,7 +184,7 @@ func (m *Mixed) processIPv6(packet clashtcpip.IPv6Packet) error {
182184
pkt.DecRef()
183185
return nil
184186
case clashtcpip.ICMPv6:
185-
return m.processIPv6ICMP(packet, packet.Payload())
187+
return m.processIPv6ICMP(rawPacket, packet, packet.Payload())
186188
default:
187189
return common.Error(m.tun.Write(packet))
188190
}

0 commit comments

Comments
 (0)