-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (195 loc) · 4.63 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
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
const {Address6, Address4} = require('ip-address');
const IPV4_SPLIT = 2;
const IPV6_SPLIT = 2;
const IP_V4 = 'v4';
const IP_V6 = 'v6';
const IP_UNK = 'unk';
module.exports = class IpCollection {
dataV4 = {};
dataV6 = {};
/**
* Convert ipv6 to big number
* @param ip
* @return {string}
*/
castIpV6ToNum(ip) {
return BigInt(new Address6(ip, 8).bigInteger()).toString();
}
/**
* Convert ipv4 to big number
* @param ip
* @return {string}
*/
castIpV4ToNum(ip) {
return new Address4(ip).bigInteger().toString();
}
/**
* Find partition
* @param keyPath
* @param collection
* @private
* @return {*|null}
*/
#findPartition(keyPath, collection) {
if (typeof keyPath == 'string') keyPath = keyPath.split('.');
let current = collection[keyPath[0]];
if (current === void 0) {
return null;
}
let lastKeyIndex = keyPath.length - 1;
for (let i = 1; i <= lastKeyIndex; ++i) {
let key = keyPath[i];
if (current[key] === void 0) {
return null;
}
current = current[key];
}
return current;
}
/**
* @param {*} ipNum
* @param {Object[]} collection
* @param {boolean} all
* @return {[]}
* @private
*/
eachLookup(ipNum, collection, all) {
let ip = BigInt(ipNum);
let result = [];
for (let value in collection)
for (let i = 0, l = collection[value].length; i < l; i++) {
let range = collection[value][i];
let rangeStart = BigInt(range[0]);
let rangeEnd = BigInt(range[1]);
let check = ip >= rangeStart && ip <= rangeEnd;
if (check) {
result.indexOf(value) === -1 && result.push(value);
if (!all) {
return result;
}
break;
}
}
return result;
}
/**
* Find ip in range
* @param {string} rawIp
* @param {boolean} all
* @return {[]|*[]}
*/
lookup(rawIp, all = false) {
let format = this.formatIP(rawIp);
let result = [];
if (format === IP_UNK) {
return [];
}
if (format === IP_V4) {
let ip = this.castIpV4ToNum(rawIp);
let path = ip.split('', IPV4_SPLIT);
let collection = this.#findPartition(path, this.dataV4);
if (collection === null) {
return [];
}
result = this.eachLookup(ip, collection, all);
}
if (format === IP_V6) {
let ip = this.castIpV6ToNum(rawIp);
let path = ip.split('', IPV6_SPLIT);
let collection = this.#findPartition(path, this.dataV6);
if (collection === null) {
return [];
}
result = this.eachLookup(ip, collection, all);
}
return result;
}
/**
* insert range to object
* @param {Object} obj
* @param {string} keyPath
* @param {*} value
*/
insert(obj, keyPath, value) {
if (typeof keyPath == 'string'){
keyPath = keyPath.split('.');
}
const lastKeyIndex = keyPath.length - 1;
for (let i = 0; i <= lastKeyIndex; ++i) {
const key = keyPath[i];
if (obj[key] === void 0) {
obj[key] = i === lastKeyIndex ? [] : {};
}
obj = obj[key];
}
obj.push(value);
}
/**
* get format ip name by ip
* @param ip
* @returns {string}
*/
formatIP(ip) {
if (Address4.isValid(ip)) {
return IP_V4;
}
if (Address6.isValid(ip)) {
return IP_V6;
}
return IP_UNK;
}
/**
* parse ip and insert to a boring collection
* @param {string} startIp
* @param {string} endIp
* @param {string|number} value
*/
parse(startIp, endIp, value = 0) {
let format = this.formatIP(startIp);
if (format === IP_UNK) {
return;
}
if (format === IP_V4) {
let left = this.castIpV4ToNum(startIp);
let right = this.castIpV4ToNum(endIp);
let path = left.split('', IPV4_SPLIT);
path.push(value);
this.insert(this.dataV4, path, [left, right]);
return;
}
if (format === IP_V6) {
let left = this.castIpV6ToNum(startIp);
let right = this.castIpV6ToNum(endIp);
let path = left.split('', IPV6_SPLIT);
path.push(value);
this.insert(this.dataV6, path, [left, right]);
}
}
/**
* load ips to database
* @format
* ```
* 1.1.0.1-1.1.0.3
* 1.1.1.1-1.1.1.4
* ```
* @param {string} list
* @param {string|number} value
*/
loadFromString(list, value = 0) {
const data = list.split('\n');
for (let row of data) {
if (!row) {
continue;
}
const ipPart = row.split('-');
ipPart.length === 2 && this.parse(ipPart[0], ipPart[1], value);
}
}
/**
* clear collections
*/
clear() {
this.dataV4 = {};
this.dataV6 = {};
}
}