This commit is contained in:
Fangliding
2026-09-24 22:14:50 +08:00
parent ea3275a77b
commit 3b905381fb
5 changed files with 9 additions and 65 deletions
+1 -14
View File
@@ -85,6 +85,7 @@ func (i *Inbound) Process(ctx context.Context, network net.Network, connection s
inbound := session.InboundFromContext(ctx)
inbound.Name = "shadowsocks-2022"
inbound.CanSpliceCopy = 3
inbound.User = i.user
if network == net.Network_TCP {
return i.processTCP(ctx, connection, dispatcher)
@@ -130,13 +131,6 @@ func (i *Inbound) processTCP(ctx context.Context, conn net.Conn, dispatcher rout
return err
}
inbound := session.InboundFromContext(ctx)
if inbound == nil {
inbound = new(session.Inbound)
ctx = session.ContextWithInbound(ctx, inbound)
}
inbound.User = i.user
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: conn.RemoteAddr(),
To: dest,
@@ -187,13 +181,6 @@ func (i *Inbound) processUDP(ctx context.Context, conn stat.Connection, dispatch
})
}()
inbound := session.InboundFromContext(ctx)
if inbound == nil {
inbound = new(session.Inbound)
ctx = session.ContextWithInbound(ctx, inbound)
}
inbound.User = i.user
reader := buf.NewReader(conn)
for {
mb, err := reader.ReadMultiBuffer()
-8
View File
@@ -266,10 +266,6 @@ func (i *MultiUserInbound) processTCP(ctx context.Context, conn net.Conn, dispat
// 7. Dispatch Connection to Xray routing with matched User
inbound := session.InboundFromContext(ctx)
if inbound == nil {
inbound = new(session.Inbound)
ctx = session.ContextWithInbound(ctx, inbound)
}
inbound.User = user
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
@@ -442,10 +438,6 @@ func (i *MultiUserInbound) processUDP(ctx context.Context, conn stat.Connection,
if !ok {
sessCtx, cancel := context.WithCancel(ctx)
inbound := session.InboundFromContext(sessCtx)
if inbound == nil {
inbound = new(session.Inbound)
sessCtx = session.ContextWithInbound(sessCtx, inbound)
}
inbound.User = currentUser
sessCtx = log.ContextWithAccessMessage(sessCtx, &log.AccessMessage{
-8
View File
@@ -166,10 +166,6 @@ func (i *RelayInbound) processTCP(ctx context.Context, conn net.Conn, dispatcher
_ = conn.SetReadDeadline(time.Time{})
inbound := session.InboundFromContext(ctx)
if inbound == nil {
inbound = new(session.Inbound)
ctx = session.ContextWithInbound(ctx, inbound)
}
inbound.User = &protocol.MemoryUser{
Email: targetDest.email,
Level: targetDest.level,
@@ -271,10 +267,6 @@ func (i *RelayInbound) processUDP(ctx context.Context, conn stat.Connection, dis
if !ok {
sessCtx, cancel := context.WithCancel(ctx)
inbound := session.InboundFromContext(sessCtx)
if inbound == nil {
inbound = new(session.Inbound)
sessCtx = session.ContextWithInbound(sessCtx, inbound)
}
inbound.User = &protocol.MemoryUser{
Email: targetDest.email,
Level: targetDest.level,
+8 -30
View File
@@ -26,8 +26,10 @@ type UDPCodec struct {
sessions *UDPSessionManager
}
type UDPPacketCodec = UDPCodec
type UDPServerCodec = UDPCodec
type (
UDPPacketCodec = UDPCodec
UDPServerCodec = UDPCodec
)
func newUDPCodec(method *CipherMethod, psk []byte) (*UDPCodec, error) {
c := &UDPCodec{
@@ -145,15 +147,6 @@ func (c *UDPCodec) EncodeClientPacket(dest net.Destination, payload []byte) (*bu
outBuf.Write(encryptedHeader[:])
bodyAead := c.clientBodyCipher
if bodyAead == nil {
bodyKey := DeriveSessionSubKey(c.psk, rawHeader[:8], c.method.KeySaltLength)
var err error
bodyAead, err = c.method.NewAEAD(bodyKey)
if err != nil {
outBuf.Release()
return nil, err
}
}
var hdr [1 + 8 + 2]byte
hdr[0] = HeaderTypeClient
@@ -353,10 +346,6 @@ func (c *UDPCodec) DecodePacket(data []byte) (DecodedUDPPacket, error) {
return parsePlainUDPPacket(sessionID, packetID, bodyPlain)
}
func (c *UDPCodec) Sessions() *UDPSessionManager {
return c.sessions
}
func (s *ServerUDPSession) EnsureServerState(method *CipherMethod, headerBlock cipher.Block, chachaCipher cipher.AEAD, psk []byte) error {
s.Lock()
defer s.Unlock()
@@ -454,23 +443,12 @@ func (s *ServerUDPSession) EncodeServerPacket(method *CipherMethod, clientSessio
return res, nil
}
func EncodeServerPacket(method *CipherMethod, headerBlock cipher.Block, chachaAEAD cipher.AEAD, psk []byte, clientSessionID uint64, dest net.Destination, payload []byte) ([]byte, error) {
tempSession := &ServerUDPSession{SessionID: clientSessionID}
if err := tempSession.EnsureServerState(method, headerBlock, chachaAEAD, psk); err != nil {
func (c *UDPCodec) EncodeServerPacket(clientSessionID uint64, dest net.Destination, payload []byte) ([]byte, error) {
sessionItem, _ := c.sessions.GetOrCreate(clientSessionID)
if err := sessionItem.EnsureServerState(c.method, c.blockCipher, c.chachaCipher, c.psk); err != nil {
return nil, err
}
return tempSession.EncodeServerPacket(method, clientSessionID, dest, payload)
}
func (c *UDPCodec) EncodeServerPacket(clientSessionID uint64, dest net.Destination, payload []byte) ([]byte, error) {
if c.sessions != nil {
sessionItem, _ := c.sessions.GetOrCreate(clientSessionID)
if err := sessionItem.EnsureServerState(c.method, c.blockCipher, c.chachaCipher, c.psk); err != nil {
return nil, err
}
return sessionItem.EncodeServerPacket(c.method, clientSessionID, dest, payload)
}
return EncodeServerPacket(c.method, c.blockCipher, c.chachaCipher, c.psk, clientSessionID, dest, payload)
return sessionItem.EncodeServerPacket(c.method, clientSessionID, dest, payload)
}
func (c *UDPCodec) EncodePacket(clientSessionID uint64, dest net.Destination, payload []byte) ([]byte, error) {
-5
View File
@@ -94,11 +94,6 @@ func (w *StreamWriter) WriteChunk(payload []byte) error {
return errors.New("payload exceeds MaxPacketSize")
}
totalSize := 2 + AEADTagSize + payloadLen + AEADTagSize
if cap(w.buf) < totalSize {
w.buf = make([]byte, 0, totalSize)
}
binary.BigEndian.PutUint16(w.lenBuf[:], uint16(payloadLen))
w.buf = w.cipher.Seal(w.buf[:0], w.nonce[:], w.lenBuf[:], nil)
IncreaseNonce(w.nonce[:])