ipaddr.ts
This file
Path
src/utils/ipaddr.ts
154 lines · 4.0 KiB · TypeScript
Last changed by
Merge commit from forkYusuke Wadabefore adoption48fa223
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
4.9.0Yusuke Wadabefore adoption82bdc4d
lines 1–35why, one step back1/**2 * Utils for IP Addresses3 * @module4 */56import type { AddressType } from '../helper/conninfo'78/**9 * Expand IPv6 Address10 * @param ipV6 Shorten IPv6 Address11 * @return expanded IPv6 Address12 */13export const expandIPv6 = (ipV6: string): string => {14 const sections = ipV6.split(':')15 if (IPV4_REGEX.test(sections.at(-1) as string)) {16 sections.splice(17 -1,18 1,19 ...convertIPv6BinaryToString(convertIPv4ToBinary(sections.at(-1) as string)) // => ::7f00:000120 .substring(2) // => 7f00:000121 .split(':') // => ['7f00', '0001']22 )23 }24 for (let i = 0; i < sections.length; i++) {25 const node = sections[i]26 if (node !== '') {27 sections[i] = node.padStart(4, '0')28 } else {29 sections[i + 1] === '' && sections.splice(i + 1, 1)30 sections[i] = new Array(8 - sections.length + 1).fill('0000').join(':')31 }32 }33 return sections.join(':')34}35Merge commit from forkYusuke Wadabefore adoptionedbf6ee
lines 36–37why, one step back36const IPV4_OCTET_PART = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])'37const IPV4_REGEX = new RegExp(`^(?:${IPV4_OCTET_PART}\\.){3}${IPV4_OCTET_PART}$`)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 38–96why, one step back3839/**40 * Distinct Remote Addr41 * @param remoteAddr Remote Addr42 */43export const distinctRemoteAddr = (remoteAddr: string): AddressType => {44 if (IPV4_REGEX.test(remoteAddr)) {45 return 'IPv4'46 }47 if (remoteAddr.includes(':')) {48 // Domain can't include `:`49 return 'IPv6'50 }51}5253/**54 * Convert IPv4 to Uint8Array55 * @param ipv4 IPv4 Address56 * @returns BigInt57 */58export const convertIPv4ToBinary = (ipv4: string): bigint => {59 const parts = ipv4.split('.')60 let result = 0n61 for (let i = 0; i < 4; i++) {62 result <<= 8n63 result += BigInt(parts[i])64 }65 return result66}6768/**69 * Convert IPv6 to Uint8Array70 * @param ipv6 IPv6 Address71 * @returns BigInt72 */73export const convertIPv6ToBinary = (ipv6: string): bigint => {74 const sections = expandIPv6(ipv6).split(':')75 let result = 0n76 for (let i = 0; i < 8; i++) {77 result <<= 16n78 result += BigInt(parseInt(sections[i], 16))79 }80 return result81}8283/**84 * Convert a binary representation of an IPv4 address to a string.85 * @param ipV4 binary IPv4 Address86 * @return IPv4 Address in string87 */88export const convertIPv4BinaryToString = (ipV4: bigint): string => {89 const sections = []90 for (let i = 0; i < 4; i++) {91 sections.push((ipV4 >> BigInt(8 * (3 - i))) & 0xffn)92 }93 return sections.join('.')94}9596/**Merge commit from forkYusuke Wadabefore adoption48fa223
lines 97–110why, one step back97 * Check if a binary IPv6 address is an IPv4-mapped IPv6 address (::ffff:x.x.x.x)98 * @param ipv6binary binary IPv6 Address99 * @return true if the address is an IPv4-mapped IPv6 address100 */101export const isIPv4MappedIPv6 = (ipv6binary: bigint): boolean => ipv6binary >> 32n === 0xffffn102103/**104 * Extract the IPv4 portion from an IPv4-mapped IPv6 address105 * @param ipv6binary binary IPv4-mapped IPv6 Address106 * @return binary IPv4 Address107 */108export const convertIPv4MappedIPv6ToIPv4 = (ipv6binary: bigint): bigint => ipv6binary & 0xffffffffn109110/**4.9.0Yusuke Wadabefore adoption82bdc4d
lines 111–115why, one step back111 * Convert a binary representation of an IPv6 address to a string.112 * @param ipV6 binary IPv6 Address113 * @return normalized IPv6 Address in string114 */115export const convertIPv6BinaryToString = (ipV6: bigint): string => {Merge commit from forkYusuke Wadabefore adoption48fa223
lines 116–117why, one step back116 if (isIPv4MappedIPv6(ipV6)) {117 return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}`4.9.0Yusuke Wadabefore adoption82bdc4d
lines 118–154why, one step back118 }119120 const sections = []121 for (let i = 0; i < 8; i++) {122 sections.push(((ipV6 >> BigInt(16 * (7 - i))) & 0xffffn).toString(16))123 }124125 let currentZeroStart = -1126 let maxZeroStart = -1127 let maxZeroEnd = -1128 for (let i = 0; i < 8; i++) {129 if (sections[i] === '0') {130 if (currentZeroStart === -1) {131 currentZeroStart = i132 }133 } else {134 if (currentZeroStart > -1) {135 if (i - currentZeroStart > maxZeroEnd - maxZeroStart) {136 maxZeroStart = currentZeroStart137 maxZeroEnd = i138 }139 currentZeroStart = -1140 }141 }142 }143 if (currentZeroStart > -1) {144 if (8 - currentZeroStart > maxZeroEnd - maxZeroStart) {145 maxZeroStart = currentZeroStart146 maxZeroEnd = 8147 }148 }149 if (maxZeroStart !== -1) {150 sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ':')151 }152153 return sections.join(':').replace(/:{2,}/g, '::')154}