update waf api (#91)

This commit is contained in:
UUBulb
2025-01-04 20:47:14 +08:00
committed by GitHub
parent 231f483876
commit 2d7c41a5b2
2 changed files with 15 additions and 114 deletions
-104
View File
@@ -145,110 +145,6 @@ export function joinIP(p?: ModelIP) {
return ""
}
function base64toUint8Array(base64str: string) {
const binary = atob(base64str)
const len = binary.length
const buf = new Uint8Array(len)
for (let i = 0; i < len; i++) {
buf[i] = binary.charCodeAt(i)
}
return buf
}
export function ip16Str(base64str: string) {
const buf = base64toUint8Array(base64str)
const ip4 = buf.slice(-6)
if (ip4[0] === 255 && ip4[1] === 255) {
return ip4.slice(2).join(".")
}
return ipv6BinaryToString(buf)
}
const digits = "0123456789abcdef"
function appendHex(b: string[], x: number): void {
if (x >= 0x1000) {
b.push(digits[(x >> 12) & 0xf])
}
if (x >= 0x100) {
b.push(digits[(x >> 8) & 0xf])
}
if (x >= 0x10) {
b.push(digits[(x >> 4) & 0xf])
}
b.push(digits[x & 0xf])
}
function ipv6BinaryToString(ip: Uint8Array): string {
let ipBytes: Uint8Array
if (ip.length !== 16) {
ipBytes = new Uint8Array(16)
const len = Math.min(ip.length, 16)
ipBytes.set(ip.subarray(0, len))
} else {
ipBytes = ip
}
const hextets: number[] = []
for (let i = 0; i < 16; i += 2) {
hextets.push((ipBytes[i] << 8) | ipBytes[i + 1])
}
let zeroStart = -1
let zeroLength = 0
for (let i = 0; i <= hextets.length; ) {
let j = i
while (j < hextets.length && hextets[j] === 0) {
j++
}
const length = j - i
if (length >= 2 && length > zeroLength) {
zeroStart = i
zeroLength = length
}
if (j === i) {
i++
} else {
i = j
}
}
const parts: string[] = []
for (let i = 0; i < hextets.length; i++) {
if (zeroLength > 0 && i === zeroStart) {
parts.push("")
i += zeroLength - 1
continue
}
if (parts.length > 0) {
parts.push(":")
}
const b: string[] = []
appendHex(b, hextets[i])
parts.push(b.join(""))
}
let ipv6 = parts.join("")
if (ipv6.startsWith("::")) {
} else if (ipv6.startsWith(":")) {
ipv6 = ":" + ipv6
}
if (ipv6.endsWith("::")) {
} else if (ipv6.endsWith(":")) {
ipv6 = ipv6 + ":"
}
if (ipv6 === "") {
ipv6 = "::"
}
return ipv6
}
export async function copyToClipboard(text: string) {
try {
return await navigator.clipboard.writeText(text)