Skip to content

Commit

Permalink
Revert "Update udpant usages"
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 27, 2024
1 parent 59a6bdc commit 091b5da
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/sagernet/gvisor v0.0.0-20241123041152-536d05261cff
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a
github.com/sagernet/nftables v0.3.0-beta.4
github.com/sagernet/sing v0.6.0-alpha.24
github.com/sagernet/sing v0.6.0-alpha.25
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.26.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.6.0-alpha.24 h1:qPc9i0mHADIFNYlWMg7fWWZZ0kBxWHEs8npsAG6KqAo=
github.com/sagernet/sing v0.6.0-alpha.24/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing v0.6.0-alpha.25 h1:r/UxU+1O6436MjvEXEMRfBBtqMEImgA6uqxezXKZ/Rs=
github.com/sagernet/sing v0.6.0-alpha.25/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=
Expand Down
6 changes: 1 addition & 5 deletions stack_gvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type GVisor struct {
logger logger.Logger
stack *stack.Stack
endpoint stack.LinkEndpoint
udpForwarder *UDPForwarder
}

type GVisorTun interface {
Expand Down Expand Up @@ -72,11 +71,9 @@ func (t *GVisor) Start() error {
return err
}
ipStack.SetTransportProtocolHandler(tcp.ProtocolNumber, NewTCPForwarder(t.ctx, ipStack, t.handler).HandlePacket)
udpForwarder := NewUDPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout)
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout).HandlePacket)
t.stack = ipStack
t.endpoint = linkEndpoint
t.udpForwarder = udpForwarder
return nil
}

Expand All @@ -89,7 +86,6 @@ func (t *GVisor) Close() error {
for _, endpoint := range t.stack.CleanupEndpoints() {
endpoint.Abort()
}
t.udpForwarder.Close()
return nil
}

Expand Down
8 changes: 1 addition & 7 deletions stack_gvisor_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,10 @@ func NewUDPForwarder(ctx context.Context, stack *stack.Stack, handler Handler, t
stack: stack,
handler: handler,
}
udpNat := udpnat.New(handler, forwarder.PreparePacketConnection, timeout, true)
udpNat.Start()
forwarder.udpNat = udpNat
forwarder.udpNat = udpnat.New(handler, forwarder.PreparePacketConnection, timeout, true)
return forwarder
}

func (f *UDPForwarder) Close() {
f.udpNat.Close()
}

func (f *UDPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.PacketBuffer) bool {
source := M.SocksaddrFrom(AddrFromAddress(id.RemoteAddress), id.RemotePort)
destination := M.SocksaddrFrom(AddrFromAddress(id.LocalAddress), id.LocalPort)
Expand Down
10 changes: 3 additions & 7 deletions stack_mixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (

type Mixed struct {
*System
stack *stack.Stack
endpoint *channel.Endpoint
udpForwarder *UDPForwarder
stack *stack.Stack
endpoint *channel.Endpoint
}

func NewMixed(
Expand All @@ -43,11 +42,9 @@ func (m *Mixed) Start() error {
if err != nil {
return err
}
udpForwarder := NewUDPForwarder(m.ctx, ipStack, m.handler, m.udpTimeout)
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(m.ctx, ipStack, m.handler, m.udpTimeout).HandlePacket)
m.stack = ipStack
m.endpoint = endpoint
m.udpForwarder = udpForwarder
go m.tunLoop()
go m.packetLoop()
return nil
Expand All @@ -62,7 +59,6 @@ func (m *Mixed) Close() error {
for _, endpoint := range m.stack.CleanupEndpoints() {
endpoint.Abort()
}
m.udpNat.Close()
return m.System.Close()
}

Expand Down

0 comments on commit 091b5da

Please sign in to comment.