Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

proxy-chain

apify474.1kApache-2.02.5.6TypeScript support: included

Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, and protocol tunneling.

proxy, squid, apify, tunnel, puppeteer

readme

Programmable HTTP proxy server for Node.js

npm version

A programmable proxy server (think Squid) with support for SSL/TLS, authentication, upstream proxy chaining, SOCKS4/5 protocol, custom HTTP responses, and traffic statistics. 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.

For example, the proxy-chain package is useful if you need to use headless Chrome web browser and proxies with authentication, because Chrome doesn't support proxy URLs with password, such as http://username:password@proxy.example.com:8080. With this package, you can set up a local proxy server without any password that will forward requests to the upstream proxy with password. For details, read How to make headless Chrome and Puppeteer use a proxy server with authentication.

The proxy-chain package is developed by Apify, the full-stack web scraping and data extraction platform, to support their Apify Proxy product, 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, the world's most popular web craling library for Node.js.

The proxy-chain package currently supports HTTP/SOCKS forwarding and HTTP CONNECT tunneling to forward arbitrary protocols such as HTTPS or FTP (learn more). The HTTP CONNECT tunneling also supports the SOCKS protocol. Also, proxy-chain only supports the Basic Proxy-Authorization.

Run a simple HTTP/HTTPS proxy server

const ProxyChain = require('proxy-chain');

const server = new ProxyChain.Server({ port: 8000 });

server.listen(() => {
    console.log(`Proxy server is listening on port ${8000}`);
});

Run a HTTP/HTTPS proxy server with credentials and upstream proxy

const ProxyChain = require('proxy-chain');

const server = new ProxyChain.Server({
    // Port where the server will listen. By default 8000.
    port: 8000,

    // Optional host where the proxy server will listen.
    // If not specified, the sever listens on an unspecified IP address (0.0.0.0 in IPv4, :: in IPv6)
    // You can use this option to limit the access to the proxy server.
    host: 'localhost',

    // Enables verbose logging
    verbose: true,

    // Custom user-defined function to authenticate incoming proxy requests,
    // and optionally provide the URL to chained upstream proxy.
    // The function must return an object (or promise resolving to the object) with the following signature:
    // { requestAuthentication: boolean, upstreamProxyUrl: string, failMsg?: string, customTag?: unknown }
    // If the function is not defined or is null, the server runs in simple mode.
    // Note that the function takes a single argument with the following properties:
    // * request      - An instance of http.IncomingMessage class with information about the client request
    //                  (which is either HTTP CONNECT for SSL protocol, or other HTTP request)
    // * username     - Username parsed from the Proxy-Authorization header. Might be empty string.
    // * password     - Password parsed from the Proxy-Authorization header. Might be empty string.
    // * hostname     - Hostname of the target server
    // * port         - Port of the target server
    // * isHttp       - If true, this is a HTTP request, otherwise it's a HTTP CONNECT tunnel for SSL
    //                  or other protocols
    // * connectionId - Unique ID of the HTTP connection. It can be used to obtain traffic statistics.
    prepareRequestFunction: ({ request, username, password, hostname, port, isHttp, connectionId }) => {
        return {
            // If set to true, the client is sent HTTP 407 resposne with the Proxy-Authenticate header set,
            // requiring Basic authentication. Here you can verify user credentials.
            requestAuthentication: username !== 'bob' || password !== 'TopSecret',

            // 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
            // to the target server. This field is ignored if "requestAuthentication" is true.
            // The username and password must be URI-encoded.
            upstreamProxyUrl: `http://username:password@proxy.example.com:3128`,
            // Or use SOCKS4/5 proxy, e.g.
            // upstreamProxyUrl: `socks://username:password@proxy.example.com:1080`,

            // If "requestAuthentication" is true, you can use the following property
            // to define a custom error message to return to the client instead of the default "Proxy credentials required"
            failMsg: 'Bad username or password, please try again.',

            // Optional custom tag that will be passed back via
            // `tunnelConnectResponded` or `tunnelConnectFailed` events
            // Can be used to pass information between proxy-chain
            // and any external code or application using it
            customTag: { userId: '123' },
        };
    },
});

server.listen(() => {
  console.log(`Proxy server is listening on port ${server.port}`);
});

// Emitted when HTTP connection is closed
server.on('connectionClosed', ({ connectionId, stats }) => {
  console.log(`Connection ${connectionId} closed`);
  console.dir(stats);
});

// Emitted when HTTP request fails
server.on('requestFailed', ({ request, error }) => {
  console.log(`Request ${request.url} failed`);
  console.error(error);
});

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

