Code Monkey home page Code Monkey logo

proxy-chain's Introduction

Programmable HTTP proxy server for Node.js

npm version Build Status

Node.js implementation of a proxy server (think Squid) with support for SSL, authentication, upstream proxy chaining, custom HTTP responses and measuring traffic statistics. 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:[email protected]:8080. With this library, you can set up a local proxy server without any password that will forward requests to the upstream proxy with password. The package is used for this exact purpose 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 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 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:[email protected]:3128`,

            // 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);
});

A different approach to 502 Bad Gateway

502 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!');
});
-if (request.url.toLowerCase() === 'example.com:80') {
+if (request.url.toLowerCase() === 'example.com:443') {

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 is 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:[email protected]: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:[email protected]: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:[email protected]'));

proxy-chain's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

proxy-chain's Issues

Fix unhandled exception in HandlerTunnelChain.onTrgRequestConnect()

Mar 07 16:38:50 ip-172-31-62-12 [email protected] TypeError: Cannot read property '_httpMessage' of null
    at ServerResponse.detachSocket (_http_server.js:223:17)
    at HandlerTunnelChain.onTrgRequestConnect (/home/apifier/proxy/node_modules/proxy-chain/build/handler_tunnel_chain.js:91:30)
    at Object.onceWrapper (events.js:418:26)
    at ClientRequest.emit (events.js:311:20)
    at Socket.socketOnData (_http_client.js:510:11)
    at Socket.emit (events.js:311:20)
    at addChunk (_stream_readable.js:294:12)
    at readableAddChunk (_stream_readable.js:275:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)

Windows : permission denied

const credentials = require(".././credentials");
const proxyChain = require("proxy-chain");

async function createSimpleProxy() {
    const proxyHttpPort = 15000;
    const proxyIp = `myip`;
    const oldProxyUrl = `http://${credentials.username}:${
        credentials.pw
        }@${proxyIp}:${proxyHttpPort}`;

    const newProxyUrl = await proxyChain.anonymizeProxy(oldProxyUrl);
    console.log(newProxyUrl);
    return newProxyUrl;
}

exports.createSimpleProxy = createSimpleProxy;

its being created properly getting http://127.0.0.1:26563
but after some time im getting

Unhandled rejection Error: listen EACCES: permission denied 0.0.0.0:49785
    at Server.setupListenHandle [as _listen2] (net.js:1260:19)
    at listenInCluster (net.js:1325:12)
    at Server.listen (net.js:1412:7)

no process is running at this port and im using admin rights

Unhandled error event on incorrect client password

Fresh install in a clean project with a copy/paste of the exact demo script from the readme,. Happens with the upstreamProxyUrl set to a valid proxy or null.

Executing the following curl command:
curl -v -x http://bob:[email protected]:8000 https://ifconfig.co/json
works fine. The proxy responds with the correct response (200 + data).

When I try sending an invalid password, the client receives the correct response (407). The issue is that the server crashes when this occurs (see logs for the node process below).

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
* Establish HTTP proxy tunnel to ifconfig.co:443
* Proxy auth using Basic with user 'bob'
> CONNECT ifconfig.co:443 HTTP/1.1
> Host: ifconfig.co:443
> Proxy-Authorization: Basic Ym9iOlRvcHNlY3JldA==
> User-Agent: curl/7.54.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 407 Proxy Authentication Required
< Content-Type: text/html; charset=utf-8
< Proxy-Authenticate: Basic realm="ProxyChain"
* Authentication problem. Ignoring this.
< Server: ProxyChain
< Connection: close
< Content-Length: 27
<
* Received HTTP code 407 from proxy after CONNECT
* Closing connection 0
curl: (56) Received HTTP code 407 from proxy after CONNECT

Logs from node process:

ProxyServer[8000]: Listening...
Proxy server is listening on port 8000
ProxyServer[8000]: 1 | !!! Handling CONNECT ifconfig.co:443 HTTP/1.1
ProxyServer[8000]: 1 | Using HandlerTunnelDirect
ProxyServer[8000]: 1 | Connecting to target ifconfig.co:443
ProxyServer[8000]: 1 | Target socket assigned
ProxyServer[8000]: 1 | Connected
ProxyServer[8000]: 1 | Source socket ended
ProxyServer[8000]: 1 | Closing handler
Connection 1 closed
{ srcTxBytes: 4684,
  srcRxBytes: 713,
  trgTxBytes: 550,
  trgRxBytes: 4589 }
ProxyServer[8000]: 1 | !!! Closed and removed from server
ProxyServer[8000]: 2 | !!! Handling CONNECT ifconfig.co:443 HTTP/1.1
ProxyServer[8000]: Request failed (status 407): Proxy credentials required.
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Process finished with exit code 1

Client network socket disconnected before secure TLS connection was established

Hi,

I ran into an error where sometimes when running a server, it fails to secure a socket connection. Here's the stack trace:

{ Error: Client network socket disconnected before secure TLS connection was established
at TLSSocket.onConnectEnd (_tls_wrap.js:1088:19)
at Object.onceWrapper (events.js:277:13)
at TLSSocket.emit (events.js:194:15)
at endReadableNT (_stream_readable.js:1103:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
code: 'ECONNRESET',
path: undefined,
host: null,
port: null,
localAddress: undefined }

Killing and starting the server over again fixes it (does not re-occur), but it happens in about 1 in 3 runs, and happens for every request. This is the trace on the server side:

Server[80]: 3 | Using HandlerTunnelChain
Server[80]: 3 | Connecting to upstream proxy...
Server[80]: 3 | Target socket assigned
Server[80]: 3 | Connected to upstream proxy
Server[80]: 3 | Target socket ended
Server[80]: 3 | Target socket closed
Server[80]: 3 | Source socket ended
Server[80]: 3 | Closing handler
Connection 3 closed
{ srcTxBytes: 203,
srcRxBytes: 139,
trgTxBytes: 143,
trgRxBytes: 205 }

Here's the request I'm making (which goes to an upstream proxy provider):

`
const userAgent = new UserAgent();
const userAgentString = userAgent.toString();
var agent = new HttpsProxyAgent(proxy_url);

