mirror of
https://github.com/ZSCGR/CloudFlare-ImgBed.git
synced 2026-08-21 16:13:44 +08:00
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
/**
|
|
* 远程资源代理读取 API
|
|
* 负责在鉴权后拉取请求体中指定 URL 的资源并透传响应内容
|
|
*/
|
|
import { dualAuthCheck } from '../utils/auth/dualAuth.js';
|
|
|
|
export async function onRequest(context) {
|
|
// 获取请求体中URL的内容
|
|
const {
|
|
request,
|
|
env,
|
|
params,
|
|
waitUntil,
|
|
next,
|
|
data
|
|
} = context;
|
|
|
|
// 双重鉴权检查
|
|
const url = new URL(request.url);
|
|
const { authorized } = await dualAuthCheck(env, url, request);
|
|
if (!authorized) {
|
|
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
|
status: 401,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
|
|
const jsonRequest = await request.json();
|
|
const targetUrl = jsonRequest.url;
|
|
if (targetUrl === undefined) {
|
|
return new Response('URL is required', { status: 400 })
|
|
}
|
|
const response = await fetch(targetUrl);
|
|
const headers = new Headers(response.headers);
|
|
return new Response(response.body, {
|
|
headers: headers
|
|
})
|
|
}
|