Spaces:
Paused

afsdzfgdz / functions /api /fetchRes.js
DOOOOOGF's picture
Upload 217 files
3246b69 verified
Raw History Blame Contribute Delete
2.15 kB
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 })
}
// 限制协议只能是 http 或 https
let parsedUrl;
try {
parsedUrl = new URL(targetUrl);
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
return new Response('Forbidden: Only HTTP and HTTPS protocols are allowed', { status: 403 });
}
// 阻止对本地/内网/私有IP的访问以预防内网SSRF
const hostname = parsedUrl.hostname.toLowerCase();
if (
hostname === 'localhost' ||
hostname === '127.0.0.1' ||
hostname.startsWith('192.168.') ||
hostname.startsWith('10.') ||
hostname.startsWith('172.16.') ||
hostname.startsWith('172.17.') ||
hostname.startsWith('172.18.') ||
hostname.startsWith('172.19.') ||
hostname.startsWith('172.2') ||
hostname.startsWith('172.30.') ||
hostname.startsWith('172.31.') ||
hostname === '0.0.0.0'
) {
return new Response('Forbidden: Private IP ranges are not allowed', { status: 403 });
}
} catch (e) {
return new Response('Invalid URL format', { status: 400 });
}
const response = await fetch(targetUrl);
const headers = new Headers(response.headers);
return new Response(response.body, {
headers: headers
})
}