@@ -105,6 +105,7 @@ const {
105105 ERR_INVALID_FD_TYPE ,
106106 ERR_INVALID_HANDLE_TYPE ,
107107 ERR_INVALID_IP_ADDRESS ,
108+ ERR_IP_BLOCKED ,
108109 ERR_MISSING_ARGS ,
109110 ERR_SERVER_ALREADY_LISTEN ,
110111 ERR_SERVER_NOT_RUNNING ,
@@ -204,6 +205,10 @@ function isPipeName(s) {
204205 return typeof s === 'string' && toNumber ( s ) === false ;
205206}
206207
208+ function isBlockList ( obj ) {
209+ return obj instanceof module . exports . BlockList ;
210+ }
211+
207212/**
208213 * Creates a new TCP or IPC server
209214 * @param {{
@@ -510,6 +515,12 @@ function Socket(options) {
510515 // Used after `.destroy()`
511516 this [ kBytesRead ] = 0 ;
512517 this [ kBytesWritten ] = 0 ;
518+ if ( options . blocklist ) {
519+ if ( ! isBlockList ( options . blocklist ) ) {
520+ throw new ERR_INVALID_ARG_TYPE ( 'options.blocklist' , 'net.BlockList' , options . blocklist ) ;
521+ }
522+ this . blocklist = options . blocklist ;
523+ }
513524}
514525ObjectSetPrototypeOf ( Socket . prototype , stream . Duplex . prototype ) ;
515526ObjectSetPrototypeOf ( Socket , stream . Duplex ) ;
@@ -1073,6 +1084,10 @@ function internalConnect(
10731084 self . emit ( 'connectionAttempt' , address , port , addressType ) ;
10741085
10751086 if ( addressType === 6 || addressType === 4 ) {
1087+ if ( self . blocklist ?. check ( address , `ipv${ addressType } ` ) ) {
1088+ self . destroy ( new ERR_IP_BLOCKED ( address ) ) ;
1089+ return ;
1090+ }
10761091 const req = new TCPConnectWrap ( ) ;
10771092 req . oncomplete = afterConnect ;
10781093 req . address = address ;
@@ -1162,6 +1177,14 @@ function internalConnectMultiple(context, canceled) {
11621177 }
11631178 }
11641179
1180+ if ( self . blocklist ?. check ( address , `ipv${ addressType } ` ) ) {
1181+ const ex = new ERR_IP_BLOCKED ( address ) ;
1182+ ArrayPrototypePush ( context . errors , ex ) ;
1183+ self . emit ( 'connectionAttemptFailed' , address , port , addressType , ex ) ;
1184+ internalConnectMultiple ( context ) ;
1185+ return ;
1186+ }
1187+
11651188 debug ( 'connect/multiple: attempting to connect to %s:%d (addressType: %d)' , address , port , addressType ) ;
11661189 self . emit ( 'connectionAttempt' , address , port , addressType ) ;
11671190
0 commit comments