UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

55 lines (54 loc) 1.76 kB
"use strict"; // Based on https://github.com/demskie/netparser // MIT License - Copyright (c) 2019 alex Object.defineProperty(exports, "__esModule", { value: true }); exports.IPMatcher = void 0; const shared = require("./shared"); const sort = require("./sort"); class IPMatcher { constructor(networks) { this.sorted = []; const subnets = []; if (networks) { for (const s of networks) { const net = shared.parseBaseNetwork(s, false); if (net && net.isValid()) { subnets.push(net); } } shared.sortNetworks(subnets); this.sorted = shared.summarizeSortedNetworks(subnets); } } // Checks if the given IP address is in the list of networks. has(network) { const net = shared.parseBaseNetwork(network, false); if (!net || !net.isValid()) { return false; } const idx = sort.binarySearchForInsertionIndex(net, this.sorted); if (idx < 0) { return false; } if (idx < this.sorted.length && this.sorted[idx].contains(net)) { return true; } if (idx - 1 >= 0 && this.sorted[idx - 1].contains(net)) { return true; } return false; } add(network) { const net = shared.parseBaseNetwork(network, false); if (!net || !net.isValid()) { return this; } const idx = sort.binarySearchForInsertionIndex(net, this.sorted); if (idx < this.sorted.length && this.sorted[idx].compare(net) === 0) { return this; } this.sorted.splice(idx, 0, net); return this; } } exports.IPMatcher = IPMatcher;