Minor fixes

This commit is contained in:
世界 2024-10-23 13:41:50 +08:00
parent ae4fa7739a
commit e47300d843
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 23 additions and 83 deletions

2
go.mod
View file

@ -9,7 +9,7 @@ require (
github.com/sagernet/gvisor v0.0.0-20241021032506-a4324256e4a3 github.com/sagernet/gvisor v0.0.0-20241021032506-a4324256e4a3
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a
github.com/sagernet/nftables v0.3.0-beta.4 github.com/sagernet/nftables v0.3.0-beta.4
github.com/sagernet/sing v0.5.0-rc.4.0.20241021153852-cf58af1a4627 github.com/sagernet/sing v0.5.0-rc.4.0.20241031132246-8303fb022213
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.26.0 golang.org/x/net v0.26.0

4
go.sum
View file

@ -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/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 h1:kbULlAwAC3jvdGAC1P5Fa3GSxVwQJibNenDW2zaXr8I=
github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8= github.com/sagernet/nftables v0.3.0-beta.4/go.mod h1:OQXAjvjNGGFxaTgVCSTRIhYB5/llyVDeapVoENYBDS8=
github.com/sagernet/sing v0.5.0-rc.4.0.20241021153852-cf58af1a4627 h1:wWRmqHPHfyWRPUIGsjAmYshvXF+pC/csl9pAmo/vGpo= github.com/sagernet/sing v0.5.0-rc.4.0.20241031132246-8303fb022213 h1:A0vRogREgWAQf8j+2fx6GymZwahvxYUImkFxU75dqLk=
github.com/sagernet/sing v0.5.0-rc.4.0.20241021153852-cf58af1a4627/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak= github.com/sagernet/sing v0.5.0-rc.4.0.20241031132246-8303fb022213/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= 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 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=

View file

@ -23,7 +23,6 @@ type StackOptions struct {
Context context.Context Context context.Context
Tun Tun Tun Tun
TunOptions Options TunOptions Options
EndpointIndependentNat bool
UDPTimeout time.Duration UDPTimeout time.Duration
Handler Handler Handler Handler
Logger logger.Logger Logger logger.Logger

View file

@ -17,9 +17,6 @@ import (
"github.com/sagernet/gvisor/pkg/tcpip/transport/icmp" "github.com/sagernet/gvisor/pkg/tcpip/transport/icmp"
"github.com/sagernet/gvisor/pkg/tcpip/transport/tcp" "github.com/sagernet/gvisor/pkg/tcpip/transport/tcp"
"github.com/sagernet/gvisor/pkg/tcpip/transport/udp" "github.com/sagernet/gvisor/pkg/tcpip/transport/udp"
"github.com/sagernet/gvisor/pkg/waiter"
"github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
@ -31,15 +28,14 @@ const WithGVisor = true
const defaultNIC tcpip.NICID = 1 const defaultNIC tcpip.NICID = 1
type GVisor struct { type GVisor struct {
ctx context.Context ctx context.Context
tun GVisorTun tun GVisorTun
endpointIndependentNat bool udpTimeout time.Duration
udpTimeout time.Duration broadcastAddr netip.Addr
broadcastAddr netip.Addr handler Handler
handler Handler logger logger.Logger
logger logger.Logger stack *stack.Stack
stack *stack.Stack endpoint stack.LinkEndpoint
endpoint stack.LinkEndpoint
} }
type GVisorTun interface { type GVisorTun interface {
@ -56,13 +52,12 @@ func NewGVisor(
} }
gStack := &GVisor{ gStack := &GVisor{
ctx: options.Context, ctx: options.Context,
tun: gTun, tun: gTun,
endpointIndependentNat: options.EndpointIndependentNat, udpTimeout: options.UDPTimeout,
udpTimeout: options.UDPTimeout, broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address), handler: options.Handler,
handler: options.Handler, logger: options.Logger,
logger: options.Logger,
} }
return gStack, nil return gStack, nil
} }
@ -95,31 +90,7 @@ func (t *GVisor) Start() error {
go t.handler.NewConnectionEx(t.ctx, conn, source, destination, nil) go t.handler.NewConnectionEx(t.ctx, conn, source, destination, nil)
}) })
ipStack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket) ipStack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket)
if !t.endpointIndependentNat { ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout).HandlePacket)
udpForwarder := udp.NewForwarder(ipStack, func(r *udp.ForwarderRequest) {
source := M.SocksaddrFrom(AddrFromAddress(r.ID().RemoteAddress), r.ID().RemotePort)
destination := M.SocksaddrFrom(AddrFromAddress(r.ID().LocalAddress), r.ID().LocalPort)
pErr := t.handler.PrepareConnection(N.NetworkUDP, source, destination)
if pErr != nil {
gWriteUnreachable(t.stack, r.Packet(), err)
r.Packet().DecRef()
return
}
var wq waiter.Queue
endpoint, err := r.CreateEndpoint(&wq)
if err != nil {
return
}
go func() {
ctx, conn := canceler.NewPacketConn(t.ctx, bufio.NewUnbindPacketConnWithAddr(gonet.NewUDPConn(&wq, endpoint), destination), t.udpTimeout)
t.handler.NewPacketConnectionEx(ctx, conn, source, destination, nil)
}()
})
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
} else {
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout).HandlePacket)
}
t.stack = ipStack t.stack = ipStack
t.endpoint = linkEndpoint t.endpoint = linkEndpoint
return nil return nil

