socks

Fully featured SOCKS proxy client supporting SOCKSv4, SOCKSv4a, and SOCKSv5. Includes Bind and Associate functionality.
Features
- Supports SOCKS v4, v4a, and v5 protocols.
- Supports the CONNECT, BIND, and ASSOCIATE commands.
- Supports callbacks, promises, and events for proxy connection creation async flow control.
- Supports proxy chaining (CONNECT only).
- Supports user/pass authentication.
- Built in UDP frame creation & parse functions.
- Created with TypeScript, type definitions are provided.
Requirements
- Node.js v6.0+ (Please use v1 for older versions of Node.js)
Looking for v1?
- Docs for v1 are available here
Installation
yarn add socks
or
npm install --save socks
Usage
// TypeScript; // ES6 JavaScript; // Legacy JavaScript;Quick Start Example
Connect to github.com (192.30.253.113) on port 80, using a SOCKS proxy.
const options = proxy: host: '159.203.75.200' // ipv4 or ipv6 or hostname port: 1080 type: 5 // Proxy version (4 or 5) command: 'connect' // SOCKS command (createConnection factory function only supports the connect command) destination: host: '192.30.253.113' // github.com (hostname lookups are supported with SOCKS v4a and 5) port: 80 ; // Async/Awaittry const info = await SocksClient; console; // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) catch err // Handle errors // PromisesSocksClient; // CallbacksSocksClient;Chaining Proxies
Note: Chaining is only supported when using the SOCKS connect command, and chaining can only be done through the special factory chaining function.
This example makes a proxy chain through two SOCKS proxies to ip-api.com. Once the connection to the destination is established it sends an HTTP request to get a JSON response that returns ip info for the requesting ip.
const options = destination: host: 'ip-api.com' // host names are supported with SOCKS v4a and SOCKS v5. port: 80 command: 'connect' // Only the connect command is supported when chaining proxies. proxies: // The chain order is the order in the proxies array, meaning the last proxy will establish a connection to the destination. host: '159.203.75.235' // ipv4, ipv6, or hostname port: 1081 type: 5 host: '104.131.124.203' // ipv4, ipv6, or hostname port: 1081 type: 5 // Async/Awaittry const info = await SocksClient; console; // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers) console // The remote address of the returned socket is the first proxy in the chain. // 159.203.75.235 infosocket; infosocket; catch err // Handle errors // PromisesSocksClient; // CallbacksSocksClient;Bind Example (TCP Relay)
When the bind command is sent to a SOCKS v4/v5 proxy server, the proxy server starts listening on a new TCP port and the proxy relays then remote host information back to the client. When another remote client connects to the proxy server on this port the SOCKS proxy sends a notification that an incoming connection has been accepted to the initial client and a full duplex stream is now established to the initial client and the client that connected to that special port.
const options = proxy: host: '159.203.75.235' // ipv4, ipv6, or hostname port: 1081 type: 5 command: 'bind' // When using BIND, the destination should be the remote client that is expected to connect to the SOCKS proxy. Using 0.0.0.0 makes the Proxy accept any incoming connection on that port. destination: host: '0.0.0.0' port: 0 ; // Creates a new SocksClient instance.const client = options; // When the SOCKS proxy has bound a new port and started listening, this event is fired.client; // When a client connects to the newly bound port on the SOCKS proxy, this event is fired.client; // An error occurred trying to establish this SOCKS connection.client; // Start connection to proxyclient;Associate Example (UDP Relay)
When the associate command is sent to a SOCKS v5 proxy server, it sets up a UDP relay that allows the client to send UDP packets to a remote host through the proxy server, and also receive UDP packet responses back through the proxy server.
const options = proxy: host: '159.203.75.235' // ipv4, ipv6, or hostname port: 1081 type: 5 command: 'associate' // When using associate, the destination should be the remote client that is expected to send UDP packets to the proxy server to be forwarded. This should be your local ip, or optionally the wildcard address (0.0.0.0) UDP Client <-> Proxy <-> UDP Client destination: host: '0.0.0.0' port: 0 ; // Create a local UDP socket for sending packets to the proxy.const udpSocket = dgram;udpSocket; // Listen for incoming UDP packets from the proxy server.udpSocket; let client = associateOptions; // When the UDP relay is established, this event is fired and includes the UDP relay port to send data to on the proxy server.client; // Start connectionclient;Note: The associate TCP connection to the proxy must remain open for the UDP relay to work.
Additional Examples
Migrating from v1
Looking for a guide to migrate from v1? Look here
Api Reference:
Note: socks includes full TypeScript definitions. These can even be used without using TypeScript as most IDEs (such as VS Code) will use these type definition files for auto completion intellisense even in JavaScript files.
- Class: SocksClient
- new SocksClient(options[, callback])
- Class Method: SocksClient.createConnection(options[, callback])
- Class Method: SocksClient.createConnectionChain(options[, callback])
- Class Method: SocksClient.createUDPFrame(options)
- Class Method: SocksClient.parseUDPFrame(data)
- Event: 'error'
- Event: 'bound'
- Event: 'established'
- client.connect()
- client.socksClientOptions
SocksClient
SocksClient establishes SOCKS proxy connections to remote destination hosts. These proxy connections are fully transparent to the server and once established act as full duplex streams. SOCKS v4, v4a, and v5 are supported, as well as the connect, bind, and associate commands.
SocksClient supports creating connections using callbacks, promises, and async/await flow control using two static factory functions createConnection and createConnectionChain. It also internally extends EventEmitter which results in allowing event handling based async flow control.
SOCKS Compatibility Table
| Socks Version | TCP | UDP | IPv4 | IPv6 | Hostname |
|---|---|---|---|---|---|
| SOCKS v4 | ✅ | ❌ | ✅ | ❌ | ❌ |
| SOCKS v4a | ✅ | ❌ | ✅ | ❌ | ✅ |
| SOCKS v5 | ✅ | ✅ | ✅ | ✅ | ✅ |
new SocksClient(options)
options{SocksClientOptions} - An object describing the SOCKS proxy to use, the command to send and establish, and the destination host to connect to.
SocksClientOptions
Class Method: SocksClient.createConnection(options[, callback])
options{ SocksClientOptions } - An object describing the SOCKS proxy to use, the command to send and establish, and the destination host to connect to.callback{ Function } - Optional callback function that is called when the proxy connection is established, or an error occurs.returns{ Promise } - A Promise is returned that is resolved when the proxy connection is established, or rejected when an error occurs.
Creates a new proxy connection through the given proxy to the given destination host. This factory function supports callbacks and promises for async flow control.
Note: If a callback function is provided, the promise will always resolve regardless of an error occurring. Please be sure to exclusively use either promises or callbacks when using this factory function.
// Await/Async (uses a Promise)try catch err // PromiseSocksClient.createConnectionoptions.then.catch; // CallbackSocksClient.createConnectionoptions,;Class Method: SocksClient.createConnectionChain(options[, callback])
options{ SocksClientChainOptions } - An object describing a list of SOCKS proxies to use, the command to send and establish, and the destination host to connect to.callback{ Function } - Optional callback function that is called when the proxy connection chain is established, or an error occurs.returns{ Promise } - A Promise is returned that is resolved when the proxy connection chain is established, or rejected when an error occurs.
Creates a new proxy connection chain through a list of at least two SOCKS proxies to the given destination host. This factory method supports callbacks and promises for async flow control.
Note: If a callback function is provided, the promise will always resolve regardless of an error occurring. Please be sure to exclusively use either promises or callbacks when using this factory function.
Note: At least two proxies must be provided for the chain to be established.
Class Method: SocksClient.createUDPFrame(details)
details{ SocksUDPFrameDetails } - An object containing the remote host, frame number, and frame data to use when creating a SOCKS UDP frame packet.returns{ Buffer } - A Buffer containing all of the UDP frame data.
Creates a SOCKS UDP frame relay packet that is sent and received via a SOCKS proxy when using the associate command for UDP packet forwarding.
SocksUDPFrameDetails
Class Method: SocksClient.parseUDPFrame(data)
data{ Buffer } - A Buffer instance containing SOCKS UDP frame data to parse.returns{ SocksUDPFrameDetails } - An object containing the remote host, frame number, and frame data of the SOCKS UDP frame.
;console.logframe;/*{ frameNumber: 0, remoteHost: { host: '1.2.3.4', port: 1234 }, data: <Buffer 01 02 03 04 ...>}*/Parses a Buffer instance and returns the parsed SocksUDPFrameDetails object.
Event: 'error'
err{ SocksClientError } - An Error object containing an error message and the original SocksClientOptions.
This event is emitted if an error occurs when trying to establish the proxy connection.
Event: 'bound'
info{ SocksClientBoundEvent } An object containing a Socket and SocksRemoteHost info.
This event is emitted when using the BIND command on a remote SOCKS proxy server. This event indicates the proxy server is now listening for incoming connections on a specified port.
SocksClientBoundEvent
Event: 'established'
info{ SocksClientEstablishedEvent } An object containing a Socket and SocksRemoteHost info.
This event is emitted when the following conditions are met:
- When using the CONNECT command, and a proxy connection has been established to the remote host.
- When using the BIND command, and an incoming connection has been accepted by the proxy and a TCP relay has been established.
- When using the ASSOCIATE command, and a UDP relay has been established.
When using BIND, 'bound' is first emitted to indicate the SOCKS server is waiting for an incoming connection, and provides the remote port the SOCKS server is listening on.
When using ASSOCIATE, 'established' is emitted with the remote UDP port the SOCKS server is accepting UDP frame packets on.
SocksClientEstablishedEvent
client.connect()
Starts connecting to the remote SOCKS proxy server to establish a proxy connection to the destination host.
client.socksClientOptions
returns{ SocksClientOptions } The options that were passed to the SocksClient.
Gets the options that were passed to the SocksClient when it was created.
SocksClientError
Further Reading:
Please read the SOCKS 5 specifications for more information on how to use BIND and Associate. http://www.ietf.org/rfc/rfc1928.txt
License
This work is licensed under the MIT license.