proxy-chain 3.0.2-beta.9 → 3.0.3-beta.0

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.
package/README.md CHANGED
@@ -241,6 +241,8 @@ console.log(`Proxy server is listening on port ${server.port}`);
241
241
 
242
242
  **Note:** Custom agents are only supported for HTTP and HTTPS upstream proxies. SOCKS upstream proxies use direct socket connections and do not support custom agents.
243
243
 
244
+ **Note:** Pooling applies to requests for `http://` URLs, whether the upstream proxy is HTTP or HTTPS. Requests for `https://` URLs reach the upstream as a `CONNECT` tunnel. A connection that has carried a tunnel is never returned to the agent's pool, so each tunnel gets its own upstream connection, even with `keepAlive: true`. Requests sent inside one tunnel share its connection, but separate tunnels don't share connections.
245
+
244
246
  ## SOCKS support
245
247
  SOCKS protocol is supported for versions 4 and 5, specifically: `['socks', 'socks4', 'socks4a', 'socks5', 'socks5h']`, where `socks` will default to version 5.
246
248
 
package/dist/forward.js CHANGED
@@ -3,8 +3,9 @@ import https from 'node:https';
3
3
  import { pipeline } from 'node:stream/promises';
4
4
  import { badGatewayStatusCodes, errorCodeToStatusCode } from './statuses.js';
5
5
  import { countTargetBytes } from './utils/count_target_bytes.js';
6
+ import { forwardedRequestHeaders } from './utils/forwarded_request_headers.js';
7
+ import { forwardedResponseHeaders } from './utils/forwarded_response_headers.js';
6
8
  import { getBasicAuthorizationHeader } from './utils/get_basic.js';
7
- import { validHeadersOnly } from './utils/valid_headers_only.js';
8
9
  /**
9
10
  * The request is read from the client and is resent.
10
11
  * This is similar to Direct / Chain, however it uses the CONNECT protocol instead.
@@ -22,12 +23,12 @@ import { validHeadersOnly } from './utils/valid_headers_only.js';
22
23
  * Client <- Apify (HTTP) <- Upstream (HTTP) <- Web
23
24
  * ```
24
25
  */
