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

352 lines
10 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";
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-31 10:32:05 +08:00
super(error.message || error.response?.statusText);
2024-10-03 01:29:12 +08:00
2024-11-04 16:39:02 +08:00
const message = error?.message;
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) {
this.message = `${this.message}(${errorMap[key]})`;
break;
}
2024-11-04 16:39:02 +08:00
}
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;
2024-11-11 13:46:06 +08:00
if (!this.message) {
this.message = 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();
export function setGlobalProxy(opts: { httpProxy?: string; httpsProxy?: string }) {
2025-04-22 07:26:11 +08:00
logger.info("setGlobalProxy:", opts);
defaultAgents = createAgent(opts);
2024-10-12 16:49:49 +08:00
}
export function getGlobalAgents() {
return defaultAgents;
}
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;
}
2024-10-21 11:15:41 +08:00
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)}`);
}
if (config.timeout == null) {
2024-09-10 17:39:41 +08:00
config.timeout = 15000;
}
2024-09-22 00:33:09 +08:00
let agents = defaultAgents;
2024-12-02 14:06:55 +08:00
if (config.skipSslVerify || config.httpProxy) {
let rejectUnauthorized = true;
if (config.skipSslVerify) {
2025-04-22 07:26:11 +08:00
logger.info("跳过SSL验证");
2024-12-02 14:06:55 +08:00
rejectUnauthorized = false;
}
const proxy: any = {};
if (config.httpProxy) {
2025-04-22 07:26:11 +08:00
logger.info("使用自定义http代理:", config.httpProxy);
2024-12-02 14:06:55 +08:00
proxy.httpProxy = config.httpProxy;
proxy.httpsProxy = config.httpProxy;
}
agents = createAgent({ rejectUnauthorized, ...proxy } as any);
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
// const agent = new https.Agent({
// rejectUnauthorized: false // 允许自签名证书
// });
// config.httpsAgent = agent;
config.proxy = false; //必须 否则还会走一层代理,
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;
},
(error: any) => {
2024-09-10 17:39:41 +08:00
const status = error.response?.status;
switch (status) {
case 400:
2025-04-22 07:26:11 +08:00
error.message = "请求错误";
2024-09-10 17:39:41 +08:00
break;
case 401:
2025-04-22 07:26:11 +08:00
error.message = "认证/登录失败";
2024-09-10 17:39:41 +08:00
break;
case 403:
2025-04-22 07:26:11 +08:00
error.message = "拒绝访问";
2024-09-10 17:39:41 +08:00
break;
case 404:
2024-12-18 00:38:27 +08:00
error.message = `请求地址出错`;
2024-09-10 17:39:41 +08:00
break;
case 408:
2025-04-22 07:26:11 +08:00
error.message = "请求超时";
2024-09-10 17:39:41 +08:00
break;
case 500:
2025-04-22 07:26:11 +08:00
error.message = "服务器内部错误";
2024-09-10 17:39:41 +08:00
break;
case 501:
2025-04-22 07:26:11 +08:00
error.message = "服务未实现";
2024-09-10 17:39:41 +08:00
break;
case 502:
2025-04-22 07:26:11 +08:00
error.message = "网关错误";
2024-09-10 17:39:41 +08:00
break;
case 503:
2025-04-22 07:26:11 +08:00
error.message = "服务不可用";
2024-09-10 17:39:41 +08:00
break;
case 504:
2025-04-22 07:26:11 +08:00
error.message = "网关超时";
2024-09-10 17:39:41 +08:00
break;
case 505:
2025-04-22 07:26:11 +08:00
error.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);
2024-09-10 17:39:41 +08:00
default:
break;
}
2025-09-24 00:55:31 +08:00
2025-04-22 07:26:11 +08:00
logger.error(`请求出错status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}`);
logger.error("返回数据:", JSON.stringify(error.response?.data));
2024-09-24 11:11:08 +08:00
if (error.response?.data) {
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
}
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;
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 = {}) {
opts = merge(
{
autoSelectFamily: true,
2024-11-13 23:51:34 +08:00
autoSelectFamilyAttemptTimeout: 1000,
},
opts
);
2024-09-22 00:33:09 +08:00
let httpAgent, httpsAgent;
const httpProxy = opts.httpProxy;
2024-09-22 00:33:09 +08:00
if (httpProxy) {
process.env.HTTP_PROXY = httpProxy;
process.env.http_proxy = httpProxy;
2025-04-22 07:26:11 +08:00
logger.info("use httpProxy:", httpProxy);
2024-09-22 00:33:09 +08:00
httpAgent = new HttpProxyAgent(httpProxy, opts as any);
2024-10-22 18:46:29 +08:00
merge(httpAgent.options, opts);
} else {
2025-04-22 07:26:11 +08:00
process.env.HTTP_PROXY = "";
process.env.http_proxy = "";
httpAgent = new nodeHttp.Agent(opts);
2024-09-22 00:33:09 +08:00
}
const httpsProxy = opts.httpsProxy;
2024-09-22 00:33:09 +08:00
if (httpsProxy) {
process.env.HTTPS_PROXY = httpsProxy;
process.env.https_proxy = httpsProxy;
2025-04-22 07:26:11 +08:00
logger.info("use httpsProxy:", httpsProxy);
2024-09-22 00:33:09 +08:00
httpsAgent = new HttpsProxyAgent(httpsProxy, opts as any);
2024-10-22 18:46:29 +08:00
merge(httpsAgent.options, opts);
} else {
2025-04-22 07:26:11 +08:00
process.env.HTTPS_PROXY = "";
process.env.https_proxy = "";
httpsAgent = new https.Agent(opts);
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
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
}