is-reachable
Advanced tools
Comparing version 1.2.0 to 1.3.0
63
index.js
'use strict'; | ||
var dns = require('dns'); | ||
var arrify = require('arrify'); | ||
var eachAsync = require('each-async'); | ||
var isPortReachable = require('is-port-reachable'); | ||
var onetime = require('onetime'); | ||
var arrify = require('arrify'); | ||
var isPublicDomain = require('is-public-domain'); | ||
var isPortReachable = require('is-port-reachable'); | ||
var ip = require('ip'); | ||
var routerIps = require('router-ips'); | ||
var url = require('url'); | ||
var urlParseLax = require('url-parse-lax'); | ||
module.exports = function (hosts, cb) { | ||
module.exports = function (dests, cb) { | ||
cb = onetime(cb); | ||
eachAsync(arrify(hosts), function (host, i, done) { | ||
host = urlParseLax(host); | ||
eachAsync(arrify(dests), function (dest, i, done) { | ||
dest = urlParseLax(dest); | ||
dns.lookup(host.hostname, function (_, address) { | ||
var host = dest.hostname; | ||
var port = dest.port || 80; | ||
dns.lookup(host, function (_, address) { | ||
// Ignore `err` as we only care about `address`. | ||
@@ -25,7 +28,5 @@ // Skip connecting if there is nothing to connect to. | ||
// When a public domain returns a private IP address we declare the host | ||
// as unreachable. This will fail intentionally when a intranet resource | ||
// uses a public top level domain with a private IP address, which itself | ||
// is a violation of RFC 1918 (https://www.ietf.org/rfc/rfc1918.txt). | ||
if (isPublicDomain(host.hostname) && ip.isPrivate(address)) { | ||
// When the returned address is a well-known router ip, we might | ||
// have been redirected to a router's captive portal. | ||
if (routerIps.indexOf(address) !== -1) { | ||
done(); | ||
@@ -35,12 +36,32 @@ return; | ||
isPortReachable(host.port || 80, {host: address}, function (_, reachable) { | ||
if (reachable) { | ||
cb(null, true); | ||
if (port === 80 || port === 443) { | ||
// Try to detect HTTP redirection by checking if the `Location` | ||
// header contains a well-known router ip. | ||
// https://github.com/sindresorhus/is-reachable/issues/3#issuecomment-138735338 | ||
require(port === 80 ? 'http' : 'https').get({host: host}, function (res) { | ||
var redirectHost = url.parse(res.headers.location || '').host; | ||
if (routerIps.indexOf(redirectHost) === -1) { | ||
cb(null, true); | ||
// skip to end | ||
done(new Error()); | ||
} else { | ||
// skip to end | ||
done(new Error()); | ||
} else { | ||
done(); | ||
} | ||
}).on('error', function () { | ||
done(); | ||
} | ||
}); | ||
}); | ||
} else { | ||
// Just test if the port answers to a SYN | ||
isPortReachable(port, {host: address}, function (_, reachable) { | ||
if (reachable) { | ||
cb(null, true); | ||
// skip to end | ||
done(new Error()); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
} | ||
}); | ||
@@ -47,0 +68,0 @@ }, function () { |
{ | ||
"name": "is-reachable", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Check if servers are reachable", | ||
@@ -45,8 +45,7 @@ "license": "MIT", | ||
"dependencies": { | ||
"arrify": "^1.0.0", | ||
"each-async": "^1.1.0", | ||
"ip": "^1.0.2", | ||
"arrify": "^1.0.1", | ||
"each-async": "^1.1.1", | ||
"is-port-reachable": "^1.0.0", | ||
"is-public-domain": "^1.0.0", | ||
"onetime": "^1.0.0", | ||
"onetime": "^1.1.0", | ||
"router-ips": "^0.2.0", | ||
"url-parse-lax": "^1.0.0" | ||
@@ -53,0 +52,0 @@ }, |
@@ -18,5 +18,5 @@ # is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable) | ||
```js | ||
var isReachable = require('is-reachable'); | ||
const isReachable = require('is-reachable'); | ||
isReachable('sindresorhus.com', function (err, reachable) { | ||
isReachable('sindresorhus.com', (err, reachable) => { | ||
console.log(reachable); | ||
@@ -26,3 +26,3 @@ //=> true | ||
isReachable('google.com:80', function (err, reachable) { | ||
isReachable('google.com:80', (err, reachable) => { | ||
console.log(reachable); | ||
@@ -29,0 +29,0 @@ //=> true |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5764
6
83
2
2
+ Addedrouter-ips@^0.2.0
+ Addedrouter-ips@0.2.0(transitive)
- Removedip@^1.0.2
- Removedis-public-domain@^1.0.0
- Removedip@1.1.9(transitive)
- Removedis-public-domain@1.0.0(transitive)
- Removedtlds@1.255.0(transitive)
Updatedarrify@^1.0.1
Updatedeach-async@^1.1.1
Updatedonetime@^1.1.0