|
| 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 | +constkHandle=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 | +classBlockList{ |
| 29 | +constructor(){ |
| 30 | +this[kHandle]=newBlockListHandle(); |
| 31 | +this[kHandle][owner_symbol]=this; |
| 32 | +} |
| 33 | + |
| 34 | +[kInspect](depth,options){ |
| 35 | +if(depth<0) |
| 36 | +returnthis; |
| 37 | + |
| 38 | +constopts={ |
| 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(typeofaddress!=='string') |
| 50 | +thrownewERR_INVALID_ARG_TYPE('address','string',address); |
| 51 | +if(typeoffamily!=='string') |
| 52 | +thrownewERR_INVALID_ARG_TYPE('family','string',family); |
| 53 | +if(family!=='ipv4'&&family!=='ipv6') |
| 54 | +thrownewERR_INVALID_ARG_VALUE('family',family); |
| 55 | +consttype=family==='ipv4' ? AF_INET : AF_INET6; |
| 56 | +this[kHandle].addAddress(address,type); |
| 57 | +} |
| 58 | + |
| 59 | +addRange(start,end,family='ipv4'){ |
| 60 | +if(typeofstart!=='string') |
| 61 | +thrownewERR_INVALID_ARG_TYPE('start','string',start); |
| 62 | +if(typeofend!=='string') |
| 63 | +thrownewERR_INVALID_ARG_TYPE('end','string',end); |
| 64 | +if(typeoffamily!=='string') |
| 65 | +thrownewERR_INVALID_ARG_TYPE('family','string',family); |
| 66 | +if(family!=='ipv4'&&family!=='ipv6') |
| 67 | +thrownewERR_INVALID_ARG_VALUE('family',family); |
| 68 | +consttype=family==='ipv4' ? AF_INET : AF_INET6; |
| 69 | +constret=this[kHandle].addRange(start,end,type); |
| 70 | +if(ret===false) |
| 71 | +thrownewERR_INVALID_ARG_VALUE('start',start,'must come before end'); |
| 72 | +} |
| 73 | + |
| 74 | +addSubnet(network,prefix,family='ipv4'){ |
| 75 | +if(typeofnetwork!=='string') |
| 76 | +thrownewERR_INVALID_ARG_TYPE('network','string',network); |
| 77 | +if(typeofprefix!=='number') |
| 78 | +thrownewERR_INVALID_ARG_TYPE('prefix','number',prefix); |
| 79 | +if(typeoffamily!=='string') |
| 80 | +thrownewERR_INVALID_ARG_TYPE('family','string',family); |
| 81 | +lettype; |
| 82 | +switch(family){ |
| 83 | +case'ipv4': |
| 84 | +type=AF_INET; |
| 85 | +if(prefix<0||prefix>32) |
| 86 | +thrownewERR_OUT_OF_RANGE(prefix,'>= 0 and <= 32',prefix); |
| 87 | +break; |
| 88 | +case'ipv6': |
| 89 | +type=AF_INET6; |
| 90 | +if(prefix<0||prefix>128) |
| 91 | +thrownewERR_OUT_OF_RANGE(prefix,'>= 0 and <= 128',prefix); |
| 92 | +break; |
| 93 | +default: |
| 94 | +thrownewERR_INVALID_ARG_VALUE('family',family); |
| 95 | +} |
| 96 | +this[kHandle].addSubnet(network,type,prefix); |
| 97 | +} |
| 98 | + |
| 99 | +check(address,family='ipv4'){ |
| 100 | +if(typeofaddress!=='string') |
| 101 | +thrownewERR_INVALID_ARG_TYPE('address','string',address); |
| 102 | +if(typeoffamily!=='string') |
| 103 | +thrownewERR_INVALID_ARG_TYPE('family','string',family); |
| 104 | +if(family!=='ipv4'&&family!=='ipv6') |
| 105 | +thrownewERR_INVALID_ARG_VALUE('family',family); |
| 106 | +consttype=family==='ipv4' ? AF_INET : AF_INET6; |
| 107 | +returnBoolean(this[kHandle].check(address,type)); |
| 108 | +} |
| 109 | + |
| 110 | +getrules(){ |
| 111 | +returnthis[kHandle].getRules(); |
| 112 | +} |
| 113 | +} |
| 114 | + |
| 115 | +module.exports=BlockList; |
0 commit comments