@@ -41,6 +41,7 @@ const {
4141 ERR_BUFFER_OUT_OF_BOUNDS ,
4242 ERR_INVALID_ARG_TYPE ,
4343 ERR_INVALID_FD_TYPE ,
44+ ERR_IP_BLOCKED ,
4445 ERR_MISSING_ARGS ,
4546 ERR_SOCKET_ALREADY_BOUND ,
4647 ERR_SOCKET_BAD_BUFFER_SIZE ,
@@ -55,6 +56,7 @@ const {
5556 _createSocketHandle,
5657 newHandle,
5758} = require ( 'internal/dgram' ) ;
59+ const { isIP } = require ( 'internal/net' ) ;
5860const {
5961 isInt32,
6062 validateAbortSignal,
@@ -99,12 +101,18 @@ let _cluster = null;
99101function lazyLoadCluster ( ) {
100102 return _cluster ??= require ( 'cluster' ) ;
101103}
104+ let _blockList = null ;
105+ function lazyLoadBlockList ( ) {
106+ return _blockList ??= require ( 'internal/blocklist' ) . BlockList ;
107+ }
102108
103109function Socket ( type , listener ) {
104110 FunctionPrototypeCall ( EventEmitter , this ) ;
105111 let lookup ;
106112 let recvBufferSize ;
107113 let sendBufferSize ;
114+ let receiveBlockList ;
115+ let sendBlockList ;
108116
109117 let options ;
110118 if ( type !== null && typeof type === 'object' ) {
@@ -119,6 +127,18 @@ function Socket(type, listener) {
119127 }
120128 recvBufferSize = options . recvBufferSize ;
121129 sendBufferSize = options . sendBufferSize ;
130+ if ( options . receiveBlockList ) {
131+ if ( ! lazyLoadBlockList ( ) . isBlockList ( options . receiveBlockList ) ) {
132+ throw new ERR_INVALID_ARG_TYPE ( 'options.receiveBlockList' , 'net.BlockList' , options . receiveBlockList ) ;
133+ }
134+ receiveBlockList = options . receiveBlockList ;
135+ }
136+ if ( options . sendBlockList ) {
137+ if ( ! lazyLoadBlockList ( ) . isBlockList ( options . sendBlockList ) ) {
138+ throw new ERR_INVALID_ARG_TYPE ( 'options.sendBlockList' , 'net.BlockList' , options . sendBlockList ) ;
139+ }
140+ sendBlockList = options . sendBlockList ;
141+ }
122142 }
123143
124144 const handle = newHandle ( type , lookup ) ;
@@ -141,6 +161,8 @@ function Socket(type, listener) {
141161 ipv6Only : options ?. ipv6Only ,
142162 recvBufferSize,
143163 sendBufferSize,
164+ receiveBlockList,
165+ sendBlockList,
144166 } ;
145167
146168 if ( options ?. signal !== undefined ) {
@@ -439,7 +461,9 @@ function doConnect(ex, self, ip, address, port, callback) {
439461 const state = self [ kStateSymbol ] ;
440462 if ( ! state . handle )
441463 return ;
442-
464+ if ( ! ex && state . sendBlockList ?. check ( ip , `ipv${ isIP ( ip ) } ` ) ) {
465+ ex = new ERR_IP_BLOCKED ( ip ) ;
466+ }
443467 if ( ! ex ) {
444468 const err = state . handle . connect ( ip , port ) ;
445469 if ( err ) {
@@ -703,6 +727,13 @@ function doSend(ex, self, ip, list, address, port, callback) {
703727 return ;
704728 }
705729
730+ if ( ip && state . sendBlockList ?. check ( ip , `ipv${ isIP ( ip ) } ` ) ) {
731+ if ( callback ) {
732+ process . nextTick ( callback , new ERR_IP_BLOCKED ( ip ) ) ;
733+ }
734+ return ;
735+ }
736+
706737 const req = new SendWrap ( ) ;
707738 req . list = list ; // Keep reference alive.
708739 req . address = address ;
@@ -951,6 +982,10 @@ function onMessage(nread, handle, buf, rinfo) {
951982 if ( nread < 0 ) {
952983 return self . emit ( 'error' , new ErrnoException ( nread , 'recvmsg' ) ) ;
953984 }
985+ if ( self [ kStateSymbol ] ?. receiveBlockList ?. check ( rinfo . address ,
986+ rinfo . family ?. toLocaleLowerCase ( ) ) ) {
987+ return ;
988+ }
954989 rinfo . size = buf . length ; // compatibility
955990 self . emit ( 'message' , buf , rinfo ) ;
956991}
0 commit comments