2018-05-15 23:31:03 +01:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
2018-08-28 20:14:40 +10:00
* @ author Klaxon [ klaxon @ veyr . com ]
2018-05-15 23:31:03 +01:00
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
2024-09-09 16:14:56 +00:00
import { ipv4CidrRange , ipv4HyphenatedRange , ipv4SubnetMask , ipv4ListedRange , ipv6CidrRange , ipv6HyphenatedRange , ipv6ListedRange } from "../lib/IP.mjs" ;
2018-05-15 23:31:03 +01:00
/ * *
* Parse IP range operation
* /
class ParseIPRange extends Operation {
/ * *
* ParseIPRange constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Parse IP range" ;
2018-12-25 23:58:00 +00:00
this . module = "Default" ;
2024-09-09 21:06:12 +00:00
this . description = "This operation provides network information and enumerates all IP addresses in the given range.<br>Supported inputs:<br><ul><li>IP with a CIDR (e.g. <code>10.0.0.0/24</code>)</li><li>IP with a subnet mask (e.g <code>10.0.0.0/255.255.255.0</code>)</li><li>Hyphenated range (e.g. <code>10.0.0.0 - 10.0.1.0</code>). Only one hyphenated range is allowed</li><li>List of IPs and/or CIDR ranges/subnet masks separated by a new line</li></ul><br>IPv6 is supported but will not be enumerated." ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/Subnetwork" ;
2018-05-15 23:31:03 +01:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Include network info" ,
"type" : "boolean" ,
2018-05-29 00:48:30 +01:00
"value" : true
2018-05-15 23:31:03 +01:00
} ,
{
"name" : "Enumerate IP addresses" ,
"type" : "boolean" ,
2018-05-29 00:48:30 +01:00
"value" : true
2018-05-15 23:31:03 +01:00
} ,
{
"name" : "Allow large queries" ,
"type" : "boolean" ,
2018-05-29 00:48:30 +01:00
"value" : false
2018-05-15 23:31:03 +01:00
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2018-05-29 00:48:30 +01:00
const [
includeNetworkInfo ,
enumerateAddresses ,
allowLargeList
] = args ;
2018-05-15 23:31:03 +01:00
// Check what type of input we are looking at
const ipv4CidrRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\/(\d\d?)\s*$/ ,
ipv4RangeRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\s*-\s*((?:\d{1,3}\.){3}\d{1,3})\s*$/ ,
2024-09-09 16:14:56 +00:00
ipv4SubnetMaskRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\/((?:\d{1,3}\.){3}\d{1,3})\s*$/ ,
2024-09-09 20:23:51 +00:00
ipv4ListRegex = /^\s*(((?:\d{1,3}\.){3}\d{1,3})(\/((?:\d\d?)|(?:\d{1,3}\.){3}\d{1,3}))?(\n|$)(\n*))+\s*$/ ,
2018-05-15 23:31:03 +01:00
ipv6CidrRegex = /^\s*(((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))\/(\d\d?\d?)\s*$/i ,
2018-08-28 15:35:54 +10:00
ipv6RangeRegex = /^\s*(((?=.*::)(?!.*::[^-]+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))\s*-\s*(((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\17)::|:\b|(?![\dA-F])))|(?!\16\17)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))\s*$/i ,
2018-08-28 20:15:59 +10:00
ipv6ListRegex = /^\s*((((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))(\/(\d\d?\d?))?(\n|$)(\n*))+\s*$/i ;
2018-05-15 23:31:03 +01:00
let match ;
if ( ( match = ipv4CidrRegex . exec ( input ) ) ) {
2018-05-16 22:32:46 +01:00
return ipv4CidrRange ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2018-05-15 23:31:03 +01:00
} else if ( ( match = ipv4RangeRegex . exec ( input ) ) ) {
2018-05-16 22:32:46 +01:00
return ipv4HyphenatedRange ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2024-09-09 16:14:56 +00:00
} else if ( ( match = ipv4SubnetMaskRegex . exec ( input ) ) ) {
return ipv4SubnetMask ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2018-08-28 14:48:19 +10:00
} else if ( ( match = ipv4ListRegex . exec ( input ) ) ) {
return ipv4ListedRange ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2018-05-15 23:31:03 +01:00
} else if ( ( match = ipv6CidrRegex . exec ( input ) ) ) {
2018-05-16 22:32:46 +01:00
return ipv6CidrRange ( match , includeNetworkInfo ) ;
2018-05-15 23:31:03 +01:00
} else if ( ( match = ipv6RangeRegex . exec ( input ) ) ) {
2018-05-16 22:32:46 +01:00
return ipv6HyphenatedRange ( match , includeNetworkInfo ) ;
2018-08-28 15:35:54 +10:00
} else if ( ( match = ipv6ListRegex . exec ( input ) ) ) {
return ipv6ListedRange ( match , includeNetworkInfo ) ;
2018-05-15 23:31:03 +01:00
} else {
2024-09-09 21:06:12 +00:00
throw new OperationError ( "Invalid input.\n\nThe following input strings are supported:\nCIDR range (e.g. 10.0.0.0/24)\nSubnet mask (e.g. 10.0.0.0/255.255.255.0)\nHyphenated range (e.g. 10.0.0.0 - 10.0.1.0). Only one hyphenated range is allowed\nIPv6 also supported." ) ;
2018-05-15 23:31:03 +01:00
}
}
}
export default ParseIPRange ;