4

Here is a simple nodejs script for using node as an https client to interact with the outside world. How do you modify it so you don't get "ECONNREFUSED" errors when you run it behind a corporate proxy like http://10.10.10.10:8080 ?

var https = require('https');

var options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET'
};

var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);

  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
  console.error(e);
});

----Update---

I figured out how to with the npm 'request' package (https://github.com/mikeal/request). Still wondering how to do this with vanilla node libraries.

var request = require('request').defaults({proxy:'http://my.proxy.server:8080', agent:false});
request('https://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})
l.cotonea
  • 2,037
  • 1
  • 15
  • 17
Chad Brewbaker
  • 2,523
  • 2
  • 19
  • 26
  • check out answers on previously asked [how-can-i-use-an-http-proxy-with-node-js-http-client](http://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client) on SO – palanik Feb 17 '14 at 20:31

1 Answers1

1

Normally its best to handle proxying at the system level - configure your operating system to use the proxy not the script. However this may also fail, as most corporate proxies don't have all the ports open.

A universal solution would be to have a server (EC2 or Rackspace instance) to act as your own proxy and have it listen for ssh on port 443.Port 443 is open in corporate firewalls for https.

At which point you can either tunnel all you local network traffic to the server, or ssh to the server and run the script there.

If you were to make scripts proxy specific, they wouldn't be very portable, and not production hardened as they'd break when the proxy configuration changed.

Darren White
  • 525
  • 5
  • 14