diff --git a/proxy/shadowsocks_2022/inbound.go b/proxy/shadowsocks_2022/inbound.go index 9883e00e8..713c05d79 100644 --- a/proxy/shadowsocks_2022/inbound.go +++ b/proxy/shadowsocks_2022/inbound.go @@ -169,11 +169,17 @@ func (i *Inbound) processUDP(ctx context.Context, conn stat.Connection, dispatch } sessionItem.Unlock() } - link, err := sessionItem.EnsureLink(ctx, conn, decoded.Destination, dispatcher, i.policyManager, func(dest net.Destination, payload []byte) ([]byte, error) { - return i.udpCodec.EncodeServerPacket(decoded.SessionID, dest, payload) - }) - if err != nil { - continue + link := sessionItem.link.Load() + if link != nil { + sessionItem.UpdateConn(conn) + } else { + var err error + link, err = sessionItem.EnsureLink(ctx, conn, decoded.Destination, dispatcher, i.policyManager, func(dest net.Destination, payload []byte) ([]byte, error) { + return i.udpCodec.EncodeServerPacket(decoded.SessionID, dest, payload) + }) + if err != nil { + continue + } } payloadBuf := buf.New() diff --git a/proxy/shadowsocks_2022/inbound_multi.go b/proxy/shadowsocks_2022/inbound_multi.go index 746bd27f0..185e06196 100644 --- a/proxy/shadowsocks_2022/inbound_multi.go +++ b/proxy/shadowsocks_2022/inbound_multi.go @@ -336,11 +336,17 @@ func (i *MultiUserInbound) processUDP(ctx context.Context, conn stat.Connection, } sessionItem.Unlock() - link, err := sessionItem.EnsureLink(ctx, conn, decoded.Destination, dispatcher, i.policyManager, func(replyDest net.Destination, payload []byte) ([]byte, error) { - return i.encodeServerUDPPacket(sessionID, userPSK, replyDest, payload) - }) - if err != nil { - continue + link := sessionItem.link.Load() + if link != nil { + sessionItem.UpdateConn(conn) + } else { + var err error + link, err = sessionItem.EnsureLink(ctx, conn, decoded.Destination, dispatcher, i.policyManager, func(replyDest net.Destination, payload []byte) ([]byte, error) { + return i.encodeServerUDPPacket(sessionID, userPSK, replyDest, payload) + }) + if err != nil { + continue + } } pBuf := buf.New() diff --git a/proxy/shadowsocks_2022/inbound_relay.go b/proxy/shadowsocks_2022/inbound_relay.go index 914cb91f0..f92ce2257 100644 --- a/proxy/shadowsocks_2022/inbound_relay.go +++ b/proxy/shadowsocks_2022/inbound_relay.go @@ -253,10 +253,16 @@ func (i *RelayInbound) processUDP(ctx context.Context, conn stat.Connection, dis } sessionItem.Unlock() } - link, err := sessionItem.EnsureLink(ctx, conn, dest, dispatcher, i.policyManager, nil) - if err != nil { - b.Release() - continue + link := sessionItem.link.Load() + if link != nil { + sessionItem.UpdateConn(conn) + } else { + var err error + link, err = sessionItem.EnsureLink(ctx, conn, dest, dispatcher, i.policyManager, nil) + if err != nil { + b.Release() + continue + } } _ = link.Writer.WriteMultiBuffer(buf.MultiBuffer{b}) diff --git a/proxy/shadowsocks_2022/shadowsocks_2022.go b/proxy/shadowsocks_2022/shadowsocks_2022.go index 892e6ed49..1556c330f 100644 --- a/proxy/shadowsocks_2022/shadowsocks_2022.go +++ b/proxy/shadowsocks_2022/shadowsocks_2022.go @@ -19,9 +19,7 @@ import ( func (s *ServerUDPSession) UpdateConn(conn stat.Connection) { s.currentConn.Store(conn) - if s.timer != nil { - s.timer.Update() - } + s.timer.Update() } func (s *ServerUDPSession) WriteToClient(b []byte) error { @@ -55,16 +53,11 @@ func (s *ServerUDPSession) EnsureLink( policyManager policy.Manager, responseEncoder func(dest net.Destination, payload []byte) ([]byte, error), ) (*transport.Link, error) { - s.UpdateConn(conn) - - if link := s.link.Load(); link != nil { - return link, nil - } - s.Lock() defer s.Unlock() if link := s.link.Load(); link != nil { + s.UpdateConn(conn) return link, nil } @@ -118,19 +111,13 @@ func ResetTCPConn(conn net.Conn) { } func handleUDPResponse(s *ServerUDPSession, link *transport.Link, fallbackDest net.Destination, encode func(dest net.Destination, payload []byte) ([]byte, error)) { - defer func() { - if s.timer != nil { - s.timer.SetTimeout(0) - } - }() + defer s.timer.SetTimeout(0) for { resMb, err := link.Reader.ReadMultiBuffer() if err != nil { return } - if s.timer != nil { - s.timer.Update() - } + s.timer.Update() for i, rb := range resMb { b := rb.Bytes() if encode != nil {