@@ -105,6 +105,7 @@ const {
105
105
ERR_INVALID_FD_TYPE ,
106
106
ERR_INVALID_HANDLE_TYPE ,
107
107
ERR_INVALID_IP_ADDRESS ,
108
+ ERR_IP_BLOCKED ,
108
109
ERR_MISSING_ARGS ,
109
110
ERR_SERVER_ALREADY_LISTEN ,
110
111
ERR_SERVER_NOT_RUNNING ,
@@ -510,6 +511,12 @@ function Socket(options) {
510
511
// Used after `.destroy()`
511
512
this [ kBytesRead ] = 0 ;
512
513
this [ kBytesWritten ] = 0 ;
514
+ if ( options . blockList ) {
515
+ if ( ! module . exports . BlockList . isBlockList ( options . blockList ) ) {
516
+ throw new ERR_INVALID_ARG_TYPE ( 'options.blockList' , 'net.BlockList' , options . blockList ) ;
517
+ }
518
+ this . blockList = options . blockList ;
519
+ }
513
520
}
514
521
ObjectSetPrototypeOf ( Socket . prototype , stream . Duplex . prototype ) ;
515
522
ObjectSetPrototypeOf ( Socket , stream . Duplex ) ;
@@ -1073,6 +1080,10 @@ function internalConnect(
1073
1080
self . emit ( 'connectionAttempt' , address , port , addressType ) ;
1074
1081
1075
1082
if ( addressType === 6 || addressType === 4 ) {
1083
+ if ( self . blockList ?. check ( address , `ipv${ addressType } ` ) ) {
1084
+ self . destroy ( new ERR_IP_BLOCKED ( address ) ) ;
1085
+ return ;
1086
+ }
1076
1087
const req = new TCPConnectWrap ( ) ;
1077
1088
req . oncomplete = afterConnect ;
1078
1089
req . address = address ;
@@ -1162,6 +1173,14 @@ function internalConnectMultiple(context, canceled) {
1162
1173
}
1163
1174
}
1164
1175
1176
+ if ( self . blockList ?. check ( address , `ipv${ addressType } ` ) ) {
1177
+ const ex = new ERR_IP_BLOCKED ( address ) ;
1178
+ ArrayPrototypePush ( context . errors , ex ) ;
1179
+ self . emit ( 'connectionAttemptFailed' , address , port , addressType , ex ) ;
1180
+ internalConnectMultiple ( context ) ;
1181
+ return ;
1182
+ }
1183
+
1165
1184
debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
1166
1185
self . emit ( 'connectionAttempt' , address , port , addressType ) ;
1167
1186
@@ -1792,8 +1811,7 @@ function Server(options, connectionListener) {
1792
1811
this . keepAliveInitialDelay = ~ ~ ( options . keepAliveInitialDelay / 1000 ) ;
1793
1812
this . highWaterMark = options . highWaterMark ?? getDefaultHighWaterMark ( ) ;
1794
1813
if ( options . blockList ) {
1795
- // TODO: use BlockList.isBlockList (https://github.com/nodejs/node/pull/56078)
1796
- if ( ! ( options . blockList instanceof module . exports . BlockList ) ) {
1814
+ if ( ! module . exports . BlockList . isBlockList ( options . blockList ) ) {
1797
1815
throw new ERR_INVALID_ARG_TYPE ( 'options.blockList' , 'net.BlockList' , options . blockList ) ;
1798
1816
}
1799
1817
this . blockList = options . blockList ;
0 commit comments