2018-05-16 00:31:03 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
import Operation from "../Operation" ;
2018-05-29 01:48:30 +02:00
import OperationError from "../errors/OperationError" ;
2018-08-28 07:35:54 +02:00
import { ipv4CidrRange , ipv4HyphenatedRange , ipv4ListedRange , ipv6CidrRange , ipv6HyphenatedRange , ipv6ListedRange } from "../lib/IP" ;
2018-05-16 00:31:03 +02:00
/ * *
* Parse IP range operation
* /
class ParseIPRange extends Operation {
/ * *
* ParseIPRange constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Parse IP range" ;
this . module = "JSBN" ;
2018-08-28 07:35:54 +02:00
this . description = "Given a CIDR range (e.g. <code>10.0.0.0/24</code>), hyphenated range (e.g. <code>10.0.0.0 - 10.0.1.0</code>), or a list of IPs and CIDR ranges (new line separated), (this operation provides network information and enumerates all IP addresses in the range.<br><br>IPv6 is supported but will not be enumerated" ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Subnetwork" ;
2018-05-16 00:31:03 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Include network info" ,
"type" : "boolean" ,
2018-05-29 01:48:30 +02:00
"value" : true
2018-05-16 00:31:03 +02:00
} ,
{
"name" : "Enumerate IP addresses" ,
"type" : "boolean" ,
2018-05-29 01:48:30 +02:00
"value" : true
2018-05-16 00:31:03 +02:00
} ,
{
"name" : "Allow large queries" ,
"type" : "boolean" ,
2018-05-29 01:48:30 +02:00
"value" : false
2018-05-16 00:31:03 +02:00
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2018-05-29 01:48:30 +02:00
const [
includeNetworkInfo ,
enumerateAddresses ,
allowLargeList
] = args ;
2018-05-16 00:31:03 +02: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*$/ ,
2018-08-28 06:48:19 +02:00
ipv4ListRegex = /^\s*(((?:\d{1,3}\.){3}\d{1,3})(\/(\d\d?))?(\n|$)(\n*))*$/ ,
2018-05-16 00:31:03 +02: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 07:35:54 +02: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 ,
ipv6ListRegex = /^((((?=.*::)(?!.*::.+::)(::)?([\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*))*$/ig ;
2018-05-16 00:31:03 +02:00
let match ;
if ( ( match = ipv4CidrRegex . exec ( input ) ) ) {
2018-05-16 23:32:46 +02:00
return ipv4CidrRange ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2018-05-16 00:31:03 +02:00
} else if ( ( match = ipv4RangeRegex . exec ( input ) ) ) {
2018-05-16 23:32:46 +02:00
return ipv4HyphenatedRange ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2018-08-28 06:48:19 +02:00
} else if ( ( match = ipv4ListRegex . exec ( input ) ) ) {
return ipv4ListedRange ( match , includeNetworkInfo , enumerateAddresses , allowLargeList ) ;
2018-05-16 00:31:03 +02:00
} else if ( ( match = ipv6CidrRegex . exec ( input ) ) ) {
2018-05-16 23:32:46 +02:00
return ipv6CidrRange ( match , includeNetworkInfo ) ;
2018-05-16 00:31:03 +02:00
} else if ( ( match = ipv6RangeRegex . exec ( input ) ) ) {
2018-05-16 23:32:46 +02:00
return ipv6HyphenatedRange ( match , includeNetworkInfo ) ;
2018-08-28 07:35:54 +02:00
} else if ( ( match = ipv6ListRegex . exec ( input ) ) ) {
return ipv6ListedRange ( match , includeNetworkInfo ) ;
2018-05-16 00:31:03 +02:00
} else {
2018-05-29 01:48:30 +02:00
throw new OperationError ( "Invalid input.\n\nEnter either a CIDR range (e.g. 10.0.0.0/24) or a hyphenated range (e.g. 10.0.0.0 - 10.0.1.0). IPv6 also supported." ) ;
2018-05-16 00:31:03 +02:00
}
}
}
export default ParseIPRange ;