2024-12-18 10:22:22 +08:00
|
|
|
|
import axios, { AxiosHeaders, AxiosRequestConfig } from 'axios';
|
2024-11-04 16:39:02 +08:00
|
|
|
|
import { ILogger, logger } from './util.log.js';
|
2024-10-08 19:02:51 +08:00
|
|
|
|
import { Logger } from 'log4js';
|
|
|
|
|
|
import { HttpProxyAgent } from 'http-proxy-agent';
|
|
|
|
|
|
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
|
|
|
|
import nodeHttp from 'http';
|
|
|
|
|
|
import * as https from 'node:https';
|
2024-10-22 18:46:29 +08:00
|
|
|
|
import { merge } from 'lodash-es';
|
2024-11-04 16:39:02 +08:00
|
|
|
|
import { safePromise } from './util.promise.js';
|
2024-11-04 15:14:56 +08:00
|
|
|
|
import fs from 'fs';
|
2024-08-30 18:50:53 +08:00
|
|
|
|
export class HttpError extends Error {
|
|
|
|
|
|
status?: number;
|
|
|
|
|
|
statusText?: string;
|
2024-09-09 16:01:42 +08:00
|
|
|
|
code?: string;
|
2024-09-30 00:22:50 +08:00
|
|
|
|
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;
|
2024-08-30 18:50:53 +08:00
|
|
|
|
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;
|
|
|
|
|
|
if (message && typeof message === 'string') {
|
|
|
|
|
|
if (message.indexOf && message.indexOf('ssl3_get_record:wrong version number') >= 0) {
|
|
|
|
|
|
this.message = `${message}(http协议错误,服务端要求http协议,请检查是否使用了https请求)`;
|
2024-11-06 01:17:36 +08:00
|
|
|
|
} else if (message.indexOf('getaddrinfo EAI_AGAIN') >= 0) {
|
2024-11-04 16:39:02 +08:00
|
|
|
|
this.message = `${message}(无法解析域名,请检查网络连接或dns配置,更换docker-compose.yaml中dns配置)`;
|
|
|
|
|
|
}
|
2024-10-03 01:29:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-30 18:50:53 +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;
|
|
|
|
|
|
}
|
2024-08-30 18:50:53 +08:00
|
|
|
|
this.request = {
|
2024-09-30 00:22:50 +08:00
|
|
|
|
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,
|
2024-08-30 18:50:53 +08:00
|
|
|
|
};
|
2024-10-22 01:01:04 +08:00
|
|
|
|
let url = error.config?.url;
|
|
|
|
|
|
if (error.config?.baseURL) {
|
2024-10-31 10:32:05 +08:00
|
|
|
|
url = (error.config?.baseURL || '') + url;
|
2024-10-22 01:01:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (url) {
|
2024-11-12 10:37:01 +08:00
|
|
|
|
this.message = `${this.message} 【${url}】`;
|
2024-10-22 01:01:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-30 18:50:53 +08:00
|
|
|
|
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-08-30 18:50:53 +08:00
|
|
|
|
};
|
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-08-30 18:50:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
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 }) {
|
|
|
|
|
|
logger.info('setGlobalProxy:', opts);
|
2024-10-26 23:24:26 +08:00
|
|
|
|
defaultAgents = createAgent(opts);
|
2024-10-12 16:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 01:01:04 +08:00
|
|
|
|
export function getGlobalAgents() {
|
|
|
|
|
|
return defaultAgents;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-07 23:31:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 创建请求实例
|
|
|
|
|
|
*/
|
2024-06-15 02:17:34 +08:00
|
|
|
|
export function createAxiosService({ logger }: { logger: Logger }) {
|
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) => {
|
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)}`);
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|
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) {
|
|
|
|
|
|
logger.info('跳过SSL验证');
|
|
|
|
|
|
rejectUnauthorized = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
const proxy: any = {};
|
|
|
|
|
|
if (config.httpProxy) {
|
|
|
|
|
|
logger.info('使用自定义http代理:', config.httpProxy);
|
|
|
|
|
|
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;
|
2024-10-03 22:03:20 +08:00
|
|
|
|
config.proxy = false; //必须 否则还会走一层代理,
|
2022-11-07 23:31:20 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: Error) => {
|
|
|
|
|
|
// 发送失败
|
2024-10-08 19:02:51 +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 {
|
|
|
|
|
|
logger.info('http response status:', response?.status);
|
|
|
|
|
|
}
|
2024-12-02 18:15:27 +08:00
|
|
|
|
if (response?.config?.returnResponse) {
|
|
|
|
|
|
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:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '请求错误';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 401:
|
2024-12-18 00:38:27 +08:00
|
|
|
|
error.message = '认证/登录失败';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 403:
|
2024-10-08 19:02:51 +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:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '请求超时';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '服务器内部错误';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 501:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '服务未实现';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 502:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '网关错误';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 503:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '服务不可用';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 504:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = '网关超时';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 505:
|
2024-10-08 19:02:51 +08:00
|
|
|
|
error.message = 'HTTP版本不受支持';
|
2024-09-10 17:39:41 +08:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2024-08-30 18:50:53 +08:00
|
|
|
|
logger.error(
|
2024-09-09 16:01:42 +08:00
|
|
|
|
`请求出错:status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}。`
|
2024-08-30 18:50:53 +08:00
|
|
|
|
);
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.error('返回数据:', JSON.stringify(error.response?.data));
|
2024-09-24 11:11:08 +08:00
|
|
|
|
if (error.response?.data) {
|
2024-11-17 02:01:44 +08:00
|
|
|
|
const message = error.response.data.message || error.response.data.msg || error.response.data.error;
|
|
|
|
|
|
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) {
|
2024-10-08 19:02:51 +08:00
|
|
|
|
logger.error('AggregateError', error);
|
2024-09-09 10:17:40 +08:00
|
|
|
|
}
|
2024-08-30 18:50:53 +08:00
|
|
|
|
const err = new HttpError(error);
|
|
|
|
|
|
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;
|
2024-12-02 14:06:55 +08:00
|
|
|
|
httpProxy?: string;
|
2024-12-18 00:38:27 +08:00
|
|
|
|
returnResponse?: 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
|
|
|
|
|
2024-11-15 23:52:18 +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;
|
|
|
|
|
|
|
2024-10-26 23:24:26 +08:00
|
|
|
|
export type CreateAgentOptions = {
|
|
|
|
|
|
httpProxy?: string;
|
|
|
|
|
|
httpsProxy?: string;
|
|
|
|
|
|
} & nodeHttp.AgentOptions;
|
|
|
|
|
|
export function createAgent(opts: CreateAgentOptions = {}) {
|
2024-11-12 11:14:48 +08:00
|
|
|
|
opts = merge(
|
|
|
|
|
|
{
|
|
|
|
|
|
autoSelectFamily: true,
|
2024-11-13 23:51:34 +08:00
|
|
|
|
autoSelectFamilyAttemptTimeout: 1000,
|
2024-11-12 11:14:48 +08:00
|
|
|
|
},
|
|
|
|
|
|
opts
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2024-09-22 00:33:09 +08:00
|
|
|
|
let httpAgent, httpsAgent;
|
2024-11-15 23:52:18 +08:00
|
|
|
|
const httpProxy = opts.httpProxy;
|
2024-09-22 00:33:09 +08:00
|
|
|
|
if (httpProxy) {
|
2024-11-15 23:52:18 +08:00
|
|
|
|
process.env.HTTP_PROXY = httpProxy;
|
|
|
|
|
|
process.env.http_proxy = httpProxy;
|
2024-10-08 19:02:51 +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);
|
2024-10-03 22:03:20 +08:00
|
|
|
|
} else {
|
2024-11-15 23:52:18 +08:00
|
|
|
|
process.env.HTTP_PROXY = '';
|
|
|
|
|
|
process.env.http_proxy = '';
|
2024-10-03 22:03:20 +08:00
|
|
|
|
httpAgent = new nodeHttp.Agent(opts);
|
2024-09-22 00:33:09 +08:00
|
|
|
|
}
|
2024-11-15 23:52:18 +08:00
|
|
|
|
const httpsProxy = opts.httpsProxy;
|
2024-09-22 00:33:09 +08:00
|
|
|
|
if (httpsProxy) {
|
2024-11-27 12:36:28 +08:00
|
|
|
|
process.env.HTTPS_PROXY = httpsProxy;
|
|
|
|
|
|
process.env.https_proxy = httpsProxy;
|
2024-10-08 19:02:51 +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);
|
2024-10-03 22:03:20 +08:00
|
|
|
|
} else {
|
2024-11-15 23:52:18 +08:00
|
|
|
|
process.env.HTTPS_PROXY = '';
|
|
|
|
|
|
process.env.https_proxy = '';
|
2024-10-03 22:03:20 +08:00
|
|
|
|
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,
|
2024-11-04 15:14:56 +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);
|
2024-11-04 15:14:56 +08:00
|
|
|
|
writer.on('close', () => {
|
2024-11-04 16:39:02 +08:00
|
|
|
|
logger.info('文件下载成功');
|
2024-11-04 15:14:56 +08:00
|
|
|
|
resolve(true);
|
|
|
|
|
|
});
|
|
|
|
|
|
//error
|
|
|
|
|
|
writer.on('error', err => {
|
2024-11-04 16:39:02 +08:00
|
|
|
|
logger.error('下载失败', err);
|
2024-11-04 15:14:56 +08:00
|
|
|
|
reject(err);
|
|
|
|
|
|
});
|
|
|
|
|
|
//进度条打印
|
|
|
|
|
|
const totalLength = res.headers['content-length'];
|
|
|
|
|
|
let currentLength = 0;
|
2024-11-04 16:39:02 +08:00
|
|
|
|
// 每5%打印一次
|
|
|
|
|
|
const step = (totalLength / 100) * 5;
|
|
|
|
|
|
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 => {
|
2024-11-04 16:39:02 +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) {
|
|
|
|
|
|
const cookies = response.headers['set-cookie'];
|
|
|
|
|
|
//根据name 返回对应的cookie
|
|
|
|
|
|
const found = cookies.find((cookie: any) => cookie.includes(name));
|
|
|
|
|
|
if (!found) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
const cookie = found.split(';')[0];
|
|
|
|
|
|
return cookie.substring(cookie.indexOf('=') + 1);
|
|
|
|
|
|
}
|