request(
{
url: "https://example.com",
agent: agent,
method: 'get',
gzip: true,
headers: {
'User-Agent': userAgentString,
}
},
function(err, res, body) {
console.log(err)
}
)
`

Proxies Hang When sending concurrent requests with upstream proxy & local proxy

Having an issue where inbound requests are being received by proxy-chain, when submitting multiple requests to the upstream proxy there is a massive delay. This only happens when using about 100 - 200 requests. Using lower requests i can get response delay down to a 1-3 seconds but anymore and delay turns to about 10 - 50 seconds. Tried using local proxy-chains http proxy to rule out any rate limits set by proxy provider, issue still exists.

Script otherwise is very powerful.

proxy-chain send one header with unique name

If request have multiple headers with same name, proxy-chain will leave only one (last). Example:

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

const server = http.createServer((req, res) => { 
    res.writeHead(200, [['Set-Cookie', 'foo=bar; path=/; HttpOnly'], ['Set-Cookie', 'bar=foo; path=/; HttpOnly']]) 
    res.end();
}).listen(3000);

const proxyServer = (new ProxyChain.Server()).listen(8000);

Without proxy: curl 127.0.0.1:3000 -vvv

> GET / HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.55.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Set-Cookie: foo=bar; path=/; HttpOnly
< Set-Cookie: bar=foo; path=/; HttpOnly
< Date: Mon, 18 Dec 2017 09:18:05 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

With proxy: curl -x 127.0.0.1:8000 127.0.0.1:3000 -vvv

> GET http://127.0.0.1:3000/ HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.55.1
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 200 OK
< Set-Cookie: bar=foo; path=/; HttpOnly
< Date: Mon, 18 Dec 2017 09:17:49 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

Can you elaborate on the statistics method?

Hi,

Can you elaborate at all on what these numbers represent:

{
    srcTxBytes: Number,
    srcRxBytes: Number,
    trgTxBytes: Number,
    trgRxBytes: Number,
}

For example, I tried doing a test with postman by doing a GET request to the README of your repository, but the size of request and response did not match up with any combination of the metrics above. Please help! This is a great tool but I'm having trouble measuring the traffic.

Thanks.

Switching upstream proxy without restarting server

Is it possible to switch the upstream proxy host without restarting the server? I'm currently using the anonymizeProxy helper like so:

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

const user = '...';
const pass = '...';
const vpnHost = process.argv[2];
const oldProxyUrl = `http://${user}:${pass}@${vpnHost}:80`;
const newProxyUrl = proxyChain.anonymizeProxy(oldProxyUrl);

