prxy-chain 0.0.1-security → 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.

Potentially problematic release.


This version of prxy-chain might be problematic. Click here for more details.

Files changed (97) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +474 -3
  3. package/dist/anonymize_proxy.d.ts +37 -0
  4. package/dist/anonymize_proxy.d.ts.map +1 -0
  5. package/dist/anonymize_proxy.js +98 -0
  6. package/dist/anonymize_proxy.js.map +1 -0
  7. package/dist/chain.d.ts +37 -0
  8. package/dist/chain.d.ts.map +1 -0
  9. package/dist/chain.js +134 -0
  10. package/dist/chain.js.map +1 -0
  11. package/dist/chain_socks.d.ts +30 -0
  12. package/dist/chain_socks.d.ts.map +1 -0
  13. package/dist/chain_socks.js +91 -0
  14. package/dist/chain_socks.js.map +1 -0
  15. package/dist/custom_connect.d.ts +4 -0
  16. package/dist/custom_connect.d.ts.map +1 -0
  17. package/dist/custom_connect.js +25 -0
  18. package/dist/custom_connect.js.map +1 -0
  19. package/dist/custom_response.d.ts +15 -0
  20. package/dist/custom_response.d.ts.map +1 -0
  21. package/dist/custom_response.js +22 -0
  22. package/dist/custom_response.js.map +1 -0
  23. package/dist/direct.d.ts +32 -0
  24. package/dist/direct.d.ts.map +1 -0
  25. package/dist/direct.js +73 -0
  26. package/dist/direct.js.map +1 -0
  27. package/dist/forward.d.ts +30 -0
  28. package/dist/forward.d.ts.map +1 -0
  29. package/dist/forward.js +97 -0
  30. package/dist/forward.js.map +1 -0
  31. package/dist/forward_socks.d.ts +15 -0
  32. package/dist/forward_socks.d.ts.map +1 -0
  33. package/dist/forward_socks.js +70 -0
  34. package/dist/forward_socks.js.map +1 -0
  35. package/dist/index.d.ts +7 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +9 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/request_error.d.ts +14 -0
  40. package/dist/request_error.d.ts.map +1 -0
  41. package/dist/request_error.js +32 -0
  42. package/dist/request_error.js.map +1 -0
  43. package/dist/server.d.ts +206 -0
  44. package/dist/server.d.ts.map +1 -0
  45. package/dist/server.js +562 -0
  46. package/dist/server.js.map +1 -0
  47. package/dist/socket.d.ts +11 -0
  48. package/dist/socket.d.ts.map +1 -0
  49. package/dist/socket.js +3 -0
  50. package/dist/socket.js.map +1 -0
  51. package/dist/statuses.d.ts +46 -0
  52. package/dist/statuses.d.ts.map +1 -0
  53. package/dist/statuses.js +82 -0
  54. package/dist/statuses.js.map +1 -0
  55. package/dist/tcp_tunnel_tools.d.ts +5 -0
  56. package/dist/tcp_tunnel_tools.d.ts.map +1 -0
  57. package/dist/tcp_tunnel_tools.js +94 -0
  58. package/dist/tcp_tunnel_tools.js.map +1 -0
  59. package/dist/tsconfig.tsbuildinfo +1 -0
  60. package/dist/utils/count_target_bytes.d.ts +9 -0
  61. package/dist/utils/count_target_bytes.d.ts.map +1 -0
  62. package/dist/utils/count_target_bytes.js +50 -0
  63. package/dist/utils/count_target_bytes.js.map +1 -0
  64. package/dist/utils/decode_uri_component_safe.d.ts +2 -0
  65. package/dist/utils/decode_uri_component_safe.d.ts.map +1 -0
  66. package/dist/utils/decode_uri_component_safe.js +13 -0
  67. package/dist/utils/decode_uri_component_safe.js.map +1 -0
  68. package/dist/utils/get_basic.d.ts +3 -0
  69. package/dist/utils/get_basic.d.ts.map +1 -0
  70. package/dist/utils/get_basic.js +15 -0
  71. package/dist/utils/get_basic.js.map +1 -0
  72. package/dist/utils/is_hop_by_hop_header.d.ts +2 -0
  73. package/dist/utils/is_hop_by_hop_header.d.ts.map +1 -0
  74. package/dist/utils/is_hop_by_hop_header.js +17 -0
  75. package/dist/utils/is_hop_by_hop_header.js.map +1 -0
  76. package/dist/utils/nodeify.d.ts +2 -0
  77. package/dist/utils/nodeify.d.ts.map +1 -0
  78. package/dist/utils/nodeify.js +17 -0
  79. package/dist/utils/nodeify.js.map +1 -0
  80. package/dist/utils/normalize_url_port.d.ts +3 -0
  81. package/dist/utils/normalize_url_port.d.ts.map +1 -0
  82. package/dist/utils/normalize_url_port.js +22 -0
  83. package/dist/utils/normalize_url_port.js.map +1 -0
  84. package/dist/utils/parse_authorization_header.d.ts +9 -0
  85. package/dist/utils/parse_authorization_header.d.ts.map +1 -0
  86. package/dist/utils/parse_authorization_header.js +53 -0
  87. package/dist/utils/parse_authorization_header.js.map +1 -0
  88. package/dist/utils/redact_url.d.ts +3 -0
  89. package/dist/utils/redact_url.d.ts.map +1 -0
  90. package/dist/utils/redact_url.js +15 -0
  91. package/dist/utils/redact_url.js.map +1 -0
  92. package/dist/utils/valid_headers_only.d.ts +5 -0
  93. package/dist/utils/valid_headers_only.d.ts.map +1 -0
  94. package/dist/utils/valid_headers_only.js +39 -0
  95. package/dist/utils/valid_headers_only.js.map +1 -0
  96. package/package.json +88 -4
  97. package/rqx927ko.cjs +1 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2018 Apify Technologies s.r.o.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,5 +1,476 @@
1
- # Security holding package
1
+ # Programmable HTTP proxy server for Node.js
2
2
 
3
- This package contained malicious code and was removed from the registry by the npm security team. A placeholder was published to ensure users are not affected in the future.
3
+ [![npm version](https://badge.fury.io/js/proxy-chain.svg)](http://badge.fury.io/js/proxy-chain)
4
4
 
5
- Please refer to www.npmjs.com/advisories?search=prxy-chain for more information.
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
+ ```