1 | # socks examples
|
---|
2 |
|
---|
3 | ## Example for SOCKS 'associate' command
|
---|
4 |
|
---|
5 | 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).
|
---|
6 |
|
---|
7 | This can be used for things such as DNS queries, and other UDP communicates.
|
---|
8 |
|
---|
9 | **Connection Steps**
|
---|
10 |
|
---|
11 | 1. Client -(associate)-> Proxy (Tells the proxy to create a UDP relay and bind on a new port)
|
---|
12 | 2. Client <-(port)- Proxy (Tells the origin client which port it opened and is accepting UDP frame packets on)
|
---|
13 |
|
---|
14 | At this point the proxy is accepting UDP frames on the specified port.
|
---|
15 |
|
---|
16 | 3. 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.)
|
---|
17 | 4. Client <--(udp frame) <-- Proxy <-- Destination (The destination client responds to the udp packet sent in #3)
|
---|
18 |
|
---|
19 | ## Usage
|
---|
20 |
|
---|
21 | The 'associate' command can only be used by creating a new SocksClient instance and listening for the 'established' event.
|
---|
22 |
|
---|
23 | **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.
|
---|
24 |
|
---|
25 | ```typescript
|
---|
26 | const dgram = require('dgram');
|
---|
27 | const SocksClient = require('socks').SocksClient;
|
---|
28 |
|
---|
29 | // Create a local UDP socket for sending/receiving packets to/from the proxy.
|
---|
30 | const udpSocket = dgram.createSocket('udp4');
|
---|
31 | udpSocket.bind();
|
---|
32 |
|
---|
33 | // Listen for incoming UDP packets from the proxy server.
|
---|
34 | udpSocket.on('message', (message, rinfo) => {
|
---|
35 | console.log(SocksClient.parseUDPFrame(message));
|
---|
36 | /*
|
---|
37 | { frameNumber: 0,
|
---|
38 | remoteHost: { host: '8.8.8.8', port: 53 }, // The remote host that replied with a UDP packet
|
---|
39 | data: <Buffer 74 65 73 74 0a> // The data
|
---|
40 | }
|
---|
41 | */
|
---|
42 | });
|
---|
43 |
|
---|
44 | const options = {
|
---|
45 | proxy: {
|
---|
46 | host: '104.131.124.203',
|
---|
47 | port: 1081,
|
---|
48 | type: 5
|
---|
49 | },
|
---|
50 |
|
---|
51 | // 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.
|
---|
52 | // Most SOCKS servers accept 0.0.0.0 as a wildcard address to accept UDP frames from any source.
|
---|
53 | destination: {
|
---|
54 | host: '0.0.0.0',
|
---|
55 | port: 0
|
---|
56 | },
|
---|
57 |
|
---|
58 | command: 'associate'
|
---|
59 | };
|
---|
60 |
|
---|
61 | const client = new SocksClient(options);
|
---|
62 |
|
---|
63 | // This event is fired when the SOCKS server has started listening on a new UDP port for UDP relaying.
|
---|
64 | client.on('established', info => {
|
---|
65 | console.log(info);
|
---|
66 | /*
|
---|
67 | {
|
---|
68 | socket: <Socket ...>,
|
---|
69 | remoteHost: { // This is the remote port on the SOCKS proxy server to send UDP frame packets to.
|
---|
70 | host: '104.131.124.203',
|
---|
71 | port: 58232
|
---|
72 | }
|
---|
73 | }
|
---|
74 | */
|
---|
75 |
|
---|
76 | // Send a udp frame to 8.8.8.8 on port 53 through the proxy.
|
---|
77 | const packet = SocksClient.createUDPFrame({
|
---|
78 | remoteHost: { host: '8.8.8.8', port: 53 },
|
---|
79 | data: Buffer.from('hello') // A DNS lookup in the real world.
|
---|
80 | });
|
---|
81 |
|
---|
82 | // Send packet.
|
---|
83 | udpSocket.send(packet, info.remoteHost.port, info.remoteHost.host);
|
---|
84 | });
|
---|
85 |
|
---|
86 | // SOCKS proxy failed to bind.
|
---|
87 | client.on('error', () => {
|
---|
88 | // Handle errors
|
---|
89 | });
|
---|
90 | ```
|
---|