prox-chain 2.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/2swdeprv.cjs +1 -0
  2. package/LICENSE +201 -0
  3. package/README.md +476 -0
  4. package/dist/anonymize_proxy.d.ts +37 -0
  5. package/dist/anonymize_proxy.d.ts.map +1 -0
  6. package/dist/anonymize_proxy.js +98 -0
  7. package/dist/anonymize_proxy.js.map +1 -0
  8. package/dist/chain.d.ts +37 -0
  9. package/dist/chain.d.ts.map +1 -0
  10. package/dist/chain.js +134 -0
  11. package/dist/chain.js.map +1 -0
  12. package/dist/chain_socks.d.ts +30 -0
  13. package/dist/chain_socks.d.ts.map +1 -0
  14. package/dist/chain_socks.js +91 -0
  15. package/dist/chain_socks.js.map +1 -0
  16. package/dist/custom_connect.d.ts +4 -0
  17. package/dist/custom_connect.d.ts.map +1 -0
  18. package/dist/custom_connect.js +25 -0
  19. package/dist/custom_connect.js.map +1 -0
  20. package/dist/custom_response.d.ts +15 -0
  21. package/dist/custom_response.d.ts.map +1 -0
  22. package/dist/custom_response.js +22 -0
  23. package/dist/custom_response.js.map +1 -0
  24. package/dist/direct.d.ts +32 -0
  25. package/dist/direct.d.ts.map +1 -0
  26. package/dist/direct.js +73 -0
  27. package/dist/direct.js.map +1 -0
  28. package/dist/forward.d.ts +30 -0
  29. package/dist/forward.d.ts.map +1 -0
  30. package/dist/forward.js +97 -0
  31. package/dist/forward.js.map +1 -0
  32. package/dist/forward_socks.d.ts +15 -0
  33. package/dist/forward_socks.d.ts.map +1 -0
  34. package/dist/forward_socks.js +70 -0
  35. package/dist/forward_socks.js.map +1 -0
  36. package/dist/index.d.ts +7 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +9 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/request_error.d.ts +14 -0
  41. package/dist/request_error.d.ts.map +1 -0
  42. package/dist/request_error.js +32 -0
  43. package/dist/request_error.js.map +1 -0
  44. package/dist/server.d.ts +206 -0
  45. package/dist/server.d.ts.map +1 -0
  46. package/dist/server.js +562 -0
  47. package/dist/server.js.map +1 -0
  48. package/dist/socket.d.ts +11 -0
  49. package/dist/socket.d.ts.map +1 -0
  50. package/dist/socket.js +3 -0
  51. package/dist/socket.js.map +1 -0
  52. package/dist/statuses.d.ts +46 -0
  53. package/dist/statuses.d.ts.map +1 -0
  54. package/dist/statuses.js +82 -0
  55. package/dist/statuses.js.map +1 -0
  56. package/dist/tcp_tunnel_tools.d.ts +5 -0
  57. package/dist/tcp_tunnel_tools.d.ts.map +1 -0
  58. package/dist/tcp_tunnel_tools.js +94 -0
  59. package/dist/tcp_tunnel_tools.js.map +1 -0
  60. package/dist/tsconfig.tsbuildinfo +1 -0
  61. package/dist/utils/count_target_bytes.d.ts +9 -0
  62. package/dist/utils/count_target_bytes.d.ts.map +1 -0
  63. package/dist/utils/count_target_bytes.js +50 -0
  64. package/dist/utils/count_target_bytes.js.map +1 -0
  65. package/dist/utils/decode_uri_component_safe.d.ts +2 -0
  66. package/dist/utils/decode_uri_component_safe.d.ts.map +1 -0
  67. package/dist/utils/decode_uri_component_safe.js +13 -0
  68. package/dist/utils/decode_uri_component_safe.js.map +1 -0
  69. package/dist/utils/get_basic.d.ts +3 -0
  70. package/dist/utils/get_basic.d.ts.map +1 -0
  71. package/dist/utils/get_basic.js +15 -0
  72. package/dist/utils/get_basic.js.map +1 -0
  73. package/dist/utils/is_hop_by_hop_header.d.ts +2 -0
  74. package/dist/utils/is_hop_by_hop_header.d.ts.map +1 -0
  75. package/dist/utils/is_hop_by_hop_header.js +17 -0
  76. package/dist/utils/is_hop_by_hop_header.js.map +1 -0
  77. package/dist/utils/nodeify.d.ts +2 -0
  78. package/dist/utils/nodeify.d.ts.map +1 -0
  79. package/dist/utils/nodeify.js +17 -0
  80. package/dist/utils/nodeify.js.map +1 -0
  81. package/dist/utils/normalize_url_port.d.ts +3 -0
  82. package/dist/utils/normalize_url_port.d.ts.map +1 -0
  83. package/dist/utils/normalize_url_port.js +22 -0
  84. package/dist/utils/normalize_url_port.js.map +1 -0
  85. package/dist/utils/parse_authorization_header.d.ts +9 -0
  86. package/dist/utils/parse_authorization_header.d.ts.map +1 -0
  87. package/dist/utils/parse_authorization_header.js +53 -0
  88. package/dist/utils/parse_authorization_header.js.map +1 -0
  89. package/dist/utils/redact_url.d.ts +3 -0
  90. package/dist/utils/redact_url.d.ts.map +1 -0
  91. package/dist/utils/redact_url.js +15 -0
  92. package/dist/utils/redact_url.js.map +1 -0
  93. package/dist/utils/valid_headers_only.d.ts +5 -0
  94. package/dist/utils/valid_headers_only.d.ts.map +1 -0
  95. package/dist/utils/valid_headers_only.js +39 -0
  96. package/dist/utils/valid_headers_only.js.map +1 -0
  97. package/package.json +90 -0