function cleanup(url) {
  proxyChain.closeAnonymizedProxy(url);
}

newProxyUrl.then(url => {
  console.log(url);
  process.on('SIGINT',  () => cleanup(url));
  process.on('SIGTERM', () => cleanup(url));
});

But I have to restart the process to change the host. It would be nice if I could send a signal to the process and it would switch the upstream proxy without changing its own port.

I think one way to solve this might be to allow passing a desired port to anonymizeProxy.


Edit: I hacked around this by modifying PORT_SELECTION_CONFIG:

const proxyChainTools = require('proxy-chain/build/tools');
...
const vpnPort = process.argv[3];
...
// HACK: Force the proxy to choose a port by narrowing the min/max in config.
if (vpnPort) {
  proxyChainTools.PORT_SELECTION_CONFIG.FROM = parseInt(vpnPort, 10);
  proxyChainTools.PORT_SELECTION_CONFIG.TO = parseInt(vpnPort, 10);
}

Now I can quickly restart the node process to switch the host and keep the same port ๐Ÿคทโ€โ™‚๏ธ

Method proxyChain.anonymizeProxy(oldProxyUrl) fails cause cannot pick the port

OS: MacOS
Version: 0.1.26
Problem: Method proxyChain.anonymizeProxy(oldProxyUrl) fails cause cannot pick the port.
It happens over and over again:

  portastic:find Result reached the maximum of 1 ports, returning... +0ms
  portastic:test Trying to test port 39565 +0ms
  portastic:test TCP server on port 39565 closed +0ms
  portastic:test Port 39565 was free +0ms
  portastic:find Result reached the maximum of 1 ports, returning... +0ms
  portastic:test Trying to test port 39566 +0ms
  portastic:test TCP server on port 39566 closed +1ms
  portastic:test Port 39566 was free +0ms
  portastic:find Result reached the maximum of 1 ports, returning... +0ms

Do you have any guesses of the roots of this problem?

Server not closing

using closeAnonymizedProxy without callback and using 'await' hangs and never closes the server.
version : latest
repro: example with anonymized proxy util function + add closeAnonymizedProxy at the end(before or after browser.close)

System : windows

SSL Error when using proxy-chain behind load balancer

Hi,

I'm getting the following response from my proxy-chain server:

{ Error: unable to verify the first certificate at TLSSocket.onConnectSecure (_tls_wrap.js:1051:34) at TLSSocket.emit (events.js:189:13) at TLSSocket._finishInit (_tls_wrap.js:633:8) code: 'UNABLE_TO_VERIFY_LEAF_S IGNATURE' }

I have it behind a GCP load balancer - when connecting to it via an instance directly, it runs perfectly fine. However when it's behind a load balancer, something with the intermediate certificate isn't set correctly.

I've seen solutions to this problem using something like:

var rootCas = require('ssl-root-cas/latest').create(); require('https').globalAgent.options.ca = rootCas;

Is it possible to configure proxy-chain like this? I can't find any references to https in the source.

Thanks!

HTTP forwarder should only forward valid HTTP status codes

Otherwise the proxy can crash with this error:

RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:194:11)
    at HandlerForward.onTrgResponse (/home/bob/proxy/node_modules/proxy-chain/build/handler_forward.js:158:30)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:543:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:112:17)
    at Socket.socketOnData (_http_client.js:440:20)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

