ParseIPRange.mjs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import OperationError from "../errors/OperationError";
  8. import {ipv4CidrRange, ipv4HyphenatedRange, ipv4ListedRange, ipv6CidrRange, ipv6HyphenatedRange, ipv6ListedRange} from "../lib/IP";
  9. /**
  10. * Parse IP range operation
  11. */
  12. class ParseIPRange extends Operation {
  13. /**
  14. * ParseIPRange constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "Parse IP range";
  19. this.module = "JSBN";
  20. 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/or CIDR ranges (separated by a new line), this operation provides network information and enumerates all IP addresses in the range.<br><br>IPv6 is supported but will not be enumerated";
  21. this.infoURL = "https://wikipedia.org/wiki/Subnetwork";
  22. this.inputType = "string";
  23. this.outputType = "string";
  24. this.args = [
  25. {
  26. "name": "Include network info",
  27. "type": "boolean",
  28. "value": true
  29. },
  30. {
  31. "name": "Enumerate IP addresses",
  32. "type": "boolean",
  33. "value": true
  34. },
  35. {
  36. "name": "Allow large queries",
  37. "type": "boolean",
  38. "value": false
  39. }
  40. ];
  41. }
  42. /**
  43. * @param {string} input
  44. * @param {Object[]} args
  45. * @returns {string}
  46. */
  47. run(input, args) {
  48. const [
  49. includeNetworkInfo,
  50. enumerateAddresses,
  51. allowLargeList
  52. ] = args;
  53. // Check what type of input we are looking at
  54. const ipv4CidrRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\/(\d\d?)\s*$/,
  55. ipv4RangeRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\s*-\s*((?:\d{1,3}\.){3}\d{1,3})\s*$/,
  56. ipv4ListRegex = /^\s*(((?:\d{1,3}\.){3}\d{1,3})(\/(\d\d?))?(\n|$)(\n*))*$/,
  57. 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,
  58. 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,
  59. 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;
  60. let match;
  61. if ((match = ipv4CidrRegex.exec(input))) {
  62. return ipv4CidrRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList);
  63. } else if ((match = ipv4RangeRegex.exec(input))) {
  64. return ipv4HyphenatedRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList);
  65. } else if ((match = ipv4ListRegex.exec(input))) {
  66. return ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList);
  67. } else if ((match = ipv6CidrRegex.exec(input))) {
  68. return ipv6CidrRange(match, includeNetworkInfo);
  69. } else if ((match = ipv6RangeRegex.exec(input))) {
  70. return ipv6HyphenatedRange(match, includeNetworkInfo);
  71. } else if ((match = ipv6ListRegex.exec(input))) {
  72. return ipv6ListedRange(match, includeNetworkInfo);
  73. } else {
  74. 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.");
  75. }
  76. }
  77. }
  78. export default ParseIPRange;