- main
- manifest
- deps
- main
- Install
- Run
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
async function handleRequest(event) {
const req = event.request;
const url = new URL(req.url);
const params = url.searchParams;
const logger = fastly.getLogger("logtest");
let fastlyClientIP = event.client.address;
logger.log("Client IP is " + fastlyClientIP);
const banTable = new ConfigStore("ban_dict");
if (banTable.get(fastlyClientIP)) {
const banTime = banTable.get(fastlyClientIP);
const currentTime = Math.floor(Date.now() / 1000);
if (banTime > currentTime) {
logger.log("Ban in effect for " + fastlyClientIP);
return new Response(
"The IP address " + fastlyClientIP + " is banned until " + banTime,
{ status: 403 }
);
}
logger.log(
"Ban for " +
fastlyClientIP +
" expired at " +
banTime +
", allowing access"
);
}
logger.log(fastlyClientIP + " is not banned.");
const backendResponse = fetch(req, {
backend: "origin_0"
});
return backendResponse;
}