View file

@ -5,25 +5,19 @@ package tun
import ( import (
"github.com/sagernet/gvisor/pkg/buffer" "github.com/sagernet/gvisor/pkg/buffer"
"github.com/sagernet/gvisor/pkg/tcpip" "github.com/sagernet/gvisor/pkg/tcpip"
"github.com/sagernet/gvisor/pkg/tcpip/adapters/gonet"
gHdr "github.com/sagernet/gvisor/pkg/tcpip/header" gHdr "github.com/sagernet/gvisor/pkg/tcpip/header"
"github.com/sagernet/gvisor/pkg/tcpip/link/channel" "github.com/sagernet/gvisor/pkg/tcpip/link/channel"
"github.com/sagernet/gvisor/pkg/tcpip/stack" "github.com/sagernet/gvisor/pkg/tcpip/stack"
"github.com/sagernet/gvisor/pkg/tcpip/transport/udp" "github.com/sagernet/gvisor/pkg/tcpip/transport/udp"
"github.com/sagernet/gvisor/pkg/waiter"
"github.com/sagernet/sing-tun/internal/gtcpip/header" "github.com/sagernet/sing-tun/internal/gtcpip/header"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
"github.com/sagernet/sing/common/canceler"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
) )
type Mixed struct { type Mixed struct {
*System *System
endpointIndependentNat bool stack *stack.Stack
stack *stack.Stack endpoint *channel.Endpoint
endpoint *channel.Endpoint
} }
func NewMixed( func NewMixed(
@ -34,8 +28,7 @@ func NewMixed(
return nil, err return nil, err
} }
return &Mixed{ return &Mixed{
System: system.(*System), System: system.(*System),
endpointIndependentNat: options.EndpointIndependentNat,
}, nil }, nil
} }
@ -49,30 +42,7 @@ func (m *Mixed) Start() error {
if err != nil { if err != nil {
return err return err
} }
if !m.endpointIndependentNat { ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(m.ctx, ipStack, m.handler, m.udpTimeout).HandlePacket)
udpForwarder := udp.NewForwarder(ipStack, func(r *udp.ForwarderRequest) {
source := M.SocksaddrFrom(AddrFromAddress(r.ID().RemoteAddress), r.ID().RemotePort)
destination := M.SocksaddrFrom(AddrFromAddress(r.ID().LocalAddress), r.ID().LocalPort)
pErr := m.handler.PrepareConnection(N.NetworkUDP, source, destination)
if pErr != nil {
gWriteUnreachable(m.stack, r.Packet(), err)
r.Packet().DecRef()
return
}
var wq waiter.Queue
endpoint, err := r.CreateEndpoint(&wq)
if err != nil {
return
}
go func() {
ctx, conn := canceler.NewPacketConn(m.ctx, bufio.NewUnbindPacketConnWithAddr(gonet.NewUDPConn(&wq, endpoint), destination), m.udpTimeout)
m.handler.NewPacketConnectionEx(ctx, conn, source, destination, nil)
}()
})
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)
} else {
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(m.ctx, ipStack, m.handler, m.udpTimeout).HandlePacket)
}
m.stack = ipStack m.stack = ipStack
m.endpoint = endpoint m.endpoint = endpoint
go m.tunLoop() go m.tunLoop()

View file

@ -731,7 +731,7 @@ func (w *systemUDPPacketWriter4) WritePacket(buffer *buf.Buffer, destination M.S
newPacket.Write(buffer.Bytes()) newPacket.Write(buffer.Bytes())
ipHdr := header.IPv4(newPacket.Bytes()) ipHdr := header.IPv4(newPacket.Bytes())
ipHdr.SetTotalLength(uint16(newPacket.Len())) ipHdr.SetTotalLength(uint16(newPacket.Len()))
ipHdr.SetSourceAddress(ipHdr.SourceAddress()) ipHdr.SetDestinationAddress(ipHdr.SourceAddress())
ipHdr.SetSourceAddr(destination.Addr) ipHdr.SetSourceAddr(destination.Addr)
udpHdr := header.UDP(ipHdr.Payload()) udpHdr := header.UDP(ipHdr.Payload())
udpHdr.SetDestinationPort(udpHdr.SourcePort()) udpHdr.SetDestinationPort(udpHdr.SourcePort())