mirror of
https://github.com/SagerNet/sing-shadowsocks.git
synced 2025-04-03 20:07:40 +03:00
Improve user context
This commit is contained in:
parent
952ae62e05
commit
45a2ad9cd4
5 changed files with 12 additions and 28 deletions
4
go.mod
4
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
|
||||
)
|
||||
|
|
8
go.sum
8
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=
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue