-
Notifications
You must be signed in to change notification settings - Fork 9
/
dnsbl.js
369 lines (350 loc) · 10.7 KB
/
dnsbl.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* IP-based blocking
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This file has various tools for IP parsing and IP-based blocking.
*
* These include DNSBLs: DNS-based blackhole lists, which list IPs known for
* running proxies, spamming, or other abuse.
*
* We also maintain our own database of datacenter IP ranges (usually
* proxies). These are taken from https://github.com/client9/ipcat
* but include our own database as well.
*
* @license MIT license
*/
'use strict';
const BLOCKLISTS = ['sbl.spamhaus.org', 'rbl.efnetrbl.org'];
const dns = require('dns');
const FS = require('./lib/fs');
let Dnsbl = module.exports;
/** @type {Map<string, string?>} */
let dnsblCache = Dnsbl.cache = new Map();
dnsblCache.set('127.0.0.1', null);
/**
* @param {string} ip
* @param {function(string?): void} callback
* @param {string} reversedIpDot
* @param {number} index
*/
function queryDnsblLoop(ip, callback, reversedIpDot, index) {
if (index >= BLOCKLISTS.length) {
// not in any blocklist
dnsblCache.set(ip, null);
callback(null);
return;
}
let blocklist = BLOCKLISTS[index];
dns.resolve4(reversedIpDot + blocklist, (err, addresses) => {
if (!err) {
// blocked
dnsblCache.set(ip, blocklist);
callback(blocklist);
return;
}
// not blocked, try next blocklist
queryDnsblLoop(ip, callback, reversedIpDot, index + 1);
});
}
/**
* Dnsbl.query(ip, callback)
*
* Calls callback(blocklist), where blocklist is the blocklist domain
* if the passed IP is in a blocklist, or boolean false if the IP is
* not in any blocklist.
*
* @param {string} ip
* @return {Promise<string?>}
*/
Dnsbl.query = function queryDnsbl(ip) {
if (dnsblCache.has(ip)) {
return Promise.resolve(dnsblCache.get(ip) || null);
}
let reversedIpDot = ip.split('.').reverse().join('.') + '.';
return new Promise((resolve, reject) => {
queryDnsblLoop(ip, resolve, reversedIpDot, 0);
});
};
/*********************************************************
* IP parsing
*********************************************************/
/**
* @param {string} ip
* @return {number}
*/
Dnsbl.ipToNumber = function (ip) {
let num = 0;
let parts = ip.split('.');
for (const part of parts) {
num *= 256;
num += parseInt(part);
}
return num;
};
/**
* @param {string} cidr
* @return {?[number, number]}
*/
Dnsbl.getCidrPattern = function (cidr) {
if (!cidr) return null;
let index = cidr.indexOf('/');
if (index <= 0) {
return [Dnsbl.ipToNumber(cidr), Dnsbl.ipToNumber(cidr)];
}
let low = Dnsbl.ipToNumber(cidr.slice(0, index));
let bits = parseInt(cidr.slice(index + 1));
// fun fact: this fails if bits <= 1 because JavaScript
// does << with signed int32s.
let high = low + (1 << (32 - bits)) - 1;
return [low, high];
};
/**
* @param {string} range
* @return {?[number, number]}
*/
Dnsbl.getRangePattern = function (range) {
if (!range) return null;
let index = range.indexOf(' - ');
if (index <= 0) {
return [Dnsbl.ipToNumber(range), Dnsbl.ipToNumber(range)];
}
let low = Dnsbl.ipToNumber(range.slice(0, index));
let high = Dnsbl.ipToNumber(range.slice(index + 3));
return [low, high];
};
/**
* @param {string} str
* @return {?[number, number]}
*/
Dnsbl.getPattern = function (str) {
if (!str) return null;
if (str.indexOf(' - ') > 0) return Dnsbl.getRangePattern(str);
return Dnsbl.getCidrPattern(str);
};
/**
* @param {string | string[]} cidr
* @return {[number, number][]}
*/
Dnsbl.cidrToPattern = function (cidr) {
if (!cidr || !cidr.length) {
return [];
}
if (typeof cidr === 'string') {
let pattern = Dnsbl.getCidrPattern(cidr);
if (!pattern) return [];
return [pattern];
}
// @ts-ignore
return cidr.map(Dnsbl.getCidrPattern).filter(x => x);
};
/**
* @param {string | string[]} range
* @return {[number, number][]}
*/
Dnsbl.rangeToPattern = function (range) {
if (!range || !range.length) {
return [];
}
if (typeof range === 'string') {
let pattern = Dnsbl.getRangePattern(range);
if (!pattern) return [];
return [pattern];
}
// @ts-ignore
return range.map(Dnsbl.getRangePattern).filter(x => x);
};
/**
* @param {number[][]} patterns
* @param {number} num
* @return {boolean}
*/
Dnsbl.checkPattern = function (patterns, num) {
for (const pattern of patterns) {
if (num >= pattern[0] && num <= pattern[1]) {
return true;
}
}
return false;
};
/**
* Returns a checker function for the passed IP range or array of
* ranges. The checker function returns true if its passed IP is
* in the range.
*
* @param {string | string[]} ranges
* @return {function(string): boolean}
*/
Dnsbl.checker = function (ranges) {
if (!ranges || !ranges.length) return () => false;
/** @type {[number, number][]} */
let patterns = [];
if (typeof ranges === 'string') {
let rangePatterns = Dnsbl.getPattern(ranges);
if (rangePatterns) patterns = [rangePatterns];
} else {
// @ts-ignore
patterns = ranges.map(Dnsbl.getPattern).filter(x => x);
}
return ip => Dnsbl.checkPattern(patterns, Dnsbl.ipToNumber(ip));
};
/*********************************************************
* Datacenter parsing
*********************************************************/
/**
* @param {string} url
* @return {string}
*/
Dnsbl.urlToHost = function (url) {
if (url.startsWith('http://')) url = url.slice(7);
if (url.startsWith('https://')) url = url.slice(8);
if (url.startsWith('www.')) url = url.slice(4);
let slashIndex = url.indexOf('/');
if (slashIndex > 0) url = url.slice(0, slashIndex);
return url;
};
/**@type {[number, number, string][]} */
Dnsbl.datacenters = [];
Dnsbl.loadDatacenters = async function () {
const data = await FS('config/datacenters.csv').readIfExists();
const rows = data.split('\n');
/**@type {[number, number, string][]} */
let datacenters = [];
for (const row of rows) {
if (!row) continue;
const rowSplit = row.split(',');
/**@type {[number, number, string]} */
const rowData = [
Dnsbl.ipToNumber(rowSplit[0]),
Dnsbl.ipToNumber(rowSplit[1]),
Dnsbl.urlToHost(rowSplit[3]),
];
datacenters.push(rowData);
}
Dnsbl.datacenters = datacenters;
};
let rangeTmobile = Dnsbl.cidrToPattern('172.32.0.0/11');
let rangeCenet = Dnsbl.cidrToPattern('27.111.64.0/21');
let rangeQlded = Dnsbl.cidrToPattern('203.104.0.0/20');
let rangeCathednet = Dnsbl.cidrToPattern('180.95.40.0/21');
let rangeTelefonica = Dnsbl.cidrToPattern('181.64.0.0/14');
let rangeStarhub = Dnsbl.cidrToPattern(['27.125.128.0/18', '58.96.192.0/18', '101.127.0.0/17', '116.88.0.0/17', '122.11.192.0/18', '182.19.128.0/17', '182.55.0.0/16', '183.90.0.0/17', '203.116.122.0/23']);
let rangeTelstra = Dnsbl.cidrToPattern('101.160.0.0/11');
let rangeOVHres = Dnsbl.rangeToPattern(['109.190.0.0 - 109.190.63.255', '109.190.64.0 - 109.190.127.255', '109.190.128.0 - 109.190.191.255', '109.190.192.0 - 109.190.255.255', '151.80.228.0 - 151.80.228.255', '178.32.37.0 - 178.32.37.255', '178.33.101.0 - 178.33.101.255', '185.15.68.0 - 185.15.69.255', '185.15.70.0 - 185.15.71.255']);
/**
* Will not reject; IPs with no RDNS entry will resolve to
* '[byte1].[byte2].unknown-nohost'.
*
* @param {string} ip
* @return {Promise<string>}
*/
Dnsbl.reverse = function reverseDns(ip) {
return new Promise((resolve, reject) => {
if (!ip) {
resolve('');
return;
}
let ipNumber = Dnsbl.ipToNumber(ip);
if (Dnsbl.checkPattern(rangeOVHres, ipNumber)) {
resolve('ovh.fr.res-nohost');
return;
}
for (const row of Dnsbl.datacenters) {
if (ipNumber >= row[0] && ipNumber <= row[1]) {
resolve(row[2] + '.proxy-nohost');
return;
}
}
if (ip.startsWith('106.76.') || ip.startsWith('106.77.') || ip.startsWith('106.78.') || ip.startsWith('106.79.') || ip.startsWith('112.110.') || ip.startsWith('27.97.') || ip.startsWith('49.15.') || ip.startsWith('49.14.') || ip.startsWith('1.187.')) {
resolve('ideacellular.mobile-nohost');
return;
}
if (Dnsbl.checkPattern(rangeTmobile, ipNumber) || ip.startsWith('149.254.')) {
resolve('tmobile.mobile-nohost');
return;
}
if (Dnsbl.checkPattern(rangeCenet, ipNumber) || Dnsbl.checkPattern(rangeQlded, ipNumber) || ip.startsWith('153.107.') || Dnsbl.checkPattern(rangeCathednet, ipNumber)) {
resolve('edu.au.res-nohost');
return;
}
if (ip.startsWith('179.7.')) {
resolve('claro.com.pe.mobile-nohost');
return;
}
if (ip.startsWith('190.') || Dnsbl.checkPattern(rangeTelefonica, ipNumber)) {
resolve('telefonica.net.pe.mobile-nohost');
return;
}
if (ip.startsWith('180.191.') || ip.startsWith('112.198.')) {
resolve('globe.com.ph.mobile-nohost');
return;
}
if (ip.startsWith('218.188.') || ip.startsWith('218.189.')) {
resolve('hgc.com.hk.mobile-nohost');
return;
}
if (ip.startsWith('172.242.') || ip.startsWith('172.243.')) {
resolve('viasat.com.mobile-nohost');
return;
}
if (ip.startsWith('201.141.')) {
resolve('cablevision.net.mx.mobile-nohost');
return;
}
if (Dnsbl.checkPattern(rangeStarhub, ipNumber)) {
resolve('starhub.com.mobile-nohost');
return;
}
if (ip.startsWith('202.12.94.') || ip.startsWith('202.12.95.')) {
resolve('nyp.edu.sg.res-nohost');
return;
}
if (ip.startsWith('64.150.')) {
resolve('illinois.net.res-nohost');
return;
}
if (ip.startsWith('147.129.')) {
resolve('ithaca.edu.res-nohost');
return;
}
if (ip.startsWith('189.204.')) {
resolve('bestel.com.mx.res-nohost');
return;
}
if (ip.startsWith('121.54.')) {
resolve('smart.com.ph.mobile-nohost');
return;
}
if (ip.startsWith('179.52.') || ip.startsWith('179.53.')) {
resolve('codetel.net.do.mobile-nohost');
return;
}
if (ip.startsWith('46.16.36.')) {
resolve('anchorfree.proxy-nohost');
return;
}
if (ip.startsWith('198.144.104.') || ip.startsWith('198.47.115.') || ip.startsWith('199.255.215.') || ip.startsWith('204.14.76.') || ip.startsWith('204.14.77.') || ip.startsWith('204.14.78.') || ip.startsWith('204.14.79.') || ip.startsWith('205.164.32.') || ip.startsWith('209.73.132.') || ip.startsWith('209.73.151.') || ip.startsWith('216.172.135.') || ip.startsWith('46.16.34.') || ip.startsWith('46.16.35.') || ip.startsWith('50.117.45.') || ip.startsWith('63.141.198.') || ip.startsWith('63.141.199.') || ip.startsWith('74.115.1.') || ip.startsWith('74.115.5.') || ip.startsWith('85.237.197.') || ip.startsWith('85.237.222.')) {
resolve('anchorfree.proxy-nohost');
return;
}
if (ip === '127.0.0.1') {
resolve('localhost');
return;
}
dns.reverse(ip, (err, hosts) => {
if (err) {
resolve('' + ip.split('.').slice(0, 2).join('.') + '.unknown-nohost');
return;
}
if (!hosts || !hosts[0]) {
if (ip.startsWith('50.')) {
resolve('comcast.net.res-nohost');
} else if (Dnsbl.checkPattern(rangeTelstra, ipNumber)) {
resolve('telstra.net.res-nohost');
} else {
resolve('' + ip.split('.').slice(0, 2).join('.') + '.unknown-nohost');
}
}
resolve(hosts[0]);
});
});
};