D
D
Dmitry2018-09-02 01:37:23
Node.js
Dmitry, 2018-09-02 01:37:23

Node.JS tunneling HTTP/HTTPS/etc requests through a proxy?

I am creating a stable socket connection to a proxy server and I need to send requests to different domains and protocols (http, https, etc). Faced the problem of sending requests using the received socket after connecting to the proxy server:

var socket = net.connect(
    1111, // proxy port
    'xx.xx.xxx.xx' // proxy host
);

socket.on('connect', function(a) {
    console.log('--- connect', socket.remoteAddress);

    // connecting to https host
    socket.write(
        'CONNECT www.google.com:443 HTTP/1.1\r\n' +
        'Proxy-Connections: keep-alive\r\n' +
        'Host: www.google.com\r\n\r\n',
    function() {

        // send GET request and wait for response
        socket.write(
            'GET / HTTP/1.0\r\n' +
            'User-Agent: Node.js/0.6.6\r\n' +
            'Connection: keep-alive\r\n' +
            'Host: www.google.com\r\n\r\n',
            function(a) {
                return socket;
            }
        );

        /*
            another requests to different protocols/ports...
            socket.write(...)
        */

        return socket;
    });
});

socket.on('data', function(a) {
    console.log('--- data', a.toString());
});
socket.on('drain', function() {
    console.log('drain!');
});

socket.on('timeout', function() {
    console.log('timeout!');
});
socket.on('close', function() {
    console.log('Connection closed');
});
socket.on('end', function() {
    console.log('Connection end');
});

Running the code gives me the following:
--- connect xx.xx.xxx.xx
--- data HTTP/1.0 200 Connection established

Connection end
Connection closed

That is, the https connection is created successfully, judging by the "HTTP/1.0 200 Connection established" response, but then the proxy connection is closed without issuing a response to the GET request and other requests, although Keep-Alive is specified.
Where is the mistake? Googling resulted in creating different connections for https and for other protocols, which is not suitable for my task, since I need to send requests in the context of one stable connection to the proxy server.
UPD:
Sometimes I get an error:
ssl3_get_record:wrong version number
But when I try to specify the SSLv3_method encryption protocol, I get an error:
ssl3_methods is disabled

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question