On this page

C

net.BlockList

History

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.

M

blockList.addAddress

History
blockList.addAddress(address, type?): void
Attributes
An IPv4 or IPv6 address.
type?:string
Either 'ipv4' or 'ipv6'. Default: 'ipv4'.

Adds a rule to block the given IP address.

M

blockList.addAddresses

History
blockList.addAddresses(addresses, type?): void
Attributes
addresses:string[] | net.SocketAddress[]
An array of IPv4 or IPv6 addresses.
type?:string
Either '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.

M

blockList.addCIDR

History
blockList.addCIDR(cidr): void
Attributes
cidr:string
An IPv4 or IPv6 subnet in CIDR notation (e.g. '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.

M

blockList.addCIDRs

History
blockList.addCIDRs(cidrs): void
Attributes
cidrs:string[]
An array of IPv4 or IPv6 subnets in CIDR notation.

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.

M

blockList.addRange

History
blockList.addRange(start, end, type?): void
Attributes
The starting IPv4 or IPv6 address in the range.
The ending IPv4 or IPv6 address in the range.
type?:string
Either 'ipv4' or 'ipv6'. Default: 'ipv4'.

Adds a rule to block a range of IP addresses from start (inclusive) to end (inclusive).

M

blockList.addSubnet

History
blockList.addSubnet(net, prefix, type?): void
Attributes
The network IPv4 or IPv6 address.
prefix:number
The number of CIDR prefix bits. For IPv4, this must be a value between 0 and 32. For IPv6, this must be between 0 and 128.
type?:string
Either 'ipv4' or 'ipv6'. Default: 'ipv4'.

Adds a rule to block a range of IP addresses specified as a subnet mask.

M

blockList.check

History
blockList.check(address, type?): boolean
Attributes
The IP address to check
type?:string
Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
Returns:boolean

Returns 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
Stability: 1.2Release candidate
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));
  • value Blocklist.rules
M

BlockList.isBlockList

History
BlockList.isBlockList(value): void
Attributes
value:any
Any JS value
Returns:
true if the value is a net.BlockList.
P

BlockList.PRIVATE_RANGES

History
Type: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 IPv4
  • 172.16.0.0/12 — RFC 1918 private IPv4
  • 192.168.0.0/16 — RFC 1918 private IPv4
  • 127.0.0.0/8 — IPv4 loopback
  • ::1/128 — IPv6 loopback
  • 169.254.0.0/16 — IPv4 link-local
  • fe80::/10 — IPv6 link-local
  • fc00::/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
M

blockList.removeAddress

History
blockList.removeAddress(address, type?): void
Attributes
An IPv4 or IPv6 address.
type?:string
Either '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.

M

blockList.removeCIDR

History
blockList.removeCIDR(cidr): void
Attributes
cidr:string
An IPv4 or IPv6 subnet in CIDR notation (e.g. '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.

M

blockList.removeRange

History
blockList.removeRange(start, end, type?): void
Attributes
The starting IPv4 or IPv6 address in the range.
The ending IPv4 or IPv6 address in the range.
type?:string
Either '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.

M

blockList.removeSubnet

History
blockList.removeSubnet(net, prefix, type?): void
Attributes
The network IPv4 or IPv6 address.
prefix:number
The number of CIDR prefix bits. For IPv4, this must be a value between 0 and 32. For IPv6, this must be between 0 and 128.
type?:string
Either '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.

P

blockList.rules

History
Type:string[]

The list of rules added to the blocklist.

P

blockList.size

History
Type:number

The number of rules in the blocklist. This is equivalent to blockList.rules.length but does not allocate the rules array.

blockList.toJSON(): void
Stability: 1.2Release candidate
Returns:
Blocklist.rules