25
- export const forward = async (request, response, handlerOpts) => new Promise(async (resolve, reject) => {
26
+ export const forward = async (request, response, handlerOpts) => new Promise((resolve, reject) => {
26
27
  const proxy = handlerOpts.upstreamProxyUrlParsed;
27
28
  const origin = proxy ? proxy.origin : request.url;
28
29
  const options = {
29
30
  method: request.method,
30
- headers: validHeadersOnly(request.rawHeaders),
31
+ headers: forwardedRequestHeaders(request),
31
32
  insecureHTTPParser: true,
32
33
  localAddress: handlerOpts.localAddress,
33
34
  family: handlerOpts.ipFamily,
@@ -58,7 +59,7 @@ export const forward = async (request, response, handlerOpts) => new Promise(asy
58
59
  reject(new Error('407 Proxy Authentication Required'));
59
60
  return;
60
61
  }
61
- response.writeHead(statusCode, clientResponse.statusMessage, validHeadersOnly(clientResponse.rawHeaders));
62
+ response.writeHead(statusCode, clientResponse.statusMessage, forwardedResponseHeaders(clientResponse));
62
63
  // `pipeline` automatically handles all the events and data
63
64
  await pipeline(clientResponse, response);
64
65
  resolve();
@@ -3,7 +3,8 @@ import { pipeline } from 'node:stream/promises';
3
3
  import { SocksProxyAgent } from 'socks-proxy-agent';
4
4
  import { badGatewayStatusCodes, errorCodeToStatusCode } from './statuses.js';
5
5
  import { countTargetBytes } from './utils/count_target_bytes.js';
6
- import { validHeadersOnly } from './utils/valid_headers_only.js';
6
+ import { forwardedRequestHeaders } from './utils/forwarded_request_headers.js';
7
+ import { forwardedResponseHeaders } from './utils/forwarded_response_headers.js';
7
8
  /**
8
9
  * Forwards HTTP requests through a SOCKS upstream proxy.
9
10
  *
@@ -16,11 +17,11 @@ import { validHeadersOnly } from './utils/valid_headers_only.js';
16
17
  * Client <- Apify (HTTP) <- Upstream (SOCKS) <- Web
17
18
  * ```
18
19
  */
19
- export const forwardSocks = async (request, response, handlerOpts) => new Promise(async (resolve, reject) => {
20
+ export const forwardSocks = async (request, response, handlerOpts) => new Promise((resolve, reject) => {
20
21
  const agent = new SocksProxyAgent(handlerOpts.upstreamProxyUrlParsed);
21
22
  const options = {
22
23
  method: request.method,
23
- headers: validHeadersOnly(request.rawHeaders),
24
+ headers: forwardedRequestHeaders(request),
24
25
  insecureHTTPParser: true,
25
26
  localAddress: handlerOpts.localAddress,
26
27
  agent,
@@ -39,7 +40,7 @@ export const forwardSocks = async (request, response, handlerOpts) => new Promis
39
40
  reject(new Error('407 Proxy Authentication Required'));
40
41
  return;
41
42
  }
42
- response.writeHead(statusCode, clientResponse.statusMessage, validHeadersOnly(clientResponse.rawHeaders));
43
+ response.writeHead(statusCode, clientResponse.statusMessage, forwardedResponseHeaders(clientResponse));
43
44
  // `pipeline` automatically handles all the events and data
44
45
  await pipeline(clientResponse, response);
45
46
  resolve();
@@ -0,0 +1,2 @@
1
+ import type http from 'node:http';
2
+ export declare const forwardedRequestHeaders: (request: http.IncomingMessage) => string[];
@@ -0,0 +1,16 @@
1
+ import { validHeadersOnly } from './valid_headers_only.js';
2
+ // Node de-chunks the inbound body but only re-chunks outbound bodies on methods that have one by default.
3
+ // On GET, HEAD, DELETE and OPTIONS the body would otherwise be written raw and parsed upstream as a new request.
4
+ export const forwardedRequestHeaders = (request) => {
5
+ const headers = validHeadersOnly(request.rawHeaders);
6
+ if (request.headers['transfer-encoding'] === undefined) {
7
+ return headers;
8
+ }
9
+ const withoutContentLength = [];
10
+ for (let index = 0; index < headers.length; index += 2) {
11
+ if (headers[index].toLowerCase() !== 'content-length') {
12
+ withoutContentLength.push(headers[index], headers[index + 1]);
13
+ }
14
+ }
15
+ return [...withoutContentLength, 'transfer-encoding', 'chunked'];
16
+ };
@@ -0,0 +1,2 @@
1
+ import type http from 'node:http';
2
+ export declare const forwardedResponseHeaders: (clientResponse: http.IncomingMessage) => string[];
@@ -0,0 +1,17 @@
1
+ import { validHeadersOnly } from './valid_headers_only.js';
2
+ // Transfer-Encoding is hop-by-hop and gets stripped, so a Content-Length sent alongside it no longer describes
3
+ // the body. RFC 9112 section 6.3 requires an intermediary to remove it; Node then re-frames the body as chunked.
4
+ export const forwardedResponseHeaders = (clientResponse) => {
5
+ const headers = validHeadersOnly(clientResponse.rawHeaders);
6
+ if (clientResponse.headers['transfer-encoding'] === undefined) {
7
+ return headers;
8
+ }
9
+ const withoutContentLength = [];
10
+ // `headers` is a flat [name, value, name, value, ...] array, so step by 2 and drop the content-length pair.
11
+ for (let index = 0; index < headers.length; index += 2) {
12
+ if (headers[index].toLowerCase() !== 'content-length') {
13
+ withoutContentLength.push(headers[index], headers[index + 1]);
14
+ }
15
+ }
16
+ return withoutContentLength;
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "proxy-chain",
3
- "version": "3.0.2-beta.9",
3
+ "version": "3.0.3-beta.0",
4
4
  "description": "Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -54,33 +54,33 @@
54
54
  "@types/basic-auth": "^1.1.8",
55
55
  "@types/body-parser": "^1.19.6",
56
56
  "@types/express": "^5.0.6",
57
- "@types/node": "^26.4.0",
57
+ "@types/node": "^26.6.4",
58
58
  "@types/underscore": "^1.13.0",
59
- "@types/ws": "^8.18.1",
60
- "@vitest/coverage-v8": "^4.1.11",
59
+ "@types/ws": "^8.18.2",
60
+ "@vitest/coverage-v8": "^4.1.12",
61
61
  "basic-auth": "^3.0.0",
62
62
  "basic-auth-parser": "^0.0.2",
63
63
  "body-parser": "^2.3.0",
64
64
  "cross-env": "^7.0.3",
65
- "eslint": "^9.18.0",
65
+ "eslint": "^9.39.5",
66
66
  "express": "^5.2.1",
67
67
  "portastic": "^1.0.1",
68
68
  "proxy": "^4.1.0",
69
- "puppeteer": "^25.1.0",
69
+ "puppeteer": "^25.12.0",
70
70
  "rimraf": "^6.1.3",
71
71
  "socksv5": "^0.0.6",
72
- "tsx": "^4.21.0",
72
+ "tsx": "^4.23.15",
73
73
  "typescript": "^6.0.3",
74
- "typescript-eslint": "^8.20.0",
75
- "underscore": "^1.13.1",
76
- "undici": "^7.29.1",
77
- "vitest": "^4.1.11",
78
- "ws": "^8.2.2"
74
+ "typescript-eslint": "^8.71.0",
75
+ "underscore": "^1.13.8",
76
+ "undici": "^7.30.0",
77
+ "vitest": "^4.1.12",
78
+ "ws": "^8.22.0"
79
79
  },
80
80
  "dependencies": {
81
- "socks": "^2.8.3",
81
+ "socks": "^2.8.10",
82
82
  "socks-proxy-agent": "^10.1.0",
83
- "tslib": "^2.3.1"
83
+ "tslib": "^2.8.1"
84
84
  },
85
85
  "scripts": {
86
86
  "build:watch": "tsc -w",