* Compile trust function for multiple subnets.
(subnets: any[])
| 147 | * Compile trust function for multiple subnets. |
| 148 | */ |
| 149 | function trustMulti(subnets: any[]) { |
| 150 | return function trust(addr: string) { |
| 151 | if (!isip(addr)) return false |
| 152 | const ip = parseip(addr) |
| 153 | let ipconv |
| 154 | const kind = ip.kind() |
| 155 | for (let i = 0; i < subnets.length; i++) { |
| 156 | const subnet = subnets[i] |
| 157 | const subnetip = subnet[0] |
| 158 | const subnetkind = subnetip.kind() |
| 159 | const subnetrange = subnet[1] |
| 160 | let trusted = ip |
| 161 | if (kind !== subnetkind) { |
| 162 | if (subnetkind === 'ipv4' && !(ip as IPv6).isIPv4MappedAddress()) continue |
| 163 | |
| 164 | if (!ipconv) ipconv = subnetkind === 'ipv4' ? (ip as IPv6).toIPv4Address() : (ip as IPv4).toIPv4MappedAddress() |
| 165 | |
| 166 | trusted = ipconv |
| 167 | } |
| 168 | if ((trusted as IPv4).match(subnetip, subnetrange)) return true |
| 169 | } |
| 170 | return false |
| 171 | } |
| 172 | } |
| 173 | /** |
| 174 | * Compile trust function for single subnet. |
| 175 | * |