TypeError: Cannot read property 'pipe' of null

Null trgSocket breaks Apify Actor run (see attached log for more details).

2020-02-04T14:32:40.741Z /home/myuser/node_modules/proxy-chain/build/handler_tunnel_chain.js:110
2020-02-04T14:32:40.743Z             this.trgSocket.pipe(this.srcSocket);
2020-02-04T14:32:40.745Z                            ^
2020-02-04T14:32:40.747Z 
2020-02-04T14:32:40.749Z TypeError: Cannot read property 'pipe' of null
2020-02-04T14:32:40.751Z     at HandlerTunnelChain.onTrgRequestConnect (/home/myuser/node_modules/proxy-chain/build/handler_tunnel_chain.js:110:28)
2020-02-04T14:32:40.753Z     at Object.onceWrapper (events.js:313:26)
2020-02-04T14:32:40.755Z     at ClientRequest.emit (events.js:223:5)
2020-02-04T14:32:40.758Z     at Socket.socketOnData (_http_client.js:490:11)
2020-02-04T14:32:40.760Z     at Socket.emit (events.js:223:5)
2020-02-04T14:32:40.762Z     at addChunk (_stream_readable.js:309:12)
2020-02-04T14:32:40.764Z     at readableAddChunk (_stream_readable.js:290:11)
2020-02-04T14:32:40.766Z     at Socket.Readable.push (_stream_readable.js:224:10)
2020-02-04T14:32:40.768Z     at TCP.onStreamRead (internal/stream_base_commons.js:181:23)

On Apify platform, Node.js v12.14.1, NPM version 6.13.4.

Break connection with custom status code

Hello.
I have an issue with breaking connection, that I don't want to continue. Let's say, I want to use proxy-chain as a load-balancer for my other proxies. But I want to select node depending on username and password. If password is incorrect or i have no addresses/nodes available - I want to reject this connection and set custom error status. For now I can only throw new Error(...) from customResponseFunction to break this connection. Can I do it more graceful, with setting statusCode and message? Thank you.

How to get headers from Connect Event

Hi I'm using your proxy-chain module for some it works very well with my previous proxy with the anonymize function but I have a new one : ProxyMesh,
I need to send some headers only to the proxy and not to the final website, how can I achieve this ?
need to send this header "X-ProxyMesh-Not-IP"

also I need to get some headers from the proxy after the first call to "connect" on the proxy
I need to get the "X-ProxyMesh-IP"
find more info here: https://docs.proxymesh.com/article/7-request-response-headers#https

whole documentation : https://docs.proxymesh.com/article/7-request-response-headers

I suppose I need to use the prepareRequestFunction but I could not get it working !

I need this on HTTPS final sites !! the proxy is http

Fix "The header content contains invalid characters" error

The stack trace is below. Also, it seems that ProxyChain server is emitting connectionClosed event for this connection, although prepareRequestFunction is never called for the connection.

Aug 17 02:43:32 ip-172-31-92-139 [email protected] ERROR {"level":"ERROR","msg":"Unexpected error in ProxyChain server","url":"http://www.acmetools.com/shop/tools/router-tables","exception":{"name":"TypeError","message":"The header content contains invalid characters","stack":"TypeError: The header content contains invalid characters\n at validateHeader (_http_outgoing.js:494:11)\n at ClientRequest.setHeader (_http_outgoing.js:498:3)\n at new ClientRequest (_http_client.js:173:14)\n at Object.request (http.js:38:10)\n at HandlerForward.run (/home/apifier/proxy/node_modules/proxy-chain/build/handler_forward.js:125:46)\n at Server.handlerRun (/home/apifier/proxy/node_modules/proxy-chain/build/server.js:411:21)\n at /home/apifier/proxy/node_modules/proxy-chain/build/server.js:208:24\n at tryCatcher (/home/apifier/proxy/node_modules/bluebird/js/release/util.js:16:23)\n at Promise._settlePromiseFromHandler (/home/apifier/proxy/node_modules/bluebird/js/release/promise.js:512:31)\n at Promise._settlePromise (/home/apifier/proxy/node_modules/bluebird/js/release/promise.js:569:18)\n at Promise._settlePromise0 (/home/apifier/proxy/node_modules/bluebird/js/release/promise.js:614:10)\n at Promise._settlePromises (/home/apifier/proxy/node_modules/bluebird/js/release/promise.js:693:18)\n at Async._drainQueue (/home/apifier/proxy/node_modules/bluebird/js/release/async.js:133:16)\n at Async._drainQueues (/home/apifier/proxy/node_modules/bluebird/js/release/async.js:143:10)\n at Immediate.Async.drainQueues (/home/apifier/proxy/node_modules/bluebird/js/release/async.js:17:14)\n at runCallback (timers.js:810:20)\n at tryOnImmediate (timers.js:768:5)\n at processImmediate [as _immediateCallback] (timers.js:745:5)"}}

