Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use an https proxy with node.js https/request Client?

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);
});
like image 239
JamesNW Avatar asked Dec 04 '15 00:12

JamesNW


People also ask

Can a HTTP proxy handle HTTPS?

Introduction. Modern proxy servers can be used as gateways for requests that access both HTTP and HTTPS resources.

How do I make a HTTPS post in node JS?

get function working here... const https = require("https"); function get(url, callback) { "use-strict"; https. get(url, function (result) { var dataQueue = ""; result. on("data", function (dataBuffer) { dataQueue += dataBuffer; }); result.

What is HTTP request in Node JS?

Node.js | https.request () Function Last Updated : 31 May, 2020 Node.js provides two core modules for making http requests. The http module can be used to make http requests and the https module can be used to make https requests.

How to use an HTTP proxy with NodeJS?

path has the path that we want to proxy with the proxy. And headers has the Host with the host URL to proxy. To use an http proxy with Node.js http.Client, we can use the http.get method to connect to the proxy.

How do I use an HTTP proxy?

Using a HTTP proxy (for non secure requests) is very simple. You connect to the proxy and make the request normally except that the path part includes the full url and the host header is set to the host you want to connect to. Tim was very close with his answer but he missed setting the host header properly. var http = require("http");

How long does it take to make HTTPS requests through proxy?

The main reason is that for each HTTPS request you make, the getURL function establishes a new connection with the proxy, which is highly inefficient. We use the program below to measure the time it takes to make 50 HTTPS requests through an HTTP proxy: In total, it takes ~4 min 48s to make the 50 HTTPS requests (5.8 s/request):


1 Answers

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();
like image 200
Kevin M Avatar answered Dec 20 '22 11:12

Kevin M