diff --git a/internal/socks5/socks5.go b/internal/socks5/socks5.go index 2dc66ed4..c7595629 100644 --- a/internal/socks5/socks5.go +++ b/internal/socks5/socks5.go @@ -211,13 +211,6 @@ func (c *socksConn) handleUDPAssociateRequest(ctx context.Context, return fmt.Errorf("getting udp association addresses: %w", err) } - err = c.encodeSuccessResponse(c.clientConn, socksVersion, succeeded, - bindAddrType, bindAddress, bindPort) - if err != nil { - c.encodeFailedResponse(c.clientConn, socksVersion, generalServerFailure) - return fmt.Errorf("writing successful %s response: %w", udpAssociate, err) - } - association, err := c.udpRouter.registerAssociation(c.clientConn, expectedAddrPort) if err != nil { c.encodeFailedResponse(c.clientConn, socksVersion, generalServerFailure) @@ -225,6 +218,13 @@ func (c *socksConn) handleUDPAssociateRequest(ctx context.Context, } defer c.udpRouter.unregisterAssociation(association) + err = c.encodeSuccessResponse(c.clientConn, socksVersion, succeeded, + bindAddrType, bindAddress, bindPort) + if err != nil { + c.encodeFailedResponse(c.clientConn, socksVersion, generalServerFailure) + return fmt.Errorf("writing successful %s response: %w", udpAssociate, err) + } + associationCtx, associationCancel := context.WithCancel(ctx) defer associationCancel()