Files
certd/packages/core/basic/src/utils/util.request.ts
T

594 lines
17 KiB
TypeScript
Raw Normal View History

2025-04-22 07:26:11 +08:00
import axios, { AxiosHeaders, AxiosRequestConfig } from "axios";
import { ILogger, logger } from "./util.log.js";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import nodeHttp from "http";
import * as https from "node:https";
import { merge } from "lodash-es";
import { safePromise } from "./util.promise.js";
import fs from "fs";
2026-02-13 19:02:53 +08:00
import sleep from "./util.sleep.js";
2025-09-16 09:31:02 +08:00
const errorMap: Record<string, string> = {
"ssl3_get_record:wrong version number": "http协议错误,服务端要求http协议,请检查是否使用了https请求",
"getaddrinfo EAI_AGAIN": "无法解析域名,请检查网络连接或dns配置,更换docker-compose.yaml中dns配置",
"self-signed certificate": "目标站点为自签名证书,请勾选忽略证书校验",
};
export class HttpError extends Error {
status?: number;
statusText?: string;
2024-09-09 16:01:42 +08:00
code?: string;
request?: { baseURL: string; url: string; method: string; params?: any; data?: any };
2024-12-18 10:22:22 +08:00
response?: { data: any; headers: AxiosHeaders };
2024-09-09 16:01:42 +08:00
cause?: any;
constructor(error: any) {
if (!error) {
return;
}
2024-10-03 01:29:12 +08:00
2026-05-08 22:28:10 +08:00
let message = error?.message || error?.response?.statusText || error?.code;
2025-09-16 09:31:02 +08:00
if (message && typeof message === "string" && message.indexOf) {
for (const key in errorMap) {
if (message.indexOf(key) > -1) {
message = `${message}(${errorMap[key]})`;
2025-09-16 09:31:02 +08:00
break;
}
2024-11-04 16:39:02 +08:00
}
2024-10-03 01:29:12 +08:00
}
if (!message) {
message = error.message;
}
if (error.errors && error.errors.length > 0) {
message += " \n" + error.errors.map((item: any) => item.message).join("\n ");
}
super(message);
2024-10-03 01:29:12 +08:00
this.name = error.name;
2024-09-09 16:01:42 +08:00
this.code = error.code;
2024-11-11 13:44:49 +08:00
this.status = error.response?.status;
2024-11-11 13:43:25 +08:00
this.statusText = error.response?.statusText || error.code;
this.request = {
baseURL: error.config?.baseURL,
2024-09-09 16:01:42 +08:00
url: error.config?.url,
method: error.config?.method,
params: error.config?.params,
data: error.config?.data,
};
let url = error.config?.url;
if (error.config?.baseURL) {
2025-04-22 07:26:11 +08:00
url = (error.config?.baseURL || "") + url;
}
if (url) {
2024-11-12 10:37:01 +08:00
this.message = `${this.message}${url}`;
}
this.response = {
2024-09-09 16:01:42 +08:00
data: error.response?.data,
2024-12-18 10:22:22 +08:00
headers: error.response?.headers,
};
2024-09-09 16:01:42 +08:00
2024-11-12 10:12:10 +08:00
const { stack, cause } = error;
this.cause = cause;
this.stack = stack;
2024-09-09 16:01:42 +08:00
delete error.response;
delete error.config;
delete error.request;
2024-09-19 14:23:15 +08:00
// logger.error(error);
}
}
2024-09-19 14:23:15 +08:00
export const HttpCommonError = HttpError;
2024-10-12 16:49:49 +08:00
let defaultAgents = createAgent();
const directAgents = createAgent();
let defaultProxyOptions: GlobalProxyOptions = {};
let defaultHeaders: Record<string, string> = {};
2024-10-12 16:49:49 +08:00
export type GlobalProxyOptions = {
httpProxy?: string;
httpsProxy?: string;
noProxy?: string;
};
export function setGlobalProxy(opts: GlobalProxyOptions) {
2025-04-22 07:26:11 +08:00
logger.info("setGlobalProxy:", opts);
defaultProxyOptions = { ...opts };
defaultAgents = createAgent({
httpProxy: opts.httpProxy,
httpsProxy: opts.httpsProxy,
});
setProxyEnvironment(opts);
2024-10-12 16:49:49 +08:00
}
export function getGlobalAgents() {
return defaultAgents;
}
export function setGlobalHeaders(headers: Record<string, string> = {}) {
logger.info("setGlobalHeaders:", Object.keys(headers));
defaultHeaders = { ...headers };
}
export function getGlobalHeaders() {
return defaultHeaders;
}
2022-11-07 23:31:20 +08:00
/**
* @description 创建请求实例
*/
2025-09-06 00:29:55 +08:00
export function createAxiosService({ logger }: { logger: ILogger }) {
2022-11-07 23:31:20 +08:00
// 创建一个 axios 实例
const service = axios.create();
2024-09-14 10:28:06 +08:00
2022-11-07 23:31:20 +08:00
// 请求拦截
service.interceptors.request.use(
(config: any) => {
if (config.logParams == null) {
config.logParams = false;
}
if (config.logRes == null) {
config.logRes = false;
}
2025-05-27 00:03:15 +08:00
if (config.logData == null) {
config.logData = false;
}
2026-04-27 00:57:53 +08:00
if (config.logReq == null) {
config.logReq = true;
}
2026-04-27 00:57:53 +08:00
if (config.logReq !== false) {
logger.info(`http request:${config.url}method:${config.method}`);
}
2024-10-26 18:01:06 +08:00
if (config.logParams !== false && config.params) {
2024-10-21 11:15:41 +08:00
logger.info(`params:${JSON.stringify(config.params)}`);
}
2025-05-27 00:03:15 +08:00
if (config.logData !== false && config.data) {
logger.info(`data:${JSON.stringify(config.data)}`);
}
2024-09-10 11:58:58 +08:00
if (config.timeout == null) {
2024-09-10 17:39:41 +08:00
config.timeout = 15000;
2024-09-10 11:58:58 +08:00
}
const bypassProxy = shouldBypassProxy(config, defaultProxyOptions.noProxy);
const useCustomProxy = !!config.httpProxy && !bypassProxy;
let agents = bypassProxy ? directAgents : defaultAgents;
if (bypassProxy) {
logger.info("命中no_proxy配置,跳过代理:", config.url);
}
if (config.skipSslVerify || useCustomProxy) {
const agentOptions: any = {};
2024-12-02 14:06:55 +08:00
if (config.skipSslVerify) {
logger.info("忽略接口请求的SSL校验");
agentOptions.rejectUnauthorized = false;
2024-12-02 14:06:55 +08:00
}
if (useCustomProxy) {
2025-04-22 07:26:11 +08:00
logger.info("使用自定义http代理:", config.httpProxy);
agentOptions.httpProxy = config.httpProxy;
agentOptions.httpsProxy = config.httpProxy;
2024-12-02 14:06:55 +08:00
}
agents = createAgent(agentOptions);
2024-09-22 00:33:09 +08:00
}
2024-12-02 14:06:55 +08:00
2024-09-22 00:33:09 +08:00
delete config.skipSslVerify;
config.httpsAgent = agents.httpsAgent;
config.httpAgent = agents.httpAgent;
2024-10-22 18:46:29 +08:00
if (Object.keys(defaultHeaders).length > 0) {
const headers = AxiosHeaders.from(defaultHeaders);
headers.set(config.headers || {});
config.headers = headers;
}
2024-10-22 18:46:29 +08:00
// const agent = new https.Agent({
// rejectUnauthorized: false // 允许自签名证书
// });
// config.httpsAgent = agent;
config.proxy = false; //必须 否则还会走一层代理,
2026-02-13 19:02:53 +08:00
config.retry = merge(
{
2026-04-27 23:51:27 +08:00
status: [421, 524],
2026-02-13 19:02:53 +08:00
count: 0,
max: 3,
2026-04-27 23:51:27 +08:00
delay: 2000,
includes: ["[524]"],
2026-02-13 19:02:53 +08:00
},
config.retry
);
2022-11-07 23:31:20 +08:00
return config;
},
(error: Error) => {
// 发送失败
2025-04-22 07:26:11 +08:00
logger.error("接口请求失败:", error);
2022-11-07 23:31:20 +08:00
return Promise.reject(error);
}
);
// 响应拦截
service.interceptors.response.use(
(response: any) => {
2024-10-21 11:15:41 +08:00
if (response?.config?.logRes !== false) {
2024-11-04 16:39:02 +08:00
let resData = response?.data;
try {
resData = JSON.stringify(response?.data);
} catch (e) {}
logger.info(`http response : status=${response?.status},data=${resData}`);
2024-10-21 11:15:41 +08:00
} else {
2025-04-22 07:26:11 +08:00
logger.info("http response status:", response?.status);
2024-10-21 11:15:41 +08:00
}
2025-05-11 10:22:10 +08:00
2025-05-10 17:29:10 +08:00
if (response?.config?.returnOriginRes) {
2024-12-02 18:15:27 +08:00
return response;
}
2022-11-07 23:31:20 +08:00
return response.data;
},
2026-02-13 19:02:53 +08:00
async (error: any) => {
2024-09-10 17:39:41 +08:00
const status = error.response?.status;
2026-01-29 23:39:57 +08:00
let message = "";
2024-09-10 17:39:41 +08:00
switch (status) {
case 400:
2026-01-29 23:39:57 +08:00
message = "请求错误";
2024-09-10 17:39:41 +08:00
break;
case 401:
2026-01-29 23:39:57 +08:00
message = "认证/登录失败";
2024-09-10 17:39:41 +08:00
break;
case 403:
2026-01-29 23:39:57 +08:00
message = "拒绝访问";
2024-09-10 17:39:41 +08:00
break;
case 404:
2026-01-29 23:39:57 +08:00
message = `请求地址出错`;
2024-09-10 17:39:41 +08:00
break;
case 408:
2026-01-29 23:39:57 +08:00
message = "请求超时";
2024-09-10 17:39:41 +08:00
break;
case 500:
2026-01-29 23:39:57 +08:00
message = "服务器内部错误";
2024-09-10 17:39:41 +08:00
break;
case 501:
2026-01-29 23:39:57 +08:00
message = "服务未实现";
2024-09-10 17:39:41 +08:00
break;
case 502:
2026-01-29 23:39:57 +08:00
message = "网关错误";
2024-09-10 17:39:41 +08:00
break;
case 503:
2026-01-29 23:39:57 +08:00
message = "服务不可用";
2024-09-10 17:39:41 +08:00
break;
case 504:
2026-01-29 23:39:57 +08:00
message = "网关超时";
2024-09-10 17:39:41 +08:00
break;
case 505:
2026-01-29 23:39:57 +08:00
message = "HTTP版本不受支持";
2024-09-10 17:39:41 +08:00
break;
2025-09-24 00:55:31 +08:00
case 302:
//重定向
return Promise.resolve(error.response);
2026-02-13 19:02:53 +08:00
case 421:
message = "源站请求超时";
break;
2024-09-10 17:39:41 +08:00
default:
break;
}
2026-01-29 23:39:57 +08:00
if (status) {
message += ` [${status}] `;
}
2025-09-24 00:55:31 +08:00
2025-09-24 01:40:11 +08:00
const errorCode = error.code;
2026-01-29 23:39:57 +08:00
let errorMessage = "";
2025-09-24 01:40:11 +08:00
if (errorCode === "ECONNABORTED") {
errorMessage = "请求连接终止";
} else if (errorCode === "ETIMEDOUT") {
errorMessage = "请求连接超时";
} else if (errorCode === "ECONNRESET") {
errorMessage = "请求连接被重置";
} else if (errorCode === "ECONNREFUSED") {
errorMessage = "请求连接被服务端拒绝";
} else if (errorCode === "ENOTFOUND") {
errorMessage = "请求地址不存在";
}
2026-01-29 23:39:57 +08:00
if (errorCode) {
errorMessage += ` [${errorCode}] `;
2025-09-24 01:40:11 +08:00
}
2026-01-29 23:39:57 +08:00
if (message) {
errorMessage += `,${message}`;
}
if (error.message) {
errorMessage += `(${error.message})`;
}
error.message = errorMessage;
logger.error(`请求出错:${errorMessage} status:${status},statusText:${error.response?.statusText || error.code},url:${error.config?.url},method:${error.config?.method}`);
2025-04-22 07:26:11 +08:00
logger.error("返回数据:", JSON.stringify(error.response?.data));
2024-09-24 11:11:08 +08:00
if (error.response?.data) {
2026-05-08 22:28:10 +08:00
const message = error.response?.data?.message || error.response?.data?.msg || error.response?.data?.error;
2025-04-22 07:26:11 +08:00
if (typeof message === "string") {
error.message = message;
}
2024-09-24 11:11:08 +08:00
}
2024-09-09 10:17:40 +08:00
if (error instanceof AggregateError) {
2025-04-22 07:26:11 +08:00
logger.error("AggregateError", error);
2024-09-09 10:17:40 +08:00
}
2026-02-13 19:02:53 +08:00
const originalRequest = error.config || {};
// logger.info(`config`, originalRequest);
2026-02-13 19:02:53 +08:00
const retry = originalRequest.retry || {};
2026-04-27 23:51:27 +08:00
const isRetryStatus = retry.status && retry.status.includes(status);
let isRetryMessage = false;
if (retry.includes) {
for (const item of retry.includes) {
if (error.message?.includes(item)) {
isRetryMessage = true;
break;
}
}
}
if (isRetryStatus || isRetryMessage) {
2026-02-13 19:02:53 +08:00
if (retry.max > 0 && retry.count < retry.max) {
// 重试次数增加
retry.count++;
const delay = retry.delay * retry.count;
logger.error(`status=${status},重试次数${retry.count},将在${delay}ms后重试,请求地址:${originalRequest.url}`);
await sleep(delay);
return service.request(originalRequest); // 重试请求
}
logger.error(`重试超过最大次数${retry.max},请求失败:${originalRequest.url}`);
}
const err = new HttpError(error);
2025-05-20 00:27:18 +08:00
if (error.response?.config?.logParams === false) {
delete err.request?.params;
delete err.request?.data;
}
return Promise.reject(err);
2022-11-07 23:31:20 +08:00
}
);
return service;
}
2024-09-09 16:01:42 +08:00
export const http = createAxiosService({ logger }) as HttpClient;
export type HttpClientResponse<R> = any;
2024-10-26 18:01:06 +08:00
export type HttpRequestConfig<D = any> = {
2024-09-14 10:28:06 +08:00
skipSslVerify?: boolean;
2024-09-30 18:00:51 +08:00
skipCheckRes?: boolean;
2026-04-27 00:57:53 +08:00
logReq?: boolean;
2024-10-21 11:15:41 +08:00
logParams?: boolean;
logRes?: boolean;
2025-05-27 00:03:15 +08:00
logData?: boolean;
2024-12-02 14:06:55 +08:00
httpProxy?: string;
2025-05-10 17:29:10 +08:00
returnOriginRes?: boolean;
2024-09-14 10:28:06 +08:00
} & AxiosRequestConfig<D>;
2024-09-09 16:01:42 +08:00
export type HttpClient = {
2024-09-14 10:28:06 +08:00
request<D = any, R = any>(config: HttpRequestConfig<D>): Promise<HttpClientResponse<R>>;
2024-09-09 16:01:42 +08:00
};
2024-09-14 10:28:06 +08:00
// const http_proxy_backup = process.env.HTTP_PROXY || process.env.http_proxy;
// const https_proxy_backup = process.env.HTTPS_PROXY || process.env.https_proxy;
export type CreateAgentOptions = {
httpProxy?: string;
httpsProxy?: string;
} & nodeHttp.AgentOptions;
export function createAgent(opts: CreateAgentOptions = {}) {
const { httpProxy, httpsProxy, ...agentOptions } = merge(
{
autoSelectFamily: true,
2024-11-13 23:51:34 +08:00
autoSelectFamilyAttemptTimeout: 1000,
connectTimeout: 5000, // 连接建立超时
},
opts
);
2024-09-22 00:33:09 +08:00
let httpAgent, httpsAgent;
if (httpProxy) {
2025-04-22 07:26:11 +08:00
logger.info("use httpProxy:", httpProxy);
httpAgent = new HttpProxyAgent(httpProxy, agentOptions as any);
merge(httpAgent.options, agentOptions);
} else {
httpAgent = new nodeHttp.Agent(agentOptions);
2024-09-22 00:33:09 +08:00
}
if (httpsProxy) {
2025-04-22 07:26:11 +08:00
logger.info("use httpsProxy:", httpsProxy);
httpsAgent = new HttpsProxyAgent(httpsProxy, agentOptions as any);
merge(httpsAgent.options, agentOptions);
} else {
httpsAgent = new https.Agent(agentOptions);
2024-09-22 00:33:09 +08:00
}
2024-09-14 10:28:06 +08:00
return {
httpAgent,
2024-09-22 00:33:09 +08:00
httpsAgent,
2024-09-14 10:28:06 +08:00
};
}
2024-11-04 15:14:56 +08:00
function setProxyEnvironment(opts: GlobalProxyOptions = {}) {
setEnvValue("HTTP_PROXY", opts.httpProxy);
setEnvValue("http_proxy", opts.httpProxy);
setEnvValue("HTTPS_PROXY", opts.httpsProxy);
setEnvValue("https_proxy", opts.httpsProxy);
const noProxy = normalizeNoProxyText(opts.noProxy);
setEnvValue("NO_PROXY", noProxy);
setEnvValue("no_proxy", noProxy);
}
function setEnvValue(key: string, value?: string) {
process.env[key] = value || "";
}
function shouldBypassProxy(config: AxiosRequestConfig, noProxy?: string) {
if (!noProxy) {
return false;
}
const target = getRequestTarget(config);
if (!target) {
return false;
}
return splitNoProxyRules(noProxy).some(item => isNoProxyMatched(item, target));
}
function getRequestTarget(config: AxiosRequestConfig) {
try {
const baseURL = config.baseURL || undefined;
const url = new URL(config.url || "", baseURL);
return {
hostname: normalizeHost(url.hostname),
port: url.port,
};
} catch (e) {
return null;
}
}
export function isNoProxyMatched(rule: string, target: { hostname: string; port: string }) {
if (rule === "*") {
return true;
}
const normalizedRule = normalizeNoProxyRule(rule);
if (!normalizedRule.host) {
return false;
}
if (normalizedRule.port && normalizedRule.port !== target.port) {
return false;
}
const host = normalizeHost(target.hostname);
if (normalizedRule.host.includes("*")) {
return wildcardHostMatched(normalizedRule.host, host);
}
if (normalizedRule.host.startsWith("*.")) {
const suffix = normalizedRule.host.substring(1);
return host.endsWith(suffix);
}
if (normalizedRule.host.startsWith(".")) {
return host === normalizedRule.host.substring(1) || host.endsWith(normalizedRule.host);
}
return host === normalizedRule.host || host.endsWith(`.${normalizedRule.host}`);
}
function normalizeNoProxyRule(rule: string) {
let value = rule.trim().toLowerCase();
if (value.includes("://")) {
try {
const url = new URL(value);
return {
host: normalizeHost(url.hostname),
port: url.port,
};
} catch (e) {
return {
host: "",
port: "",
};
}
}
let port = "";
if (value.startsWith("[")) {
const closeIndex = value.indexOf("]");
const host = value.substring(1, closeIndex);
const rest = value.substring(closeIndex + 1);
if (rest.startsWith(":")) {
port = rest.substring(1);
}
return {
host: normalizeHost(host),
port,
};
}
const colonCount = (value.match(/:/g) || []).length;
const portIndex = value.lastIndexOf(":");
if (colonCount === 1 && portIndex > -1) {
port = value.substring(portIndex + 1);
value = value.substring(0, portIndex);
}
return {
host: normalizeHost(value),
port,
};
}
function normalizeHost(host: string) {
let value = host.trim().toLowerCase();
if (value.startsWith("[") && value.endsWith("]")) {
value = value.substring(1, value.length - 1);
}
return value;
}
function wildcardHostMatched(rule: string, host: string) {
const pattern = rule.split("*").map(escapeRegExp).join(".*");
return new RegExp(`^${pattern}$`).test(host);
}
function escapeRegExp(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function normalizeNoProxyText(noProxy?: string) {
return splitNoProxyRules(noProxy).join(",");
}
function splitNoProxyRules(noProxy?: string) {
if (!noProxy) {
return [];
}
return noProxy
.split(/[,\s]+/)
.map(item => item.trim())
.filter(Boolean);
}
2024-11-04 16:39:02 +08:00
export async function download(req: { http: HttpClient; config: HttpRequestConfig; savePath: string; logger: ILogger }) {
const { http, config, savePath, logger } = req;
2024-11-04 15:14:56 +08:00
return safePromise((resolve, reject) => {
http
.request({
2024-11-04 16:39:02 +08:00
logRes: false,
2025-04-22 07:26:11 +08:00
responseType: "stream",
2024-11-04 16:39:02 +08:00
...config,
2024-11-04 15:14:56 +08:00
})
.then(res => {
const writer = fs.createWriteStream(savePath);
2024-11-04 16:39:02 +08:00
res.pipe(writer);
2025-04-22 07:26:11 +08:00
writer.on("close", () => {
logger.info("文件下载成功");
2024-11-04 15:14:56 +08:00
resolve(true);
});
//error
2025-04-22 07:26:11 +08:00
writer.on("error", err => {
logger.error("下载失败", err);
2024-11-04 15:14:56 +08:00
reject(err);
});
//进度条打印
2025-04-22 07:26:11 +08:00
const totalLength = res.headers["content-length"];
2024-11-04 15:14:56 +08:00
let currentLength = 0;
2024-11-04 16:39:02 +08:00
// 每5%打印一次
const step = (totalLength / 100) * 5;
2025-04-22 07:26:11 +08:00
res.on("data", (chunk: any) => {
2024-11-04 15:14:56 +08:00
currentLength += chunk.length;
2024-11-04 16:39:02 +08:00
if (currentLength % step < chunk.length) {
const percent = ((currentLength / totalLength) * 100).toFixed(2);
logger.info(`下载进度:${percent}%`);
}
2024-11-04 15:14:56 +08:00
});
})
.catch(err => {
2025-04-22 07:26:11 +08:00
logger.info("下载失败", err);
2024-11-04 15:14:56 +08:00
reject(err);
});
});
}
2025-03-09 01:08:57 +08:00
export function getCookie(response: any, name: string) {
2025-04-22 07:26:11 +08:00
const cookies = response.headers["set-cookie"];
2025-03-09 01:08:57 +08:00
//根据name 返回对应的cookie
const found = cookies.find((cookie: any) => cookie.includes(name));
if (!found) {
return null;
}
2025-04-22 07:26:11 +08:00
const cookie = found.split(";")[0];
return cookie.substring(cookie.indexOf("=") + 1);
2025-03-09 01:08:57 +08:00
}