The 502 Bad Gateway HTTP status code is not comprehensive enough. Therefore, the server may respond with 590-599 instead:

590 Non Successful

Upstream responded with non-200 status code.

591 RESERVED

This status code is reserved for further use.

592 Status Code Out Of Range

Upstream respondend with status code different than 100-999.

593 Not Found

DNS lookup failed - EAI_NODATA or EAI_NONAME.

594 Connection Refused

Upstream refused connection.

595 Connection Reset

Connection reset due to loss of connection or timeout.

596 Broken Pipe

Trying to write on a closed socket.

597 Auth Failed

Incorrect upstream credentials.

598 RESERVED

This status code is reserved for further use.

599 Upstream Error

Generic upstream error.


590 and 592 indicate an issue on the upstream side. \ 593 indicates an incorrect proxy-chain configuration.\ 594, 595 and 596 may occur due to connection loss.\ 597 indicates incorrect upstream credentials.\ 599 is a generic error, where the above is not applicable.

Custom error responses

To return a custom HTTP response to indicate an error to the client, you can throw the RequestError from inside of the prepareRequestFunction function. The class constructor has the following parameters: RequestError(body, statusCode, headers). By default, the response will have Content-Type: text/plain; charset=utf-8.

const ProxyChain = require('proxy-chain');

const server = new ProxyChain.Server({
    prepareRequestFunction: ({ request, username, password, hostname, port, isHttp, connectionId }) => {
        if (username !== 'bob') {
           throw new ProxyChain.RequestError('Only Bob can use this proxy!', 400);
        }
    },
});

Measuring traffic statistics

To get traffic statistics for a certain HTTP connection, you can use:

const stats = server.getConnectionStats(connectionId);
console.dir(stats);

The resulting object looks like:

{
    // Number of bytes sent to client
    srcTxBytes: Number,
    // Number of bytes received from client
    srcRxBytes: Number,
    // Number of bytes sent to target server (proxy or website)
    trgTxBytes: Number,
    // Number of bytes received from target server (proxy or website)
    trgRxBytes: Number,
}

If the underlying sockets were closed, the corresponding values will be null, rather than 0.

Custom responses

Custom responses allow you to override the response to a HTTP requests to the proxy, without contacting any target host. For example, this is useful if you want to provide a HTTP proxy-style interface to an external API or respond with some custom page to certain requests. Note that this feature is only available for HTTP connections. That's because HTTPS connections cannot be intercepted without access to the target host's private key.

To provide a custom response, the result of the prepareRequestFunction function must define the customResponseFunction property, which contains a function that generates the custom response. The function is passed no parameters and it must return an object (or a promise resolving to an object) with the following properties:

{
  // Optional HTTP status code of the response. By default it is 200.
  statusCode: 200,

  // Optional HTTP headers of the response
  headers: {
    'X-My-Header': 'bla bla',
  }

  // Optional string with the body of the HTTP response
  body: 'My custom response',

  // Optional encoding of the body. If not provided, defaults to 'UTF-8'
  encoding: 'UTF-8',
}

Here is a simple example:

const ProxyChain = require('proxy-chain');

const server = new ProxyChain.Server({
    port: 8000,
    prepareRequestFunction: ({ request, username, password, hostname, port, isHttp }) => {
        return {
            customResponseFunction: () => {
                return {
                    statusCode: 200,
                    body: `My custom response to ${request.url}`,
                };
            },
        };
    },
});

server.listen(() => {
  console.log(`Proxy server is listening on port ${server.port}`);
});

Routing CONNECT to another HTTP server

While customResponseFunction enables custom handling methods such as GET and POST, many HTTP clients rely on CONNECT tunnels. It's possible to route those requests differently using the customConnectServer option. It accepts an instance of Node.js HTTP server.

const http = require('http');
const ProxyChain = require('proxy-chain');

const exampleServer = http.createServer((request, response) => {
    response.end('Hello from a custom server!');
});

const server = new ProxyChain.Server({
    port: 8000,
    prepareRequestFunction: ({ request, username, password, hostname, port, isHttp }) => {
        if (request.url.toLowerCase() === 'example.com:80') {
            return {
                customConnectServer: exampleServer,
            };
        }

        return {};
    },
});

server.listen(() => {
  console.log(`Proxy server is listening on port ${server.port}`);
});

In the example above, all CONNECT tunnels to example.com are overridden. This is an unsecure server, so it accepts only http: requests.

In order to intercept https: requests, https.createServer should be used instead, along with a self signed certificate.

