From 3b905381fbb252c1a411c227604290c0c741a520 Mon Sep 17 00:00:00 2001 From: Fangliding Date: Thu, 24 Sep 2026 22:14:50 +0800 Subject: [PATCH] fmt --- proxy/shadowsocks_2022/inbound.go | 15 +--------- proxy/shadowsocks_2022/inbound_multi.go | 8 ------ proxy/shadowsocks_2022/inbound_relay.go | 8 ------ proxy/shadowsocks_2022/packet.go | 38 ++++++------------------- proxy/shadowsocks_2022/stream.go | 5 ---- 5 files changed, 9 insertions(+), 65 deletions(-) diff --git a/proxy/shadowsocks_2022/inbound.go b/proxy/shadowsocks_2022/inbound.go index ca9ef9a5d..19c768ee7 100644 --- a/proxy/shadowsocks_2022/inbound.go +++ b/proxy/shadowsocks_2022/inbound.go @@ -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() diff --git a/proxy/shadowsocks_2022/inbound_multi.go b/proxy/shadowsocks_2022/inbound_multi.go index 7de2ab8a7..ba54e4a21 100644 --- a/proxy/shadowsocks_2022/inbound_multi.go +++ b/proxy/shadowsocks_2022/inbound_multi.go @@ -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{ diff --git a/proxy/shadowsocks_2022/inbound_relay.go b/proxy/shadowsocks_2022/inbound_relay.go index 625ba19a2..53eea1b4d 100644 --- a/proxy/shadowsocks_2022/inbound_relay.go +++ b/proxy/shadowsocks_2022/inbound_relay.go @@ -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, diff --git a/proxy/shadowsocks_2022/packet.go b/proxy/shadowsocks_2022/packet.go index d13a9b3a6..7a0429fdf 100644 --- a/proxy/shadowsocks_2022/packet.go +++ b/proxy/shadowsocks_2022/packet.go @@ -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) { diff --git a/proxy/shadowsocks_2022/stream.go b/proxy/shadowsocks_2022/stream.go index 87a3668c5..5d4a3c99f 100644 --- a/proxy/shadowsocks_2022/stream.go +++ b/proxy/shadowsocks_2022/stream.go @@ -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[:])