* Compile trust function for single subnet. * * @param subnet
(subnet: IPv4 | IPv6)
| 176 | * @param subnet |
| 177 | */ |
| 178 | function trustSingle(subnet: IPv4 | IPv6) { |
| 179 | const subnetip = subnet[0] |
| 180 | const subnetkind = subnetip.kind() |
| 181 | const subnetisipv4 = subnetkind === 'ipv4' |
| 182 | const subnetrange = subnet[1] |
| 183 | return function trust(addr: string) { |
| 184 | if (!isip(addr)) return false |
| 185 | let ip = parseip(addr) |
| 186 | const kind = ip.kind() |
| 187 | if (kind !== subnetkind) { |
| 188 | if (subnetisipv4 && !(ip as IPv6).isIPv4MappedAddress()) return false |
| 189 | |
| 190 | ip = subnetisipv4 ? (ip as IPv6).toIPv4Address() : (ip as IPv4).toIPv4MappedAddress() |
| 191 | } |
| 192 | return (ip as IPv6).match(subnetip, subnetrange) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | export { alladdrs as all } |
| 197 | export { compile } |