-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
343 lines (314 loc) · 8.3 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
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
const { Address6, Address4 } = require('ip-address');
const Timer = require('./utils/timer');
const IP4 = 'v4';
const IP6 = 'v6';
const IP_UNK = 'unk';
const RESULT_FORMAT_DEFAULT = 'default';
const RESULT_FORMAT_STAT = 'stat-result';
/**
* Fix Math.max(min, max) for BigInt range 128bit
* @param args
* @return {*}
*/
const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
/**
* hash big string to number hash
* @param {string|number} str
* @return {number}
*/
const stringHash = (str) => {
if (typeof str === 'number') {
return str;
}
let hash = 0;
for (let i = 0, len = str.length; i < len; i = i + 1) {
const c = str.charCodeAt(i);
hash = (((hash << 5) - hash) + c) | 0;
}
return hash;
};
class IntervalNode {
/**
*
* @param {Interval} interval
* @param {*} value
*/
constructor(interval, value) {
this.interval = interval; // { start: BigInt, end: BigInt }
this.value = value;
this.left = null;
this.right = null;
this.maxEnd = interval.end; // Maximum end of any interval in this subtree
this.intervals = [interval]; // Store all intervals for this node
}
}
class IntervalMultiTree {
/**
* @type {IntervalNode|null}
*/
root = null;
/**
* @param {Interval} a
* @param {Interval} b
* @return {boolean}
* @private
*/
#overlaps(a, b) {
return a.start <= b.end && b.start <= a.end;
}
/**
*
* @param {IntervalNode} node
* @param {Interval} interval
* @param {any} value
* @return {IntervalNode|*}
* @private
*/
#insertNode(node, interval, value) {
if (!node) {
return new IntervalNode(interval, value);
}
// Decide where to insert based on the start of the interval
if (interval.start < node.interval.start) {
node.left = this.#insertNode(node.left, interval, value);
} else {
node.right = this.#insertNode(node.right, interval, value);
}
// Update maxEnd for the node
node.maxEnd = bigIntMax(node.maxEnd, interval.end);
// Add the interval to the node if it overlaps
if (this.#overlaps(node.interval, interval)) {
node.intervals.push(interval);
}
return node;
}
/**
* Insert range to tree
* @param {string|number|bigint} start
* @param {string|number|bigint} end
* @param {any} value
*/
insert(start, end, value) {
this.root = this.#insertNode(this.root, { start: BigInt(start), end: BigInt(end) }, value);
}
/**
* Search range for ip bigint string
* @param {string|number|bigint} ip
* @return {*[]}
*/
search(ip) {
const results = [];
this.#searchNode(this.root, BigInt(ip), results);
return results;
}
/**
* search avl nodes ang aggregate result to results argument
* @param {IntervalNode|null} node
* @param {bigint} ip
* @param {any[]} results
*/
#searchNode(node, ip, results) {
if (!node) return;
if (ip <= node.maxEnd) {
for (const interval of node.intervals) {
if (ip >= interval.start && ip <= interval.end) {
results.push(node.value);
}
}
this.#searchNode(node.left, ip, results);
}
this.#searchNode(node.right, ip, results);
}
}
class IpCollection {
/**
* @type {DataCollection}
*/
dataV4 = {};
/**
* @type {DataCollection}
*/
dataV6 = {};
/**
* @type {ResultFormat}
*/
resultFormat = RESULT_FORMAT_DEFAULT;
/**
* @param {IpCollectionOptions} options
*/
constructor(options = {}) {
if (options.resultFormat) {
this.resultFormat = options.resultFormat;
}
}
/**
* cast ip v6 string to ip bigint string
* @param {string} ip
* @return {string}
*/
castIpV6ToNum(ip) {
return new Address6(ip, void 0).bigInteger().toString();
}
/**
* cast ip v4 string to ip bigint string
* @param {string} ip
* @return {string}
*/
castIpV4ToNum(ip) {
return new Address4(ip).bigInteger().toString();
}
/**
* cast bigint to ip v4 string
* @param {bigint} val
* @return {string}
*/
castBigIntIpToV4Str(val) {
return Address4.fromBigInteger(val).correctForm();
}
/**
* cast bigint to ip v6 string
* @param {bigint} val
* @return {string}
*/
castBigIntIpToV6Str(val) {
return Address6.fromBigInteger(val).correctForm();
}
/**
* @param {string} ipNum
* @param {DataCollection} collection
* @param {boolean} all
* @return {DefaultResult|StatResult}
* @private
*/
#eachLookup(ipNum, collection, all = true) {
const result = [];
const timer = new Timer();
const prefix = ipNum.substring(0, 2);
if (!collection[prefix]) {
return this.#result({ result: [], time: timer.end() });
}
const ip = BigInt(ipNum);
result.push(...(collection[prefix].search(ip) || []));
return this.#result({
result, time: timer.end()
});
}
/**
* @param {*[]} result
* @param {number|string} time
* @return {DefaultResult|StatResult}
*/
#result({ result = [], time = 0 }) {
const uniqueResult = [...new Set(result)];
if (this.resultFormat === RESULT_FORMAT_STAT) {
return {
time, result: uniqueResult
};
}
return uniqueResult;
}
/**
* find ip in range collection
* @param {string} ip
* @param {boolean} all
* @return {DefaultResult|StatResult}
*/
lookup(ip, all = false) {
const format = this.formatIP(ip);
if (format === IP4) {
return this.#eachLookup(this.castIpV4ToNum(ip), this.dataV4, all);
}
if (format === IP6) {
return this.#eachLookup(this.castIpV6ToNum(ip), this.dataV6, all);
}
return this.#result({ result: [] });
}
/**
* get format ip name by ip
* @param {string} ip
* @return {string}
*/
formatIP(ip) {
if (Address4.isValid(ip)) {
return IP4;
}
if (Address6.isValid(ip)) {
return IP6;
}
return IP_UNK;
}
/**
* insert range to data
* @param {string} start - string bigInt ip range start
* @param {string} end - string bigInt ip range end
* @param {IpType} ipType - ip type
* @param {string|number} value
*/
insertRange(start, end, ipType, value) {
const startPrefix = start.toString().substring(0, 2);
const endPrefix = end.toString().substring(0, 2);
const tree = ipType === IP6 ? this.dataV6 : this.dataV4;
tree[startPrefix] = tree[startPrefix] || new IntervalMultiTree();
tree[startPrefix].insert(start, end, value);
if (startPrefix !== endPrefix) {
tree[endPrefix] = tree[endPrefix] || new IntervalMultiTree();
tree[endPrefix].insert(start, end, value);
}
}
/**
* insert range by Address object to data
* @param {Address6|Address4} startAddr
* @param {Address6|Address4} endAddr
* @param {'v4'|'v6'} ipType
* @param {string|number} value
*/
insertRangeAddress(startAddr, endAddr, ipType, value) {
this.insertRange(startAddr.bigInteger().toString(), endAddr.bigInteger().toString(), ipType, value);
}
/**
* load ips to database
* format line:
* 1) ip-ip
* 2) ip/mask
* 3) string bigint-string bigint
* @param listString
* @param {string|number} value
*/
loadFromString(listString, value = 0) {
let list = listString.split('\n');
for (let i in list) {
let range = list[i];
if (!range) {
continue;
}
let ipType = '';
// is CIDR range
if (/\/\d+$/.test(range)) {
ipType = range.split('.').length === 4 ? IP4 : IP6;
let addrCIDR = ipType === IP6 ? new Address6(range) : new Address4(range);
this.insertRangeAddress(addrCIDR.startAddress(), addrCIDR.endAddress(), ipType, value);
continue;
}
// is range delimiter '-'
let [startRange, endRange] = range.split('-');
ipType = startRange.split('.').length === 4 ? IP4 : IP6;
// is range bignumber string
if (/^\d+$/.test(startRange)) {
ipType = startRange.length <= 14 ? IP4 : IP6;
this.insertRange(startRange, endRange, ipType, value);
} else {
let startAddr = ipType === IP6 ? new Address6(startRange) : new Address4(startRange);
let endAddr = ipType === IP6 ? new Address6(endRange) : new Address4(endRange);
this.insertRangeAddress(startAddr, endAddr, ipType, value);
}
}
}
/**
* clear all data
*/
clear() {
this.dataV4 = {};
this.dataV6 = {};
}
}
module.exports = IpCollection;