调整了agent

This commit is contained in:
lik
2026-10-28 11:15:29 +08:00
parent b8f53d3660
commit c4f6be6494
29 changed files with 703 additions and 921 deletions
+4 -11
View File
@@ -1,12 +1,10 @@
/**
* Agent 出网 HTTP 客户端:统一承载 SSRF 校验、重定向控制、超时上限与响应体截断。
*
* http_get / http_post 只负责参数整理,真正的安全约束集中在这里,
* 避免两个工具各写一套、修了一个漏掉另一个。
* 目前只有 http_get 使用,安全约束集中在这里,避免工具各自实现一套。
*
* 约束:
* - 每一跳都重新做同步 + DNS 校验,最多 3 跳(重定向可被用来绕过首次校验);
* - POST 不跟随重定向(303/302 会改写成 GET,语义与安全性都不可控);
* - 超时由服务端夹紧到 1s~15s,不接受模型传入的任意大值;
* - 响应体最多读取 2MB,超出即截断并标记 truncated。
*/
@@ -63,17 +61,14 @@ async function readBodyLimited(response) {
}
/**
* 发起受控的 HTTP 请求
* 发起受控的 GET 请求
* @param {string} url 目标地址
* @param {Object} options
* @param {string} [options.method] 请求方法
* @param {Object} [options]
* @param {Object} [options.headers] 请求头
* @param {string} [options.body] 请求体
* @param {number} [options.timeout] 超时毫秒数(会被夹紧)
* @returns {Promise<{status:number,statusText:string,headers:Object,buffer:Buffer,text:string,truncated:boolean,url:string}>}
*/
async function safeFetch(url, options = {}) {
const method = (options.method || "GET").toUpperCase();
const timeout = clampTimeout(options.timeout);
let currentUrl = url;
@@ -87,9 +82,8 @@ async function safeFetch(url, options = {}) {
try {
const response = await fetch(currentUrl, {
method,
method: "GET",
headers: options.headers,
body: options.body,
redirect: "manual",
signal: controller.signal,
});
@@ -98,7 +92,6 @@ async function safeFetch(url, options = {}) {
const location = response.headers.get("location");
await response.body?.cancel().catch(() => {});
if (!location) throw new Error(`Redirect without location: ${response.status}`);
if (method !== "GET") throw new Error("Redirects are not followed for non-GET requests");
currentUrl = new URL(location, currentUrl).toString();
continue;
}