Fully featured SOCKS proxy client supporting SOCKSv4, SOCKSv4a, and SOCKSv5 with Bind and Associate functionality.
—
SOCKS v5 UDP frame creation and parsing functionality for use with the ASSOCIATE command. This enables UDP data transmission through SOCKS proxies with proper frame encapsulation.
Creates a properly formatted SOCKS v5 UDP frame for transmission through a SOCKS proxy.
/**
* Creates a SOCKS UDP Frame for SOCKS v5 ASSOCIATE command
* @param options - Frame configuration including remote host and data
* @returns Buffer containing the formatted UDP frame
*/
static createUDPFrame(options: SocksUDPFrameDetails): Buffer;
interface SocksUDPFrameDetails {
/** Frame number identifier (optional) */
frameNumber?: number;
/** Remote host information (destination) */
remoteHost: SocksRemoteHost;
/** UDP packet data to encapsulate */
data: Buffer;
}
interface SocksRemoteHost {
/** IPv4, IPv6 address, or hostname */
host: string;
/** Port number (0-65535) */
port: number;
}Usage Example:
import { SocksClient } from "socks";
const udpData = Buffer.from("Hello UDP!");
const frame = SocksClient.createUDPFrame({
frameNumber: 1,
remoteHost: {
host: "example.com",
port: 53
},
data: udpData
});
// Send frame through established ASSOCIATE connection
associateSocket.write(frame);Parses a received SOCKS v5 UDP frame to extract the remote host information and data payload.
/**
* Parses a SOCKS UDP frame received from a SOCKS proxy
* @param data - Buffer containing the UDP frame data
* @returns Parsed frame details including remote host and payload
*/
static parseUDPFrame(data: Buffer): SocksUDPFrameDetails;Usage Example:
import { SocksClient } from "socks";
// Receive UDP frame from SOCKS proxy
associateSocket.on('data', (frameData: Buffer) => {
const parsedFrame = SocksClient.parseUDPFrame(frameData);
console.log('Frame number:', parsedFrame.frameNumber);
console.log('From:', parsedFrame.remoteHost.host, ':', parsedFrame.remoteHost.port);
console.log('Data:', parsedFrame.data.toString());
});SOCKS v5 UDP frames follow this binary structure:
+------+------+------+----------+----------+----------+
| RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
+------+------+------+----------+----------+----------+
| 2 | 1 | 1 | Variable | 2 | Variable |
+------+------+------+----------+----------+----------+The frame creation and parsing automatically handles different address types:
import { SocksClient } from "socks";
// Establish ASSOCIATE connection
const info = await SocksClient.createConnection({
proxy: {
host: '127.0.0.1',
port: 1080,
type: 5
},
command: 'associate',
destination: {
host: '0.0.0.0', // Placeholder for UDP
port: 0
}
});
const udpSocket = info.socket;
// Create and send UDP frame
const frame = SocksClient.createUDPFrame({
frameNumber: 1,
remoteHost: {
host: "8.8.8.8",
port: 53
},
data: Buffer.from([/* DNS query bytes */])
});
udpSocket.write(frame);
// Parse received UDP frames
udpSocket.on('data', (data: Buffer) => {
const parsedFrame = SocksClient.parseUDPFrame(data);
console.log('UDP response from:', parsedFrame.remoteHost.host);
// Process parsedFrame.data
});Install with Tessl CLI
npx tessl i tessl/npm-socks