More context for udp

This commit is contained in:
世界 2022-05-18 14:47:17 +08:00
parent c97267395d
commit 170aea03de
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
13 changed files with 41 additions and 42 deletions

View file

@ -203,9 +203,9 @@ func (s *server) NewPacketConnection(ctx context.Context, conn N.PacketConn, met
return N.CopyNetPacketConn(ctx, conn, udpConn)
}
func (s *server) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *server) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
logrus.Trace("inbound raw UDP from ", metadata.Source)
return s.service.NewPacket(conn, buffer, metadata)
return s.service.NewPacket(ctx, conn, buffer, metadata)
}
func (s *server) HandleError(err error) {

View file

@ -38,7 +38,7 @@ type NetPacketConn interface {
}
type UDPHandler interface {
NewPacket(conn PacketConn, buffer *buf.Buffer, metadata M.Metadata) error
NewPacket(ctx context.Context, conn PacketConn, buffer *buf.Buffer, metadata M.Metadata) error
}
type UDPConnectionHandler interface {

View file

@ -263,7 +263,7 @@ func (t *Stack) processIPv4UDP(ipHdr header.IPv4, hdr header.UDP) error {
logger.Trace("[UDP] ", metadata.Source, "=>", metadata.Destination)
t.udpNat.NewPacket(metadata.Source.AddrPort(), func() N.PacketWriter {
t.udpNat.NewPacket(context.Background(), metadata.Source.AddrPort(), func() N.PacketWriter {
return &inetPacketWriter{
tun: t,
headerCache: headerCache,
@ -341,7 +341,7 @@ func (t *Stack) processIPv6UDP(ipHdr header.IPv6, hdr header.UDP) error {
return err
}
t.udpNat.NewPacket(metadata.Source.AddrPort(), func() N.PacketWriter {
t.udpNat.NewPacket(context.Background(), metadata.Source.AddrPort(), func() N.PacketWriter {
return &inet6PacketWriter{
tun: t,
headerCache: headerCache,

View file

@ -39,10 +39,8 @@ func New[K comparable](maxAge int64, handler Handler) *Service[K] {
}
}
func (s *Service[T]) NewPacket(key T, writer func() N.PacketWriter, buffer *buf.Buffer, metadata M.Metadata) {
s.NewContextPacket(context.Background(), key, func() (context.Context, N.PacketWriter) {
return context.Background(), writer()
}, buffer, metadata)
func (s *Service[T]) NewPacket(ctx context.Context, key T, writer func() N.PacketWriter, buffer *buf.Buffer, metadata M.Metadata) {
s.NewContextPacket(ctx, key, func() (context.Context, N.PacketWriter) { return ctx, writer() }, buffer, metadata)
}
func (s *Service[T]) NewContextPacket(ctx context.Context, key T, init func() (context.Context, N.PacketWriter), buffer *buf.Buffer, metadata M.Metadata) {

6
go.mod
View file

@ -19,9 +19,9 @@ require (
github.com/ulikunitz/xz v0.5.10
github.com/v2fly/v2ray-core/v5 v5.0.6
github.com/vishvananda/netlink v1.2.0-beta
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9
golang.org/x/net v0.0.0-20220513224357-95641704303c
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e
golang.zx2c4.com/wireguard v0.0.0-20220407013110-ef5c587f782d
google.golang.org/protobuf v1.28.0
gvisor.dev/gvisor v0.0.0-20220428010907-8082b77961ba

12
go.sum
View file

@ -559,8 +559,8 @@ golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWP
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@ -634,8 +634,8 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220513224357-95641704303c h1:nF9mHSvoKBLkQNQhJZNsc66z2UzAMUbLGjC95CF3pU0=
golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87 h1:cCR+9mKLOGyX4Zx+uBZDXEDAQsvKQ/XbW4vreG5v1jU=
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@ -709,8 +709,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a h1:N2T1jUrTQE9Re6TFF5PhvEHXHCguynGhKjWVsIUt5cY=
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e h1:w36l2Uw3dRan1K3TyXriXvY+6T56GNmlKGcqiQUJDfM=
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View file

@ -205,14 +205,14 @@ func (s *NoneService) NewConnection(ctx context.Context, conn net.Conn, metadata
return s.handler.NewConnection(ctx, conn, metadata)
}
func (s *NoneService) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *NoneService) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
destination, err := M.SocksaddrSerializer.ReadAddrPort(buffer)
if err != nil {
return err
}
metadata.Protocol = "shadowsocks"
metadata.Destination = destination
s.udp.NewPacket(metadata.Source.AddrPort(), func() N.PacketWriter {
s.udp.NewPacket(ctx, metadata.Source.AddrPort(), func() N.PacketWriter {
return &nonePacketWriter{conn, metadata.Source}
}, buffer, metadata)
return nil

View file

@ -190,15 +190,15 @@ func (c *serverConn) Upstream() any {
return c.Conn
}
func (s *Service) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(conn, buffer, metadata)
func (s *Service) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {
err = &shadowsocks.ServerPacketError{Source: metadata.Source, Cause: err}
}
return err
}
func (s *Service) newPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *Service) newPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
if buffer.Len() < s.keySaltLength {
return E.New("bad packet")
}
@ -219,7 +219,7 @@ func (s *Service) newPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Me
metadata.Protocol = "shadowsocks"
metadata.Destination = destination
s.udpNat.NewPacket(metadata.Source.AddrPort(), func() N.PacketWriter {
s.udpNat.NewPacket(ctx, metadata.Source.AddrPort(), func() N.PacketWriter {
return &serverPacketWriter{s, conn, metadata.Source}
}, buffer, metadata)
return nil

View file

@ -170,15 +170,15 @@ func (s *Relay[U]) newConnection(ctx context.Context, conn net.Conn, metadata M.
return s.handler.NewConnection(ctx, conn, metadata)
}
func (s *Relay[U]) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(conn, buffer, metadata)
func (s *Relay[U]) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {
err = &shadowsocks.ServerPacketError{Source: metadata.Source, Cause: err}
}
return err
}
func (s *Relay[U]) newPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *Relay[U]) newPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
packetHeader := buffer.To(aes.BlockSize)
s.udpBlockCipher.Decrypt(packetHeader, packetHeader)
@ -210,9 +210,9 @@ func (s *Relay[U]) newPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.M
metadata.Protocol = "shadowsocks-relay"
metadata.Destination = s.uDestination[user]
s.udpNat.NewContextPacket(context.Background(), sessionId, func() (context.Context, N.PacketWriter) {
s.udpNat.NewContextPacket(ctx, sessionId, func() (context.Context, N.PacketWriter) {
return &shadowsocks.UserContext[U]{
context.Background(),
ctx,
user,
}, &relayPacketWriter[U]{conn, session}
}, buffer, metadata)

View file

@ -285,15 +285,15 @@ func (c *serverConn) Upstream() any {
return c.Conn
}
func (s *Service) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(conn, buffer, metadata)
func (s *Service) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {
err = &shadowsocks.ServerPacketError{Source: metadata.Source, Cause: err}
}
return err
}
func (s *Service) newPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *Service) newPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
var packetHeader []byte
if s.udpCipher != nil {
_, err := s.udpCipher.Open(buffer.Index(PacketNonceSize), buffer.To(PacketNonceSize), buffer.From(PacketNonceSize), nil)
@ -386,7 +386,7 @@ process:
metadata.Destination = destination
session.remoteAddr = metadata.Source
s.udpNat.NewPacket(sessionId, func() N.PacketWriter {
s.udpNat.NewPacket(ctx, sessionId, func() N.PacketWriter {
return &serverPacketWriter{s, conn, session}
}, buffer, metadata)
return nil

View file

@ -205,15 +205,15 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta
}, metadata)
}
func (s *MultiService[U]) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(conn, buffer, metadata)
func (s *MultiService[U]) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
err := s.newPacket(ctx, conn, buffer, metadata)
if err != nil {
err = &shadowsocks.ServerPacketError{Source: metadata.Source, Cause: err}
}
return err
}
func (s *MultiService[U]) newPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
func (s *MultiService[U]) newPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
packetHeader := buffer.To(aes.BlockSize)
s.udpBlockCipher.Decrypt(packetHeader, packetHeader)
@ -315,9 +315,9 @@ process:
metadata.Destination = destination
session.remoteAddr = metadata.Source
s.udpNat.NewContextPacket(context.Background(), sessionId, func() (context.Context, N.PacketWriter) {
s.udpNat.NewContextPacket(ctx, sessionId, func() (context.Context, N.PacketWriter) {
return &shadowsocks.UserContext[U]{
context.Background(),
ctx,
user,
}, &serverPacketWriter{s.Service, conn, session}
}, buffer, metadata)

View file

@ -116,8 +116,8 @@ func (l *Listener) NewConnection(ctx context.Context, conn net.Conn, metadata M.
return http.HandleRequest(ctx, request, conn, l.authenticator, l.handler, metadata)
}
func (l *Listener) NewPacket(conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
l.udpNat.NewPacket(metadata.Source.AddrPort(), func() N.PacketWriter {
func (l *Listener) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error {
l.udpNat.NewPacket(ctx, metadata.Source.AddrPort(), func() N.PacketWriter {
return &tproxyPacketWriter{metadata.Source.UDPAddr()}
}, buffer, metadata)
return nil

View file

@ -1,6 +1,7 @@
package udp
import (
"context"
"net"
"net/netip"
"runtime"
@ -103,7 +104,7 @@ func (l *Listener) loop() {
return
}
buffer.Resize(buf.ReversedHeader, n)
err = l.handler.NewPacket(l, buffer, M.Metadata{
err = l.handler.NewPacket(context.Background(), l, buffer, M.Metadata{
Protocol: "udp",
Source: M.SocksaddrFromNetIP(addr),
})
@ -127,7 +128,7 @@ func (l *Listener) loop() {
continue
}
buffer.Resize(buf.ReversedHeader, n)
err = l.handler.NewPacket(l, buffer, M.Metadata{
err = l.handler.NewPacket(context.Background(), l, buffer, M.Metadata{
Protocol: "tproxy",
Source: M.SocksaddrFromNetIP(addr),
Destination: M.SocksaddrFromNetIP(destination),