Socks5UdpAssociation

A live SOCKS5 UDP ASSOCIATE relay (RFC 1928 §6–§7). The control TCP connection (control) is held open for the lifetime of the association (the proxy shuts the UDP relay down when that connection closes), while datagrams flow over relay, a plain UdpSocket.

The engine routes uTP / DHT / UDP-tracker traffic through this object: it wraps an outbound payload with the SOCKS5 request header and sends it to relayHost:relayPort, and unwraps inbound datagrams (which carry the SOCKS5 reply header) back into (host, port, payload).

This mirrors libtorrent's socks5 helper in udp_socket.cpp, whose wrap/socks5_unwrap functions build and parse the exact same RSV[2] FRAG ATYP ADDR PORT DATA framing.

Properties

Link copied to clipboard

The kept-alive control connection; closing it ends the association.

Link copied to clipboard

The UDP socket all relayed datagrams are sent on / received from.

Link copied to clipboard

The relay endpoint host the proxy told us to send wrapped datagrams to.

Link copied to clipboard

The relay endpoint port the proxy told us to send wrapped datagrams to.

Functions

Link copied to clipboard
fun close()

Close the relay socket and the control connection, ending the UDP association.

Link copied to clipboard
suspend fun receive(): Socks5UdpDatagram?

Receive one datagram from the relay and unwrap it. Returns null when the datagram is not a valid SOCKS5-wrapped packet (e.g. fragmented), so the caller can simply loop again.

Link copied to clipboard
suspend fun send(targetHost: String, targetPort: Int, payload: ByteArray)

Send payload to targetHost:targetPort through the relay (a thin wrap + UDP send).

Link copied to clipboard

Parse a datagram received on relay off the relay, stripping the SOCKS5 reply header (RSV[2] FRAG ATYP ADDR PORT). Returns the origin (host, port) and the inner payload, or null if the datagram is too short, fragmented (FRAG != 0), or otherwise malformed: the same packets libtorrent's socks5_unwrap returns false for and the caller drops.

Link copied to clipboard
fun wrap(targetHost: String, targetPort: Int, payload: ByteArray): ByteArray

Build a SOCKS5 UDP request datagram: RSV(0x0000) FRAG(0x00) ATYP DST.ADDR DST.PORT DATA targeting targetHost:targetPort with body payload. The result is ready to be sent to relayHost:relayPort. FRAG is always 0 (no fragmentation), matching libtorrent.