Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5993eda

Browse files
jasnellfoxxyz
authored andcommittedOct 18, 2021
net: allow net.BlockList to use net.SocketAddress objects
Signed-off-by: James M Snell <[email protected]> PR-URL: nodejs#37917 Reviewed-By: Matteo Collina <[email protected]>
1 parent 6a0cad2 commit 5993eda

File tree

8 files changed

+238
-162
lines changed

8 files changed

+238
-162
lines changed
 

‎doc/api/net.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ IP subnets.
6969
added: REPLACEME
7070
-->
7171

72-
* `address` {string} An IPv4 or IPv6 address.
73-
* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`.
72+
* `address` {string|net.SocketAddress} An IPv4 or IPv6 address.
73+
* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
7474

7575
Adds a rule to block the given IP address.
7676

@@ -79,9 +79,10 @@ Adds a rule to block the given IP address.
7979
added: REPLACEME
8080
-->
8181

82-
* `start` {string} The starting IPv4 or IPv6 address in the range.
83-
* `end` {string} The ending IPv4 or IPv6 address in the range.
84-
* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`.
82+
* `start` {string|net.SocketAddress} The starting IPv4 or IPv6 address in the
83+
range.
84+
* `end` {string|net.SocketAddress} The ending IPv4 or IPv6 address in the range.
85+
* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
8586

8687
Adds a rule to block a range of IP addresses from `start` (inclusive) to
8788
`end` (inclusive).
@@ -91,7 +92,7 @@ Adds a rule to block a range of IP addresses from `start` (inclusive) to
9192
added: REPLACEME
9293
-->
9394

94-
* `net` {string} The network IPv4 or IPv6 address.
95+
* `net` {string|net.SocketAddress} The network IPv4 or IPv6 address.
9596
* `prefix` {number} The number of CIDR prefix bits. For IPv4, this
9697
must be a value between `0` and `32`. For IPv6, this must be between
9798
`0` and `128`.
@@ -104,8 +105,8 @@ Adds a rule to block a range of IP addresses specified as a subnet mask.
104105
added: REPLACEME
105106
-->
106107

