/
blocklist.js
120 lines (105 loc) Β· 3.63 KB
/
blocklist.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
'use strict';
const {
Boolean,
Symbol
} = primordials;
const {
BlockList: BlockListHandle,
AF_INET,
AF_INET6,
} = internalBinding('block_list');
const {
customInspectSymbol: kInspect,
} = require('internal/util');
const { inspect } = require('internal/util/inspect');
const kHandle = Symbol('kHandle');
const { owner_symbol } = internalBinding('symbols');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
} = require('internal/errors').codes;
class BlockList {
constructor(handle = new BlockListHandle()) {
// The handle argument is an intentionally undocumented
// internal API. User code will not be able to create
// a BlockListHandle object directly.
if (!(handle instanceof BlockListHandle))
throw new ERR_INVALID_ARG_TYPE('handle', 'BlockListHandle', handle);
this[kHandle] = handle;
this[kHandle][owner_symbol] = this;
}
[kInspect](depth, options) {
if (depth < 0)
return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};
return `BlockList ${inspect({
rules: this.rules
}, opts)}`;
}
addAddress(address, family = 'ipv4') {
if (typeof address !== 'string')
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
if (typeof family !== 'string')
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
if (family !== 'ipv4' && family !== 'ipv6')
throw new ERR_INVALID_ARG_VALUE('family', family);
const type = family === 'ipv4' ? AF_INET : AF_INET6;
this[kHandle].addAddress(address, type);
}
addRange(start, end, family = 'ipv4') {
if (typeof start !== 'string')
throw new ERR_INVALID_ARG_TYPE('start', 'string', start);
if (typeof end !== 'string')
throw new ERR_INVALID_ARG_TYPE('end', 'string', end);
if (typeof family !== 'string')
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
if (family !== 'ipv4' && family !== 'ipv6')
throw new ERR_INVALID_ARG_VALUE('family', family);
const type = family === 'ipv4' ? AF_INET : AF_INET6;
const ret = this[kHandle].addRange(start, end, type);
if (ret === false)
throw new ERR_INVALID_ARG_VALUE('start', start, 'must come before end');
}
addSubnet(network, prefix, family = 'ipv4') {
if (typeof network !== 'string')
throw new ERR_INVALID_ARG_TYPE('network', 'string', network);
if (typeof prefix !== 'number')
throw new ERR_INVALID_ARG_TYPE('prefix', 'number', prefix);
if (typeof family !== 'string')
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
let type;
switch (family) {
case 'ipv4':
type = AF_INET;
if (prefix < 0 || prefix > 32)
throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 32', prefix);
break;
case 'ipv6':
type = AF_INET6;
if (prefix < 0 || prefix > 128)
throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 128', prefix);
break;
default:
throw new ERR_INVALID_ARG_VALUE('family', family);
}
this[kHandle].addSubnet(network, type, prefix);
}
check(address, family = 'ipv4') {
if (typeof address !== 'string')
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
if (typeof family !== 'string')
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
if (family !== 'ipv4' && family !== 'ipv6')
throw new ERR_INVALID_ARG_VALUE('family', family);
const type = family === 'ipv4' ? AF_INET : AF_INET6;
return Boolean(this[kHandle].check(address, type));
}
get rules() {
return this[kHandle].getRules();
}
}
module.exports = BlockList;