Skip to content

feat(socks5): UDP proxying#3353

Draft
qdm12 wants to merge 3 commits into
masterfrom
qdm12/socks5-udp
Draft

feat(socks5): UDP proxying#3353
qdm12 wants to merge 3 commits into
masterfrom
qdm12/socks5-udp

Conversation

@qdm12
Copy link
Copy Markdown
Member

@qdm12 qdm12 commented May 27, 2026

Description

Issue

#3349

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds SOCKS5 UDP proxying support to address issue #3349 (UDP traffic not working through the SOCKS5 proxy), extending the existing SOCKS5 TCP proxy implementation with UDP_ASSOCIATE handling and routing.

Changes:

  • Add a UDP router/association mechanism to relay SOCKS5 UDP datagrams between client and destination.
  • Extend SOCKS5 request handling to support the UDP associate command.
  • Add tests covering parallel TCP + UDP proxying and update server logging expectations.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
internal/socks5/udp_router.go Introduces UDP listener and association routing/forwarding logic.
internal/socks5/socks5.go Adds UDP_ASSOCIATE handling plus SOCKS5 UDP datagram encode/decode helpers.
internal/socks5/socks5_test.go Adds parallel TCP+UDP proxy round-trip tests and adjusts expectations.
internal/socks5/server.go Starts UDP router alongside TCP listener; updates stop/start behavior and logging.
internal/socks5/response.go Minor signature/lint adjustment for failure responses.
internal/socks5/constants.go Removes unused bind command constant and related string mapping.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/socks5/udp_router.go Outdated
Comment thread internal/socks5/socks5.go
Comment on lines +114 to +126
switch request.command {
case connect:
err = c.handleConnectRequest(ctx, socksVersion, request)
if err != nil {
return fmt.Errorf("handling %s request: %w", request.command, err)
}
return nil
case udpAssociate:
err = c.handleUDPAssociateRequest(ctx, socksVersion)
if err != nil {
return fmt.Errorf("handling %s request: %w", request.command, err)
}
return nil
Comment thread internal/socks5/socks5.go
Comment on lines +242 to +266
localAddress := c.udpRouter.localAddress().String()
host, portString, err := net.SplitHostPort(localAddress)
if err != nil {
return "", 0, 0, fmt.Errorf("splitting local address: %w", err)
}
port, err := strconv.ParseUint(portString, 10, 16)
if err != nil {
return "", 0, 0, fmt.Errorf("parsing local port: %w", err)
}
bindAddress = host
bindPort = uint16(port)

ipAddress := net.ParseIP(bindAddress)
if ipAddress == nil {
bindAddrType = domainName
return bindAddress, bindPort, bindAddrType, nil
}

if ipAddress.To4() != nil {
bindAddrType = ipv4
} else {
bindAddrType = ipv6
}

return bindAddress, bindPort, bindAddrType, nil
Comment on lines +180 to +198
sourceAddr := sourceAddrPort.Addr()

pendingAssociations := r.clientIPToPendingAssociations[sourceAddr]
if len(pendingAssociations) == 0 {
return udpAssociation{}, false
}

association = pendingAssociations[0]
r.clientIPToPendingAssociations[sourceAddr] = pendingAssociations[1:]
if len(r.clientIPToPendingAssociations[sourceAddr]) == 0 {
delete(r.clientIPToPendingAssociations, sourceAddr)
}

association.clientAddrPort = sourceAddrPort
r.clientAddrPortToAssociation[sourceAddrPort] = association
r.associationIDToClientAddrPort[association.id] = sourceAddrPort

return association, true
}
Comment thread internal/socks5/socks5.go
@qdm12 qdm12 deployed to secrets May 27, 2026 01:01 — with GitHub Actions Active
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Comment thread .vscode/settings.json
Comment on lines +114 to +117
if association.clientAddrPort.IsValid() {
delete(r.clientAddrPortToAssociation, association.clientAddrPort)
}

listener: listener,
bufferPool: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, pooledUDPPacketBufferCapacity))
Comment on lines +164 to +180
func (r *udpRouter) routePacket(sourceAddrPort netip.AddrPort, packet *bytes.Buffer) error {
r.mutex.Lock()
defer r.mutex.Unlock()
association, packetFromClient := r.findClientAssociation(sourceAddrPort)
if !packetFromClient {
r.bufferPool.Put(packet)
return nil
}

select {
case association.packetCh <- packet:
return nil
default:
r.bufferPool.Put(packet)
return errors.New("association packet queue full")
}
}
Comment thread internal/socks5/socks5.go
Comment on lines +236 to +241
go func() {
_, _ = io.Copy(io.Discard, c.clientConn)
associationCancel()
}()
<-associationCtx.Done()
<-handlerDone
Comment thread internal/socks5/server.go
Comment on lines +55 to +56
s.logger.Infof("SOCKS5 TCP server listening on %s", s.tcpListener.Addr())
s.logger.Infof("SOCKS5 UDP server listening on %s", s.udpRouter.localAddress())
Comment on lines +329 to +347
func (r *udpRouter) writeClientPacketToDestination(ctx context.Context,
socket net.PacketConn, packet *bytes.Buffer,
) error {
destination, payload, err := decodeUDPDatagram(packet.Bytes())
if err != nil {
return fmt.Errorf("decoding UDP datagram: %w", err)
}

resolvedDestinationUDPAddress, err := net.ResolveUDPAddr("udp", destination)
if err != nil {
return fmt.Errorf("resolving destination UDP address: %w", err)
}

_, err = socket.WriteTo(payload, resolvedDestinationUDPAddress)
if err != nil && ctx.Err() == nil {
return fmt.Errorf("writing payload to destination: %w", err)
}

return nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants