MCPcopy Create free account
hub / github.com/dan-v/lambda-nat-proxy / handleSOCKS5Connection

Method handleSOCKS5Connection

internal/socks5/proxy.go:51–182  ·  view source on GitHub ↗

handleSOCKS5Connection handles a single SOCKS5 connection

(clientConn net.Conn, quicConn quic.Connection)

Source from the content-addressed store, hash-verified

49
50// handleSOCKS5Connection handles a single SOCKS5 connection
51func (p *DefaultProxy) handleSOCKS5Connection(clientConn net.Conn, quicConn quic.Connection) {
52 // Generate unique connection ID for tracking
53 connID := generateConnectionID()
54
55 defer func() {
56 clientConn.Close()
57 metrics.DecrementActiveSOCKS5Connections()
58 // Clean up connection tracking
59 dashboard.GlobalConnectionTracker.RemoveConnection(connID)
60 }()
61
62 // Record new connection
63 metrics.RecordSOCKS5Connection()
64 metrics.IncrementActiveSOCKS5Connections()
65 connStart := time.Now()
66
67 log.Printf("📞 New SOCKS5 connection from %s", clientConn.RemoteAddr())
68
69 // Handle SOCKS5 handshake (use optimized buffer size)
70 buf := make([]byte, shared.OptimizedBufferSize)
71 _, err := clientConn.Read(buf)
72 if err != nil {
73 log.Printf("Failed to read SOCKS5 handshake: %v", err)
74 metrics.RecordSOCKS5FailedConnection()
75 return
76 }
77
78 // Respond to SOCKS5 handshake (no auth)
79 if buf[0] == shared.SOCKS5Version {
80 clientConn.Write(shared.SOCKS5AuthResponse)
81 } else {
82 log.Printf("Not a SOCKS5 connection")
83 return
84 }
85
86 // Read SOCKS5 request
87 _, err = clientConn.Read(buf)
88 if err != nil {
89 log.Printf("Failed to read SOCKS5 request: %v", err)
90 return
91 }
92
93 if buf[0] != shared.SOCKS5Version || buf[1] != shared.SOCKS5Connect {
94 log.Printf("Only SOCKS5 CONNECT supported")
95 return
96 }
97
98 // Parse target address
99 var targetAddr string
100 var targetPort uint16
101
102 switch buf[3] { // Address type
103 case shared.SOCKS5IPv4:
104 targetAddr = fmt.Sprintf("%d.%d.%d.%d", buf[4], buf[5], buf[6], buf[7])
105 targetPort = binary.BigEndian.Uint16(buf[8:10])
106 case shared.SOCKS5DomainName:
107 domainLen := buf[4]
108 targetAddr = string(buf[5 : 5+domainLen])

Callers

nothing calls this directly

Calls 12

RecordSOCKS5ConnectionFunction · 0.92
OptimizedCopyWithMetricsFunction · 0.92
RecordSOCKS5LatencyFunction · 0.92
generateConnectionIDFunction · 0.85
RemoveConnectionMethod · 0.80
RemoteAddrMethod · 0.80
AddConnectionMethod · 0.80
UpdateConnectionMethod · 0.80

Tested by

no test coverage detected