Skip to content

Commit

Permalink
fix: fixed hostfile parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Mar 10, 2023
1 parent ccdbeae commit 516c53f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
53 changes: 27 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const pWaitFor = require('p-wait-for');
const packet = require('dns-packet');
const semver = require('semver');
const structuredClone = require('@ungap/structured-clone').default;
const { Hosts } = require('hosts-parser');
const { getService } = require('port-numbers');

const pkg = require('./package.json');
Expand All @@ -41,19 +40,24 @@ import('private-ip').then((obj) => {
isPrivateIP = obj.default;
});

const HOSTFILE = hostile
.get(true)
.map((s) => (Array.isArray(s) ? s.join(' ') : s))
.join('\n');

const HOSTS = [];
const hosts = hostile.get();
for (const line of hosts) {
const [ip, str] = line;
const hosts = str.split(' ');
HOSTS.push({ ip, hosts });
}

// <https://github.com/szmarczak/cacheable-lookup/pull/76>
class Tangerine extends dns.promises.Resolver {
static HOSTFILE = hostile
.get(true)
.map((s) => (Array.isArray(s) ? s.join(' ') : s))
.join('\n');

static HOSTS = new Hosts(
hostile
.get()
.map((arr) => arr.join(' '))
.join('\n')
);
static HOSTFILE = HOSTFILE;

static HOSTS = HOSTS;

static isValidPort(port) {
return Number.isSafeInteger(port) && port >= 0 && port <= 65535;
Expand Down Expand Up @@ -686,8 +690,8 @@ class Tangerine extends dns.promises.Resolver {

const lower = name.toLowerCase();

for (const rule of this.constructor.HOSTS._origin) {
if (rule.hostname.toLowerCase() !== lower && rule.ip !== name) continue;
for (const rule of this.constructor.HOSTS) {
if (rule.hosts.every((h) => h.toLowerCase() !== lower)) continue;
const type = isIP(rule.ip);
if (!resolve4 && type === 4) {
if (!Array.isArray(resolve4)) resolve4 = [rule.ip];
Expand Down Expand Up @@ -930,25 +934,22 @@ class Tangerine extends dns.promises.Resolver {
// edge case where localhost IP returns matches
if (!isPrivateIP) await pWaitFor(() => Boolean(isPrivateIP));

const answers = [];
const matches = [];
const answers = new Set();
let match = false;

for (const rule of this.constructor.HOSTS._origin) {
for (const rule of this.constructor.HOSTS) {
if (rule.ip === ip) {
matches.push(rule.ip);
if (
matches.filter((m) => m === ip).length > 1 &&
!answers.includes(rule.hostname)
)
answers.push(rule.hostname);
match = true;
for (const host of rule.hosts.slice(1)) {
answers.add(host);
}
}
}

// if (answers.length > 0 || (matches.includes(ip) && isPrivateIP(ip)))
if (answers.length > 0 || matches.includes(ip)) return answers;
if (answers.size > 0 || match) return [...answers];

// NOTE: we can prob remove this (?)
if (ip === '::1' || ip === '127.0.0.1') return [];
// if (ip === '::1' || ip === '127.0.0.1') return [];

// reverse the IP address
if (!dohdec) await pWaitFor(() => Boolean(dohdec));
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"dohdec": "^5.0.3",
"get-stream": "6",
"hostile": "^1.3.3",
"hosts-parser": "^0.3.2",
"ipaddr.js": "^2.0.1",
"merge-options": "3.0.4",
"p-map": "4",
Expand Down
4 changes: 1 addition & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const { Resolver } = dns.promises;
//
test.before(async (t) => {
// log the hosts (useful for debugging)
t.log('hostfile');
t.log(Tangerine.HOSTFILE);
t.log('parsed hosts');
t.log(Tangerine.HOSTS);

// attempt to setServers and perform a DNS lookup
const tangerine = new Tangerine();
Expand Down Expand Up @@ -243,6 +240,7 @@ for (const host of [
'.,',
'localhost..',
'beep..',
'beep.com',
'beep.com..',
'beep..com..',
'foo..com',
Expand Down

0 comments on commit 516c53f

Please sign in to comment.