import * as ipaddr from "ipaddr.js"
async function handler(event) {
const req = event.request
let address = ipaddr.parse(event.client.address)
if (address.kind() === "ipv6") {
console.log("Received a request via IPv6")
const encoder = new TextEncoder()
const data = encoder.encode(address)
const hash = await crypto.subtle.digest("SHA-1", data)
const hashArray = Array.from(new Uint8Array(hash))
address = ipaddr.fromByteArray([0xf0 | hashArray[0], hashArray[1], hashArray[2], hashArray[3]])
console.log("Constructed IPv4 address " + address.toString())
} else {
console.log("Received a request via IPv4")
}
req.headers.set("x-ipv4", address.toString())
return fetch(req, {
backend: "origin_0",
})
}
addEventListener("fetch", (event) => event.respondWith(handler(event)))
function wordToByteArray(word, length) {
let ba = [],
xFF = 0xff
if (length > 0) ba.push(word >>> 24)
if (length > 1) ba.push((word >>> 16) & xFF)
if (length > 2) ba.push((word >>> 8) & xFF)
if (length > 3) ba.push(word & xFF)
return ba
}
function wordArrayToByteArray(wordArray, length) {
if (wordArray.hasOwnProperty("sigBytes") && wordArray.hasOwnProperty("words")) {
length = wordArray.sigBytes
wordArray = wordArray.words
}
let result = [],
bytes,
i = 0
while (length > 0) {
bytes = wordToByteArray(wordArray[i], Math.min(4, length))
length -= bytes.length
result.push(bytes)
i++
}
return [].concat.apply([], result)
}