|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + Boolean, |
| 5 | + Symbol |
| 6 | +} = primordials; |
| 7 | + |
| 8 | +const { |
| 9 | + BlockList: BlockListHandle, |
| 10 | + AF_INET, |
| 11 | + AF_INET6, |
| 12 | +} = internalBinding('block_list'); |
| 13 | + |
| 14 | +const { |
| 15 | + customInspectSymbol: kInspect, |
| 16 | +} = require('internal/util'); |
| 17 | +const { inspect } = require('internal/util/inspect'); |
| 18 | + |
| 19 | +const kHandle = Symbol('kHandle'); |
| 20 | +const { owner_symbol } = internalBinding('symbols'); |
| 21 | + |
| 22 | +const { |
| 23 | + ERR_INVALID_ARG_TYPE, |
| 24 | + ERR_INVALID_ARG_VALUE, |
| 25 | + ERR_OUT_OF_RANGE, |
| 26 | +} = require('internal/errors').codes; |
| 27 | + |
| 28 | +class BlockList { |
| 29 | + constructor() { |
| 30 | + this[kHandle] = new BlockListHandle(); |
| 31 | + this[kHandle][owner_symbol] = this; |
| 32 | + } |
| 33 | + |
| 34 | + [kInspect](depth, options) { |
| 35 | + if (depth < 0) |
| 36 | + return this; |
| 37 | + |
| 38 | + const opts = { |
| 39 | + ...options, |
| 40 | + depth: options.depth == null ? null : options.depth - 1 |
| 41 | + }; |
| 42 | + |
| 43 | + return `BlockList ${inspect({ |
| 44 | + rules: this.rules |
| 45 | + }, opts)}`; |
| 46 | + } |
| 47 | + |
| 48 | + addAddress(address, family = 'ipv4') { |
| 49 | + if (typeof address !== 'string') |
| 50 | + throw new ERR_INVALID_ARG_TYPE('address', 'string', address); |
| 51 | + if (typeof family !== 'string') |
| 52 | + throw new ERR_INVALID_ARG_TYPE('family', 'string', family); |
| 53 | + if (family !== 'ipv4' && family !== 'ipv6') |
| 54 | + throw new ERR_INVALID_ARG_VALUE('family', family); |
| 55 | + const type = family === 'ipv4' ? AF_INET : AF_INET6; |
| 56 | + this[kHandle].addAddress(address, type); |
| 57 | + } |
| 58 | + |
| 59 | + addRange(start, end, family = 'ipv4') { |
| 60 | + if (typeof start !== 'string') |
| 61 | + throw new ERR_INVALID_ARG_TYPE('start', 'string', start); |
| 62 | + if (typeof end !== 'string') |
| 63 | + throw new ERR_INVALID_ARG_TYPE('end', 'string', end); |
| 64 | + if (typeof family !== 'string') |
| 65 | + throw new ERR_INVALID_ARG_TYPE('family', 'string', family); |
| 66 | + if (family !== 'ipv4' && family !== 'ipv6') |
| 67 | + throw new ERR_INVALID_ARG_VALUE('family', family); |
| 68 | + const type = family === 'ipv4' ? AF_INET : AF_INET6; |
| 69 | + const ret = this[kHandle].addRange(start, end, type); |
| 70 | + if (ret === false) |
| 71 | + throw new ERR_INVALID_ARG_VALUE('start', start, 'must come before end'); |
| 72 | + } |
| 73 | + |
| 74 | + addSubnet(network, prefix, family = 'ipv4') { |
| 75 | + if (typeof network !== 'string') |
| 76 | + throw new ERR_INVALID_ARG_TYPE('network', 'string', network); |
| 77 | + if (typeof prefix !== 'number') |
| 78 | + throw new ERR_INVALID_ARG_TYPE('prefix', 'number', prefix); |
| 79 | + if (typeof family !== 'string') |
| 80 | + throw new ERR_INVALID_ARG_TYPE('family', 'string', family); |
| 81 | + let type; |
| 82 | + switch (family) { |
| 83 | + case 'ipv4': |
| 84 | + type = AF_INET; |
| 85 | + if (prefix < 0 || prefix > 32) |
| 86 | + throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 32', prefix); |
| 87 | + break; |
| 88 | + case 'ipv6': |
| 89 | + type = AF_INET6; |
| 90 | + if (prefix < 0 || prefix > 128) |
| 91 | + throw new ERR_OUT_OF_RANGE(prefix, '>= 0 and <= 128', prefix); |
| 92 | + break; |
| 93 | + default: |
| 94 | + throw new ERR_INVALID_ARG_VALUE('family', family); |
| 95 | + } |
| 96 | + this[kHandle].addSubnet(network, type, prefix); |
| 97 | + } |
| 98 | + |
| 99 | + check(address, family = 'ipv4') { |
| 100 | + if (typeof address !== 'string') |
| 101 | + throw new ERR_INVALID_ARG_TYPE('address', 'string', address); |
| 102 | + if (typeof family !== 'string') |
| 103 | + throw new ERR_INVALID_ARG_TYPE('family', 'string', family); |
| 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)); |
| 108 | + } |
| 109 | + |
| 110 | + get rules() { |
| 111 | + return this[kHandle].getRules(); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +module.exports = BlockList; |
0 commit comments