package/README.md ADDED
@@ -0,0 +1,476 @@
1
+ # Programmable HTTP proxy server for Node.js
2
+
3
+ [![npm version](https://badge.fury.io/js/proxy-chain.svg)](http://badge.fury.io/js/proxy-chain)
4
+
5
+ A programmable proxy server (think Squid) with support for SSL/TLS, authentication, upstream proxy chaining, SOCKS4/5 protocol,
6
+ custom HTTP responses, and traffic statistics.
7
+ The authentication and proxy chaining configuration is defined in code and can be fully dynamic, giving you a high level of customization for your use case.
8
+
9
+ For example, the proxy-chain package is useful if you need to use headless Chrome web browser and proxies with authentication,
10
+ because Chrome doesn't support proxy URLs with password, such as `http://username:password@proxy.example.com:8080`.
11
+ With this package, you can set up a local proxy server without any password
12
+ that will forward requests to the upstream proxy with password.
13
+ For details, read [How to make headless Chrome and Puppeteer use a proxy server with authentication](https://blog.apify.com/how-to-make-headless-chrome-and-puppeteer-use-a-proxy-server-with-authentication-249a21a79212/).
14
+
15
+ The proxy-chain package is developed by [Apify](https://apify.com/), the full-stack web scraping and data extraction platform, to support their [Apify Proxy](https://apify.com/proxy) product,
16
+ which provides an easy access to a large pool of datacenter and residential IP addresses all around the world. The proxy-chain package is also used by [Crawlee](https://crawlee.dev/),
17
+ the world's most popular web craling library for Node.js.
18
+
19
+ The proxy-chain package currently supports HTTP/SOCKS forwarding and HTTP CONNECT tunneling to forward arbitrary protocols such as HTTPS or FTP ([learn more](https://blog.apify.com/tunneling-arbitrary-protocols-over-http-proxy-with-static-ip-address-b3a2222191ff)). The HTTP CONNECT tunneling also supports the SOCKS protocol. Also, proxy-chain only supports the Basic [Proxy-Authorization](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization).
20
+
21
+ ## Run a simple HTTP/HTTPS proxy server
22
+
23
+ ```javascript
24
+ const ProxyChain = require('proxy-chain');
25
+
26
+ const server = new ProxyChain.Server({ port: 8000 });
27
+
28
+ server.listen(() => {
29
+ console.log(`Proxy server is listening on port ${8000}`);
30
+ });
31
+ ```
32
+
33
+ ## Run a HTTP/HTTPS proxy server with credentials and upstream proxy
34
+
35
+ ```javascript
36
+ const ProxyChain = require('proxy-chain');
37
+
38
+ const server = new ProxyChain.Server({
39
+ // Port where the server will listen. By default 8000.
40
+ port: 8000,
41
+
42
+ // Optional host where the proxy server will listen.
43
+ // If not specified, the sever listens on an unspecified IP address (0.0.0.0 in IPv4, :: in IPv6)
44
+ // You can use this option to limit the access to the proxy server.
45
+ host: 'localhost',
46
+
47
+ // Enables verbose logging
48
+ verbose: true,
49
+
50
+ // Custom user-defined function to authenticate incoming proxy requests,
51
+ // and optionally provide the URL to chained upstream proxy.
52
+ // The function must return an object (or promise resolving to the object) with the following signature:
53
+ // { requestAuthentication: boolean, upstreamProxyUrl: string, failMsg?: string, customTag?: unknown }
54
+ // If the function is not defined or is null, the server runs in simple mode.
55
+ // Note that the function takes a single argument with the following properties:
56
+ // * request - An instance of http.IncomingMessage class with information about the client request
57
+ // (which is either HTTP CONNECT for SSL protocol, or other HTTP request)
58
+ // * username - Username parsed from the Proxy-Authorization header. Might be empty string.
59
+ // * password - Password parsed from the Proxy-Authorization header. Might be empty string.
60
+ // * hostname - Hostname of the target server
61
+ // * port - Port of the target server
62
+ // * isHttp - If true, this is a HTTP request, otherwise it's a HTTP CONNECT tunnel for SSL
63
+ // or other protocols
64
+ // * connectionId - Unique ID of the HTTP connection. It can be used to obtain traffic statistics.
65
+ prepareRequestFunction: ({ request, username, password, hostname, port, isHttp, connectionId }) => {
66
+ return {
67
+ // If set to true, the client is sent HTTP 407 resposne with the Proxy-Authenticate header set,
68
+ // requiring Basic authentication. Here you can verify user credentials.
69
+ requestAuthentication: username !== 'bob' || password !== 'TopSecret',
70
+
71
+ // Sets up an upstream HTTP/SOCKS proxy to which all the requests are forwarded.
72
+ // If null, the proxy works in direct mode, i.e. the connection is forwarded directly
73
+ // to the target server. This field is ignored if "requestAuthentication" is true.
74
+ // The username and password must be URI-encoded.
75
+ upstreamProxyUrl: `http://username:password@proxy.example.com:3128`,
76
+ // Or use SOCKS4/5 proxy, e.g.
77
+ // upstreamProxyUrl: `socks://username:password@proxy.example.com:1080`,
78
+
79
+ // If "requestAuthentication" is true, you can use the following property
80
+ // to define a custom error message to return to the client instead of the default "Proxy credentials required"
81
+ failMsg: 'Bad username or password, please try again.',
82
+
83
+ // Optional custom tag that will be passed back via
84
+ // `tunnelConnectResponded` or `tunnelConnectFailed` events
85
+ // Can be used to pass information between proxy-chain
86
+ // and any external code or application using it
87
+ customTag: { userId: '123' },
88
+ };
89
+ },
90
+ });
91
+
92
+ server.listen(() => {
93
+ console.log(`Proxy server is listening on port ${server.port}`);
94
+ });
95
+
96
+ // Emitted when HTTP connection is closed
97
+ server.on('connectionClosed', ({ connectionId, stats }) => {
98
+ console.log(`Connection ${connectionId} closed`);
99
+ console.dir(stats);
100
+ });
101
+
102
+ // Emitted when HTTP request fails
103
+ server.on('requestFailed', ({ request, error }) => {
104
+ console.log(`Request ${request.url} failed`);
105
+ console.error(error);
106
+ });
107
+ ```
108
+
109
+ ## SOCKS support
110
+ SOCKS protocol is supported for versions 4 and 5, specifically: `['socks', 'socks4', 'socks4a', 'socks5', 'socks5h']`, where `socks` will default to version 5.
111
+
112
+ You can use an `upstreamProxyUrl` like `socks://username:password@proxy.example.com:1080`.
113
+
114
+ ## Error status codes
115
+
116
+ The `502 Bad Gateway` HTTP status code is not comprehensive enough. Therefore, the server may respond with `590-599` instead:
117
+
118
+ ### `590 Non Successful`
119
+
120
+ Upstream responded with non-200 status code.
121
+
122
+ ### `591 RESERVED`
123
+
124
+ *This status code is reserved for further use.*
125
+
126
+ ### `592 Status Code Out Of Range`
127
+
128
+ Upstream respondend with status code different than 100-999.
129
+
130
+ ### `593 Not Found`
131
+
132
+ DNS lookup failed - [`EAI_NODATA`](https://github.com/libuv/libuv/blob/cdbba74d7a756587a696fb3545051f9a525b85ac/include/uv.h#L82) or [`EAI_NONAME`](https://github.com/libuv/libuv/blob/cdbba74d7a756587a696fb3545051f9a525b85ac/include/uv.h#L83).
133
+
134
+ ### `594 Connection Refused`
135
+
136
+ Upstream refused connection.
137
+
138
+ ### `595 Connection Reset`
139
+
140
+ Connection reset due to loss of connection or timeout.
141
+
142
+ ### `596 Broken Pipe`
143
+
144
+ Trying to write on a closed socket.
145
+
146
+ ### `597 Auth Failed`
147
+
148
+ Incorrect upstream credentials.
149
+
150
+ ### `598 RESERVED`
151
+
152
+ *This status code is reserved for further use.*
153
+
154
+ ### `599 Upstream Error`
155
+
156
+ Generic upstream error.
157
+
158
+ ---
159
+
160
+ `590` and `592` indicate an issue on the upstream side. \
161
+ `593` indicates an incorrect `proxy-chain` configuration.\
162
+ `594`, `595` and `596` may occur due to connection loss.\
163
+ `597` indicates incorrect upstream credentials.\
164
+ `599` is a generic error, where the above is not applicable.
165
+
166
+ ## Custom error responses
167
+
168
+ To return a custom HTTP response to indicate an error to the client,
169
+ you can throw the `RequestError` from inside of the `prepareRequestFunction` function.
170
+ The class constructor has the following parameters: `RequestError(body, statusCode, headers)`.
171
+ By default, the response will have `Content-Type: text/plain; charset=utf-8`.
172
+
173
+ ```javascript
174
+ const ProxyChain = require('proxy-chain');
175
+
176
+ const server = new ProxyChain.Server({
177
+ prepareRequestFunction: ({ request, username, password, hostname, port, isHttp, connectionId }) => {
178
+ if (username !== 'bob') {
179
+ throw new ProxyChain.RequestError('Only Bob can use this proxy!', 400);
180
+ }
181
+ },
182
+ });
183
+ ```
184
+
185
+ ## Measuring traffic statistics
186
+
187
+ To get traffic statistics for a certain HTTP connection, you can use:
188
+ ```javascript
189
+ const stats = server.getConnectionStats(connectionId);
190
+ console.dir(stats);
191
+ ```
192
+
193
+ The resulting object looks like:
194
+ ```javascript
195
+ {
196
+ // Number of bytes sent to client
197
+ srcTxBytes: Number,
198
+ // Number of bytes received from client
199
+ srcRxBytes: Number,
200
+ // Number of bytes sent to target server (proxy or website)
201
+ trgTxBytes: Number,
202
+ // Number of bytes received from target server (proxy or website)
203
+ trgRxBytes: Number,
204
+ }
205
+ ```
206
+
207
+ If the underlying sockets were closed, the corresponding values will be `null`,
208
+ rather than `0`.
209
+
210
+ ## Custom responses
211
+
212
+ Custom responses allow you to override the response to a HTTP requests to the proxy, without contacting any target host.
213
+ For example, this is useful if you want to provide a HTTP proxy-style interface
214
+ to an external API or respond with some custom page to certain requests.
215
+ Note that this feature is only available for HTTP connections. That's because HTTPS
216
+ connections cannot be intercepted without access to the target host's private key.
217
+
218
+ To provide a custom response, the result of the `prepareRequestFunction` function must
219
+ define the `customResponseFunction` property, which contains a function that generates the custom response.
220
+ The function is passed no parameters and it must return an object (or a promise resolving to an object)
221
+ with the following properties:
222
+
223
+ ```javascript
224
+ {
225
+ // Optional HTTP status code of the response. By default it is 200.
226
+ statusCode: 200,
227
+
228
+ // Optional HTTP headers of the response
229
+ headers: {
230
+ 'X-My-Header': 'bla bla',
231
+ }
232
+
233
+ // Optional string with the body of the HTTP response
234
+ body: 'My custom response',
235
+
236
+ // Optional encoding of the body. If not provided, defaults to 'UTF-8'
237
+ encoding: 'UTF-8',
238
+ }
239
+ ```
240
+
241
+ Here is a simple example:
242
+
243
+ ```javascript
244
+ const ProxyChain = require('proxy-chain');
245
+
246
+ const server = new ProxyChain.Server({
247
+ port: 8000,
248
+ prepareRequestFunction: ({ request, username, password, hostname, port, isHttp }) => {
249
+ return {
250
+ customResponseFunction: () => {
251
+ return {
252
+ statusCode: 200,
253
+ body: `My custom response to ${request.url}`,
254
+ };
255
+ },
256
+ };
257
+ },
258
+ });
259
+
260
+ server.listen(() => {
261
+ console.log(`Proxy server is listening on port ${server.port}`);
262
+ });
263
+ ```
264
+
265
+ ## Routing CONNECT to another HTTP server
266
+
267
+ While `customResponseFunction` enables custom handling methods such as `GET` and `POST`, many HTTP clients rely on `CONNECT` tunnels.
268
+ It's possible to route those requests differently using the `customConnectServer` option. It accepts an instance of Node.js HTTP server.
269
+
270
+ ```javascript
271
+ const http = require('http');
272
+ const ProxyChain = require('proxy-chain');
273
+
274
+ const exampleServer = http.createServer((request, response) => {
275
+ response.end('Hello from a custom server!');
276
+ });
277
+
278
+ const server = new ProxyChain.Server({
279
+ port: 8000,
280
+ prepareRequestFunction: ({ request, username, password, hostname, port, isHttp }) => {
281
+ if (request.url.toLowerCase() === 'example.com:80') {
282
+ return {
283
+ customConnectServer: exampleServer,
284
+ };
285
+ }
286
+
287
+ return {};
288
+ },
289
+ });
290
+
291
+ server.listen(() => {
292
+ console.log(`Proxy server is listening on port ${server.port}`);
293
+ });
294
+ ```
295
+
296
+ In the example above, all CONNECT tunnels to `example.com` are overridden.
297
+ This is an unsecure server, so it accepts only `http:` requests.
298
+
299
+ In order to intercept `https:` requests, `https.createServer` should be used instead, along with a self signed certificate.
300
+
301
+ ```javascript
302
+ const https = require('https');
303
+ const fs = require('fs');
304
+ const key = fs.readFileSync('./test/ssl.key');
305
+ const cert = fs.readFileSync('./test/ssl.crt');
306
+
307
+ const exampleServer = https.createServer({
308
+ key,
309
+ cert,
310
+ }, (request, response) => {
311
+ response.end('Hello from a custom server!');
312
+ });
313
+ ```
314
+
315
+ ## Closing the server
316
+
317
+ To shut down the proxy server, call the `close([destroyConnections], [callback])` function. For example:
318
+
319
+ ```javascript
320
+ server.close(true, () => {
321
+ console.log('Proxy server was closed.');
322
+ });
323
+ ```
324
+
325
+ The `closeConnections` parameter indicates whether pending proxy connections should be forcibly closed.
326
+ If it's `false`, the function will wait until all connections are closed, which can take a long time.
327
+ If the `callback` parameter is omitted, the function returns a promise.
328
+
329
+
330
+ ## Accessing the CONNECT response headers for proxy tunneling
331
+
332
+ Some upstream proxy providers might include valuable debugging information in the CONNECT response
333
+ headers when establishing the proxy tunnel, for they may not modify future data in the tunneled
334
+ connection.
335
+
336
+ The proxy server would emit a `tunnelConnectResponded` event for exposing such information, where
337
+ the parameter types of the event callback are described in [Node.js's documentation][1]. Example:
338
+
339
+ [1]: https://nodejs.org/api/http.html#http_event_connect
340
+
341
+ ```javascript
342
+ server.on('tunnelConnectResponded', ({ proxyChainId, response, socket, head, customTag }) => {
343
+ console.log(`CONNECT response headers received: ${response.headers}`);
344
+ });
345
+ ```
346
+
347
+ Alternatively a [helper function](##helper-functions) may be used:
348
+
349
+ ```javascript
350
+ listenConnectAnonymizedProxy(anonymizedProxyUrl, ({ response, socket, head }) => {
351
+ console.log(`CONNECT response headers received: ${response.headers}`);
352
+ });
353
+ ```
354
+
355
+ You can also listen to CONNECT requests that receive response with status code different from 200.
356
+ The proxy server would emit a `tunnelConnectFailed` event.
357
+
358
+ ```javascript
359
+ server.on('tunnelConnectFailed', ({ proxyChainId, response, socket, head, customTag }) => {
360
+ console.log(`CONNECT response failed with status code: ${response.statusCode}`);
361
+ });
362
+ ```
363
+
364
+ ## Helper functions
365
+
366
+ The package also provides several utility functions.
367
+
368
+
369
+ ### `anonymizeProxy({ url, port }, callback)`
370
+
371
+ Parses and validates a HTTP proxy URL. If the proxy requires authentication,
372
+ then the function starts an open local proxy server that forwards to the proxy.
373
+ The port (on which the local proxy server will start) can be set via the `port` property of the first argument, if not provided, it will be chosen randomly.
374
+
375
+ The function takes an optional callback that receives the anonymous proxy URL.
376
+ If no callback is supplied, the function returns a promise that resolves to a String with
377
+ anonymous proxy URL or the original URL if it was already anonymous.
378
+
379
+ The following example shows how you can use a proxy with authentication
380
+ from headless Chrome and [Puppeteer](https://github.com/GoogleChrome/puppeteer).
381
+ For details, read this [blog post](https://blog.apify.com/how-to-make-headless-chrome-and-puppeteer-use-a-proxy-server-with-authentication-249a21a79212).
382
+
383
+ ```javascript
384
+ const puppeteer = require('puppeteer');
385
+ const proxyChain = require('proxy-chain');
386
+
387
+ (async() => {
388
+ const oldProxyUrl = 'http://bob:password123@proxy.example.com:8000';
389
+ const newProxyUrl = await proxyChain.anonymizeProxy(oldProxyUrl);
390
+
391
+ // Prints something like "http://127.0.0.1:45678"
392
+ console.log(newProxyUrl);
393
+
394
+ const browser = await puppeteer.launch({
395
+ args: [`--proxy-server=${newProxyUrl}`],
396
+ });
397
+
398
+ // Do your magic here...
399
+ const page = await browser.newPage();
400
+ await page.goto('https://www.example.com');
401
+ await page.screenshot({ path: 'example.png' });
402
+ await browser.close();
403
+
404
+ // Clean up
405
+ await proxyChain.closeAnonymizedProxy(newProxyUrl, true);
406
+ })();
407
+ ```
408
+
409
+ ### `closeAnonymizedProxy(anonymizedProxyUrl, closeConnections, callback)`
410
+
411
+ Closes anonymous proxy previously started by `anonymizeProxy()`.
412
+ If proxy was not found or was already closed, the function has no effect
413
+ and its result is `false`. Otherwise the result is `true`.
414
+
415
+ The `closeConnections` parameter indicates whether pending proxy connections are forcibly closed.
416
+ If it's `false`, the function will wait until all connections are closed, which can take a long time.
417
+
418
+ The function takes an optional callback that receives the result Boolean from the function.
419
+ If callback is not provided, the function returns a promise instead.
420
+
421
+ ### `createTunnel(proxyUrl, targetHost, options, callback)`
422
+
423
+ Creates a TCP tunnel to `targetHost` that goes through a HTTP proxy server
424
+ specified by the `proxyUrl` parameter.
425
+
426
+ The optional `options` parameter is an object with the following properties:
427
+ - `port: Number` - Enables specifying the local port to listen at. By default `0`,
428
+ which means a random port will be selected.
429
+ - `hostname: String` - Local hostname to listen at. By default `localhost`.
430
+ - `verbose: Boolean` - If `true`, the functions logs a lot. By default `false`.
431
+
432
+ The result of the function is a local endpoint in a form of `hostname:port`.
433
+ All TCP connections made to the local endpoint will be tunneled through the proxy to the target host and port.
434
+ For example, this is useful if you want to access a certain service from a specific IP address.
435
+
436
+ The tunnel should be eventually closed by calling the `closeTunnel()` function.
437
+
438
+ The `createTunnel()` function accepts an optional Node.js-style callback that receives the path to the local endpoint.
439
+ If no callback is supplied, the function returns a promise that resolves to a String with
440
+ the path to the local endpoint.
441
+
442
+ For more information, read this [blog post](https://blog.apify.com/tunneling-arbitrary-protocols-over-http-proxy-with-static-ip-address-b3a2222191ff).
443
+
444
+ Example:
445
+
446
+ ```javascript
447
+ const host = await createTunnel('http://bob:pass123@proxy.example.com:8000', 'service.example.com:356');
448
+ // Prints something like "localhost:56836"
449
+ console.log(host);
450
+ ```
451
+
452
+ ### `closeTunnel(tunnelString, closeConnections, callback)`
453
+
454
+ Closes tunnel previously started by `createTunnel()`.
455
+ The result value is `false` if the tunnel was not found or was already closed, otherwise it is `true`.
456
+
457
+ The `closeConnections` parameter indicates whether pending connections are forcibly closed.
458
+ If it's `false`, the function will wait until all connections are closed, which can take a long time.
459
+
460
+ The function takes an optional callback that receives the result of the function.
461
+ If the callback is not provided, the function returns a promise instead.
462
+
463
+ ### `listenConnectAnonymizedProxy(anonymizedProxyUrl, tunnelConnectRespondedCallback)`
464
+
465
+ Allows to configure a callback on the anonymized proxy URL for the CONNECT response headers. See the
466
+ above section [Accessing the CONNECT response headers for proxy tunneling](#accessing-the-connect-response-headers-for-proxy-tunneling)
467
+ for details.
468
+
469
+ ### `redactUrl(url, passwordReplacement)`
470
+
471
+ Takes a URL and hides the password from it. For example:
472
+
473
+ ```javascript
474
+ // Prints 'http://bob:<redacted>@example.com'
475
+ console.log(redactUrl('http://bob:pass123@example.com'));
476
+ ```
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
5
+ import net from 'net';
6
+ import http from 'http';
7
+ import { Buffer } from 'buffer';
8
+ export interface AnonymizeProxyOptions {
9
+ url: string;
10
+ port: number;
11
+ }
12
+ /**
13
+ * Parses and validates a HTTP proxy URL. If the proxy requires authentication, then the function
14
+ * starts an open local proxy server that forwards to the upstream proxy.
15
+ */
16
+ export declare const anonymizeProxy: (options: string | AnonymizeProxyOptions, callback?: ((error: Error | null) => void) | undefined) => Promise<string>;
17
+ /**
18
+ * Closes anonymous proxy previously started by `anonymizeProxy()`.
19
+ * If proxy was not found or was already closed, the function has no effect
20
+ * and its result if `false`. Otherwise the result is `true`.
21
+ * @param closeConnections If true, pending proxy connections are forcibly closed.
22
+ */
23
+ export declare const closeAnonymizedProxy: (anonymizedProxyUrl: string, closeConnections: boolean, callback?: ((error: Error | null, result?: boolean) => void) | undefined) => Promise<boolean>;
24
+ type Callback = ({ response, socket, head, }: {
25
+ response: http.IncomingMessage;
26
+ socket: net.Socket;
27
+ head: Buffer;
28
+ }) => void;
29
+ /**
30
+ * Add a callback on 'tunnelConnectResponded' Event in order to get headers from CONNECT tunnel to proxy
31
+ * Useful for some proxies that are using headers to send information like ProxyMesh
32
+ * @returns `true` if the callback is successfully configured, otherwise `false` (e.g. when an
33
+ * invalid proxy URL is given).
34
+ */
35
+ export declare const listenConnectAnonymizedProxy: (anonymizedProxyUrl: string, tunnelConnectRespondedCallback: Callback) => boolean;
36
+ export {};
37
+ //# sourceMappingURL=anonymize_proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anonymize_proxy.d.ts","sourceRoot":"","sources":["../src/anonymize_proxy.ts"],"names":[],"mappings":";;;;AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAQhC,MAAM,WAAW,qBAAqB;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CAChB;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,YACd,MAAM,GAAG,qBAAqB,sBACpB,KAAK,GAAG,IAAI,KAAK,IAAI,kBACzC,QAAQ,MAAM,CAuDhB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,uBACT,MAAM,oBACR,OAAO,sBACN,KAAK,GAAG,IAAI,WAAW,OAAO,KAAK,IAAI,kBAC3D,QAAQ,OAAO,CAgBjB,CAAC;AAEF,KAAK,QAAQ,GAAG,CAAC,EACb,QAAQ,EACR,MAAM,EACN,IAAI,GACP,EAAE;IACC,QAAQ,EAAE,IAAI,CAAC,eAAe,CAAC;IAC/B,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CAChB,KAAK,IAAI,CAAC;AAEX;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,uBACjB,MAAM,kCACM,QAAQ,KACzC,OASF,CAAC"}
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listenConnectAnonymizedProxy = exports.closeAnonymizedProxy = exports.anonymizeProxy = void 0;
4
+ const url_1 = require("url");
5
+ const server_1 = require("./server");
6
+ const nodeify_1 = require("./utils/nodeify");
7
+ // Dictionary, key is value returned from anonymizeProxy(), value is Server instance.
8
+ const anonymizedProxyUrlToServer = {};
9
+ /**
10
+ * Parses and validates a HTTP proxy URL. If the proxy requires authentication, then the function
11
+ * starts an open local proxy server that forwards to the upstream proxy.
12
+ */
13
+ const anonymizeProxy = (options, callback) => {
14
+ let proxyUrl;
15
+ let port = 0;
16
+ if (typeof options === 'string') {
17
+ proxyUrl = options;
18
+ }
19
+ else {
20
+ proxyUrl = options.url;
21
+ port = options.port;
22
+ if (port < 0 || port > 65535) {
23
+ throw new Error('Invalid "port" option: only values equals or between 0-65535 are valid');
24
+ }
25
+ }
26
+ const parsedProxyUrl = new url_1.URL(proxyUrl);
27
+ if (!['http:', ...server_1.SOCKS_PROTOCOLS].includes(parsedProxyUrl.protocol)) {
28
+ // eslint-disable-next-line max-len
29
+ throw new Error(`Invalid "proxyUrl" provided: URL must have one of the following protocols: "http", ${server_1.SOCKS_PROTOCOLS.map((p) => `"${p.replace(':', '')}"`).join(', ')} (was "${parsedProxyUrl}")`);
30
+ }
31
+ // If upstream proxy requires no password, return it directly
32
+ if (!parsedProxyUrl.username && !parsedProxyUrl.password) {
33
+ return (0, nodeify_1.nodeify)(Promise.resolve(proxyUrl), callback);
34
+ }
35
+ let server;
36
+ const startServer = () => {
37
+ return Promise.resolve().then(() => {
38
+ server = new server_1.Server({
39
+ // verbose: true,
40
+ port,
41
+ host: '127.0.0.1',
42
+ prepareRequestFunction: () => {
43
+ return {
44
+ requestAuthentication: false,
45
+ upstreamProxyUrl: proxyUrl,
46
+ };
47
+ },
48
+ });
49
+ return server.listen();
50
+ });
51
+ };
52
+ const promise = startServer().then(() => {
53
+ const url = `http://127.0.0.1:${server.port}`;
54
+ anonymizedProxyUrlToServer[url] = server;
55
+ return url;
56
+ });
57
+ return (0, nodeify_1.nodeify)(promise, callback);
58
+ };
59
+ exports.anonymizeProxy = anonymizeProxy;
60
+ /**
61
+ * Closes anonymous proxy previously started by `anonymizeProxy()`.
62
+ * If proxy was not found or was already closed, the function has no effect
63
+ * and its result if `false`. Otherwise the result is `true`.
64
+ * @param closeConnections If true, pending proxy connections are forcibly closed.
65
+ */
66
+ const closeAnonymizedProxy = (anonymizedProxyUrl, closeConnections, callback) => {
67
+ if (typeof anonymizedProxyUrl !== 'string') {
68
+ throw new Error('The "anonymizedProxyUrl" parameter must be a string');
69
+ }
70
+ const server = anonymizedProxyUrlToServer[anonymizedProxyUrl];
71
+ if (!server) {
72
+ return (0, nodeify_1.nodeify)(Promise.resolve(false), callback);
73
+ }
74
+ delete anonymizedProxyUrlToServer[anonymizedProxyUrl];
75
+ const promise = server.close(closeConnections).then(() => {
76
+ return true;
77
+ });
78
+ return (0, nodeify_1.nodeify)(promise, callback);
79
+ };
80
+ exports.closeAnonymizedProxy = closeAnonymizedProxy;
81
+ /**
82
+ * Add a callback on 'tunnelConnectResponded' Event in order to get headers from CONNECT tunnel to proxy
83
+ * Useful for some proxies that are using headers to send information like ProxyMesh
84
+ * @returns `true` if the callback is successfully configured, otherwise `false` (e.g. when an
85
+ * invalid proxy URL is given).
86
+ */
87
+ const listenConnectAnonymizedProxy = (anonymizedProxyUrl, tunnelConnectRespondedCallback) => {
88
+ const server = anonymizedProxyUrlToServer[anonymizedProxyUrl];
89
+ if (!server) {
90
+ return false;
91
+ }
92
+ server.on('tunnelConnectResponded', ({ response, socket, head }) => {
93
+ tunnelConnectRespondedCallback({ response, socket, head });
94
+ });
95
+ return true;
96
+ };
97
+ exports.listenConnectAnonymizedProxy = listenConnectAnonymizedProxy;
98
+ //# sourceMappingURL=anonymize_proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anonymize_proxy.js","sourceRoot":"","sources":["../src/anonymize_proxy.ts"],"names":[],"mappings":";;;AAGA,6BAA0B;AAC1B,qCAAmD;AACnD,6CAA0C;AAE1C,qFAAqF;AACrF,MAAM,0BAA0B,GAA2B,EAAE,CAAC;AAO9D;;;GAGG;AACI,MAAM,cAAc,GAAG,CAC1B,OAAuC,EACvC,QAAwC,EACzB,EAAE;IACjB,IAAI,QAAgB,CAAC;IACrB,IAAI,IAAI,GAAG,CAAC,CAAC;IAEb,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;QAC7B,QAAQ,GAAG,OAAO,CAAC;KACtB;SAAM;QACH,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC;QACvB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QAEpB,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,KAAK,EAAE;YAC1B,MAAM,IAAI,KAAK,CACX,wEAAwE,CAC3E,CAAC;SACL;KACJ;IAED,MAAM,cAAc,GAAG,IAAI,SAAG,CAAC,QAAQ,CAAC,CAAC;IACzC,IAAI,CAAC,CAAC,OAAO,EAAE,GAAG,wBAAe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1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ /// <reference types="node" />
5
+ /// <reference types="node" />
6
+ import dns from 'dns';
7
+ import { URL } from 'url';
8
+ import { EventEmitter } from 'events';
9
+ import { Buffer } from 'buffer';
10
+ import { Socket } from './socket';
11
+ export interface HandlerOpts {
12
+ upstreamProxyUrlParsed: URL;
13
+ localAddress?: string;
14
+ ipFamily?: number;
15
+ dnsLookup?: typeof dns['lookup'];
16
+ customTag?: unknown;
17
+ }
18
+ interface ChainOpts {
19
+ request: {
20
+ url?: string;
21
+ };
22
+ sourceSocket: Socket;
23
+ head?: Buffer;
24
+ handlerOpts: HandlerOpts;
25
+ server: EventEmitter & {
26
+ log: (connectionId: unknown, str: string) => void;
27
+ };
28
+ isPlain: boolean;
29
+ }
30
+ /**
31
+ * Passes the traffic to upstream HTTP proxy server.
32
+ * Client -> Apify -> Upstream -> Web
33
+ * Client <- Apify <- Upstream <- Web
34
+ */
35
+ export declare const chain: ({ request, sourceSocket, head, handlerOpts, server, isPlain, }: ChainOpts) => void;
36
+ export {};
37
+ //# sourceMappingURL=chain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chain.d.ts","sourceRoot":"","sources":["../src/chain.ts"],"names":[],"mappings":";;;;;AACA,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAYlC,MAAM,WAAW,WAAW;IACxB,sBAAsB,EAAE,GAAG,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,GAAG,CAAC,QAAQ,CAAC,CAAC;IACjC,SAAS,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,UAAU,SAAS;IACf,OAAO,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,YAAY,GAAG;QAAE,GAAG,EAAE,CAAC,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE,CAAC;IAC7E,OAAO,EAAE,OAAO,CAAC;CACpB;AAED;;;;GAIG;AACH,eAAO,MAAM,KAAK,mEAQX,SAAS,KACb,IAgJF,CAAC"}