1 | # socks examples
|
---|
2 |
|
---|
3 | ## Example for SOCKS 'connect' command
|
---|
4 |
|
---|
5 | The connect command is the most common use-case for a SOCKS proxy. This establishes a direct connection to a destination host through a proxy server. The destination host only has knowledge of the proxy server connecting to it and does not know about the origin client (you).
|
---|
6 |
|
---|
7 | **Origin Client (you) <-> Proxy Server <-> Destination Server**
|
---|
8 |
|
---|
9 | In this example, we are connecting to a web server on port 80, and sending a very basic HTTP request to receive a response. It's worth noting that there are many socks-http-agents that can be used with the node http module (and libraries such as request.js) to make this easier. This HTTP request is used as a simple example.
|
---|
10 |
|
---|
11 | The 'connect' command can be used via the SocksClient.createConnection() factory function as well as by creating a SocksClient instance and using event handlers.
|
---|
12 |
|
---|
13 | ### Using createConnection with async/await
|
---|
14 |
|
---|
15 | Since SocksClient.createConnection returns a Promise, we can easily use async/await for flow control.
|
---|
16 |
|
---|
17 | ```typescript
|
---|
18 | const SocksClient = require('socks').SocksClient;
|
---|
19 |
|
---|
20 | const options = {
|
---|
21 | proxy: {
|
---|
22 | host: '104.131.124.203',
|
---|
23 | port: 1081,
|
---|
24 | type: 5
|
---|
25 | },
|
---|
26 |
|
---|
27 | destination: {
|
---|
28 | host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
|
---|
29 | port: 80
|
---|
30 | },
|
---|
31 |
|
---|
32 | command: 'connect'
|
---|
33 | };
|
---|
34 |
|
---|
35 | async function start() {
|
---|
36 | try {
|
---|
37 | const info = await SocksClient.createConnection(options);
|
---|
38 |
|
---|
39 | console.log(info.socket);
|
---|
40 | // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
|
---|
41 |
|
---|
42 | info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
|
---|
43 | info.socket.on('data', (data) => {
|
---|
44 | console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
|
---|
45 | /*
|
---|
46 | HTTP/1.1 200 OK
|
---|
47 | Access-Control-Allow-Origin: *
|
---|
48 | Content-Type: application/json; charset=utf-8
|
---|
49 | Date: Sun, 24 Dec 2017 03:47:51 GMT
|
---|
50 | Content-Length: 300
|
---|
51 |
|
---|
52 | {
|
---|
53 | "as":"AS14061 Digital Ocean, Inc.",
|
---|
54 | "city":"Clifton",
|
---|
55 | "country":"United States",
|
---|
56 | "countryCode":"US",
|
---|
57 | "isp":"Digital Ocean",
|
---|
58 | "lat":40.8326,
|
---|
59 | "lon":-74.1307,
|
---|
60 | "org":"Digital Ocean",
|
---|
61 | "query":"104.131.124.203",
|
---|
62 | "region":"NJ",
|
---|
63 | "regionName":"New Jersey",
|
---|
64 | "status":"success",
|
---|
65 | "timezone":"America/New_York",
|
---|
66 | "zip":"07014"
|
---|
67 | }
|
---|
68 | */
|
---|
69 | } catch (err) {
|
---|
70 | // Handle errors
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | start();
|
---|
75 | ```
|
---|
76 |
|
---|
77 | ### Using createConnection with Promises
|
---|
78 |
|
---|
79 | ```typescript
|
---|
80 | const SocksClient = require('socks').SocksClient;
|
---|
81 |
|
---|
82 | const options = {
|
---|
83 | proxy: {
|
---|
84 | ipaddress: '104.131.124.203',
|
---|
85 | port: 1081,
|
---|
86 | type: 5
|
---|
87 | },
|
---|
88 |
|
---|
89 | destination: {
|
---|
90 | host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
|
---|
91 | port: 80
|
---|
92 | },
|
---|
93 |
|
---|
94 | command: 'connect'
|
---|
95 | };
|
---|
96 |
|
---|
97 | SocksClient.createConnection(options)
|
---|
98 | .then(info => {
|
---|
99 | console.log(info.socket);
|
---|
100 | // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
|
---|
101 |
|
---|
102 | info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
|
---|
103 | info.socket.on('data', (data) => {
|
---|
104 | console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
|
---|
105 | /*
|
---|
106 | HTTP/1.1 200 OK
|
---|
107 | Access-Control-Allow-Origin: *
|
---|
108 | Content-Type: application/json; charset=utf-8
|
---|
109 | Date: Sun, 24 Dec 2017 03:47:51 GMT
|
---|
110 | Content-Length: 300
|
---|
111 |
|
---|
112 | {
|
---|
113 | "as":"AS14061 Digital Ocean, Inc.",
|
---|
114 | "city":"Clifton",
|
---|
115 | "country":"United States",
|
---|
116 | "countryCode":"US",
|
---|
117 | "isp":"Digital Ocean",
|
---|
118 | "lat":40.8326,
|
---|
119 | "lon":-74.1307,
|
---|
120 | "org":"Digital Ocean",
|
---|
121 | "query":"104.131.124.203",
|
---|
122 | "region":"NJ",
|
---|
123 | "regionName":"New Jersey",
|
---|
124 | "status":"success",
|
---|
125 | "timezone":"America/New_York",
|
---|
126 | "zip":"07014"
|
---|
127 | }
|
---|
128 | */
|
---|
129 | })
|
---|
130 | .catch(err => {
|
---|
131 | // handle errors
|
---|
132 | });
|
---|
133 | ```
|
---|
134 |
|
---|
135 | ### Using createConnection with callbacks
|
---|
136 |
|
---|
137 | SocksClient.createConnection() optionally accepts a callback function as a second parameter.
|
---|
138 |
|
---|
139 | **Note:** If a callback function is provided, a Promise is still returned from the function, but the promise will always resolve regardless of if there was en error. (tldr: Do not mix callbacks and Promises).
|
---|
140 |
|
---|
141 | ```typescript
|
---|
142 | const SocksClient = require('socks').SocksClient;
|
---|
143 |
|
---|
144 | const options = {
|
---|
145 | proxy: {
|
---|
146 | ipaddress: '104.131.124.203',
|
---|
147 | port: 1081,
|
---|
148 | type: 5
|
---|
149 | },
|
---|
150 |
|
---|
151 | destination: {
|
---|
152 | host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
|
---|
153 | port: 80
|
---|
154 | },
|
---|
155 |
|
---|
156 | command: 'connect'
|
---|
157 | };
|
---|
158 |
|
---|
159 | SocksClient.createConnection(options, (err, info) => {
|
---|
160 | if (err) {
|
---|
161 | // handle errors
|
---|
162 | } else {
|
---|
163 | console.log(info.socket);
|
---|
164 | // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
|
---|
165 |
|
---|
166 | info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
|
---|
167 | info.socket.on('data', (data) => {
|
---|
168 | console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
|
---|
169 | /*
|
---|
170 | HTTP/1.1 200 OK
|
---|
171 | Access-Control-Allow-Origin: *
|
---|
172 | Content-Type: application/json; charset=utf-8
|
---|
173 | Date: Sun, 24 Dec 2017 03:47:51 GMT
|
---|
174 | Content-Length: 300
|
---|
175 |
|
---|
176 | {
|
---|
177 | "as":"AS14061 Digital Ocean, Inc.",
|
---|
178 | "city":"Clifton",
|
---|
179 | "country":"United States",
|
---|
180 | "countryCode":"US",
|
---|
181 | "isp":"Digital Ocean",
|
---|
182 | "lat":40.8326,
|
---|
183 | "lon":-74.1307,
|
---|
184 | "org":"Digital Ocean",
|
---|
185 | "query":"104.131.124.203",
|
---|
186 | "region":"NJ",
|
---|
187 | "regionName":"New Jersey",
|
---|
188 | "status":"success",
|
---|
189 | "timezone":"America/New_York",
|
---|
190 | "zip":"07014"
|
---|
191 | }
|
---|
192 | */
|
---|
193 | }
|
---|
194 | })
|
---|
195 | ```
|
---|
196 |
|
---|
197 | ### Using event handlers
|
---|
198 |
|
---|
199 | SocksClient also supports instance creation of a SocksClient. This allows for event based flow control.
|
---|
200 |
|
---|
201 | ```typescript
|
---|
202 | const SocksClient = require('socks').SocksClient;
|
---|
203 |
|
---|
204 | const options = {
|
---|
205 | proxy: {
|
---|
206 | ipaddress: '104.131.124.203',
|
---|
207 | port: 1081,
|
---|
208 | type: 5
|
---|
209 | },
|
---|
210 |
|
---|
211 | destination: {
|
---|
212 | host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
|
---|
213 | port: 80
|
---|
214 | },
|
---|
215 |
|
---|
216 | command: 'connect'
|
---|
217 | };
|
---|
218 |
|
---|
219 | const client = new SocksClient(options);
|
---|
220 |
|
---|
221 | client.on('established', (info) => {
|
---|
222 | console.log(info.socket);
|
---|
223 | // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
|
---|
224 |
|
---|
225 | info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
|
---|
226 | info.socket.on('data', (data) => {
|
---|
227 | console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
|
---|
228 | /*
|
---|
229 | HTTP/1.1 200 OK
|
---|
230 | Access-Control-Allow-Origin: *
|
---|
231 | Content-Type: application/json; charset=utf-8
|
---|
232 | Date: Sun, 24 Dec 2017 03:47:51 GMT
|
---|
233 | Content-Length: 300
|
---|
234 |
|
---|
235 | {
|
---|
236 | "as":"AS14061 Digital Ocean, Inc.",
|
---|
237 | "city":"Clifton",
|
---|
238 | "country":"United States",
|
---|
239 | "countryCode":"US",
|
---|
240 | "isp":"Digital Ocean",
|
---|
241 | "lat":40.8326,
|
---|
242 | "lon":-74.1307,
|
---|
243 | "org":"Digital Ocean",
|
---|
244 | "query":"104.131.124.203",
|
---|
245 | "region":"NJ",
|
---|
246 | "regionName":"New Jersey",
|
---|
247 | "status":"success",
|
---|
248 | "timezone":"America/New_York",
|
---|
249 | "zip":"07014"
|
---|
250 | }
|
---|
251 | */
|
---|
252 | });
|
---|
253 |
|
---|
254 | // Failed to establish proxy connection to destination.
|
---|
255 | client.on('error', () => {
|
---|
256 | // Handle errors
|
---|
257 | });
|
---|
258 | ``` |
---|