-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
80 lines (68 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {Resolver} from "node:dns/promises";
import ipPtr from "ip-ptr";
import pMap from "p-map";
// using dns.opendns.com because popuplar resolvers could be rate-limited by the
// blacklist providers, spamhaus being the prime example.
const defaults = {
timeout: 5000,
servers: [
"208.67.220.220",
"208.67.222.222",
"2620:119:35::35",
"2620:119:53::53",
],
concurrency: 64,
includeTxt: false,
};
async function query(addr, blacklist, resolver, opts = {}) {
const name = `${ipPtr(addr).replace(/\.i.+/, "")}.${blacklist}`;
const timeout = setTimeout(() => {
resolver.cancel();
return opts.includeTxt ? {listed: false, txt: []} : false;
}, opts.timeout);
try {
const [addrs, txt] = await Promise.all([
resolver.resolve4(name),
opts.includeTxt && resolver.resolveTxt(name),
]);
clearTimeout(timeout);
const listed = Boolean(addrs.length);
return opts.includeTxt ? {listed, txt} : listed;
} catch {
return opts.includeTxt ? {listed: false, txt: []} : false;
}
}
export async function lookup(addr, blacklist, opts = {}) {
opts = {...defaults, ...opts};
const resolver = new Resolver();
if (opts.servers) {
resolver.setServers(Array.isArray(opts.servers) ? opts.servers : [opts.servers]);
}
return query(addr, blacklist, resolver, opts);
}
export async function batch(addrs, lists, opts = {}) {
opts = {...defaults, ...opts};
const resolver = new Resolver();
if (opts.servers) {
resolver.setServers(Array.isArray(opts.servers) ? opts.servers : [opts.servers]);
}
const items = [];
for (const address of (Array.isArray(addrs) ? addrs : [addrs])) {
for (const blacklist of (Array.isArray(lists) ? lists : [lists])) {
items.push({blacklist, address, resolver});
}
}
const results = await pMap(items, item => {
return query(item.address, item.blacklist, item.resolver, opts);
}, {concurrency: opts.concurrency});
return items.map((item, i) => {
if (opts.includeTxt) {
item.listed = results[i].listed;
item.txt = results[i].txt;
} else {
item.listed = results[i];
}
delete item.resolver;
return item;
});
}