Skip to content

Commit 0720545

Browse files
committed
net: support blocklist for net.Server
1 parent 4cf6fab commit 0720545

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/api/net.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,8 @@ changes:
17131713
**Default:** `false`.
17141714
* `pauseOnConnect` {boolean} Indicates whether the socket should be
17151715
paused on incoming connections. **Default:** `false`.
1716+
* `blocklist` {net.BlockList} `blocklist` can be used for disabling inbound
1717+
access to specific IP addresses, IP ranges, or IP subnets.
17161718

17171719
* `connectionListener` {Function} Automatically set as a listener for the
17181720
[`'connection'`][] event.

lib/net.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ function isPipeName(s) {
204204
return typeof s === 'string' && toNumber(s) === false;
205205
}
206206

207+
function isBlockList(obj) {
208+
return obj instanceof module.exports.BlockList;
209+
}
207210
/**
208211
* Creates a new TCP or IPC server
209212
* @param {{
@@ -1791,6 +1794,12 @@ function Server(options, connectionListener) {
17911794
this.keepAlive = Boolean(options.keepAlive);
17921795
this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);
17931796
this.highWaterMark = options.highWaterMark ?? getDefaultHighWaterMark();
1797+
if (options.blocklist) {
1798+
if (!isBlockList(options.blocklist)) {
1799+
throw new ERR_INVALID_ARG_TYPE('options.blockList', 'net.BlockList', options.blockList);
1800+
}
1801+
this.blocklist = options.blocklist;
1802+
}
17941803
}
17951804
ObjectSetPrototypeOf(Server.prototype, EventEmitter.prototype);
17961805
ObjectSetPrototypeOf(Server, EventEmitter);
@@ -2239,7 +2248,15 @@ function onconnection(err, clientHandle) {
22392248
clientHandle.close();
22402249
return;
22412250
}
2242-
2251+
if (self.blocklist && typeof clientHandle.getpeername === 'function') {
2252+
const remoteInfo = { __proto__: null };
2253+
clientHandle.getpeername(remoteInfo);
2254+
const addressType = isIP(remoteInfo.address);
2255+
if (addressType && self.blocklist.check(remoteInfo.address, `ipv${addressType}`)) {
2256+
clientHandle.close();
2257+
return;
2258+
}
2259+
}
22432260
const socket = new Socket({
22442261
handle: clientHandle,
22452262
allowHalfOpen: self.allowHalfOpen,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const common = require('../common');
3+
const net = require('net');
4+
5+
const blocklist = new net.BlockList();
6+
blocklist.addAddress('127.0.0.1');
7+
8+
const server = net.createServer({ blocklist }, common.mustNotCall());
9+
server.listen(0, common.mustCall(() => {
10+
const adddress = server.address();
11+
const socket = net.connect({
12+
localAddress: '127.0.0.1',
13+
host: adddress.host,
14+
port: adddress.port
15+
});
16+
socket.on('close', common.mustCall(() => {
17+
server.close();
18+
}));
19+
}));

0 commit comments

Comments
 (0)