-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathblacklist.js
112 lines (89 loc) · 2.67 KB
/
blacklist.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
import util from 'util';
import rp from 'request-promise';
import { uniq } from '../misc/array';
import { readFile } from 'fs';
import { isURL, findIPs, findIPsWithRanges } from '../misc/regexes';
import { cidrSubnet } from 'ip';
const readFilePromisify = util.promisify(readFile);
export default class Blacklist {
constructor(items) {
this.data = [];
this.counter = {
all: items.length,
done: 0
};
this.inListsCounter = {};
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.launch(items);
}).catch(error => alert(error));
}
check(ip) {
let inLists = [];
const inMultipleRanges = (ip, arrayOfRanges) => {
return arrayOfRanges.some(range => {
if (range.indexOf('/') != -1) {
return cidrSubnet(range).contains(ip);
}
return ip == range;
});
};
this.data.forEach(list => {
if (inMultipleRanges(ip, list.addresses)) {
inLists.push(list.title);
}
});
const res = inLists.length > 0 ? inLists : false;
if (res) {
this.setInListsCounter(inLists);
}
return res;
}
setInListsCounter(inLists) {
inLists.forEach(item => {
this.inListsCounter[item] = (this.inListsCounter[item] || 0) + 1;
});
}
getInListsCounter() {
const res = [];
Object.keys(this.inListsCounter).forEach(item => {
res.push({
active: true,
title: item,
count: this.inListsCounter[item]
});
});
return res;
}
getIPs(content) {
const ips = findIPs(content);
const ipsWithRanges = findIPsWithRanges(content);
const result = ips && ipsWithRanges ? [...ips, ...ipsWithRanges] : ips ? ips : ipsWithRanges;
return uniq(result);
}
async load(item) {
try {
const response = isURL(item.path) ? await rp.get(item.path) : await readFilePromisify(item.path, 'utf8');
this.onSuccess(item, response);
} catch {
this.isDone();
}
}
launch(items) {
items.forEach(item => this.load(item));
}
isDone() {
this.counter.done++;
if (this.counter.done == this.counter.all) {
this.resolve(this);
}
}
onSuccess(item, content) {
this.data.push({
title: item.title,
addresses: this.getIPs(content)
});
this.isDone();
}
}