Gathering detailed insights and metrics for socks
Gathering detailed insights and metrics for socks
Gathering detailed insights and metrics for socks
Gathering detailed insights and metrics for socks
socks-proxy-agent
A SOCKS proxy `http.Agent` implementation for HTTP and HTTPS
fetch-socks
Socks proxy for Node builtin `fetch`
socks5-client
SOCKS v5 client socket implementation.
@httptoolkit/socks-proxy-agent
A SOCKS proxy `http.Agent` implementation for HTTP and HTTPS
Fully featured SOCKS proxy client supporting SOCKSv4, SOCKSv4a, and SOCKSv5. Includes Bind and Associate functionality.
npm install socks
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
304 Stars
149 Commits
35 Forks
8 Watching
10 Branches
15 Contributors
Updated on 12 Nov 2024
TypeScript (99.7%)
JavaScript (0.3%)
Cumulative downloads
Total Downloads
Last day
-7.2%
3,879,453
Compared to previous day
Last week
1.6%
22,648,133
Compared to previous week
Last month
7.4%
95,116,454
Compared to previous month
Last year
22.6%
976,694,727
Compared to previous year
Fully featured SOCKS proxy client supporting SOCKSv4, SOCKSv4a, and SOCKSv5. Includes Bind and Associate functionality.
Looking for Node.js agent? Check node-socks-proxy-agent.
yarn add socks
or
npm install --save socks
1// TypeScript 2import { SocksClient, SocksClientOptions, SocksClientChainOptions } from 'socks'; 3 4// ES6 JavaScript 5import { SocksClient } from 'socks'; 6 7// Legacy JavaScript 8const SocksClient = require('socks').SocksClient;
Connect to github.com (192.30.253.113) on port 80, using a SOCKS proxy.
1const options = { 2 proxy: { 3 host: '159.203.75.200', // ipv4 or ipv6 or hostname 4 port: 1080, 5 type: 5 // Proxy version (4 or 5) 6 }, 7 8 command: 'connect', // SOCKS command (createConnection factory function only supports the connect command) 9 10 destination: { 11 host: '192.30.253.113', // github.com (hostname lookups are supported with SOCKS v4a and 5) 12 port: 80 13 } 14}; 15 16// Async/Await 17try { 18 const info = await SocksClient.createConnection(options); 19 20 console.log(info.socket); 21 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) 22} catch (err) { 23 // Handle errors 24} 25 26// Promises 27SocksClient.createConnection(options) 28.then(info => { 29 console.log(info.socket); 30 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) 31}) 32.catch(err => { 33 // Handle errors 34}); 35 36// Callbacks 37SocksClient.createConnection(options, (err, info) => { 38 if (!err) { 39 console.log(info.socket); 40 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) 41 } else { 42 // Handle errors 43 } 44});
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.
1const options = { 2 destination: { 3 host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5. 4 port: 80 5 }, 6 command: 'connect', // Only the connect command is supported when chaining proxies. 7 proxies: [ // The chain order is the order in the proxies array, meaning the last proxy will establish a connection to the destination. 8 { 9 host: '159.203.75.235', // ipv4, ipv6, or hostname 10 port: 1081, 11 type: 5 12 }, 13 { 14 host: '104.131.124.203', // ipv4, ipv6, or hostname 15 port: 1081, 16 type: 5 17 } 18 ] 19} 20 21// Async/Await 22try { 23 const info = await SocksClient.createConnectionChain(options); 24 25 console.log(info.socket); 26 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers) 27 28 console.log(info.socket.remoteAddress) // The remote address of the returned socket is the first proxy in the chain. 29 // 159.203.75.235 30 31 info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n'); 32 info.socket.on('data', (data) => { 33 console.log(data.toString()); // ip-api.com sees that the last proxy in the chain (104.131.124.203) is connected to it. 34 /* 35 HTTP/1.1 200 OK 36 Access-Control-Allow-Origin: * 37 Content-Type: application/json; charset=utf-8 38 Date: Sun, 24 Dec 2017 03:47:51 GMT 39 Content-Length: 300 40 41 { 42 "as":"AS14061 Digital Ocean, Inc.", 43 "city":"Clifton", 44 "country":"United States", 45 "countryCode":"US", 46 "isp":"Digital Ocean", 47 "lat":40.8326, 48 "lon":-74.1307, 49 "org":"Digital Ocean", 50 "query":"104.131.124.203", 51 "region":"NJ", 52 "regionName":"New Jersey", 53 "status":"success", 54 "timezone":"America/New_York", 55 "zip":"07014" 56 } 57 */ 58 }); 59} catch (err) { 60 // Handle errors 61} 62 63// Promises 64SocksClient.createConnectionChain(options) 65.then(info => { 66 console.log(info.socket); 67 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) 68 69 console.log(info.socket.remoteAddress) // The remote address of the returned socket is the first proxy in the chain. 70 // 159.203.75.235 71 72 info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n'); 73 info.socket.on('data', (data) => { 74 console.log(data.toString()); // ip-api.com sees that the last proxy in the chain (104.131.124.203) is connected to it. 75 /* 76 HTTP/1.1 200 OK 77 Access-Control-Allow-Origin: * 78 Content-Type: application/json; charset=utf-8 79 Date: Sun, 24 Dec 2017 03:47:51 GMT 80 Content-Length: 300 81 82 { 83 "as":"AS14061 Digital Ocean, Inc.", 84 "city":"Clifton", 85 "country":"United States", 86 "countryCode":"US", 87 "isp":"Digital Ocean", 88 "lat":40.8326, 89 "lon":-74.1307, 90 "org":"Digital Ocean", 91 "query":"104.131.124.203", 92 "region":"NJ", 93 "regionName":"New Jersey", 94 "status":"success", 95 "timezone":"America/New_York", 96 "zip":"07014" 97 } 98 */ 99 }); 100}) 101.catch(err => { 102 // Handle errors 103}); 104 105// Callbacks 106SocksClient.createConnectionChain(options, (err, info) => { 107 if (!err) { 108 console.log(info.socket); 109 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) 110 111 console.log(info.socket.remoteAddress) // The remote address of the returned socket is the first proxy in the chain. 112 // 159.203.75.235 113 114 info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n'); 115 info.socket.on('data', (data) => { 116 console.log(data.toString()); // ip-api.com sees that the last proxy in the chain (104.131.124.203) is connected to it. 117 /* 118 HTTP/1.1 200 OK 119 Access-Control-Allow-Origin: * 120 Content-Type: application/json; charset=utf-8 121 Date: Sun, 24 Dec 2017 03:47:51 GMT 122 Content-Length: 300 123 124 { 125 "as":"AS14061 Digital Ocean, Inc.", 126 "city":"Clifton", 127 "country":"United States", 128 "countryCode":"US", 129 "isp":"Digital Ocean", 130 "lat":40.8326, 131 "lon":-74.1307, 132 "org":"Digital Ocean", 133 "query":"104.131.124.203", 134 "region":"NJ", 135 "regionName":"New Jersey", 136 "status":"success", 137 "timezone":"America/New_York", 138 "zip":"07014" 139 } 140 */ 141 }); 142 } else { 143 // Handle errors 144 } 145});
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.
1const options = { 2 proxy: { 3 host: '159.203.75.235', // ipv4, ipv6, or hostname 4 port: 1081, 5 type: 5 6 }, 7 8 command: 'bind', 9 10 // 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. 11 destination: { 12 host: '0.0.0.0', 13 port: 0 14 } 15}; 16 17// Creates a new SocksClient instance. 18const client = new SocksClient(options); 19 20// When the SOCKS proxy has bound a new port and started listening, this event is fired. 21client.on('bound', info => { 22 console.log(info.remoteHost); 23 /* 24 { 25 host: "159.203.75.235", 26 port: 57362 27 } 28 */ 29}); 30 31// When a client connects to the newly bound port on the SOCKS proxy, this event is fired. 32client.on('established', info => { 33 // info.remoteHost is the remote address of the client that connected to the SOCKS proxy. 34 console.log(info.remoteHost); 35 /* 36 host: 67.171.34.23, 37 port: 49823 38 */ 39 40 console.log(info.socket); 41 // <Socket ...> (This is a raw net.Socket that is a connection between the initial client and the remote client that connected to the proxy) 42 43 // Handle received data... 44 info.socket.on('data', data => { 45 console.log('recv', data); 46 }); 47}); 48 49// An error occurred trying to establish this SOCKS connection. 50client.on('error', err => { 51 console.error(err); 52}); 53 54// Start connection to proxy 55client.connect();
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.
1const options = { 2 proxy: { 3 host: '159.203.75.235', // ipv4, ipv6, or hostname 4 port: 1081, 5 type: 5 6 }, 7 8 command: 'associate', 9 10 // 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 11 destination: { 12 host: '0.0.0.0', 13 port: 0 14 } 15}; 16 17// Create a local UDP socket for sending packets to the proxy. 18const udpSocket = dgram.createSocket('udp4'); 19udpSocket.bind(); 20 21// Listen for incoming UDP packets from the proxy server. 22udpSocket.on('message', (message, rinfo) => { 23 console.log(SocksClient.parseUDPFrame(message)); 24 /* 25 { frameNumber: 0, 26 remoteHost: { host: '165.227.108.231', port: 4444 }, // The remote host that replied with a UDP packet 27 data: <Buffer 74 65 73 74 0a> // The data 28 } 29 */ 30}); 31 32let client = new SocksClient(associateOptions); 33 34// When the UDP relay is established, this event is fired and includes the UDP relay port to send data to on the proxy server. 35client.on('established', info => { 36 console.log(info.remoteHost); 37 /* 38 { 39 host: '159.203.75.235', 40 port: 44711 41 } 42 */ 43 44 // Send 'hello' to 165.227.108.231:4444 45 const packet = SocksClient.createUDPFrame({ 46 remoteHost: { host: '165.227.108.231', port: 4444 }, 47 data: Buffer.from(line) 48 }); 49 udpSocket.send(packet, info.remoteHost.port, info.remoteHost.host); 50}); 51 52// Start connection 53client.connect();
Note: The associate TCP connection to the proxy must remain open for the UDP relay to work.
Looking for a guide to migrate from v1? Look here
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.
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, v5, and v5h 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
Note: When using 4a please specify type: 4, and when using 5h please specify type 5.
Socks Version | TCP | UDP | IPv4 | IPv6 | Hostname |
---|---|---|---|---|---|
SOCKS v4 | ✅ | ❌ | ✅ | ❌ | ❌ |
SOCKS v4a | ✅ | ❌ | ✅ | ❌ | ✅ |
SOCKS v5 (includes v5h) | ✅ | ✅ | ✅ | ✅ | ✅ |
options
{SocksClientOptions} - An object describing the SOCKS proxy to use, the command to send and establish, and the destination host to connect to.1{ 2 proxy: { 3 host: '159.203.75.200', // ipv4, ipv6, or hostname 4 port: 1080, 5 type: 5, // Proxy version (4 or 5). For v4a use 4, for v5h use 5. 6 7 // Optional fields 8 userId: 'some username', // Used for SOCKS4 userId auth, and SOCKS5 user/pass auth in conjunction with password. 9 password: 'some password', // Used in conjunction with userId for user/pass auth for SOCKS5 proxies. 10 custom_auth_method: 0x80, // If using a custom auth method, specify the type here. If this is set, ALL other custom_auth_*** options must be set as well. 11 custom_auth_request_handler: async () =>. { 12 // This will be called when it's time to send the custom auth handshake. You must return a Buffer containing the data to send as your authentication. 13 return Buffer.from([0x01,0x02,0x03]); 14 }, 15 // This is the expected size (bytes) of the custom auth response from the proxy server. 16 custom_auth_response_size: 2, 17 // This is called when the auth response is received. The received packet is passed in as a Buffer, and you must return a boolean indicating the response from the server said your custom auth was successful or failed. 18 custom_auth_response_handler: async (data) => { 19 return data[1] === 0x00; 20 } 21 }, 22 23 command: 'connect', // connect, bind, associate 24 25 destination: { 26 host: '192.30.253.113', // ipv4, ipv6, hostname. Hostnames work with v4a and v5. 27 port: 80 28 }, 29 30 // Optional fields 31 timeout: 30000, // How long to wait to establish a proxy connection. (defaults to 30 seconds) 32 33 set_tcp_nodelay: true // If true, will turn on the underlying sockets TCP_NODELAY option. 34}
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.
1const options = { 2 proxy: { 3 host: '159.203.75.200', // ipv4, ipv6, or hostname 4 port: 1080, 5 type: 5 // Proxy version (4 or 5) 6 }, 7 8 command: 'connect', // connect, bind, associate 9 10 destination: { 11 host: '192.30.253.113', // ipv4, ipv6, or hostname 12 port: 80 13 } 14} 15 16// Await/Async (uses a Promise) 17try { 18 const info = await SocksClient.createConnection(options); 19 console.log(info); 20 /* 21 { 22 socket: <Socket ...>, // Raw net.Socket 23 } 24 */ 25 / <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy server) 26 27} catch (err) { 28 // Handle error... 29} 30 31// Promise 32SocksClient.createConnection(options) 33.then(info => { 34 console.log(info); 35 /* 36 { 37 socket: <Socket ...>, // Raw net.Socket 38 } 39 */ 40}) 41.catch(err => { 42 // Handle error... 43}); 44 45// Callback 46SocksClient.createConnection(options, (err, info) => { 47 if (!err) { 48 console.log(info); 49 /* 50 { 51 socket: <Socket ...>, // Raw net.Socket 52 } 53 */ 54 } else { 55 // Handle error... 56 } 57});
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.
1const options = { 2 proxies: [ // The chain order is the order in the proxies array, meaning the last proxy will establish a connection to the destination. 3 { 4 host: '159.203.75.235', // ipv4, ipv6, or hostname 5 port: 1081, 6 type: 5 7 }, 8 { 9 host: '104.131.124.203', // ipv4, ipv6, or hostname 10 port: 1081, 11 type: 5 12 } 13 ] 14 15 command: 'connect', // Only connect is supported in chaining mode. 16 17 destination: { 18 host: '192.30.253.113', // ipv4, ipv6, hostname 19 port: 80 20 } 21}
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
1{ 2 frameNumber: 0, // The frame number (used for breaking up larger packets) 3 4 remoteHost: { // The remote host to have the proxy send data to, or the remote host that send this data. 5 host: '1.2.3.4', 6 port: 1234 7 }, 8 9 data: <Buffer 01 02 03 04...> // A Buffer instance of data to include in the packet (actual data sent to the remote host) 10} 11interface SocksUDPFrameDetails { 12 // The frame number of the packet. 13 frameNumber?: number; 14 15 // The remote host. 16 remoteHost: SocksRemoteHost; 17 18 // The packet data. 19 data: Buffer; 20}
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.1const frame = SocksClient.parseUDPFrame(data); 2console.log(frame); 3/* 4{ 5 frameNumber: 0, 6 remoteHost: { 7 host: '1.2.3.4', 8 port: 1234 9 }, 10 data: <Buffer 01 02 03 04 ...> 11} 12*/
Parses a Buffer instance and returns the parsed SocksUDPFrameDetails object.
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.
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
1{ 2 socket: net.Socket, // The underlying raw Socket 3 remoteHost: { 4 host: '1.2.3.4', // The remote host that is listening (usually the proxy itself) 5 port: 4444 // The remote port the proxy is listening on for incoming connections (when using BIND). 6 } 7}
info
{ SocksClientEstablishedEvent } An object containing a Socket and SocksRemoteHost info.This event is emitted when the following conditions are met:
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
1{ 2 socket: net.Socket, // The underlying raw Socket 3 remoteHost: { 4 host: '1.2.3.4', // The remote host that is listening (usually the proxy itself) 5 port: 52738 // The remote port the proxy is listening on for incoming connections (when using BIND). 6 } 7}
Starts connecting to the remote SOCKS proxy server to establish a proxy connection to the destination host.
returns
{ SocksClientOptions } The options that were passed to the SocksClient.Gets the options that were passed to the SocksClient when it was created.
SocksClientError
1{ // Subclassed from Error. 2 message: 'An error has occurred', 3 options: { 4 // SocksClientOptions 5 } 6}
Please read the SOCKS 5 specifications for more information on how to use BIND and Associate. http://www.ietf.org/rfc/rfc1928.txt
This work is licensed under the MIT license.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 8/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More