Download functions/api/fetchRes.js from DOOOOOGF/afsdzfgdz: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/spaces/DOOOOOGF/afsdzfgdz/resolve/main/functions/api/fetchRes.js
- Command line
-
hf download hf://spaces/DOOOOOGF/afsdzfgdz/functions/api/fetchRes.js
-
curl -L -o fetchRes.js https://huggingface.co/spaces/DOOOOOGF/afsdzfgdz/resolve/main/functions/api/fetchRes.js
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 | |
| }) | |
| } |