2018-05-15 16:30:17 +01:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import { search } from "../lib/Extract.mjs" ;
2022-04-14 18:08:16 +01:00
import { ipSort } from "../lib/Sort.mjs" ;
2018-05-15 16:30:17 +01:00
/**
* 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.<br><br>Warning: Given a string <code>710.65.0.456</code>, this will match <code>10.65.0.45</code> so always check the original input!" ;
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
2022-04-14 18:08:16 +01:00
name : "IPv4" ,
type : "boolean" ,
value : true
2018-05-15 16:30:17 +01:00
},
{
2022-04-14 18:08:16 +01:00
name : "IPv6" ,
type : "boolean" ,
value : false
2018-05-15 16:30:17 +01:00
},
{
2022-04-14 18:08:16 +01:00
name : "Remove local IPv4 addresses" ,
type : "boolean" ,
value : false
2018-05-15 16:30:17 +01:00
},
{
2022-04-14 18:08:16 +01:00
name : "Display total" ,
type : "boolean" ,
value : false
},
{
name : "Sort" ,
type : "boolean" ,
value : false
},
{
name : "Unique" ,
type : "boolean" ,
value : false
2018-05-15 16:30:17 +01:00
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run ( input , args ) {
2022-04-14 18:08:16 +01:00
const [ includeIpv4 , includeIpv6 , removeLocal , displayTotal , sort , unique ] = args ,
2025-05-12 13:35:39 +00:00
// IPv4 decimal groups can have values 0 to 255. To construct a regex the following sub-regex is reused:
2025-05-12 14:18:01 +00:00
ipv4DecimalByte = "(?:25[0-5]|2[0-4]\\d|1?[0-9]\\d|\\d)" ,
2025-05-12 13:35:39 +00:00
ipv4OctalByte = "(?:0[1-3]?[0-7]{1,2})" ,
2025-05-12 14:18:01 +00:00
// Look behind and ahead will be used to exclude matches with additional decimal digits left and right of IP address
lookBehind = "(?<!\\d)" ,
lookAhead = "(?!\\d)" ,
// Each variant requires exactly 4 groups with literal . between.
ipv4Decimal = "(?:" + lookBehind + ipv4DecimalByte + "\\.){3}" + "(?:" + ipv4DecimalByte + lookAhead + ")" ,
ipv4Octal = "(?:" + lookBehind + ipv4OctalByte + "\\.){3}" + "(?:" + ipv4OctalByte + lookAhead + ")" ,
2025-05-12 13:35:39 +00:00
// Then we allow IPv4 addresses to be expressed either entirely in decimal or entirely in Octal
ipv4 = "(?:" + ipv4Decimal + "|" + ipv4Octal + ")" ,
2023-12-05 14:40:37 +08:00
ipv6 = "((?=.*::)(?!.*::.+::)(::)?([\\dA-F]{1,4}:(:|\\b)|){5}|([\\dA-F]{1,4}:){6})(([\\dA-F]{1,4}((?!\\3)::|:\\b|(?![\\dA-F])))|(?!\\2\\3)){2}" ;
2018-05-15 16:30:17 +01:00
let ips = "" ;
if ( includeIpv4 && includeIpv6 ) {
ips = ipv4 + "|" + ipv6 ;
} else if ( includeIpv4 ) {
ips = ipv4 ;
} else if ( includeIpv6 ) {
ips = ipv6 ;
}
2022-04-14 18:08:16 +01:00
if ( ! ips ) return "" ;
2018-05-15 16:30:17 +01:00
2022-04-14 18:08:16 +01:00
const regex = new RegExp ( ips , "ig" );
2018-05-15 16:30:17 +01:00
2022-04-14 18:08:16 +01:00
const ten = "10\\..+" ,
oneninetwo = "192\\.168\\..+" ,
oneseventwo = "172\\.(?:1[6-9]|2\\d|3[01])\\..+" ,
onetwoseven = "127\\..+" ,
removeRegex = new RegExp ( "^(?:" + ten + "|" + oneninetwo +
"|" + oneseventwo + "|" + onetwoseven + ")" );
const results = search (
input ,
regex ,
removeLocal ? removeRegex : null ,
sort ? ipSort : null ,
unique
);
if ( displayTotal ) {
return `Total found: ${ results . length } \n\n ${ results . join ( "\n" ) } ` ;
2018-05-15 16:30:17 +01:00
} else {
2022-04-14 18:08:16 +01:00
return results . join ( "\n" );
2018-05-15 16:30:17 +01:00
}
}
}
export default ExtractIPAddresses ;