proxy-chain
Advanced tools
Comparing version 2.4.1 to 2.5.0-beta.0
@@ -25,3 +25,3 @@ /// <reference types="node" /> | ||
server: EventEmitter & { | ||
log: (...args: any[]) => void; | ||
log: (connectionId: unknown, str: string) => void; | ||
}; | ||
@@ -28,0 +28,0 @@ isPlain: boolean; |
@@ -6,16 +6,5 @@ "use strict"; | ||
const http_1 = tslib_1.__importDefault(require("http")); | ||
const buffer_1 = require("buffer"); | ||
const count_target_bytes_1 = require("./utils/count_target_bytes"); | ||
const get_basic_1 = require("./utils/get_basic"); | ||
const statuses_1 = require("./statuses"); | ||
const createHttpResponse = (statusCode, statusMessage, message = '') => { | ||
return [ | ||
`HTTP/1.1 ${statusCode} ${statusMessage || http_1.default.STATUS_CODES[statusCode] || 'Unknown Status Code'}`, | ||
'Connection: close', | ||
`Date: ${(new Date()).toUTCString()}`, | ||
`Content-Length: ${buffer_1.Buffer.byteLength(message)}`, | ||
``, | ||
message, | ||
].join('\r\n'); | ||
}; | ||
/** | ||
@@ -79,3 +68,3 @@ * Passes the traffic to upstream HTTP proxy server. | ||
: statuses_1.badGatewayStatusCodes.NON_200; | ||
sourceSocket.end(createHttpResponse(status, `UPSTREAM${response.statusCode}`)); | ||
sourceSocket.end((0, statuses_1.createCustomStatusHttpResponse)(status, `UPSTREAM${statusCode}`)); | ||
} | ||
@@ -132,3 +121,3 @@ server.emit('tunnelConnectFailed', { | ||
const statusCode = (_a = statuses_1.errorCodeToStatusCode[error.code]) !== null && _a !== void 0 ? _a : statuses_1.badGatewayStatusCodes.GENERIC_ERROR; | ||
const response = createHttpResponse(statusCode, (_b = error.code) !== null && _b !== void 0 ? _b : 'Upstream Closed Early'); | ||
const response = (0, statuses_1.createCustomStatusHttpResponse)(statusCode, (_b = error.code) !== null && _b !== void 0 ? _b : 'Upstream Closed Early'); | ||
sourceSocket.end(response); | ||
@@ -135,0 +124,0 @@ } |
@@ -20,3 +20,3 @@ /// <reference types="node" /> | ||
server: EventEmitter & { | ||
log: (...args: any[]) => void; | ||
log: (connectionId: unknown, str: string) => void; | ||
}; | ||
@@ -23,0 +23,0 @@ handlerOpts: HandlerOpts; |
@@ -22,2 +22,5 @@ "use strict"; | ||
const custom_connect_1 = require("./custom_connect"); | ||
const forward_socks_1 = require("./forward_socks"); | ||
const chain_socks_1 = require("./chain_socks"); | ||
const SOCKS_PROTOCOLS = ['socks:', 'socks4:', 'socks4a:', 'socks5:', 'socks5h:']; | ||
// TODO: | ||
@@ -154,2 +157,3 @@ // - Implement this requirement from rfc7230 | ||
const logPrefix = connectionId ? `${String(connectionId)} | ` : ''; | ||
// eslint-disable-next-line no-console | ||
console.log(`ProxyServer[${this.port}]: ${logPrefix}${str}`); | ||
@@ -227,6 +231,10 @@ } | ||
if (handlerOpts.customResponseFunction) { | ||
this.log(proxyChainId, 'Using HandlerCustomResponse'); | ||
this.log(proxyChainId, 'Using handleCustomResponse()'); | ||
return await (0, custom_response_1.handleCustomResponse)(request, response, handlerOpts); | ||
} | ||
this.log(proxyChainId, 'Using forward'); | ||
if (handlerOpts.upstreamProxyUrlParsed && SOCKS_PROTOCOLS.includes(handlerOpts.upstreamProxyUrlParsed.protocol)) { | ||
this.log(proxyChainId, 'Using forwardSocks()'); | ||
return await (0, forward_socks_1.forwardSocks)(request, response, handlerOpts); | ||
} | ||
this.log(proxyChainId, 'Using forward()'); | ||
return await (0, forward_1.forward)(request, response, handlerOpts); | ||
@@ -254,6 +262,10 @@ } | ||
if (handlerOpts.upstreamProxyUrlParsed) { | ||
this.log(socket.proxyChainId, `Using HandlerTunnelChain => ${request.url}`); | ||
if (SOCKS_PROTOCOLS.includes(handlerOpts.upstreamProxyUrlParsed.protocol)) { | ||
this.log(socket.proxyChainId, `Using chainSocks() => ${request.url}`); | ||
return await (0, chain_socks_1.chainSocks)(data); | ||
} | ||
this.log(socket.proxyChainId, `Using chain() => ${request.url}`); | ||
return await (0, chain_1.chain)(data); | ||
} | ||
this.log(socket.proxyChainId, `Using HandlerTunnelDirect => ${request.url}`); | ||
this.log(socket.proxyChainId, `Using direct() => ${request.url}`); | ||
return await (0, direct_1.direct)(data); | ||
@@ -382,5 +394,5 @@ } | ||
} | ||
if (handlerOpts.upstreamProxyUrlParsed.protocol !== 'http:') { | ||
if (!['http:', ...SOCKS_PROTOCOLS].includes(handlerOpts.upstreamProxyUrlParsed.protocol)) { | ||
// eslint-disable-next-line max-len | ||
throw new Error(`Invalid "upstreamProxyUrl" provided: URL must have the "http" protocol (was "${funcResult.upstreamProxyUrl}")`); | ||
throw new Error(`Invalid "upstreamProxyUrl" provided: URL must have one of the following protocols: "http", ${SOCKS_PROTOCOLS.map((p) => `"${p.replace(':', '')}"`).join(', ')} (was "${funcResult.upstreamProxyUrl}")`); | ||
} | ||
@@ -439,3 +451,2 @@ } | ||
headers['content-length'] = String(buffer_1.Buffer.byteLength(message)); | ||
// TODO: we should use ??= here | ||
headers.server = headers.server || this.authRealm; | ||
@@ -442,0 +453,0 @@ headers['content-type'] = headers['content-type'] || 'text/plain; charset=utf-8'; |
@@ -40,6 +40,8 @@ type HttpStatusCode = number; | ||
}; | ||
export declare const createCustomStatusHttpResponse: (statusCode: number, statusMessage: string, message?: string) => string; | ||
export declare const errorCodeToStatusCode: { | ||
[errorCode: string]: HttpStatusCode | undefined; | ||
}; | ||
export declare const socksErrorMessageToStatusCode: (socksErrorMessage: string) => (typeof badGatewayStatusCodes)[keyof typeof badGatewayStatusCodes]; | ||
export {}; | ||
//# sourceMappingURL=statuses.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.errorCodeToStatusCode = exports.badGatewayStatusCodes = void 0; | ||
exports.socksErrorMessageToStatusCode = exports.errorCodeToStatusCode = exports.createCustomStatusHttpResponse = exports.badGatewayStatusCodes = void 0; | ||
const http_1 = require("http"); | ||
@@ -51,2 +51,13 @@ exports.badGatewayStatusCodes = { | ||
http_1.STATUS_CODES['599'] = 'Upstream Error'; | ||
const createCustomStatusHttpResponse = (statusCode, statusMessage, message = '') => { | ||
return [ | ||
`HTTP/1.1 ${statusCode} ${statusMessage || http_1.STATUS_CODES[statusCode] || 'Unknown Status Code'}`, | ||
'Connection: close', | ||
`Date: ${(new Date()).toUTCString()}`, | ||
`Content-Length: ${Buffer.byteLength(message)}`, | ||
``, | ||
message, | ||
].join('\r\n'); | ||
}; | ||
exports.createCustomStatusHttpResponse = createCustomStatusHttpResponse; | ||
// https://nodejs.org/api/errors.html#common-system-errors | ||
@@ -60,2 +71,14 @@ exports.errorCodeToStatusCode = { | ||
}; | ||
const socksErrorMessageToStatusCode = (socksErrorMessage) => { | ||
switch (socksErrorMessage) { | ||
case 'Proxy connection timed out': | ||
return exports.badGatewayStatusCodes.TIMEOUT; | ||
case 'Socks5 Authentication failed': | ||
return exports.badGatewayStatusCodes.AUTH_FAILED; | ||
default: | ||
return exports.badGatewayStatusCodes.GENERIC_ERROR; | ||
} | ||
; | ||
}; | ||
exports.socksErrorMessageToStatusCode = socksErrorMessageToStatusCode; | ||
//# sourceMappingURL=statuses.js.map |
{ | ||
"name": "proxy-chain", | ||
"version": "2.4.1", | ||
"version": "2.5.0-beta.0", | ||
"description": "Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.", | ||
@@ -65,5 +65,5 @@ "main": "dist/index.js", | ||
"nyc": "^15.1.0", | ||
"puppeteer": "^19.6.3", | ||
"portastic": "^1.0.1", | ||
"proxy": "^1.0.2", | ||
"puppeteer": "^19.6.3", | ||
"request": "^2.88.2", | ||
@@ -90,4 +90,6 @@ "rimraf": "^4.1.2", | ||
"dependencies": { | ||
"socks": "^2.8.3", | ||
"socks-proxy-agent": "^8.0.3", | ||
"tslib": "^2.3.1" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # Programmable HTTP proxy server for Node.js | ||
A programmable proxy server (think Squid) with support for SSL/TLS, authentication, upstream proxy chaining, | ||
A programmable proxy server (think Squid) with support for SSL/TLS, authentication, upstream proxy chaining, SOCKS4/5 protocol, | ||
custom HTTP responses, and traffic statistics. | ||
@@ -73,3 +73,3 @@ 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. | ||
// Sets up an upstream HTTP proxy to which all the requests are forwarded. | ||
// Sets up an upstream HTTP/SOCKS proxy to which all the requests are forwarded. | ||
// If null, the proxy works in direct mode, i.e. the connection is forwarded directly | ||
@@ -79,2 +79,4 @@ // to the target server. This field is ignored if "requestAuthentication" is true. | ||
upstreamProxyUrl: `http://username:password@proxy.example.com:3128`, | ||
// Or use SOCKS4/5 proxy, e.g. | ||
// upstreamProxyUrl: `socks://username:password@proxy.example.com:1080`, | ||
@@ -111,2 +113,7 @@ // If "requestAuthentication" is true, you can use the following property | ||
## SOCKS support | ||
SOCKS protocol is supported for versions 4 and 5, specifically: `['socks', 'socks4', 'socks4a', 'socks5', 'socks5h']`, where `socks` will default to version 5. | ||
You can use an `upstreamProxyUrl` like `socks://username:password@proxy.example.com:1080`. | ||
## Error status codes | ||
@@ -113,0 +120,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
205998
96
2107
483
3
1
9
+ Addedsocks@^2.8.3
+ Addedsocks-proxy-agent@^8.0.3
+ Addedagent-base@7.1.1(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addedip-address@9.0.5(transitive)
+ Addedjsbn@1.1.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addedsmart-buffer@4.2.0(transitive)
+ Addedsocks@2.8.3(transitive)
+ Addedsocks-proxy-agent@8.0.4(transitive)
+ Addedsprintf-js@1.1.3(transitive)