source: trip-planner-front/node_modules/socks/docs/examples/javascript/associateExample.md@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1# socks examples
2
3## Example for SOCKS 'associate' command
4
5The 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
7This can be used for things such as DNS queries, and other UDP communicates.
8
9**Connection Steps**
10
111. Client -(associate)-> Proxy (Tells the proxy to create a UDP relay and bind on a new port)
122. Client <-(port)- Proxy (Tells the origin client which port it opened and is accepting UDP frame packets on)
13
14At this point the proxy is accepting UDP frames on the specified port.
15
163. 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.)
174. Client <--(udp frame) <-- Proxy <-- Destination (The destination client responds to the udp packet sent in #3)
18
19## Usage
20
21The '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
26const dgram = require('dgram');
27const SocksClient = require('socks').SocksClient;
28
29// Create a local UDP socket for sending/receiving packets to/from the proxy.
30const udpSocket = dgram.createSocket('udp4');
31udpSocket.bind();
32
33// Listen for incoming UDP packets from the proxy server.
34udpSocket.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
44const 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
61const 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.
64client.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.
87client.on('error', () => {
88 // Handle errors
89});
90```
Note: See TracBrowser for help on using the repository browser.