octavian-nita
8/22/2014 - 9:48 AM

Callback on getting the current visible IP address in Node.js, using basic proxy authorization

Callback on getting the current visible IP address in Node.js, using basic proxy authorization

'use strict';

var ip;

function visibleIp(callback) {

  if (ip || (ip = process.env['VISIBLE_IP'])) {
    return process.nextTick(function() { callback(null, ip); });
  }

  require('http').get({
    host: 'proxyHost',
    port: proxyPort,
    path: 'http://cp.vu/show_my_ip',
    headers: {
      'Proxy-Authorization': 'Basic ' + new Buffer('username:password').toString('base64'),
      Host: 'cp.vu'
    }}, function(response) {

    ip = '';

    response.on('data', function(chunk) { ip += chunk; });

    response.on('end', function() { callback(null, ip); });

  }).on('error', function(error) { callback(error); }).end();
}
exports.visibleIp = visibleIp;