pull/189/head
xiaojunnuo 2024-09-22 00:33:09 +08:00
parent 8b8039f42b
commit 0451fa7573
3 changed files with 27 additions and 13 deletions

View File

@ -17,6 +17,8 @@
"@certd/plus": "1.22.1",
"axios": "^1.7.2",
"fix-path": "^4.0.0",
"http-proxy-agent": "^7.0.2",
"https-proxy-agent": "^7.0.5",
"lodash-es": "^4.17.21",
"node-forge": "^1.3.1",
"nodemailer": "^6.9.3",

View File

@ -1,7 +1,9 @@
import axios, { AxiosRequestConfig } from "axios";
import { logger } from "./util.log.js";
import { Logger } from "log4js";
import { ProxyAgent, ProxyAgentOptions } from "proxy-agent";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import nodeHttp from "http";
export class HttpError extends Error {
status?: number;
statusText?: string;
@ -45,7 +47,7 @@ export function createAxiosService({ logger }: { logger: Logger }) {
// 创建一个 axios 实例
const service = axios.create();
// const defaultAgents = createAgent();
const defaultAgents = createAgent();
// 请求拦截
service.interceptors.request.use(
(config: any) => {
@ -53,14 +55,14 @@ export function createAxiosService({ logger }: { logger: Logger }) {
if (config.timeout == null) {
config.timeout = 15000;
}
// let agents = defaultAgents;
// if (config.skipSslVerify) {
// logger.info("跳过SSL验证");
// agents = createAgent({ rejectUnauthorized: config.rejectUnauthorized });
// }
// delete config.skipSslVerify;
// config.httpsAgent = agents.httpsAgent;
// config.httpAgent = agents.httpAgent;
let agents = defaultAgents;
if (config.skipSslVerify) {
logger.info("跳过SSL验证");
agents = createAgent({ rejectUnauthorized: false } as any);
}
delete config.skipSslVerify;
config.httpsAgent = agents.httpsAgent;
config.httpAgent = agents.httpAgent;
return config;
},
@ -139,10 +141,19 @@ export type HttpClient = {
request<D = any, R = any>(config: HttpRequestConfig<D>): Promise<HttpClientResponse<R>>;
};
export function createAgent(opts: ProxyAgentOptions = {}) {
const httpAgent = new ProxyAgent(opts);
export function createAgent(opts: nodeHttp.AgentOptions = {}) {
let httpAgent, httpsAgent;
const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
if (httpProxy) {
httpAgent = new HttpProxyAgent(httpProxy, opts as any);
}
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy;
if (httpsProxy) {
httpsAgent = new HttpsProxyAgent(httpsProxy, opts as any);
}
return {
httpAgent,
httpsAgent: httpAgent,
httpsAgent,
};
}

View File

@ -13,6 +13,7 @@
"preview": "vite preview"
},
"dependencies": {
"nanoid": "^4.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^23.0.4",