| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,8 +69,8 @@ IP subnets. | |||
| 69 | 69 | added: REPLACEME | |
| 70 | 70 | --> | |
| 71 | 71 | ||
| 72 | - * `address` {string} An IPv4 or IPv6 address. | ||
| 73 | - * `type` {string} Either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`. | ||
| 72 | + * `address` {string|net.SocketAddress} An IPv4 or IPv6 address. | ||
| 73 | + * `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`. | ||
| 74 | 74 | ||
| 75 | 75 | Adds a rule to block the given IP address. | |
| 76 | 76 | ||
@@ -79,9 +79,10 @@ Adds a rule to block the given IP address. | |||
| 79 | 79 | added: REPLACEME | |
| 80 | 80 | --> | |
| 81 | 81 | ||
| 82 | - * `start` {string} The starting IPv4 or IPv6 address in the range. | ||
| 83 | - * `end` {string} The ending IPv4 or IPv6 address in the range. | ||
| 84 | - * `type` {string} Either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`. | ||
| 82 | + * `start` {string|net.SocketAddress} The starting IPv4 or IPv6 address in the | ||
| 83 | + range. | ||
| 84 | + * `end` {string|net.SocketAddress} The ending IPv4 or IPv6 address in the range. | ||
| 85 | + * `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`. | ||
| 85 | 86 | ||
| 86 | 87 | Adds a rule to block a range of IP addresses from `start` (inclusive) to | |
| 87 | 88 | `end` (inclusive). | |
@@ -91,7 +92,7 @@ Adds a rule to block a range of IP addresses from `start` (inclusive) to | |||
| 91 | 92 | added: REPLACEME | |
| 92 | 93 | --> | |
| 93 | 94 | ||
| 94 | - * `net` {string} The network IPv4 or IPv6 address. | ||
| 95 | + * `net` {string|net.SocketAddress} The network IPv4 or IPv6 address. | ||
| 95 | 96 | * `prefix` {number} The number of CIDR prefix bits. For IPv4, this | |
| 96 | 97 | must be a value between `0` and `32`. For IPv6, this must be between | |
| 97 | 98 | `0` and `128`. | |
@@ -104,8 +105,8 @@ Adds a rule to block a range of IP addresses specified as a subnet mask. | |||
| 104 | 105 | added: REPLACEME | |
| 105 | 106 | --> | |
| 106 | 107 | ||
| 107 | - * `address` {string} The IP address to check | ||
| 108 | - * `type` {string} Either `'ipv4'` or `'ipv6'`. **Default**: `'ipv4'`. | ||
| 108 | + * `address` {string|net.SocketAddress} The IP address to check | ||
| 109 | + * `type` {string} Either `'ipv4'` or `'ipv6'`. **Default:** `'ipv4'`. | ||
| 109 | 110 | * Returns: {boolean} | |
| 110 | 111 | ||
| 111 | 112 | Returns `true` if the given IP address matches any of the rules added to the | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,14 +8,17 @@ const { | |||
| 8 | 8 | ||
| 9 | 9 | const { | |
| 10 | 10 | BlockList: BlockListHandle, | |
| 11 | - AF_INET, | ||
| 12 | - AF_INET6, | ||
| 13 | 11 | } = internalBinding('block_list'); | |
| 14 | 12 | ||
| 15 | 13 | const { | |
| 16 | 14 | customInspectSymbol: kInspect, | |
| 17 | 15 | } = require('internal/util'); | |
| 18 | 16 | ||
| 17 | + const { | ||
| 18 | + SocketAddress, | ||
| 19 | + kHandle: kSocketAddressHandle, | ||
| 20 | + } = require('internal/socketaddress'); | ||
| 21 | + | ||
| 19 | 22 | const { | |
| 20 | 23 | JSTransferable, | |
| 21 | 24 | kClone, | |
@@ -55,56 +58,76 @@ class BlockList extends JSTransferable { | |||
| 55 | 58 | } | |
| 56 | 59 | ||
| 57 | 60 | addAddress(address, family = 'ipv4') { | |
| 58 | - validateString(address, 'address'); | ||
| 59 | - validateString(family, 'family'); | ||
| 60 | - family = family.toLowerCase(); | ||
| 61 | - if (family !== 'ipv4' && family !== 'ipv6') | ||
| 62 | - throw new ERR_INVALID_ARG_VALUE('family', family); | ||
| 63 | - const type = family === 'ipv4' ? AF_INET : AF_INET6; | ||
| 64 | - this[kHandle].addAddress(address, type); | ||
| 61 | + if (!SocketAddress.isSocketAddress(address)) { | ||
| 62 | + validateString(address, 'address'); | ||
| 63 | + validateString(family, 'family'); | ||
| 64 | + address = new SocketAddress({ | ||
| 65 | + address, | ||
| 66 | + family, | ||
| 67 | + }); | ||
| 68 | + } | ||
| 69 | + this[kHandle].addAddress(address[kSocketAddressHandle]); | ||
| 65 | 70 | } | |
| 66 | 71 | ||
| 67 | 72 | addRange(start, end, family = 'ipv4') { | |
| 68 | - validateString(start, 'start'); | ||
| 69 | - validateString(end, 'end'); | ||
| 70 | - validateString(family, 'family'); | ||
| 71 | - family = family.toLowerCase(); | ||
| 72 | - if (family !== 'ipv4' && family !== 'ipv6') | ||
| 73 | - throw new ERR_INVALID_ARG_VALUE('family', family); | ||
| 74 | - const type = family === 'ipv4' ? AF_INET : AF_INET6; | ||
| 75 | - const ret = this[kHandle].addRange(start, end, type); | ||
| 73 | + if (!SocketAddress.isSocketAddress(start)) { | ||
| 74 | + validateString(start, 'start'); | ||
| 75 | + validateString(family, 'family'); | ||
| 76 | + start = new SocketAddress({ | ||
| 77 | + address: start, | ||
| 78 | + family, | ||
| 79 | + }); | ||
| 80 | + } | ||
| 81 | + if (!SocketAddress.isSocketAddress(end)) { | ||
| 82 | + validateString(end, 'end'); | ||
| 83 | + validateString(family, 'family'); | ||
| 84 | + end = new SocketAddress({ | ||
| 85 | + address: end, | ||
| 86 | + family, | ||
| 87 | + }); | ||
| 88 | + } | ||
| 89 | + const ret = this[kHandle].addRange( | ||
| 90 | + start[kSocketAddressHandle], | ||
| 91 | + end[kSocketAddressHandle]); | ||
| 76 | 92 | if (ret === false) | |
| 77 | 93 | throw new ERR_INVALID_ARG_VALUE('start', start, 'must come before end'); | |
| 78 | 94 | } | |
| 79 | 95 | ||
| 80 | 96 | addSubnet(network, prefix, family = 'ipv4') { | |
| 81 | - validateString(network, 'network'); | ||
| 82 | - validateString(family, 'family'); | ||
| 83 | - family = family.toLowerCase(); | ||
| 84 | - let type; | ||
| 85 | - switch (family) { | ||
| 97 | + if (!SocketAddress.isSocketAddress(network)) { | ||
| 98 | + validateString(network, 'network'); | ||
| 99 | + validateString(family, 'family'); | ||
| 100 | + network = new SocketAddress({ | ||
| 101 | + address: network, | ||
| 102 | + family, | ||
| 103 | + }); | ||
| 104 | + } | ||
| 105 | + switch (network.family) { | ||
| 86 | 106 | case 'ipv4': | |
| 87 | - type = AF_INET; | ||
| 88 | 107 | validateInt32(prefix, 'prefix', 0, 32); | |
| 89 | 108 | break; | |
| 90 | 109 | case 'ipv6': | |
| 91 | - type = AF_INET6; | ||
| 92 | 110 | validateInt32(prefix, 'prefix', 0, 128); | |
| 93 | 111 | break; | |
| 94 | - default: | ||
| 95 | - throw new ERR_INVALID_ARG_VALUE('family', family); | ||
| 96 | 112 | } | |
| 97 | - this[kHandle].addSubnet(network, type, prefix); | ||
| 113 | + this[kHandle].addSubnet(network[kSocketAddressHandle], prefix); | ||
| 98 | 114 | } | |
| 99 | 115 | ||
| 100 | 116 | check(address, family = 'ipv4') { | |
| 101 | - validateString(address, 'address'); | ||
| 102 | - validateString(family, 'family'); | ||
| 103 | - family = family.toLowerCase(); | ||
| 104 | - if (family !== 'ipv4' && family !== 'ipv6') | ||
| 105 | - throw new ERR_INVALID_ARG_VALUE('family', family); | ||
| 106 | - const type = family === 'ipv4' ? AF_INET : AF_INET6; | ||
| 107 | - return Boolean(this[kHandle].check(address, type)); | ||
| 117 | + if (!SocketAddress.isSocketAddress(address)) { | ||
| 118 | + validateString(address, 'address'); | ||
| 119 | + validateString(family, 'family'); | ||
| 120 | + try { | ||
| 121 | + address = new SocketAddress({ | ||
| 122 | + address, | ||
| 123 | + family, | ||
| 124 | + }); | ||
| 125 | + } catch { | ||
| 126 | + // Ignore the error. If it's not a valid address, return false. | ||
| 127 | + return false; | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | + return Boolean(this[kHandle].check(address[kSocketAddressHandle])); | ||
| 108 | 131 | } | |
| 109 | 132 | ||
| 110 | 133 | get rules() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,14 +47,16 @@ class SocketAddress extends JSTransferable { | |||
| 47 | 47 | constructor(options = {}) { | |
| 48 | 48 | super(); | |
| 49 | 49 | validateObject(options, 'options'); | |
| 50 | + let { family = 'ipv4' } = options; | ||
| 50 | 51 | const { | |
| 51 | - family = 'ipv4', | ||
| 52 | 52 | address = (family === 'ipv4' ? '127.0.0.1' : '::'), | |
| 53 | 53 | port = 0, | |
| 54 | 54 | flowlabel = 0, | |
| 55 | 55 | } = options; | |
| 56 | 56 | ||
| 57 | 57 | let type; | |
| 58 | + if (typeof family?.toLowerCase === 'function') | ||
| 59 | + family = family.toLowerCase(); | ||
| 58 | 60 | switch (family) { | |
| 59 | 61 | case 'ipv4': | |
| 60 | 62 | type = AF_INET; | |
@@ -63,7 +65,7 @@ class SocketAddress extends JSTransferable { | |||
| 63 | 65 | type = AF_INET6; | |
| 64 | 66 | break; | |
| 65 | 67 | default: | |
| 66 | - throw new ERR_INVALID_ARG_VALUE('options.family', family); | ||
| 68 | + throw new ERR_INVALID_ARG_VALUE('options.family', options.family); | ||
| 67 | 69 | } | |
| 68 | 70 | ||
| 69 | 71 | validateString(address, 'options.address'); | |
@@ -150,4 +152,5 @@ ObjectSetPrototypeOf(InternalSocketAddress.prototype, SocketAddress.prototype); | |||
| 150 | 152 | module.exports = { | |
| 151 | 153 | SocketAddress, | |
| 152 | 154 | InternalSocketAddress, | |
| 155 | + kHandle, | ||
| 153 | 156 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments