@@ -39,6 +39,7 @@ const {
39
39
ERR_BUFFER_OUT_OF_BOUNDS ,
40
40
ERR_INVALID_ARG_TYPE ,
41
41
ERR_INVALID_FD_TYPE ,
42
+ ERR_IP_BLOCKED ,
42
43
ERR_MISSING_ARGS ,
43
44
ERR_SOCKET_ALREADY_BOUND ,
44
45
ERR_SOCKET_BAD_BUFFER_SIZE ,
@@ -53,6 +54,7 @@ const {
53
54
_createSocketHandle,
54
55
newHandle,
55
56
} = require ( 'internal/dgram' ) ;
57
+ const { isIP } = require ( 'internal/net' ) ;
56
58
const {
57
59
isInt32,
58
60
validateAbortSignal,
@@ -97,12 +99,18 @@ let _cluster = null;
97
99
function lazyLoadCluster ( ) {
98
100
return _cluster ??= require ( 'cluster' ) ;
99
101
}
102
+ let _blockList = null ;
103
+ function lazyLoadBlockList ( ) {
104
+ return _blockList ??= require ( 'internal/blocklist' ) . BlockList ;
105
+ }
100
106
101
107
function Socket ( type , listener ) {
102
108
FunctionPrototypeCall ( EventEmitter , this ) ;
103
109
let lookup ;
104
110
let recvBufferSize ;
105
111
let sendBufferSize ;
112
+ let receiveBlockList ;
113
+ let sendBlockList ;
106
114
107
115
let options ;
108
116
if ( type !== null && typeof type === 'object' ) {
@@ -117,6 +125,18 @@ function Socket(type, listener) {
117
125
}
118
126
recvBufferSize = options . recvBufferSize ;
119
127
sendBufferSize = options . sendBufferSize ;
128
+ if ( options . receiveBlockList ) {
129
+ if ( ! lazyLoadBlockList ( ) . isBlockList ( options . receiveBlockList ) ) {
130
+ throw new ERR_INVALID_ARG_TYPE ( 'options.receiveBlockList' , 'net.BlockList' , options . receiveBlockList ) ;
131
+ }
132
+ receiveBlockList = options . receiveBlockList ;
133
+ }
134
+ if ( options . sendBlockList ) {
135
+ if ( ! lazyLoadBlockList ( ) . isBlockList ( options . sendBlockList ) ) {
136
+ throw new ERR_INVALID_ARG_TYPE ( 'options.sendBlockList' , 'net.BlockList' , options . sendBlockList ) ;
137
+ }
138
+ sendBlockList = options . sendBlockList ;
139
+ }
120
140
}
121
141
122
142
const handle = newHandle ( type , lookup ) ;
@@ -139,6 +159,8 @@ function Socket(type, listener) {
139
159
ipv6Only : options ?. ipv6Only ,
140
160
recvBufferSize,
141
161
sendBufferSize,
162
+ receiveBlockList,
163
+ sendBlockList,
142
164
} ;
143
165
144
166
if ( options ?. signal !== undefined ) {
@@ -437,7 +459,9 @@ function doConnect(ex, self, ip, address, port, callback) {
437
459
const state = self [ kStateSymbol ] ;
438
460
if ( ! state . handle )
439
461
return ;
440
-
462
+ if ( ! ex && state . sendBlockList ?. check ( ip , `ipv${ isIP ( ip ) } ` ) ) {
463
+ ex = new ERR_IP_BLOCKED ( ip ) ;
464
+ }
441
465
if ( ! ex ) {
442
466
const err = state . handle . connect ( ip , port ) ;
443
467
if ( err ) {
@@ -701,6 +725,13 @@ function doSend(ex, self, ip, list, address, port, callback) {
701
725
return ;
702
726
}
703
727
728
+ if ( ip && state . sendBlockList ?. check ( ip , `ipv${ isIP ( ip ) } ` ) ) {
729
+ if ( callback ) {
730
+ process . nextTick ( callback , new ERR_IP_BLOCKED ( ip ) ) ;
731
+ }
732
+ return ;
733
+ }
734
+
704
735
const req = new SendWrap ( ) ;
705
736
req . list = list ; // Keep reference alive.
706
737
req . address = address ;
@@ -949,6 +980,10 @@ function onMessage(nread, handle, buf, rinfo) {
949
980
if ( nread < 0 ) {
950
981
return self . emit ( 'error' , new ErrnoException ( nread , 'recvmsg' ) ) ;
951
982
}
983
+ if ( self [ kStateSymbol ] ?. receiveBlockList ?. check ( rinfo . address ,
984
+ rinfo . family ?. toLocaleLowerCase ( ) ) ) {
985
+ return ;
986
+ }
952
987
rinfo . size = buf . length ; // compatibility
953
988
self . emit ( 'message' , buf , rinfo ) ;
954
989
}
0 commit comments