IPV6 not supported

It seems that IPV6 is not supported now.
ProxyServer[8883]: 9 | !!! Closed and removed from server ProxyServer[8883]: 10 | !!! Handling CONNECT [2a00:1450:4014:80c::200a]:443 HTTP/1.1 ProxyServer[8883]: Request failed (status 400): Target "[2a00:1450:4014:80c::200a]:443" could not be parsed.

Override the request before routing it to the upstream proxy

I send a request to the proxy chain server with some specific header that i don't want to send to the upstream proxy.

I use that header to generate the upstream url in the prepareRequestFunction so i dont need to restart the server to change proxy and i already know how to fetch it but i want to remove it once i have generated the upstream url so it wont be send to the upstream proxy.

I have tried something like this but the header is still sent:

...
prepareRequestFunction: ({
      request,
      username,
      password,
      hostname,
      port,
      isHttp,
    }) => {
        let upstreamProxyUrl;
        if (request.headers['proxy-to-use'] === undefined) throw new ProxyChain.RequestError("No proxy-to-use request header",400);
        upstreamProxyUrl = `http://${_username}:${_password}@${request.headers['proxy-to-use']}`;
        delete request.headers['proxy-to-use'];
        return {
          upstreamProxyUrl 
        };
    }
...

How can i change some property of the request object before sending the request to the upstream proxy?

Cannot read property pipe of null at HandlerTunnelChain.onTrgRequestConnect

Version: 4.3

2020-03-10T04:44:10.266Z /home/myuser/node_modules/proxy-chain/build/handler_tunnel_chain.js:111
2020-03-10T04:44:10.276Z             this.trgSocket.pipe(this.srcSocket);
2020-03-10T04:44:10.278Z                            ^
2020-03-10T04:44:10.279Z 
2020-03-10T04:44:10.281Z TypeError: Cannot read property 'pipe' of null
2020-03-10T04:44:10.283Z     at HandlerTunnelChain.onTrgRequestConnect (/home/myuser/node_modules/proxy-chain/build/handler_tunnel_chain.js:111:28)
2020-03-10T04:44:10.285Z     at Object.onceWrapper (events.js:418:26)
2020-03-10T04:44:10.287Z     at ClientRequest.emit (events.js:311:20)
2020-03-10T04:44:10.289Z     at Socket.socketOnData (_http_client.js:510:11)
2020-03-10T04:44:10.291Z     at Socket.emit (events.js:311:20)
2020-03-10T04:44:10.293Z     at addChunk (_stream_readable.js:294:12)
2020-03-10T04:44:10.294Z     at readableAddChunk (_stream_readable.js:275:11)
2020-03-10T04:44:10.296Z     at Socket.Readable.push (_stream_readable.js:209:10)
2020-03-10T04:44:10.298Z     at TCP.onStreamRead (internal/stream_base_commons.js:186:23)

Keep Alive not working

I'm using this awesome project to proxy certains requests and I noticed that keep alive doesn't work. Is there a way to make it work?

Periodic ERR_TUNNEL_CONNECTION_FAILED in Chrome

