apify / proxy-chain

Node.js implementation of a proxy server (think Squid) with support for SSL, authentication and upstream proxy chaining.
https://www.npmjs.com/package/proxy-chain
Apache License 2.0
804 stars 138 forks source link

customConnectServer doesnt work #361

Closed Rumatoid closed 2 months ago

Rumatoid commented 1 year ago

From readme

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,
  }) => {
    return {
      customConnectServer: exampleServer,
    };
  },
});

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

Doesnt do anything

gokaybiz commented 6 months ago

it returns 'Not Found'

jirimoravcik commented 2 months ago

Hello, I just tested this and it works properly.

Just run the code you provided locally and then run the following code against it:

const { URL } = require('url');
const http = require('http');

const host = 'example.com:80';
const url = new URL(`http://${host}/`);

const getMessage = (socket) => {
    const request = http.request(url, {
        createConnection: () => socket,
    }, response => {
        const chunks = [];

        response.on('data', (chunk) => {
            chunks.push(chunk);
        });

        response.once('end', () => {
            console.log(Buffer.concat(chunks).toString());
        });
    });

    request.once('error', (error) => {
        console.error('tunnel error', error);
    });

    request.end();
};

const request = http.request('http://localhost:8000', {
    method: 'CONNECT',
    headers: {
        host,
    },
    path: host,
});

request.once('connect', (response, socket, head) => {
    getMessage(socket);
});

request.once('error', (error) => {
    console.error('connect error', error);
});

request.end();

You should see

Hello from a custom server!