/** * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright 2016 * @license Apache-2.0 */ import Operation from "../Operation.mjs"; import { search } from "../lib/Extract.mjs"; import { ipSort } from "../lib/Sort.mjs"; /** * Extract IP addresses operation */ class ExtractIPAddresses extends Operation { /** * ExtractIPAddresses constructor */ constructor() { super(); this.name = "Extract IP addresses"; this.module = "Regex"; this.description = "Extracts all IPv4 and IPv6 addresses.

Warning: Given a string 710.65.0.456, this will match 10.65.0.45 so always check the original input!"; this.inputType = "string"; this.outputType = "string"; this.args = [ { name: "IPv4", type: "boolean", value: true }, { name: "IPv6", type: "boolean", value: false }, { name: "Remove local IPv4 addresses", type: "boolean", value: false }, { name: "Display total", type: "boolean", value: false }, { name: "Sort", type: "boolean", value: false }, { name: "Unique", type: "boolean", value: false } ]; } /** * @param {string} input * @param {Object[]} args * @returns {string} */ run(input, args) { const [includeIpv4, includeIpv6, removeLocal, displayTotal, sort, unique] = args, // IPv4 decimal groups can have values 0 to 255. To construct a regex the following sub-regex is reused: ipv4DecimalByte = "(?:25[0-5]|2[0-4]\\d|1?[0-9]\\d|\\d)", ipv4OctalByte = "(?:0[1-3]?[0-7]{1,2})", // Look behind and ahead will be used to exclude matches with additional decimal digits left and right of IP address lookBehind = "(?