I'm using this with Capybara / Selenium WebDriver and notice sporadic ERR_TUNNEL_CONNECTION_FAILED errors when trying to load a page. Refreshing doesn't help. Some attempts work, some don't. I don't see any other error messages so I'm not really sure how to debug this. The only variable that changes between attempts is the upstream proxy hostname.

Versions:

chromedriver: ChromeDriver 2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8)
chrome: Version 68.0.3440.106 (Official Build) (64-bit)
capybara: 3.6.0
capybara-webkit: 1.15.0
proxy-chain: 0.2.4

Running the ps command, here's how the process looks that Capybara spawns:

/Applications/Google Chrome.app/Contents/MacOS/Google Chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --ignore-certificate-errors --load-extension=/var/folders/kl/y1jrp7zs55sbx075jjjl3p280000gn/T/.org.chromium.Chromium.PogJ9o/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --proxy-server=http://127.0.0.1:34700 --remote-debugging-port=12736 --test-type=webdriver --use-mock-keychain --user-data-dir=/var/folders/kl/y1jrp7zs55sbx075jjjl3p280000gn/T/.org.chromium.Chromium.XKkbMD --window-size=1440,900 data:,

Please share code example

Kindly share comprehensive code example to work from NodeJs behind corporate proxy. It will be very helpful, as the present documentation does not speak much about how to use this nice utility.

Memory Leak on pm2

Hi !

I mounted the simplest instance of proxy-chain on an Amazon EC2 instance running with pm2, but when I came back today I found the proxy was down due to a memory leak with this error:

(node:26700) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [Socket]. Use emitter.setMaxListeners() to increase limit

Do you have any idea of what is happening ?

Thanks !

Avoid requiring the _http_common module

Otherwise, the package is not usable from Electron. See apify/crawlee#522

The module is only needed for isInvalidHeader() in https://github.com/apifytech/proxy-chain/blob/master/src/tools.js#L2

Double-check that the HTTP header validation is compatible with Node.js' header validation (they change HTTP parser in version 12 I think, which has slightly different behavior), perhaps in unit tests we could test against Node's internal methods too, just to be sure.

Proxy can easily be detected

Hi,

First of all, awesome work. I really like the idea of this project.

After inspection I saw that a couple of hop-by-hop headers such as

const HOP_BY_HOP_HEADERS = [
    'Connection',
    'Keep-Alive',
    'Proxy-Authenticate',
    'Proxy-Authorization',
    'TE',
    'Trailers',
    'Transfer-Encoding',
    'Upgrade',
];

are stripped from the forwarded http request that is passed to the upstream proxy.

This essentially means that there is no way to establish a Web Socket Connection when using this proxy. Web Sockets are initiated with the Connection and Upgrade header. When those headers are stripped, a targeted website could simply block the crawling/scraping based on the fact that the client is unable to establish a WebSocket connection.

This is just an example. The proxy could be detected via many other ways.

Wouldn't it be possible to use an transparent proxy on TCP/IP level (socks) that forwards the packet stream?

I am aware that this would mean that http/s proxies cannot be used anymore, but at least it would eliminate the detection methods...

Any ideas?

Proxy server doesn't support HTTP UPGRADE for web sockets

It only supports web sockets via HTTP CONNECT method (used e.g. with SSL). The unit tests (testWsCall()) only test for that too. We should add full support for HTTP UPGRADE method in the main server and handle this correctly even with plain HTTP.

Multiple ports with alternate upstream proxies

Hey guys great project.
I am looking at either running multiple instances of proxy-chain or alternately finding a way to listen on multiple ports (but specify different upstream proxy based on the port a request lands on).

My question is:
Which do you think makes more sense?

I didn't see anything in the docs about setting a single instance to listen on more than one port so I guess if that isn't a possibility then I will fall back and spin multiple instances which may make more sense anyway?

Cannot receive RequestError on client using https request

What I'm trying to do is:
throw new ProxyChain.RequestError('Only Bob can use this proxy!', 400);
So when I use http, works fine, I receive an error I sent, but when I use https, I get -111 TUNNEL_CONNECTION_FAILED.

