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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155/** * Utils for IP Addresses * @module */
import type { AddressType } from '../helper/conninfo'
/** * Expand IPv6 Address * @param ipV6 Shorten IPv6 Address * @return expanded IPv6 Address */export const expandIPv6 = (ipV6: string): string => { const sections = ipV6.split(':') if (IPV4_REGEX.test(sections.at(-1) as string)) { sections.splice( -1, 1, ...convertIPv6BinaryToString(convertIPv4ToBinary(sections.at(-1) as string)) // => ::7f00:0001 .substring(2) // => 7f00:0001 .split(':') // => ['7f00', '0001'] ) } for (let i = 0; i < sections.length; i++) { const node = sections[i] if (node !== '') { sections[i] = node.padStart(4, '0') } else { sections[i + 1] === '' && sections.splice(i + 1, 1) sections[i] = new Array(8 - sections.length + 1).fill('0000').join(':') } } return sections.join(':')}
const IPV4_OCTET_PART = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])'const IPV4_REGEX = new RegExp(`^(?:${IPV4_OCTET_PART}\\.){3}${IPV4_OCTET_PART}$`)
/** * Distinct Remote Addr * @param remoteAddr Remote Addr */export const distinctRemoteAddr = (remoteAddr: string): AddressType => { if (IPV4_REGEX.test(remoteAddr)) { return 'IPv4' } if (remoteAddr.includes(':')) { // Domain can't include `:` return 'IPv6' }}
/** * Convert IPv4 to Uint8Array * @param ipv4 IPv4 Address * @returns BigInt */export const convertIPv4ToBinary = (ipv4: string): bigint => { const parts = ipv4.split('.') let result = 0n for (let i = 0; i < 4; i++) { result <<= 8n result += BigInt(parts[i]) } return result}
/** * Convert IPv6 to Uint8Array * @param ipv6 IPv6 Address * @returns BigInt */export const convertIPv6ToBinary = (ipv6: string): bigint => { const sections = expandIPv6(ipv6).split(':') let result = 0n for (let i = 0; i < 8; i++) { result <<= 16n result += BigInt(parseInt(sections[i], 16)) } return result}
/** * Convert a binary representation of an IPv4 address to a string. * @param ipV4 binary IPv4 Address * @return IPv4 Address in string */export const convertIPv4BinaryToString = (ipV4: bigint): string => { const sections = [] for (let i = 0; i < 4; i++) { sections.push((ipV4 >> BigInt(8 * (3 - i))) & 0xffn) } return sections.join('.')}
/** * Check if a binary IPv6 address is an IPv4-mapped IPv6 address (::ffff:x.x.x.x) * @param ipv6binary binary IPv6 Address * @return true if the address is an IPv4-mapped IPv6 address */export const isIPv4MappedIPv6 = (ipv6binary: bigint): boolean => ipv6binary >> 32n === 0xffffn
/** * Extract the IPv4 portion from an IPv4-mapped IPv6 address * @param ipv6binary binary IPv4-mapped IPv6 Address * @return binary IPv4 Address */export const convertIPv4MappedIPv6ToIPv4 = (ipv6binary: bigint): bigint => ipv6binary & 0xffffffffn
/** * Convert a binary representation of an IPv6 address to a string. * @param ipV6 binary IPv6 Address * @return normalized IPv6 Address in string */export const convertIPv6BinaryToString = (ipV6: bigint): string => { if (isIPv4MappedIPv6(ipV6)) { return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}` }
const sections = [] for (let i = 0; i < 8; i++) { sections.push(((ipV6 >> BigInt(16 * (7 - i))) & 0xffffn).toString(16)) }
let currentZeroStart = -1 let maxZeroStart = -1 let maxZeroEnd = -1 for (let i = 0; i < 8; i++) { if (sections[i] === '0') { if (currentZeroStart === -1) { currentZeroStart = i } } else { if (currentZeroStart > -1) { if (i - currentZeroStart > maxZeroEnd - maxZeroStart) { maxZeroStart = currentZeroStart maxZeroEnd = i } currentZeroStart = -1 } } } if (currentZeroStart > -1) { if (8 - currentZeroStart > maxZeroEnd - maxZeroStart) { maxZeroStart = currentZeroStart maxZeroEnd = 8 } } if (maxZeroStart !== -1) { sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ':') }
return sections.join(':').replace(/:{2,}/g, '::')}