10

I need to send my client HTTPS requests through an intranet proxy to a server. I use both https and request+global-tunnel and neither solutions seem to work.
The similar code with 'http' works. Is there other settings I missed?

The code failed with an error:

REQUEST:
problem with request: tunneling socket could not be established, cause=socket hang up

HTTPS:
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: socket hang up
    at SecurePair.error (tls.js:1011:23)
    at EncryptedStream.CryptoStream._done (tls.js:703:22)
    at CleartextStream.read [as _read] (tls.js:499:24)

The code is the simple https test.

var http = require("https");

var options = {
  host: "proxy.myplace.com",
  port: 912,
  path: "https://www.google.com",
  headers: {
    Host: "www.google.com"
  }
};

http.get(options, function(res) {
  console.log(res);
  res.pipe(process.stdout);
});
3
  • 1
    Did you solve it? having the same problem
    – Domingo C.
    Mar 15, 2016 at 13:25
  • Hi @DomingoC. hopefully my answer can provide some insight to you as well as James.
    – Kevin M
    Dec 7, 2016 at 21:03
  • @Domingo - I implemented the tunneling code to get it to work. Did you try Kevin M solution? I probably will clean up the code and post the answer here later.
    – JamesNW
    Jan 27, 2017 at 18:56

1 Answer 1

24

You probably want to establish a TLS encrypted connection between your node app and target destination through a proxy.

In order to do this you need to send a CONNECT request with the target destination host name and port. The proxy will create a TCP connection to the target host and then simply forwards packs between you and the target destination.

I highly recommend using the request client. This package simplifies the process and handling of making HTTP/S requests.

Example code using request client:

var request = require('request');

request({
    url: 'https://www.google.com',
    proxy: 'http://97.77.104.22:3128'
}, function (error, response, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

Example code using no external dependencies:

var http = require('http'),
    tls = require('tls');

var req = http.request({
    host: '97.77.104.22',
    port: 3128,
    method: 'CONNECT',
    path: 'twitter.com:443'
});

req.on('connect', function (res, socket, head) {
    var tlsConnection = tls.connect({
        host: 'twitter.com',
        socket: socket
    }, function () {
        tlsConnection.write('GET / HTTP/1.1\r\nHost: twitter.com\r\n\r\n');
    });

    tlsConnection.on('data', function (data) {
        console.log(data.toString());
    });
});

req.end();
2
  • 1
    Great answer! And you may get 'ERR_TLS_CERT_ALTNAME_INVALID' for sites in multi-homed server, try to add servername: 'twitter.com' in the option of tls.connect
    – Kevin Xi
    Nov 16, 2020 at 4:57
  • 3
    Request client is deprecated now.
    – Dormouse
    Jan 10, 2021 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.