"Fossies" - the Fresh Open Source Software Archive

Member "node-v19.8.1-win-x64/node_modules/npm/node_modules/socks/docs/examples/javascript/associateExample.md" (13 Oct 2021, 3364 Bytes) of package /windows/www/node-v19.8.1-win-x64.zip:

As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.

socks examples

Example for SOCKS ‘associate’ command

The associate command tells the SOCKS proxy server to establish a UDP relay. The server binds to a new UDP port and communicates the newly opened port back to the origin client. From here, any SOCKS UDP frame packets sent to this special UDP port on the Proxy server will be forwarded to the desired destination, and any responses will be forwarded back to the origin client (you).

This can be used for things such as DNS queries, and other UDP communicates.

Connection Steps

  1. Client -(associate)-> Proxy (Tells the proxy to create a UDP relay and bind on a new port)
  2. Client <-(port)- Proxy (Tells the origin client which port it opened and is accepting UDP frame packets on)

At this point the proxy is accepting UDP frames on the specified port.

  1. Client –(udp frame) -> Proxy -> Destination (The origin client sends a UDP frame to the proxy on the UDP port, and the proxy then forwards it to the destination specified in the UDP frame.)
  2. Client <–(udp frame) <– Proxy <– Destination (The destination client responds to the udp packet sent in #3)


The ‘associate’ command can only be used by creating a new SocksClient instance and listening for the ‘established’ event.

Note: UDP packets relayed through the proxy servers are encompassed in a special Socks UDP frame format. SocksClient.createUDPFrame() and SocksClient.parseUDPFrame() create and parse these special UDP packets.

const dgram = require('dgram');
const SocksClient = require('socks').SocksClient;

// Create a local UDP socket for sending/receiving packets to/from the proxy.
const udpSocket = dgram.createSocket('udp4');

// Listen for incoming UDP packets from the proxy server.
udpSocket.on('message', (message, rinfo) => {
  { frameNumber: 0,
    remoteHost: { host: '', port: 53 }, // The remote host that replied with a UDP packet
    data: <Buffer 74 65 73 74 0a> // The data

const options = {
  proxy: {
    host: '',
    port: 1081,
    type: 5

  // This should be the ip and port of the expected client that will be sending UDP frames to the newly opened UDP port on the server.
  // Most SOCKS servers accept as a wildcard address to accept UDP frames from any source.
  destination: {
    host: '',
    port: 0

  command: 'associate'

const client = new SocksClient(options);

// This event is fired when the SOCKS server has started listening on a new UDP port for UDP relaying.
client.on('established', info => {
    socket: <Socket ...>,
    remoteHost: { // This is the remote port on the SOCKS proxy server to send UDP frame packets to.
      host: '',
      port: 58232

  // Send a udp frame to on port 53 through the proxy.
  const packet = SocksClient.createUDPFrame({
    remoteHost: { host: '', port: 53 },
    data: Buffer.from('hello') // A DNS lookup in the real world.

  // Send packet.
  udpSocket.send(packet, info.remoteHost.port, info.remoteHost.host);

// SOCKS proxy failed to bind.
client.on('error', () => {
  // Handle errors