diff -ruN a/proxy/socks/protocol.go b/proxy/socks/protocol.go --- a/proxy/socks/protocol.go 2026-11-09 20:03:46 +++ b/proxy/socks/protocol.go 2026-11-09 20:03:46 @@ -47,6 +47,8 @@ address net.Address port net.Port localAddress net.Address + // RayRay: the client asked for UDP inside this TCP connection (see rayray_udp_in_tcp.go). + fwdUDP bool } func (s *ServerSession) handshake4(cmd byte, reader io.Reader, writer io.Writer) (*protocol.RequestHeader, error) { @@ -174,6 +176,13 @@ return nil, nil, errors.New("UDP is not enabled.") } request.Command = protocol.RequestCommandUDP + case cmdFwdUDP: + if !s.config.UdpEnabled { + writeSocks5Response(writer, statusCmdNotSupport, net.AnyIP, net.Port(0)) + return nil, nil, errors.New("UDP is not enabled.") + } + request.Command = protocol.RequestCommandUDP + s.fwdUDP = true case cmdTCPBind: writeSocks5Response(writer, statusCmdNotSupport, net.AnyIP, net.Port(0)) return nil, nil, errors.New("TCP bind is not supported.") @@ -195,7 +204,7 @@ responsePort := s.port var tempUDPConn *TempUDPConn //nolint:gocritic // Use if else chain for clarity - if request.Command == protocol.RequestCommandUDP { + if request.Command == protocol.RequestCommandUDP && !s.fwdUDP { if s.config.Address != nil { // Use configured IP as remote address in the response to UDP Associate responseAddress = s.config.Address.AsAddress() diff -ruN a/proxy/socks/rayray_udp_in_tcp.go b/proxy/socks/rayray_udp_in_tcp.go --- a/proxy/socks/rayray_udp_in_tcp.go 1970-01-01 03:00:00 +++ b/proxy/socks/rayray_udp_in_tcp.go 2026-11-09 20:04:25 @@ -0,0 +1,86 @@ +package socks + +// RayRay: UDP inside the authenticated TCP connection ("FWD UDP", command 5, as hev-socks5-tunnel sends it). +// With UDP ASSOCIATE every flow gets its own UDP port on loopback that takes the first sender it hears, so any +// app on the device could race for it without the password. Here there is no UDP port at all. +// +// Each datagram both ways: datlen (2 bytes, big endian), hdrlen (1 byte, 3 + address length), the address +// (ATYP, address, port) and the data. + +import ( + "encoding/binary" + "io" + "sync" + + "github.com/xtls/xray-core/common/errors" + "github.com/xtls/xray-core/common/net" +) + +const cmdFwdUDP = 0x05 + +// udpInTCPConn turns the framed stream into standard SOCKS5 UDP packets (RSV, FRAG, address, data) one Read at +// a time, so handleUDPPayload reads and writes it like the UDP socket of an association. +type udpInTCPConn struct { + net.Conn + reader io.Reader + mu sync.Mutex +} + +func (c *udpInTCPConn) Read(b []byte) (int, error) { + var head [3]byte + if _, err := io.ReadFull(c.reader, head[:]); err != nil { + return 0, err + } + datlen, hdrlen := int(binary.BigEndian.Uint16(head[:2])), int(head[2]) + if hdrlen < 5 { + return 0, errors.New("bad UDP-in-TCP header") + } + addrlen := hdrlen - 3 + if 3+addrlen+datlen > len(b) { + // Too big for one buffer: skipped, the stream stays in step. + if _, err := io.CopyN(io.Discard, c.reader, int64(addrlen+datlen)); err != nil { + return 0, err + } + return c.Read(b) + } + b[0], b[1], b[2] = 0, 0, 0 + if _, err := io.ReadFull(c.reader, b[3:3+addrlen+datlen]); err != nil { + return 0, err + } + return 3 + addrlen + datlen, nil +} + +func (c *udpInTCPConn) Write(b []byte) (int, error) { + if len(b) < 4 { + return len(b), nil // an empty packet: the answer was too big and was dropped + } + addrlen := 0 + switch b[3] { + case 0x01: + addrlen = 1 + 4 + 2 + case 0x04: + addrlen = 1 + 16 + 2 + case 0x03: + if len(b) < 5 { + return 0, errors.New("bad UDP packet") + } + addrlen = 1 + 1 + int(b[4]) + 2 + default: + return 0, errors.New("bad UDP address type") + } + if 3+addrlen > len(b) { + return 0, errors.New("bad UDP packet") + } + data := len(b) - 3 - addrlen + // The frame is b with its first 3 bytes (RSV, RSV, FRAG) replaced, and b is the buffer handleUDPPayload encoded this + // one packet into and releases after Write: written in place, no allocation per datagram. + binary.BigEndian.PutUint16(b[:2], uint16(data)) + b[2] = byte(3 + addrlen) + // Answers from different servers can come at once; each frame goes out whole. + c.mu.Lock() + defer c.mu.Unlock() + if _, err := c.Conn.Write(b); err != nil { + return 0, err + } + return len(b), nil +} diff -ruN a/proxy/socks/server.go b/proxy/socks/server.go --- a/proxy/socks/server.go 2026-11-09 20:03:46 +++ b/proxy/socks/server.go 2026-11-09 20:03:46 @@ -165,6 +165,11 @@ return nil } + if request.Command == protocol.RequestCommandUDP && svrSession.fwdUDP { + errors.LogInfo(ctx, "UDP in TCP from ", inbound.Source) + return s.handleUDPPayload(ctx, &udpInTCPConn{Conn: conn, reader: reader}, dispatcher) + } + if request.Command == protocol.RequestCommandUDP { if tempUDPConn == nil { return errors.New("UDP associate with listen port failed")