const https = require('https');
const fs = require('fs');
const key = fs.readFileSync('./test/ssl.key');
const cert = fs.readFileSync('./test/ssl.crt');

const exampleServer = https.createServer({
    key,
    cert,
}, (request, response) => {
    response.end('Hello from a custom server!');
});

Closing the server

To shut down the proxy server, call the close([destroyConnections], [callback]) function. For example:

server.close(true, () => {
  console.log('Proxy server was closed.');
});

The closeConnections parameter indicates whether pending proxy connections should be forcibly closed. If it's false, the function will wait until all connections are closed, which can take a long time. If the callback parameter is omitted, the function returns a promise.

Accessing the CONNECT response headers for proxy tunneling

Some upstream proxy providers might include valuable debugging information in the CONNECT response headers when establishing the proxy tunnel, for they may not modify future data in the tunneled connection.

The proxy server would emit a tunnelConnectResponded event for exposing such information, where the parameter types of the event callback are described in Node.js's documentation. Example:

server.on('tunnelConnectResponded', ({ proxyChainId, response, socket, head, customTag }) => {
    console.log(`CONNECT response headers received: ${response.headers}`);
});

Alternatively a helper function may be used:

listenConnectAnonymizedProxy(anonymizedProxyUrl, ({ response, socket, head }) => {
    console.log(`CONNECT response headers received: ${response.headers}`);
});

You can also listen to CONNECT requests that receive response with status code different from 200. The proxy server would emit a tunnelConnectFailed event.

server.on('tunnelConnectFailed', ({ proxyChainId, response, socket, head, customTag }) => {
    console.log(`CONNECT response failed with status code: ${response.statusCode}`);
});

Helper functions

The package also provides several utility functions.

anonymizeProxy({ url, port }, callback)

Parses and validates a HTTP proxy URL. If the proxy requires authentication, then the function starts an open local proxy server that forwards to the proxy. 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.

The function takes an optional callback that receives the anonymous proxy URL. If no callback is supplied, the function returns a promise that resolves to a String with anonymous proxy URL or the original URL if it was already anonymous.

The following example shows how you can use a proxy with authentication from headless Chrome and Puppeteer. For details, read this blog post.

const puppeteer = require('puppeteer');
const proxyChain = require('proxy-chain');

(async() => {
    const oldProxyUrl = 'http://bob:password123@proxy.example.com:8000';
    const newProxyUrl = await proxyChain.anonymizeProxy(oldProxyUrl);

    // Prints something like "http://127.0.0.1:45678"
    console.log(newProxyUrl);

    const browser = await puppeteer.launch({
        args: [`--proxy-server=${newProxyUrl}`],
    });

    // Do your magic here...
    const page = await browser.newPage();
    await page.goto('https://www.example.com');
    await page.screenshot({ path: 'example.png' });
    await browser.close();

    // Clean up
    await proxyChain.closeAnonymizedProxy(newProxyUrl, true);
})();

closeAnonymizedProxy(anonymizedProxyUrl, closeConnections, callback)

Closes anonymous proxy previously started by anonymizeProxy(). If proxy was not found or was already closed, the function has no effect and its result is false. Otherwise the result is true.

The closeConnections parameter indicates whether pending proxy connections are forcibly closed. If it's false, the function will wait until all connections are closed, which can take a long time.

The function takes an optional callback that receives the result Boolean from the function. If callback is not provided, the function returns a promise instead.

createTunnel(proxyUrl, targetHost, options, callback)

Creates a TCP tunnel to targetHost that goes through a HTTP proxy server specified by the proxyUrl parameter.

The optional options parameter is an object with the following properties:

  • port: Number - Enables specifying the local port to listen at. By default 0, which means a random port will be selected.
  • hostname: String - Local hostname to listen at. By default localhost.
  • verbose: Boolean - If true, the functions logs a lot. By default false.

The result of the function is a local endpoint in a form of hostname:port. All TCP connections made to the local endpoint will be tunneled through the proxy to the target host and port. For example, this is useful if you want to access a certain service from a specific IP address.

The tunnel should be eventually closed by calling the closeTunnel() function.

The createTunnel() function accepts an optional Node.js-style callback that receives the path to the local endpoint. If no callback is supplied, the function returns a promise that resolves to a String with the path to the local endpoint.

For more information, read this blog post.

Example:

const host = await createTunnel('http://bob:pass123@proxy.example.com:8000', 'service.example.com:356');
// Prints something like "localhost:56836"
console.log(host);

closeTunnel(tunnelString, closeConnections, callback)

Closes tunnel previously started by createTunnel(). The result value is false if the tunnel was not found or was already closed, otherwise it is true.

