Przeglądaj źródła

Ported "Parse IP Range".

Callum Fraser 7 lat temu
rodzic
commit
5b6d57fd47
1 zmienionych plików z 78 dodań i 0 usunięć
  1. 78 0
      src/core/operations/ParseIPRange.mjs

+ 78 - 0
src/core/operations/ParseIPRange.mjs

@@ -0,0 +1,78 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import {_ipv4CidrRange, _ipv4HyphenatedRange, _ipv6CidrRange, _ipv6HyphenatedRange} from "../lib/Ip";
+
+/**
+ * Parse IP range operation
+ */
+class ParseIPRange extends Operation {
+
+    /**
+     * ParseIPRange constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Parse IP range";
+        this.module = "JSBN";
+        this.description = "Given a CIDR range (e.g. <code>10.0.0.0/24</code>) or a hyphenated range (e.g. <code>10.0.0.0 - 10.0.1.0</code>), this operation provides network information and enumerates all IP addresses in the range.<br><br>IPv6 is supported but will not be enumerated.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Include network info",
+                "type": "boolean",
+                "value": "IP.INCLUDE_NETWORK_INFO"
+            },
+            {
+                "name": "Enumerate IP addresses",
+                "type": "boolean",
+                "value": "IP.ENUMERATE_ADDRESSES"
+            },
+            {
+                "name": "Allow large queries",
+                "type": "boolean",
+                "value": "IP.ALLOW_LARGE_LIST"
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const includeNetworkInfo = args[0],
+            enumerateAddresses = args[1],
+            allowLargeList = args[2];
+
+        // 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*$/,
+            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,
+            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;
+        let match;
+
+        if ((match = ipv4CidrRegex.exec(input))) {
+            return _ipv4CidrRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList);
+        } else if ((match = ipv4RangeRegex.exec(input))) {
+            return _ipv4HyphenatedRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList);
+        } else if ((match = ipv6CidrRegex.exec(input))) {
+            return _ipv6CidrRange(match, includeNetworkInfo);
+        } else if ((match = ipv6RangeRegex.exec(input))) {
+            return _ipv6HyphenatedRange(match, includeNetworkInfo);
+        } else {
+            return "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.";
+        }
+    }
+
+}
+
+
+export default ParseIPRange;