external-ip
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,3 +6,4 @@ #!/usr/bin/env node | ||
const extIP = require('./extIP'); | ||
const version = require('../package.json').version; | ||
const pkg = require('../package.json'); | ||
const defaultConfig = require('./defaultConfig'); | ||
@@ -14,21 +15,27 @@ const collect = (service, services) => { | ||
program | ||
.version(version) | ||
.option('-R, --replace', 'replace internal services instead of extending them.') | ||
.option('-s, --services <url>', 'service url, see examples, required if using -R', collect, []) | ||
.option('-t, --timeout <msec>', 'set timeout per request', parseInt) | ||
.option('-P, --parallel', 'set to parallel mode'); | ||
// Hack commander program name to match package name (for --help) | ||
program._name = pkg.name; | ||
program.on('--help', () => { | ||
console.log('This program prints the external IP of the machine.\n' + | ||
'All arguments are optional.'); | ||
console.log('Examples:'); | ||
console.log('$ external-ip'); | ||
console.log('$ external-ip -P -t 1500 -R -s http://icanhazip.com/ -s http://ifconfig.io/ip'); | ||
console.log(` | ||
This program prints the external IP of the machine. | ||
All arguments are optional.\n | ||
Examples: | ||
$ external-ip | ||
$ external-ip -P -t 1500 -R -s ${defaultConfig.services[0]} -s ${defaultConfig.services[1]}\n | ||
Default services: | ||
${defaultConfig.services.join('\n \t')}\n | ||
Documentation can be found at ${pkg.homepage}\n`); | ||
}); | ||
program.parse(process.argv); | ||
program | ||
.usage('[options]') | ||
.version(pkg.version) | ||
.option('-R, --replace', 'replace internal services instead of extending them.') | ||
.option('-s, --services <url>', 'service url, see examples, required if using -R', collect, []) | ||
.option('-t, --timeout <ms>', 'set timeout per request', parseInt) | ||
.option('-P, --parallel', 'set to parallel mode') | ||
.option('-u, --userAgent <User-Agent>', `provide a User-Agent header, default: ${defaultConfig.userAgent}`, null, '') | ||
.parse(process.argv); | ||
const generateConfig = (cliConf) => { | ||
@@ -46,2 +53,5 @@ let config = {}; | ||
} | ||
if (cliConf.userAgent) { | ||
config.userAgent = cliConf.userAgent; | ||
} | ||
return config; | ||
@@ -53,3 +63,3 @@ }; | ||
getIP((err, ip) =>{ | ||
getIP((err, ip) => { | ||
if (err) { | ||
@@ -56,0 +66,0 @@ console.error(err); |
@@ -13,3 +13,4 @@ 'use strict'; | ||
], | ||
timeout: 1000 | ||
timeout: 1000, | ||
userAgent: 'curl/' | ||
}; |
@@ -31,3 +31,3 @@ 'use strict'; | ||
errors.push(error); | ||
current +=1; | ||
current += 1; | ||
// when every single service has failed tell the bad news | ||
@@ -37,7 +37,10 @@ if (errors.length === requests.length) { | ||
} | ||
// try the next one | ||
return loop(); | ||
} | ||
// got an ip | ||
return cb(null, ip); | ||
}); | ||
}; | ||
// initiate the first request | ||
loop(); | ||
@@ -51,25 +54,28 @@ }, | ||
let onResponse = function (err, ip) { | ||
let onResponse = (error, ip) => { | ||
// got an ip from a previous request, so there is nothing to do here | ||
if (done) { | ||
return; | ||
} | ||
if (err) { | ||
errors.push(err); | ||
if (error) { | ||
errors.push(error); | ||
} | ||
// when every single service has failed tell the bad news | ||
if (errors.length === requests.length) { | ||
return cb(utils.concatErrors(errors), null); | ||
} | ||
if (ip) { | ||
done = true; | ||
// Abort evey pending request | ||
ongoingRequests.forEach((request) => { | ||
if (this === request) { | ||
return; | ||
} | ||
request.abort(); | ||
}); | ||
ongoingRequests = null; | ||
return cb(null, ip); | ||
} | ||
if (errors.length === requests.length) { | ||
return cb(utils.concatErrors(errors), null); | ||
} | ||
}; | ||
// initiate all the requests | ||
ongoingRequests = requests.map((service) => service(onResponse)); | ||
@@ -76,0 +82,0 @@ |
@@ -8,2 +8,3 @@ 'use strict'; | ||
/** | ||
* Checks if an IP is a valid v4 or v6 | ||
* @param str | ||
@@ -14,3 +15,7 @@ * @return boolean | ||
/** | ||
* Validate the configuration object using jsonschema | ||
* @param {Object} config | ||
* @return {Object} Errors in config if present | ||
*/ | ||
const validateConfig = (config) => { | ||
@@ -42,2 +47,7 @@ return revalidator.validate(config, { | ||
enum: ['parallel', 'sequential'] | ||
}, | ||
userAgent: { | ||
description: 'Customize the User-Agent header', | ||
type: 'string', | ||
allowEmpty: false | ||
} | ||
@@ -48,3 +58,8 @@ } | ||
/** | ||
* Merges the external configuration with the default | ||
* @param {Object} externalConfig | ||
* @param {Object} defaultConfig | ||
* @return {Object} | ||
*/ | ||
const mergeConfig = (externalConfig, defaultConfig) => { | ||
@@ -54,6 +69,13 @@ return { | ||
timeout: externalConfig.timeout || defaultConfig.timeout, | ||
getIP: externalConfig.getIP || defaultConfig.getIP | ||
getIP: externalConfig.getIP || defaultConfig.getIP, | ||
userAgent: externalConfig.userAgent || defaultConfig.userAgent | ||
}; | ||
}; | ||
/** | ||
* Creates a reusable request | ||
* @param {Object} config | ||
* @param {string} url | ||
* @return {Function} cb(error, ip) | ||
*/ | ||
const requestFactory = (config, url) => { | ||
@@ -65,3 +87,3 @@ return (cb) => { | ||
headers: { | ||
'User-Agent': 'curl/' | ||
'User-Agent': config.userAgent | ||
} | ||
@@ -79,2 +101,7 @@ }, (error, res, body = '') => { // if the body is falsey use an empty string | ||
/** | ||
* Used to prety-ish print the errors | ||
* @param {Array} errors | ||
* @return {Error} | ||
*/ | ||
const concatErrors = (errors) => { | ||
@@ -81,0 +108,0 @@ if (errors.length === 1) { |
{ | ||
"name": "external-ip", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A node.js library to get your external ip from multiple services", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -75,17 +75,31 @@ #external-ip [![Build Status](https://travis-ci.org/J-Chaniotis/external-ip.svg?branch=master)](https://travis-ci.org/J-Chaniotis/external-ip) [![Dependency Status](https://david-dm.org/j-Chaniotis/external-ip.svg)](https://david-dm.org/j-Chaniotis/external-ip) | ||
Usage: external-ip [options] | ||
Usage: external-ip [options] | ||
Options: | ||
-h, --help output usage information | ||
-R, --replace replace internal services instead of extending them. | ||
-s, --services <url> service url, see examples, required if using -R | ||
-t, --timeout <msec> set timeout per request | ||
-P, --parallel set to parallel mode | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-R, --replace replace internal services instead of extending them. | ||
-s, --services <url> service url, see examples, required if using -R | ||
-t, --timeout <ms> set timeout per request | ||
-P, --parallel set to parallel mode | ||
-u, --userAgent <User-Agent> provide a User-Agent header, default: curl/ | ||
This program prints the external IP of the machine. | ||
All arguments are optional. | ||
Examples: | ||
$ external-ip | ||
$ external-ip -P -t 1500 -R -s http://icanhazip.com/ -s http://ifconfig.io/ip | ||
This program prints the external IP of the machine. | ||
All arguments are optional. | ||
Examples: | ||
$ external-ip | ||
$ external-ip -P -t 1500 -R -s http://icanhazip.com/ -s http://ident.me/ | ||
Default services: | ||
http://icanhazip.com/ | ||
http://ident.me/ | ||
http://tnx.nl/ip | ||
http://myip.dnsomatic.com/ | ||
http://ipecho.net/plain | ||
http://diagnostic.opendns.com/myip | ||
Documentation can be found at https://github.com/J-Chaniotis/external-ip | ||
``` | ||
@@ -92,0 +106,0 @@ ##Test |
@@ -17,3 +17,3 @@ 'use strict'; | ||
// configure a request for every service | ||
const requests = defaultConfig.services.map((url) => utils.requestFactory({ timeout }, url)); | ||
const requests = defaultConfig.services.map((url) => utils.requestFactory({ timeout, userAgent: defaultConfig.userAgent }, url)); | ||
let completed = 0; | ||
@@ -20,0 +20,0 @@ // hit them and validate the results |
@@ -1,13 +0,6 @@ | ||
'http://ip.appspot.com/', | ||
'http://ifconfig.co/x-real-ip', | ||
'http://ifconfig.io/ip', | ||
'http://whatismyip.akamai.com/', | ||
request user agent at config | ||
more tests | ||
test coverage badge | ||
update docs | ||
npm version badge | ||
user agent at config | ||
better errors errors | ||
update test deps and code, mock require statements | ||
test request factory | ||
test cli | ||
npm badge | ||
https://www.npmjs.com/package/simple-get |
21455
416
111