The closeConnections parameter indicates whether pending connections are forcibly closed. If it's false, the function will wait until all connections are closed, which can take a long time.

The function takes an optional callback that receives the result of the function. If the callback is not provided, the function returns a promise instead.

listenConnectAnonymizedProxy(anonymizedProxyUrl, tunnelConnectRespondedCallback)

Allows to configure a callback on the anonymized proxy URL for the CONNECT response headers. See the above section Accessing the CONNECT response headers for proxy tunneling for details.

redactUrl(url, passwordReplacement)

Takes a URL and hides the password from it. For example:

// Prints 'http://bob:<redacted>@example.com'
console.log(redactUrl('http://bob:pass123@example.com'));

changelog

2.0.1 / 2022-05-02

  • Simplify code, fix tests, move to TypeScript #162
  • Bugfix: Memory leak in createTunnel #160
  • Bugfix: Proxy fails to handle non-standard HTTP response in HTTP forwarding mode, on certain websites #107
  • Pass proxyChainId to tunnelConnectResponded #173
  • feat: accept custom port for proxy anonymization #214
  • fix: socket close race condition
  • feat: closeConnection by id #176
  • feat: custom dns lookup #175

1.0.3 / 2021-08-17

  • Fixed EventEmitter memory leak (see issue #81)
  • Added automated tests for Node 16
  • Updated dev dependencies

1.0.2 / 2021-04-14

  • Bugfix: closeTunnel() function didn't work because of runningServers[port].connections.forEach is not a function error (see issue #127)

1.0.1 / 2021-04-09

  • Bugfix: parseUrl() result now always includes port for http(s), ftp and ws(s) (even if explicitly specified port is the default one) This fixes #123.

1.0.0 / 2021-03-17

  • BREAKING: The parseUrl() function slightly changed its behavior (see README for details):
    • it no longer returns an object on invalid URLs and throws an exception instead
    • it URI-decodes username and password if possible (if not, the function keeps the username and password as is)
    • it adds back auth property for better backwards compatibility
  • The above change should make it possible to pass upstream proxy URLs containing special characters, such as http://user:pass:wrd@proxy.example.com or http://us%35er:passwrd@proxy.example.com. The parsing is done on a best-effort basis. The safest way is to always URI-encode username and password before constructing the URL, according to RFC 3986. This change should finally fix issues: #89, #67, and #108
  • BREAKING: Improved error handling in createTunnel() and prepareRequestFunction() functions and provided better error messages. Both functions now fail if the upstream proxy URL contains colon (:) character in the username, in order to comply with RFC 7617. The functions now fail fast with a reasonable error, rather later and with cryptic errors.
  • BREAKING: The createTunnel() function now lets the system assign potentially random listening TCP port, instead of the previous selection from range from 20000 to 60000.
  • BREAKING: The undocumented findFreePort() function was moved from tools.js to test/tools.js
  • Added the ability to access proxy CONNECT headers for proxy tunneling.
  • Removed dependency on Node.js internal modules, hopefully allowing usage of this library in Electron.
  • Got rid of the "portastic" NPM package and thus reduced bundle size by ~50%
  • Various code improvements and better tests.
  • Updated packages.

0.4.9 / 2021-01-26

  • Bugfix: Added back the scheme field to result from parseUrl()

0.4.8 / 2021-01-26

  • Bugfix: parseUrl() function now handles IPv6 and other previously unsupported URLs. Fixes issues #89 and #67.

0.4.7 / 2021-01-19

  • Bugfix: closeTunnel() function was returning invalid value. see PR #98.

0.4.6 / 2020-11-09

  • Proxy.Server now supports port: 0 option to assign the port randomly, see PR #98.
  • anonymizeProxy() now uses the above port assignment rather than polling for random port => better performance
  • Updated NPM packages

0.4.5 / 2020-05-15

  • Added checks for closed handlers, in order to prevent the Cannot read property 'pipe' of null errors (see issue #64)

0.4.4 / 2020-03-12

  • Attempt to fix an unhandled exception in HandlerTunnelChain.onTrgRequestConnect (see issue #64)
  • Code cleanup

0.4.3 / 2020-03-08

  • Fixed unhandled TypeError: Cannot read property '_httpMessage' of null exception in HandlerTunnelChain.onTrgRequestConnect (see issue #63)

0.4.2 / 2020-02-28

  • Bugfix: Prevented attempted double-sending of certain HTTP responses to client, which might have caused some esoteric errors
  • Error responses now by default have Content-Type: text/plain; charset=utf-8 instead of text/html; charset=utf-8 or missing one.

0.4.1 / 2020-02-22

  • Increased socket end/destroy timeouts from 100ms to 1000ms, to ensure the client receives the data.

0.4.0 / 2020-02-22

  • BREAKING CHANGE: Dropped support for Node.js 9 and lower.
  • BUGFIX: Consume source socket errors to avoid unhandled exceptions. Fixes Issue #53.
  • BUGFIX: Renamed misspelled Trailers HTTP header to Trailer.
  • Replaced bluebird dependency with native Promises.
  • Upgraded NPM dev dependencies.
  • Fixed broken tests caused by newly introduced strict HTTP parsing in Node.js.
  • Fixed broken test on Node.js 10 by adding NODE_OPTIONS=--insecure-http-parser env var to npm test.

0.3.3 / 2019-12-27

  • More informative messages for "Invalid upstreamProxyUrl" errors

0.3.2 / 2019-09-17

  • Bugfix: Prevent the "TypeError: hostHeader.startsWith is not a function error in HandlerForward by not forwarding duplicate Host headers

0.3.1 / 2019-09-07

  • BREAKING CHANGE: closeAnonymizedProxy throws on invalid proxy URL
  • Bugfix: Attempt to prevent the unhandled "write after end" error
  • Bugfix: Proxy no longer attempts to forward invalid HTTP status codes and fails with 500 Internal Server Error
  • Fixed closing of sockets on Node 10+
  • Fixed and improved unit tests to also work on Node 10+, update dev dependencies
  • Changed HTTP 200 message from Connection established to Connection Established to be according to standards
  • Proxy source/target sockets are set to no delay (i.e. disabled Nagle's algorithm), to avoid any caching delays
  • Improved logging

0.2.7 / 2018-02-19

  • Updated README

0.2.6 / 2018-12-27

  • Bugfix: Added Host header to HTTP CONNECT requests to upstream proxies

0.2.5 / 2018-09-10

  • Bugfix: Invalid request headers broke proxy chain connection. Now they will be skipped instead.

0.2.4 / 2018-07-27

  • Bugfix: large custom responses were not delivered completely because the socket was closed too early

0.2.3 / 2018-06-21

  • Bugfix: 'requestFailed' was emitting { request, err } instead of { request, error }

0.2.2 / 2018-06-19

  • BREAKING: The 'requestFailed' event now emits object { request, error } instead of just error

0.1.35 / 2018-06-12

  • Bugfix: When target URL cannot be parsed instead of crashing, throw RequestError

0.1.34 / 2018-06-08

  • Minor improvement: HandlerBase.fail() now supports RequestError

0.1.33 / 2018-06-08

  • Renamed customResponseFunc to customResponseFunction and changed parameters for more clarity

0.1.32 / 2018-06-08

  • Added customResponseFunc option to prepareRequestFunction to support custom response to HTTP requests

0.1.31 / 2018-05-21

  • Updated project homepage in package.json

0.1.29 / 2018-04-15

  • Fix: anonymizeProxy() now supports upstream proxies with empty password

0.1.28 / 2018-03-27

  • Added createTunnel() function to create tunnels through HTTP proxies for arbitrary TCP network connections (eq. connection to mongodb/sql database through HTTP proxy)

0.1.27 / 2018-03-05

  • Better error messages for common network errors
  • Pass headers from target socket in HTTPS tunnel chains

0.1.26 / 2018-02-14

  • If connection is denied because of authentication error, optionally "prepareRequestFunction" can provide error message.

0.1.25 / 2018-02-12

  • When connection is only through socket, close srcSocket when trgSocket ends

0.1.24 / 2018-02-09

  • Fixed incorrect closing of ServerResponse object which caused phantomjs to mark resource requests as errors.

0.1.23 / 2018-02-07

  • Fixed missing variable in "Incorrect protocol" error message.

0.1.22 / 2018-02-05

  • Renamed project's GitHub organization

0.1.21 / 2018-01-26

  • Added Server.getConnectionIds() function

0.1.20 / 2018-01-26

  • Fixed "TypeError: The header content contains invalid characters" bug

0.1.19 / 2018-01-25

  • fixed uncaught error events, code improved

0.1.18 / 2018-01-25

  • fixed a memory leak, improved logging and consolidated code

0.1.17 / 2018-01-23

  • added connectionClosed event to notify users about closed proxy connections

0.1.16 / 2018-01-09

  • added measuring of proxy stats - see getConnectionStats() function

0.1.14 / 2017-12-19

  • added support for multiple headers with the same name (thx shershennm)

0.0.1 / 2017-11-06

  • Project created