ipaddr.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // src/utils/ipaddr.ts
  2. var expandIPv6 = (ipV6) => {
  3. const sections = ipV6.split(":");
  4. if (IPV4_REGEX.test(sections.at(-1))) {
  5. sections.splice(
  6. -1,
  7. 1,
  8. ...convertIPv6BinaryToString(convertIPv4ToBinary(sections.at(-1))).substring(2).split(":")
  9. // => ['7f00', '0001']
  10. );
  11. }
  12. for (let i = 0; i < sections.length; i++) {
  13. const node = sections[i];
  14. if (node !== "") {
  15. sections[i] = node.padStart(4, "0");
  16. } else {
  17. sections[i + 1] === "" && sections.splice(i + 1, 1);
  18. sections[i] = new Array(8 - sections.length + 1).fill("0000").join(":");
  19. }
  20. }
  21. return sections.join(":");
  22. };
  23. var IPV4_OCTET_PART = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])";
  24. var IPV4_REGEX = new RegExp(`^(?:${IPV4_OCTET_PART}\\.){3}${IPV4_OCTET_PART}$`);
  25. var distinctRemoteAddr = (remoteAddr) => {
  26. if (IPV4_REGEX.test(remoteAddr)) {
  27. return "IPv4";
  28. }
  29. if (remoteAddr.includes(":")) {
  30. return "IPv6";
  31. }
  32. };
  33. var convertIPv4ToBinary = (ipv4) => {
  34. const parts = ipv4.split(".");
  35. let result = 0n;
  36. for (let i = 0; i < 4; i++) {
  37. result <<= 8n;
  38. result += BigInt(parts[i]);
  39. }
  40. return result;
  41. };
  42. var convertIPv6ToBinary = (ipv6) => {
  43. const sections = expandIPv6(ipv6).split(":");
  44. let result = 0n;
  45. for (let i = 0; i < 8; i++) {
  46. result <<= 16n;
  47. result += BigInt(parseInt(sections[i], 16));
  48. }
  49. return result;
  50. };
  51. var convertIPv4BinaryToString = (ipV4) => {
  52. const sections = [];
  53. for (let i = 0; i < 4; i++) {
  54. sections.push(ipV4 >> BigInt(8 * (3 - i)) & 0xffn);
  55. }
  56. return sections.join(".");
  57. };
  58. var isIPv4MappedIPv6 = (ipv6binary) => ipv6binary >> 32n === 0xffffn;
  59. var convertIPv4MappedIPv6ToIPv4 = (ipv6binary) => ipv6binary & 0xffffffffn;
  60. var convertIPv6BinaryToString = (ipV6) => {
  61. if (isIPv4MappedIPv6(ipV6)) {
  62. return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}`;
  63. }
  64. const sections = [];
  65. for (let i = 0; i < 8; i++) {
  66. sections.push((ipV6 >> BigInt(16 * (7 - i)) & 0xffffn).toString(16));
  67. }
  68. let currentZeroStart = -1;
  69. let maxZeroStart = -1;
  70. let maxZeroEnd = -1;
  71. for (let i = 0; i < 8; i++) {
  72. if (sections[i] === "0") {
  73. if (currentZeroStart === -1) {
  74. currentZeroStart = i;
  75. }
  76. } else {
  77. if (currentZeroStart > -1) {
  78. if (i - currentZeroStart > maxZeroEnd - maxZeroStart) {
  79. maxZeroStart = currentZeroStart;
  80. maxZeroEnd = i;
  81. }
  82. currentZeroStart = -1;
  83. }
  84. }
  85. }
  86. if (currentZeroStart > -1) {
  87. if (8 - currentZeroStart > maxZeroEnd - maxZeroStart) {
  88. maxZeroStart = currentZeroStart;
  89. maxZeroEnd = 8;
  90. }
  91. }
  92. if (maxZeroStart !== -1) {
  93. sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ":");
  94. }
  95. return sections.join(":").replace(/:{2,}/g, "::");
  96. };
  97. export {
  98. convertIPv4BinaryToString,
  99. convertIPv4MappedIPv6ToIPv4,
  100. convertIPv4ToBinary,
  101. convertIPv6BinaryToString,
  102. convertIPv6ToBinary,
  103. distinctRemoteAddr,
  104. expandIPv6,
  105. isIPv4MappedIPv6
  106. };