exportasyncfunctiononRequest(context) { // Contents of context object const { request, // same as existing Worker API env, // same as existing Worker API params, // if filename includes [id] or [[path]] waitUntil, // same as ctx.waitUntil in existing Worker API next, // used for middleware or to fetch assets data, // arbitrary space for passing data between middlewares } = context;
const url = newURL(request.url); letReferer = request.headers.get('Referer') if (Referer) { try { let refererUrl = newURL(Referer) let allowedDomains = env.ALLOWED_DOMAINS.split(','); let isAllowed = allowedDomains.some(domain => { let domainPattern = newRegExp(`(^|\\.)${domain}$`); return domainPattern.test(refererUrl.hostname); }); if (!isAllowed) { returnResponse.redirect(url.origin + "/block-img.html", 302); } } catch (e) { returnResponse.redirect(url.origin + "/block-img.html", 302); } } const response = fetch('https://telegra.ph/' + url.pathname + url.search, { method: request.method, headers: request.headers, body: request.body, }).then(async (response) => { console.log(response.ok); // true if the response status is 2xx console.log(response.status); // 200 if (response.ok) { // Referer header equal to the admin page console.log(url.origin + "/admin") if (request.headers.get('Referer') == url.origin + "/admin") { //show the image return response; }
if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == "") { } else { //check the record from kv const record = await env.img_url.getWithMetadata(params.id); console.log("record") console.log(record) if (record.metadata === null) {
} else {
//if the record is not null, redirect to the image if (record.metadata.ListType == "White") { return response; } elseif (record.metadata.ListType == "Block") { console.log("Referer") console.log(request.headers.get('Referer')) if (typeof request.headers.get('Referer') == "undefined" || request.headers.get('Referer') == null || request.headers.get('Referer') == "") { returnResponse.redirect(url.origin + "/block-img.html", 302) } else { returnResponse.redirect("https://static-res.pages.dev/teleimage/img-block-compressed.png", 302) }
} elseif (record.metadata.Label == "adult") { if (typeof request.headers.get('Referer') == "undefined" || request.headers.get('Referer') == null || request.headers.get('Referer') == "") { returnResponse.redirect(url.origin + "/block-img.html", 302) } else { returnResponse.redirect("https://static-res.pages.dev/teleimage/img-block-compressed.png", 302) } } //check if the env variables WhiteList_Mode are set console.log("env.WhiteList_Mode:", env.WhiteList_Mode) if (env.WhiteList_Mode == "true") { //if the env variables WhiteList_Mode are set, redirect to the image returnResponse.redirect(url.origin + "/whitelist-on.html", 302); } else { //if the env variables WhiteList_Mode are not set, redirect to the image return response; } }