Skip to content

Commit

Permalink
fix case of unknown service port lookup - fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Nov 24, 2018
1 parent d45c4da commit f2aa0b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -6,7 +6,7 @@ const got = require('got');
const isPortReachable = require('is-port-reachable');
const pAny = require('p-any');
const pify = require('pify');
const pn = require('port-numbers');
const portNumbers = require('port-numbers');
const pTimeout = require('p-timeout');
const prependHttp = require('prepend-http');
const routerIps = require('router-ips');
Expand All @@ -21,10 +21,11 @@ const checkRedirection = target => {

function isTargetReachable(target) {
const url = new URL(prependHttp(target));
url.port = Number(url.port) || pn.getPort(url.protocol.slice(0, -1)).port || 80;
url.port = Number(url.port) || portNumbers.getPort(url.protocol.slice(0, -1)).port || 80;

if (!/^[a-z]+:\/\//.test(target)) {
url.protocol = pn.getService(url.port).name + ':';
const service = portNumbers.getService(url.port);
url.protocol = ((service && service.name) ? service.name : 'unknown') + ':';
}

return getAddress(url.hostname).then(address => {
Expand Down
6 changes: 5 additions & 1 deletion test.js
Expand Up @@ -32,7 +32,11 @@ test('ftp url', async t => {
});

test('unreachable hostname', async t => {
t.false(await m('343645335341233123125235623452344123.com'));
t.false(await m('343645335341233123125235623452344123.local'));
});

test('unknown service', async t => {
t.false(await m('343645335341233123125235623452344123.local:-1'));
});

test('unreachable pathname', async t => {
Expand Down

0 comments on commit f2aa0b0

Please sign in to comment.