feat(socks5): UDP proxying#3353
Draft
qdm12 wants to merge 3 commits into
Draft
Conversation
There was a problem hiding this comment.
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 associatecommand. - 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 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 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 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 on lines
+236
to
+241
| go func() { | ||
| _, _ = io.Copy(io.Discard, c.clientConn) | ||
| associationCancel() | ||
| }() | ||
| <-associationCtx.Done() | ||
| <-handlerDone |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Issue
#3349