public-address
Advanced tools
Comparing version 0.1.1 to 0.1.2
40
index.js
'use strict'; | ||
var http = require('http'); | ||
var dns = require('dns'); | ||
var net = require('net'); | ||
@@ -9,5 +11,5 @@ module.exports = resolve; | ||
var requestOptions = { | ||
hostname: 'www.remoteaddress.net', | ||
hostname: 'api.ipify.org', | ||
port: 80, | ||
path: '/api/ip', | ||
path: '/?format=json', | ||
method: 'GET' | ||
@@ -22,7 +24,7 @@ }; | ||
options = options || {}; | ||
Object.keys(options).forEach(function(key) { | ||
Object.keys(options).forEach(function (key) { | ||
requestOptions[key] = options[key]; | ||
}); | ||
http.get(requestOptions, function(res) { | ||
http.get(requestOptions, function (res) { | ||
var chunks = [], | ||
@@ -32,4 +34,4 @@ chunklen = 0; | ||
if (res.statusCode != 200) { | ||
res.on('data', function() {}); | ||
res.on('end', function() { | ||
res.on('data', function () {}); | ||
res.on('end', function () { | ||
callback(new Error('Invalid response code ' + res.statusCode)); | ||
@@ -40,3 +42,3 @@ }); | ||
res.on('data', function(chunk) { | ||
res.on('data', function (chunk) { | ||
chunks.push(chunk); | ||
@@ -46,4 +48,5 @@ chunklen += chunk.length; | ||
res.on('end', function() { | ||
res.on('end', function () { | ||
var data; | ||
var ip; | ||
try { | ||
@@ -53,10 +56,23 @@ data = JSON.parse(Buffer.concat(chunks, chunklen).toString()); | ||
if (!data) { | ||
callback(new Error('Invalid response from server')); | ||
return callback(new Error('Invalid response from server')); | ||
} else { | ||
callback(null, data); | ||
ip = data.ip; | ||
if (!ip || !net.isIP(ip)) { | ||
return callback(new Error('IP address missing from server response')); | ||
} | ||
dns.reverse(ip, function (err, hostnames) { | ||
var response = { | ||
address: ip | ||
}; | ||
if (!err && hostnames && hostnames.length) { | ||
response.hostname = hostnames[0]; | ||
} | ||
callback(null, response); | ||
}); | ||
} | ||
}); | ||
}).on('error', function(err) { | ||
}).on('error', function (err) { | ||
callback(err); | ||
}); | ||
} | ||
} |
{ | ||
"name": "public-address", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Resolve public IP address and hostname", | ||
@@ -20,4 +20,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"nodeunit": "~0.8.2" | ||
"nodeunit": "~0.10.2" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # public-address | ||
This module makes a HTTP request to www.remoteaddress.net IP resolving service. | ||
This module makes a HTTP request to https://www.ipify.org/ IP resolving service. | ||
@@ -8,0 +8,0 @@ ## Installation |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
3540
62
3