From 45a2ad9cd41f510c693b17c016a1530d51ca9ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 17 Jul 2022 14:39:01 +0800 Subject: [PATCH] Improve user context --- go.mod | 4 ++-- go.sum | 8 ++++---- shadowaead_2022/relay.go | 9 +++------ shadowaead_2022/service_multi.go | 9 +++------ shadowsocks.go | 10 ---------- 5 files changed, 12 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index 0b9f6f1..6765d91 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/sagernet/sing-shadowsocks go 1.18 require ( - github.com/sagernet/sing v0.0.0-20220714145306-09b55ce4b6d0 + github.com/sagernet/sing v0.0.0-20220717063925-00f98eb6bc34 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d lukechampine.com/blake3 v1.1.7 ) require ( github.com/klauspost/cpuid/v2 v2.0.12 // indirect - golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect ) diff --git a/go.sum b/go.sum index 518cc56..0d37356 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,11 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= -github.com/sagernet/sing v0.0.0-20220714145306-09b55ce4b6d0 h1:8tnMLN6jdqKkjPXwgEekwloPaAmvbxQAMMHdWYOiMj8= -github.com/sagernet/sing v0.0.0-20220714145306-09b55ce4b6d0/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c= +github.com/sagernet/sing v0.0.0-20220717063925-00f98eb6bc34 h1:1kFruA2QzuH2R6txJXEDSasfdxzsjNyzC4Z1kZjMkHg= +github.com/sagernet/sing v0.0.0-20220717063925-00f98eb6bc34/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8= -golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= lukechampine.com/blake3 v1.1.7 h1:GgRMhmdsuK8+ii6UZFDL8Nb+VyMwadAgcJyfYHxG6n0= lukechampine.com/blake3 v1.1.7/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= diff --git a/shadowaead_2022/relay.go b/shadowaead_2022/relay.go index cfdf3fd..529ffeb 100644 --- a/shadowaead_2022/relay.go +++ b/shadowaead_2022/relay.go @@ -12,6 +12,7 @@ import ( "github.com/sagernet/sing-shadowsocks" "github.com/sagernet/sing-shadowsocks/shadowaead" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/auth" "github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/bufio" E "github.com/sagernet/sing/common/exceptions" @@ -183,14 +184,10 @@ func (s *RelayService[U]) newConnection(ctx context.Context, conn net.Conn, meta copy(requestHeader.Range(aes.BlockSize, aes.BlockSize+s.keySaltLength), requestHeader.To(s.keySaltLength)) requestHeader.Advance(aes.BlockSize) - var userCtx shadowsocks.UserContext[U] - userCtx.Context = ctx - userCtx.User = user - metadata.Protocol = "shadowsocks-relay" metadata.Destination = s.uDestination[user] conn = bufio.NewCachedConn(conn, requestHeader) - return s.handler.NewConnection(&userCtx, conn, metadata) + return s.handler.NewConnection(auth.ContextWithUser(ctx, user), conn, metadata) } func (s *RelayService[U]) WriteIsThreadUnsafe() { @@ -229,7 +226,7 @@ func (s *RelayService[U]) newPacket(ctx context.Context, conn N.PacketConn, buff metadata.Protocol = "shadowsocks-relay" metadata.Destination = s.uDestination[user] s.udpNat.NewContextPacket(ctx, sessionId, buffer, metadata, func(natConn N.PacketConn) (context.Context, N.PacketWriter) { - return &shadowsocks.UserContext[U]{ctx, user}, &udpnat.DirectBackWriter{Source: conn, Nat: natConn} + return auth.ContextWithUser(ctx, user), &udpnat.DirectBackWriter{Source: conn, Nat: natConn} }) return nil } diff --git a/shadowaead_2022/service_multi.go b/shadowaead_2022/service_multi.go index 980e400..222dbda 100644 --- a/shadowaead_2022/service_multi.go +++ b/shadowaead_2022/service_multi.go @@ -15,6 +15,7 @@ import ( "github.com/sagernet/sing-shadowsocks" "github.com/sagernet/sing-shadowsocks/shadowaead" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/auth" "github.com/sagernet/sing/common/buf" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" @@ -228,10 +229,6 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta return ErrNoPadding } - var userCtx shadowsocks.UserContext[U] - userCtx.Context = ctx - userCtx.User = user - protocolConn := &serverConn{ Service: s.Service, Conn: conn, @@ -249,7 +246,7 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta metadata.Protocol = "shadowsocks" metadata.Destination = destination - return s.handler.NewConnection(&userCtx, protocolConn, metadata) + return s.handler.NewConnection(auth.ContextWithUser(ctx, user), protocolConn, metadata) } func (s *MultiService[U]) WriteIsThreadUnsafe() { @@ -373,7 +370,7 @@ process: metadata.Protocol = "shadowsocks" metadata.Destination = destination s.udpNat.NewContextPacket(ctx, sessionId, buffer, metadata, func(natConn N.PacketConn) (context.Context, N.PacketWriter) { - return &shadowsocks.UserContext[U]{ctx, user}, &serverPacketWriter{s.Service, conn, natConn, session, s.uCipher[user]} + return auth.ContextWithUser(ctx, user), &serverPacketWriter{s.Service, conn, natConn, session, s.uCipher[user]} }) return nil } diff --git a/shadowsocks.go b/shadowsocks.go index 7f9c642..0b90693 100644 --- a/shadowsocks.go +++ b/shadowsocks.go @@ -1,7 +1,6 @@ package shadowsocks import ( - "context" "crypto/md5" "net" @@ -36,15 +35,6 @@ type Handler interface { E.Handler } -type UserContext[U comparable] struct { - context.Context - User U -} - -func (c *UserContext[U]) Upstream() any { - return c.Context -} - type ServerConnError struct { net.Conn Source M.Socksaddr