The BlockList object can be used with some network APIs to specify rules for
disabling inbound or outbound access to specific IP addresses, IP ranges, or
IP subnets.
blockList.addAddress(address, type?): void
string | net.SocketAddressstring'ipv4' or 'ipv6'. Default: 'ipv4'.Adds a rule to block the given IP address.
blockList.addAddresses(addresses, type?): void
string[] | net.SocketAddress[]string'ipv4' or 'ipv6'. Default: 'ipv4'.Adds multiple address rules to the block list in a single operation.
This is more efficient than calling blockList.addAddress() repeatedly
when adding a large number of individual addresses, as the addresses
are inserted under a single internal lock acquisition.
blockList.addCIDR(cidr): void
string'10.0.0.0/8' or '2001:db8::/32').Adds a subnet rule using CIDR notation. The address family is automatically
detected from the address (IPv6 if the address contains ':', IPv4
otherwise). This is equivalent to calling blockList.addSubnet() with
the parsed network address, prefix length, and family.
blockList.addCIDRs(cidrs): void
string[]Adds multiple subnet rules using CIDR notation in a single call. The address
family for each entry is automatically detected. This is equivalent to
calling blockList.addCIDR() for each element of the array.
blockList.addRange(start, end, type?): void
string | net.SocketAddressstring | net.SocketAddressstring'ipv4' or 'ipv6'. Default: 'ipv4'.Adds a rule to block a range of IP addresses from start (inclusive) to
end (inclusive).
blockList.addSubnet(net, prefix, type?): void
string | net.SocketAddressnumber0 and 32. For IPv6, this must be between
0 and 128.string'ipv4' or 'ipv6'. Default: 'ipv4'.Adds a rule to block a range of IP addresses specified as a subnet mask.
blockList.check(address, type?): boolean
string | net.SocketAddressstring'ipv4' or 'ipv6'. Default: 'ipv4'.booleanReturns true if the given IP address matches any of the rules added to the
BlockList.
const blockList = new net.BlockList(); blockList.addAddress('123.123.123.123'); blockList.addRange('10.0.0.1', '10.0.0.10'); blockList.addSubnet('8592:757c:efae:4e45::', 64, 'ipv6'); console.log(blockList.check('123.123.123.123')); // Prints: true console.log(blockList.check('10.0.0.3')); // Prints: true console.log(blockList.check('222.111.111.222')); // Prints: false // IPv6 notation for IPv4 addresses works: console.log(blockList.check('::ffff:7b7b:7b7b', 'ipv6')); // Prints: true console.log(blockList.check('::ffff:123.123.123.123', 'ipv6')); // Prints: true
blockList.clear(): void
Clears all rules from the BlockList.
blockList.fromJSON(value): void
const blockList = new net.BlockList(); const data = [ 'Subnet: IPv4 192.168.1.0/24', 'Address: IPv4 10.0.0.5', 'Range: IPv4 192.168.2.1-192.168.2.10', 'Range: IPv4 10.0.0.1-10.0.0.10', ]; blockList.fromJSON(data); blockList.fromJSON(JSON.stringify(data));
valueBlocklist.rules
BlockList.isBlockList(value): void
anytrue if the value is a net.BlockList.string[]A frozen array of CIDR strings representing private, loopback, and link-local
IP address ranges. This can be passed to blockList.addCIDRs() to quickly
populate a blocklist with all non-routable address ranges.
The included ranges are:
10.0.0.0/8— RFC 1918 private IPv4172.16.0.0/12— RFC 1918 private IPv4192.168.0.0/16— RFC 1918 private IPv4127.0.0.0/8— IPv4 loopback::1/128— IPv6 loopback169.254.0.0/16— IPv4 link-localfe80::/10— IPv6 link-localfc00::/7— IPv6 unique local (ULA)
const blockList = new net.BlockList(); blockList.addCIDRs(net.BlockList.PRIVATE_RANGES); console.log(blockList.check('10.0.0.1')); // Prints: true console.log(blockList.check('127.0.0.1')); // Prints: true console.log(blockList.check('8.8.8.8')); // Prints: false
blockList.removeAddress(address, type?): void
string | net.SocketAddressstring'ipv4' or 'ipv6'. Default: 'ipv4'.Removes a rule that was previously added with blockList.addAddress(). The
address must match exactly the value used when the rule was added. If the
specified address does not exist, this is a no-op.
blockList.removeCIDR(cidr): void
string'10.0.0.0/8' or '2001:db8::/32').Removes a subnet rule using CIDR notation. The address family is automatically
detected from the address. This is equivalent to calling
blockList.removeSubnet() with the parsed network address, prefix length,
and family. If the specified subnet does not exist, this is a no-op.
blockList.removeRange(start, end, type?): void
string | net.SocketAddressstring | net.SocketAddressstring'ipv4' or 'ipv6'. Default: 'ipv4'.Removes a rule that was previously added with blockList.addRange(). The start
and end addresses must match exactly the values used when the rule was added.
If the specified range does not exist, this is a no-op.
blockList.removeSubnet(net, prefix, type?): void
string | net.SocketAddressnumber0 and 32. For IPv6, this must be between
0 and 128.string'ipv4' or 'ipv6'. Default: 'ipv4'.Removes a rule that was previously added with blockList.addSubnet(). The
network address and prefix must match exactly the values used when the rule was
added. If the specified subnet does not exist, this is a no-op.
string[]The list of rules added to the blocklist.
numberThe number of rules in the blocklist. This is equivalent to
blockList.rules.length but does not allocate the rules array.
blockList.toJSON(): void