From 34cba35e3bedc95a281b74bcace3a60c66a77ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 30 Apr 2022 10:25:56 +0800 Subject: [PATCH] Add more context --- cli/ss-local/main.go | 3 +- cli/ss-server/main.go | 4 +-- cli/uot-local/main.go | 4 +-- common/random/rng.go | 33 +++++++++++++++++-- common/udpnat/service.go | 2 +- protocol/shadowsocks/none.go | 4 +-- protocol/shadowsocks/shadowaead/service.go | 4 +-- .../shadowsocks/shadowaead_2022/service.go | 4 +-- protocol/socks/conn.go | 2 +- protocol/socks/listener.go | 2 +- 10 files changed, 45 insertions(+), 17 deletions(-) diff --git a/cli/ss-local/main.go b/cli/ss-local/main.go index 78a5d1c..63e2db4 100644 --- a/cli/ss-local/main.go +++ b/cli/ss-local/main.go @@ -347,9 +347,8 @@ func (c *client) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me return rw.CopyConn(ctx, serverConn, conn) } -func (c *client) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { +func (c *client) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error { logrus.Info("outbound ", metadata.Protocol, " UDP ", metadata.Source, " ==> ", metadata.Destination) - ctx := context.Background() udpConn, err := c.dialer.DialContext(ctx, "udp", c.server.String()) if err != nil { return err diff --git a/cli/ss-server/main.go b/cli/ss-server/main.go index 96a3df6..1c60168 100644 --- a/cli/ss-server/main.go +++ b/cli/ss-server/main.go @@ -228,13 +228,13 @@ func (s *server) NewConnection(ctx context.Context, conn net.Conn, metadata M.Me return rw.CopyConn(ctx, conn, destConn) } -func (s *server) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { +func (s *server) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error { logrus.Info("inbound UDP ", metadata.Source, " ==> ", metadata.Destination) udpConn, err := net.ListenUDP("udp", nil) if err != nil { return err } - return socks.CopyNetPacketConn(context.Background(), udpConn, conn) + return socks.CopyNetPacketConn(ctx, udpConn, conn) } func (s *server) NewPacket(conn socks.PacketConn, buffer *buf.Buffer, metadata M.Metadata) error { diff --git a/cli/uot-local/main.go b/cli/uot-local/main.go index 798000b..816875c 100644 --- a/cli/uot-local/main.go +++ b/cli/uot-local/main.go @@ -109,7 +109,7 @@ func (c *localClient) NewConnection(ctx context.Context, conn net.Conn, metadata return rw.CopyConn(context.Background(), upstream, conn) } -func (c *localClient) NewPacketConnection(conn socks.PacketConn, _ M.Metadata) error { +func (c *localClient) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error { upstream, err := net.Dial("tcp", c.upstream) if err != nil { return E.Cause(err, "connect to upstream") @@ -121,7 +121,7 @@ func (c *localClient) NewPacketConnection(conn socks.PacketConn, _ M.Metadata) e } client := uot.NewClientConn(upstream) - return socks.CopyPacketConn(context.Background(), client, conn) + return socks.CopyPacketConn(ctx, client, conn) } func (c *localClient) OnError(err error) { diff --git a/common/random/rng.go b/common/random/rng.go index 97e4db8..24374d7 100644 --- a/common/random/rng.go +++ b/common/random/rng.go @@ -2,6 +2,7 @@ package random import ( "crypto/rand" + "encoding/binary" "io" "github.com/sagernet/sing/common" @@ -10,9 +11,37 @@ import ( var System = rand.Reader -func Blake3KeyedHash() io.Reader { +func Blake3KeyedHash() Source { key := make([]byte, 32) common.Must1(io.ReadFull(System, key)) h := blake3.New(1024, key) - return h.XOF() + return Source{h.XOF()} +} + +const ( + rngMax = 1 << 63 + rngMask = rngMax - 1 +) + +type Source struct { + io.Reader +} + +func (s Source) Int63() int64 { + return s.Int64() & rngMask +} + +func (s Source) Int64() int64 { + var num int64 + common.Must(binary.Read(s, binary.BigEndian, &num)) + return num +} + +func (s Source) Uint64() uint64 { + var num uint64 + common.Must(binary.Read(s, binary.BigEndian, &num)) + return num +} + +func (s Source) Seed(int64) { } diff --git a/common/udpnat/service.go b/common/udpnat/service.go index b8d784e..3e655ae 100644 --- a/common/udpnat/service.go +++ b/common/udpnat/service.go @@ -44,7 +44,7 @@ func (s *Service[T]) NewPacket(key T, writer func() socks.PacketWriter, buffer * }) if !loaded { go func() { - err := s.handler.NewPacketConnection(c, metadata) + err := s.handler.NewPacketConnection(c.ctx, c, metadata) if err != nil { s.handler.HandleError(err) } diff --git a/protocol/shadowsocks/none.go b/protocol/shadowsocks/none.go index 276c52d..7abd9a5 100644 --- a/protocol/shadowsocks/none.go +++ b/protocol/shadowsocks/none.go @@ -203,8 +203,8 @@ func (s *nonePacketWriter) WritePacket(buffer *buf.Buffer, destination *M.AddrPo return s.PacketConn.WritePacket(buffer, s.sourceAddr) } -func (s *NoneService) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { - return s.handler.NewPacketConnection(conn, metadata) +func (s *NoneService) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error { + return s.handler.NewPacketConnection(ctx, conn, metadata) } func (s *NoneService) HandleError(err error) { diff --git a/protocol/shadowsocks/shadowaead/service.go b/protocol/shadowsocks/shadowaead/service.go index bc6baf6..a6c6eb2 100644 --- a/protocol/shadowsocks/shadowaead/service.go +++ b/protocol/shadowsocks/shadowaead/service.go @@ -217,8 +217,8 @@ func (w *serverPacketWriter) WritePacket(buffer *buf.Buffer, destination *M.Addr return w.PacketConn.WritePacket(buffer, w.source) } -func (s *Service) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { - return s.handler.NewPacketConnection(conn, metadata) +func (s *Service) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error { + return s.handler.NewPacketConnection(ctx, conn, metadata) } func (s *Service) HandleError(err error) { diff --git a/protocol/shadowsocks/shadowaead_2022/service.go b/protocol/shadowsocks/shadowaead_2022/service.go index 315f3cc..7faeb17 100644 --- a/protocol/shadowsocks/shadowaead_2022/service.go +++ b/protocol/shadowsocks/shadowaead_2022/service.go @@ -382,8 +382,8 @@ func (m *Service) newUDPSession() *serverUDPSession { return session } -func (s *Service) NewPacketConnection(conn socks.PacketConn, metadata M.Metadata) error { - return s.handler.NewPacketConnection(conn, metadata) +func (s *Service) NewPacketConnection(ctx context.Context, conn socks.PacketConn, metadata M.Metadata) error { + return s.handler.NewPacketConnection(ctx, conn, metadata) } func (s *Service) HandleError(err error) { diff --git a/protocol/socks/conn.go b/protocol/socks/conn.go index 0d57524..1ce4acd 100644 --- a/protocol/socks/conn.go +++ b/protocol/socks/conn.go @@ -37,7 +37,7 @@ type UDPHandler interface { } type UDPConnectionHandler interface { - NewPacketConnection(conn PacketConn, metadata M.Metadata) error + NewPacketConnection(ctx context.Context, conn PacketConn, metadata M.Metadata) error } type PacketConnStub struct{} diff --git a/protocol/socks/listener.go b/protocol/socks/listener.go index 3abff6c..40a011d 100644 --- a/protocol/socks/listener.go +++ b/protocol/socks/listener.go @@ -134,7 +134,7 @@ func HandleConnection(ctx context.Context, conn net.Conn, authenticator auth.Aut metadata.Protocol = "socks" metadata.Destination = request.Destination go func() { - err := handler.NewPacketConnection(NewAssociatePacketConn(conn, udpConn, request.Destination), metadata) + err := handler.NewPacketConnection(ctx, NewAssociatePacketConn(conn, udpConn, request.Destination), metadata) if err != nil { handler.HandleError(err) }