mirror of
https://github.com/ZSCGR/CloudFlare-ImgBed.git
synced 2026-08-20 23:53:43 +08:00
55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
import { errorHandling, telemetryData } from "./utils/middleware";
|
|
|
|
function UnauthorizedException(reason) {
|
|
return new Response(reason, {
|
|
status: 401,
|
|
statusText: "Unauthorized",
|
|
headers: {
|
|
"Content-Type": "text/plain;charset=UTF-8",
|
|
// Disables caching by default.
|
|
"Cache-Control": "no-store",
|
|
// Returns the "Content-Length" header for HTTP HEAD requests.
|
|
"Content-Length": reason.length,
|
|
},
|
|
});
|
|
}
|
|
|
|
function isValidAuthCode(envAuthCode, authCode) {
|
|
return authCode === envAuthCode;
|
|
}
|
|
|
|
function isAuthCodeDefined(authCode) {
|
|
return authCode !== undefined && authCode !== null && authCode.trim() !== '';
|
|
}
|
|
|
|
function isDomainDefined(domain) {
|
|
return domain !== undefined && domain !== null && domain.trim() !== '';
|
|
}
|
|
|
|
function isUploadPageRequest(referer) {
|
|
if (referer && isDomainDefined(env.DOMAIN)) {
|
|
let refererUrl = new URL(referer);
|
|
return refererUrl.hostname === env.DOMAIN;
|
|
}
|
|
return false;
|
|
}
|
|
export async function onRequestPost(context) { // Contents of context object
|
|
const { request, env, params, waitUntil, next, data } = context;
|
|
const referer = request.headers.get('Referer');
|
|
// const authCode = new URLSearchParams(new URL(referer).search).get('authcode');
|
|
const authCode = new URL(request.url).searchParams.get('authcode');
|
|
const clonedRequest = request.clone();
|
|
if (isUploadPageRequest(referer) || (isAuthCodeDefined(env.AUTH_CODE) && !isValidAuthCode(env.AUTH_CODE, authCode))) {
|
|
return new UnauthorizedException("error");
|
|
}
|
|
await errorHandling(context);
|
|
telemetryData(context);
|
|
const targetUrl = new URL('/upload' + new URL(request.url).search, 'https://telegra.ph');
|
|
const response = await fetch(targetUrl.href, {
|
|
method: clonedRequest.method,
|
|
headers: clonedRequest.headers,
|
|
body: clonedRequest.body,
|
|
});
|
|
return response;
|
|
}
|