net: support blocklist for net.Server · nodejs/node@b47888d · GitHub
Skip to content

Commit b47888d

Browse files
theanarkhruyadorno
authored andcommitted
net: support blocklist for net.Server
PR-URL: #56079 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 974b7b6 commit b47888d

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

doc/api/net.md

Lines changed: 5 additions & 0 deletions

lib/net.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,13 @@ function Server(options, connectionListener) {
17911791
this.keepAlive = Boolean(options.keepAlive);
17921792
this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);
17931793
this.highWaterMark = options.highWaterMark ?? getDefaultHighWaterMark();
1794+
if (options.blockList) {
1795+
// TODO: use BlockList.isBlockList (https://github.com/nodejs/node/pull/56078)
1796+
if (!(options.blockList instanceof module.exports.BlockList)) {
1797+
throw new ERR_INVALID_ARG_TYPE('options.blockList', 'net.BlockList', options.blockList);
1798+
}
1799+
this.blockList = options.blockList;
1800+
}
17941801
}
17951802
ObjectSetPrototypeOf(Server.prototype, EventEmitter.prototype);
17961803
ObjectSetPrototypeOf(Server, EventEmitter);
@@ -2239,7 +2246,15 @@ function onconnection(err, clientHandle) {
22392246
clientHandle.close();
22402247
return;
22412248
}
2242-
2249+
if (self.blockList && typeof clientHandle.getpeername === 'function') {
2250+
const remoteInfo = { __proto__: null };
2251+
clientHandle.getpeername(remoteInfo);
2252+
const addressType = isIP(remoteInfo.address);
2253+
if (addressType && self.blockList.check(remoteInfo.address, `ipv${addressType}`)) {
2254+
clientHandle.close();
2255+
return;
2256+
}
2257+
}
22432258
const socket = new Socket({
22442259
handle: clientHandle,
22452260
allowHalfOpen: self.allowHalfOpen,
Lines changed: 19 additions & 0 deletions

0 commit comments

Comments
 (0)