I kinda get why it might happen, but what do I do if I want receive my error, instead of -111?

Proxy-chain left some async operation

Im using jest to test my code and Im getting a warning in tests that are using proxy-chain.

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

It turned out that the following code is enought to get this message

const proxyChain = require('proxy-chain')

test('local proxy', async () => {
  const lp = await proxyChain.anonymizeProxy(proxy);
  await proxyChain.closeAnonymizedProxy(lp, true);
});

A temporary solution for this problem is:

afterAll(async () => {
  await new Promise(res => setTimeout(res, 5000));
});

and jest's warning is gone.

Multiple ips

Is it possible to choose which ip to use on machine with multiple ips with this?

https request identification

When I am sending request to HTTPS page through proxy-chain with prepareRequestFunction there is no useful info about request for identification.
For example if I am opening one HTTPS website two times in a row I want it to be passed through different proxies. But next time when I will open this website two times I need it to use the same proxies.

Limiting amount of incoming requests and put them to pending state

Hi,

I found this library very useful even though puppeter nowadays supports page.authenticate() but for some of my proxies I don't get authentication popup so only this library is the last hope or solutions like Squid. So thanks a lot.

Meanwhile I need a bit extend it and I'm curious now how to make it in a better way, because I have proxies that are limits me with concurrent connections, let's say 5 but browser for sure generate a waaaay more. I need something like a global throttler but not a network throttler, throttler for amount of active connections. If we reached limit then all further connections are coming into queue and awaiting of competition one of the processes that was queued already.

I checked the code a bit and basically my needs would solve a simple Promise inside onConnect callback. But to add it is not that trivial. Maybe you would have a better idea?

Thanks for any feedback.

Error: net::ERR_SSL_PROTOCOL_ERROR

I get Error: net::ERR_SSL_PROTOCOL_ERROR when trying to access https site via http socket

puppeteer.launch({
    headless: true,
    args: [
      `--window-size=${ua.data.viewportWidth},${ua.data.viewportHeight + windowFrame}`,
      `--proxy-server=${await proxyChain.anonymizeProxy(getRandomProxy())}`,
    ]
  }).then(async browser => {
const page = await browser.newPage();
await page.goto('https://some-site.com/'), {waitUntil: 'networkidle0'};
})

It seems that error is happening when site has Let'sEncrypt's cert, accessing sites with comodo certs (e.g. https://www.myip.com/) works

SOCKS5 support

Is it within the scope of this library to eventually support SOCKS5 proxies?

I had a look through your code and the tests suggest it's not supported currently.

username and password are null - but are being sent

ive noticed with some of the tools I use, basic authentication does not work - the username and password are null in the prepareRequestFunction. I think they must be getting lost somehow in the CONNECT event - any idea what might be causing this?

There is an uncaught "write after end" error somewhere

Error: write after end
    at write_ (_http_outgoing.js:622:15)
    at ServerResponse.write (_http_outgoing.js:617:10)
    at IncomingMessage.ondata (_stream_readable.js:639:20)
    at emitOne (events.js:116:13)
    at IncomingMessage.emit (events.js:211:7)
    at IncomingMessage.Readable.read (_stream_readable.js:475:10)
    at flow (_stream_readable.js:846:34)
    at ServerResponse.<anonymous> (_stream_readable.js:707:7)
    at emitNone (events.js:106:13)
    at ServerResponse.emit (events.js:208:7)
    at Socket.ondrain (internal/http.js:20:44)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at onwriteDrain (_stream_writable.js:474:12)
    at afterWrite (_stream_writable.js:462:5)
    at onwrite (_stream_writable.js:455:7)
    at WriteWrap.afterWrite [as oncomplete] (net.js:876:12)

Hangs with Received response from target (200)

Inbound request is correctly forwarded by proxy-chain to upstream HTTP proxy,
but the HTTP client (node request here for testing) doesn't get the response body until
the proxy-chain server is stopped (manually).

proxy-chain shows

Received response from target (200)

HTTP client still waits for response.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.