lokiwind
05/24/2023, 7:18 PMjavascript
async function handleRequest(request) {
// Get the IP address of the incoming user from the request headers
const ip = request.headers.get("CF-Connecting-IP");
// Generate a 3-digit key
const key = Math.random().toString().slice(2, 5);
// Save the data to the KV namespace using the generated key
await LOG_IP.put(`${ip}<${key}`, 8);
// Return the original request
return fetch(request);
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
https://cdn.discordapp.com/attachments/1111010354877694032/1111010355175510036/Screenshot_1.png▾
Chaika
05/24/2023, 7:21 PMChaika
05/24/2023, 7:22 PM/
and not /*
(and not using a custom domain)lokiwind
05/24/2023, 7:24 PMChaika
05/24/2023, 7:30 PMjs
export default {
async fetch(request, env, ctx) {
if (request.url.endsWith('/favicon.ico') == false) {
// Get the IP address of the incoming user from the request headers
const ip = request.headers.get("CF-Connecting-IP");
// Generate a 3-digit key
const key = Math.random().toString().slice(2, 5);
// Save the data to the KV namespace using the generated key
await env.LOG_IP.put(`${ip}<${key}`, 8);
}
// Return the original request
return fetch(request);
},
};
(rewritten to module format, don't use service worker format, it's ugly and slower)Chaika
05/24/2023, 7:30 PMlokiwind
05/24/2023, 7:33 PMChaika
05/24/2023, 7:34 PMlokiwind
05/24/2023, 7:35 PMlokiwind
05/24/2023, 7:35 PMlokiwind
05/24/2023, 7:38 PMlokiwind
05/24/2023, 7:54 PMlokiwind
05/24/2023, 7:55 PMlokiwind
05/24/2023, 7:55 PMlokiwind
05/24/2023, 7:56 PMChaika
05/24/2023, 8:04 PMChaika
05/24/2023, 8:05 PMlokiwind
05/24/2023, 8:07 PM