const request = require("superagent"); const ALLOWED_HOSTS = new Set(["fastgpt.anxinyun.cn"]); module.exports.fastgptImage = async (ctx) => { try { const targetUrl = String(ctx.query?.url || "").trim(); if (!targetUrl) { ctx.status = 400; ctx.body = { message: "缺少url参数" }; return; } let parsed; try { parsed = new URL(targetUrl); } catch (e) { ctx.status = 400; ctx.body = { message: "url格式不正确" }; return; } if (!ALLOWED_HOSTS.has(parsed.hostname)) { ctx.status = 400; ctx.body = { message: "非法域名" }; return; } const res = await request.get(targetUrl).responseType("binary"); ctx.status = res.status || 200; if (res.type) { ctx.set("Content-Type", res.type); } ctx.body = res.body; } catch (error) { console.error(error, "fastgpt image proxy error"); ctx.status = 400; ctx.body = { message: "代理失败" }; } };