source: trip-planner-front/node_modules/socks/docs/examples/javascript/bindExample.md@ 6a3a178

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1# socks examples
2
3## Example for SOCKS 'bind' command
4
5The bind command tells the SOCKS proxy server to bind and listen on a new TCP port for an incoming connection. It communicates the newly opened port back to the origin client. Once a incoming connection is accepted by the SOCKS proxy server it then communicates the remote host that connected to the SOCKS proxy back through the same initial connection via the origin client.
6
7This can be used for things such as FTP clients which require incoming TCP connections, etc.
8
9**Connection Steps**
10
111. Client -(bind)-> Proxy (Tells the proxy to bind to a new port)
122. Client <-(port)- Proxy (Tells the origin client which port it opened)
133. Client2 --> Proxy (Other client connects to the proxy on this port)
144. Client <--(client2's host info) (Proxy tells the origin client who connected to it)
155. Original connection to the proxy is now a full TCP stream between client (you) and client2.
166. Client <--> Proxy <--> Client2
17
18
19## Usage
20
21The 'bind' command can only be used by creating a new SocksClient instance and listening for 'bound' and 'established' events.
22
23
24```typescript
25const SocksClient = require('socks').SocksClient;
26
27const options = {
28 proxy: {
29 host: '104.131.124.203',
30 port: 1081,
31 type: 5
32 },
33
34 // This should be the ip and port of the expected client that will connect to the SOCKS proxy server on the newly bound port.
35 // Most SOCKS servers accept 0.0.0.0 as a wildcard address to accept any client.
36 destination: {
37 host: '0.0.0.0',
38 port: 0
39 },
40
41 command: 'bind'
42};
43
44const client = new SocksClient(options);
45
46// This event is fired when the SOCKS server has started listening on a new port for incoming connections.
47client.on('bound', (info) => {
48 console.log(info);
49 /*
50 {
51 socket: <Socket ...>,
52 remoteHost: { // This is the remote ip and port of the SOCKS proxy that is now accepting incoming connections.
53 host: '104.131.124.203',
54 port: 49928
55 }
56 }
57 */
58});
59
60// This event is fired when the SOCKS server has accepted an incoming connection on the newly bound port.
61client.on('established', (info) => {
62 console.log(info);
63 /*
64 {
65 socket: <Socket ...>,
66 remoteHost: { // This is the remote ip and port that connected to the SOCKS proxy on the newly bound port.
67 host: '1.2.3.4',
68 port: 58232
69 }
70 }
71 */
72
73 // At this point info.socket is a regular net.Socket TCP connection between client and client2 (1.2.3.4) (the client which connected to the proxy on the newly bound port.)
74
75 console.log(info.socket);
76 // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
77});
78
79// SOCKS proxy failed to bind.
80client.on('error', () => {
81 // Handle errors
82});
83```
Note: See TracBrowser for help on using the repository browser.