107-
* `address` {string} The IP address to check
108-
* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`.
108+
* `address` {string|net.SocketAddress} The IP address to check
109+
* `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`.
109110
* Returns: {boolean}
110111

111112
Returns `true` if the given IP address matches any of the rules added to the

‎lib/internal/blocklist.js

+57-34
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ const {
88

99
const {
1010
BlockList: BlockListHandle,
11-
AF_INET,
12-
AF_INET6,
1311
} = internalBinding('block_list');
1412

1513
const {
1614
customInspectSymbol: kInspect,
1715
} = require('internal/util');
1816

17+
const {
18+
SocketAddress,
19+
kHandle: kSocketAddressHandle,
20+
} = require('internal/socketaddress');
21+
1922
const {
2023
JSTransferable,
2124
kClone,
@@ -55,56 +58,76 @@ class BlockList extends JSTransferable {
5558
}
5659

5760
addAddress(address, family = 'ipv4') {
58-
validateString(address, 'address');
59-
validateString(family, 'family');
60-
family = family.toLowerCase();
61-
if (family !== 'ipv4' && family !== 'ipv6')
62-
throw new ERR_INVALID_ARG_VALUE('family', family);
63-
const type = family === 'ipv4' ? AF_INET : AF_INET6;
64-
this[kHandle].addAddress(address, type);
61+
if (!SocketAddress.isSocketAddress(address)) {
62+
validateString(address, 'address');
63+
validateString(family, 'family');
64+
address = new SocketAddress({
65+
address,
66+
family,
67+
});
68+
}
69+
this[kHandle].addAddress(address[kSocketAddressHandle]);
6570
}
6671

6772
addRange(start, end, family = 'ipv4') {
68-
validateString(start, 'start');
69-
validateString(end, 'end');
70-
validateString(family, 'family');
71-
family = family.toLowerCase();
72-
if (family !== 'ipv4' && family !== 'ipv6')
73-
throw new ERR_INVALID_ARG_VALUE('family', family);
74-
const type = family === 'ipv4' ? AF_INET : AF_INET6;
75-
const ret = this[kHandle].addRange(start, end, type);
73+
if (!SocketAddress.isSocketAddress(start)) {
74+
validateString(start, 'start');
75+
validateString(family, 'family');
76+
start = new SocketAddress({
77+
address: start,
78+
family,
79+
});
80+
}
81+
if (!SocketAddress.isSocketAddress(end)) {
82+
validateString(end, 'end');
83+
validateString(family, 'family');
84+
end = new SocketAddress({
85+
address: end,
86+
family,
87+
});
88+
}
89+
const ret = this[kHandle].addRange(
90+
start[kSocketAddressHandle],
91+
end[kSocketAddressHandle]);
7692
if (ret === false)
7793
throw new ERR_INVALID_ARG_VALUE('start', start, 'must come before end');
7894
}
7995

8096
addSubnet(network, prefix, family = 'ipv4') {
81-
validateString(network, 'network');
82-
validateString(family, 'family');
83-
family = family.toLowerCase();
84-
let type;
85-
switch (family) {
97+
if (!SocketAddress.isSocketAddress(network)) {
98+
validateString(network, 'network');
99+
validateString(family, 'family');
100+
network = new SocketAddress({
101+
address: network,
102+
family,
103+
});
104+
}
105+
switch (network.family) {
86106
case 'ipv4':
87-
type = AF_INET;
88107
validateInt32(prefix, 'prefix', 0, 32);
89108
break;
90109
case 'ipv6':
91-
type = AF_INET6;
92110
validateInt32(prefix, 'prefix', 0, 128);
93111
break;
94-
default:
95-
throw new ERR_INVALID_ARG_VALUE('family', family);
96112
}
97-
this[kHandle].addSubnet(network, type, prefix);
113+
this[kHandle].addSubnet(network[kSocketAddressHandle], prefix);
98114
}
99115

100116
check(address, family = 'ipv4') {
101-
validateString(address, 'address');
102-
validateString(family, 'family');
103-
family = family.toLowerCase();
104-
if (family !== 'ipv4' && family !== 'ipv6')
105-
throw new ERR_INVALID_ARG_VALUE('family', family);
106-
const type = family === 'ipv4' ? AF_INET : AF_INET6;
107-
return Boolean(this[kHandle].check(address, type));
117+
if (!SocketAddress.isSocketAddress(address)) {
118+
validateString(address, 'address');
119+
validateString(family, 'family');
120+
try {
121+
address = new SocketAddress({
122+
address,
123+
family,
124+
});
125+
} catch {
126+
// Ignore the error. If it's not a valid address, return false.
127+
return false;
128+
}
129+
}
130+
return Boolean(this[kHandle].check(address[kSocketAddressHandle]));
108131
}
109132

110133
get rules() {

‎lib/internal/socketaddress.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ class SocketAddress extends JSTransferable {
4747
constructor(options = {}) {
4848
super();
4949
validateObject(options, 'options');
50+
let { family = 'ipv4' } = options;
5051
const {
51-
family = 'ipv4',
5252
address = (family === 'ipv4' ? '127.0.0.1' : '::'),
5353
port = 0,
5454
flowlabel = 0,
5555
} = options;
5656

5757
let type;
58+
if (typeof family?.toLowerCase === 'function')
59+
family = family.toLowerCase();
5860
switch (family) {
5961
case 'ipv4':
6062
type = AF_INET;
@@ -63,7 +65,7 @@ class SocketAddress extends JSTransferable {
6365
type = AF_INET6;
6466
break;
6567
default:
66-
throw new ERR_INVALID_ARG_VALUE('options.family', family);
68+
throw new ERR_INVALID_ARG_VALUE('options.family', options.family);
6769
}
6870

6971
validateString(address, 'options.address');
@@ -150,4 +152,5 @@ ObjectSetPrototypeOf(InternalSocketAddress.prototype, SocketAddress.prototype);
150152
module.exports = {
151153
SocketAddress,
152154
InternalSocketAddress,
155+
kHandle,
153156
};

0 commit comments

Comments
 (0)
Please sign in to comment.