proxy-chain
Node.js implementation of a proxy server (think Squid) with support for SSL, authentication and upstream proxy chaining.
The authentication and proxy chaining configuration is defined in code and can be dynamic.
Note that the proxy server only supports Basic authentication
(see Proxy-Authorization for details).
For example, this package is useful if you need to use proxies with authentication
in the headless Chrome web browser, because it doesn't accept proxy URLs such as http://username:password@proxy.example.com:8080
.
With this library, you can setup a local proxy server without any password
that will forward requests to the upstream proxy with password.
For this very purpose the package is used by the Apify web scraping platform.
To learn more about the rationale behind this package,
read How to make headless Chrome and Puppeteer use a proxy server with authentication.
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: 8000,
verbose: true,
prepareRequestFunction: ({ request, username, password, hostname, port, isHttp }) => {
return {
requestAuthentication: username !== 'bob' || password !== 'TopSecret',
upstreamProxyUrl: `http://username:password@proxy.example.com:3128`,
};
},
});
server.listen(() => {
console.log(`Proxy server is listening on port ${8000}`);
});
Closing the server
To shutdown 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 the callback
parameter is omitted, the function returns a promise.
Helper functions
The package also provides several utility functions.
anonymizeProxy(proxyUrl, 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 is chosen randomly.
The function takes 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.
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 if false
. Otherwise the result is true
.
The closeConnections
parameter indicates whether pending proxy connections are forcibly closed.
The function takes optional callback that receives the result Boolean from the function.
If callback is not provided, the function returns a promise instead.
createTunnel(proxyUrl, target, options, callback)
Attempts to create a network tunnel through proxy server specified in param "proxyUrl" to a network service
specified in param "target".
The function takes optional callback that receives the path to local service.
If no callback is supplied, the function returns a promise that resolves to a String with
the path to local service.
Example usage:
const tunnel = await createTunnel('http://<username>:<password>@<proxy-server>:<port>', '<service-host>:<service-port>');
closeTunnel(tunnelString, closeConnections, callback)
Closes tunnel previously started by createTunnel()
.
Returns false if tunnel is not found or running. Otherwise the result is true
.
The closeConnections
parameter indicates whether pending connections are forcibly closed.
The function takes optional callback that receives the result Boolean from the function.
If callback is not provided, the function returns a promise instead.
parseUrl(url)
Calls Node.js's url.parse
function and extends the resulting object with the following fields: scheme
, username
and password
.
For example, for HTTP://bob:pass123@example.com
these values are
http
, bob
and pass123
, respectively.
redactUrl(url, passwordReplacement)
Takes a URL and hides the password from it. For example:
console.log(redactUrl('http://bob:pass123@example.com'));