chore: acme-client依赖于basic

pull/243/head
xiaojunnuo 2024-11-12 12:25:20 +08:00
parent 087c0b8253
commit 60a2ed48c2
5 changed files with 8 additions and 130 deletions

View File

@ -18,6 +18,7 @@
"types"
],
"dependencies": {
"@certd/basic": "^1.27.2",
"@peculiar/x509": "^1.11.0",
"asn1js": "^3.0.5",
"axios": "^1.7.2",

View File

@ -1,119 +0,0 @@
import nodeHttp from 'node:http'
import https from 'https'
import {HttpProxyAgent} from "http-proxy-agent";
import {HttpsProxyAgent} from "https-proxy-agent";
import {log} from './logger.js'
import {merge} from 'lodash-es'
function createAgent(opts = {}) {
opts = merge(
{
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 2000,
},
opts,
);
let httpAgent;
let httpsAgent;
const httpProxy = opts.httpProxy || process.env.HTTP_PROXY || process.env.http_proxy;
if (httpProxy) {
log(`acme use httpProxy:${httpProxy}`);
httpAgent = new HttpProxyAgent(httpProxy, opts);
merge(httpAgent.options, opts);
}
else {
httpAgent = new nodeHttp.Agent(opts);
}
const httpsProxy = opts.httpsProxy || process.env.HTTPS_PROXY || process.env.https_proxy;
if (httpsProxy) {
log(`acme use httpsProxy:${httpsProxy}`);
httpsAgent = new HttpsProxyAgent(httpsProxy, opts);
merge(httpsAgent.options, opts);
}
else {
httpsAgent = new https.Agent(opts);
}
return {
httpAgent,
httpsAgent,
};
}
let defaultAgents = createAgent();
function getGlobalAgents() {
return defaultAgents;
}
function setGlobalProxy(opts) {
log('acme setGlobalProxy:', opts);
defaultAgents = createAgent(opts);
}
class HttpError extends Error {
// eslint-disable-next-line constructor-super
constructor(error) {
if (!error) {
return;
}
super(error.message);
this.message = error.message;
const { message } = error;
if (message && typeof message === 'string') {
if (message.indexOf && message.indexOf('ssl3_get_record:wrong version number') >= 0) {
this.message = `${message}(http协议错误服务端要求http协议请检查是否使用了https请求)`;
}
else if (message.indexOf('getaddrinfo EAI_AGAIN')) {
this.message = `${message}(无法解析域名请检查网络连接或dns配置)`;
}
}
this.name = error.name;
this.code = error.code;
if (error.response) {
this.status = error.response.status;
this.statusText = error.response.statusText || error.code;
this.response = {
data: error.response.data,
};
if (!this.message) {
this.message = this.statusText;
}
}
let url = '';
if (error.config) {
this.request = {
baseURL: error.config.baseURL,
url: error.config.url,
method: error.config.method,
params: error.config.params,
data: error.config.data,
};
url = (error.config.baseURL || '') + error.config.url;
}
if (url) {
this.message = `${this.message}${url}`;
}
const { stack, cause } = error;
// delete this.cause;
// delete this.stack;
this.cause = cause;
this.stack = stack;
delete error.stack;
delete error.cause;
delete error.response;
delete error.config;
delete error.request;
// logger.error(error);
}
}
export {
setGlobalProxy,
createAgent,
getGlobalAgents,
HttpError,
};

View File

@ -4,9 +4,8 @@
import axios from 'axios';
import { parseRetryAfterHeader } from './util.js';
import { log } from './logger.js';
import * as Agents from './agents.js';
const { AxiosError } = axios;
import {getGlobalAgents, HttpError} from '@certd/basic'
/**
* Defaults
*/
@ -73,7 +72,7 @@ function validateStatus(response) {
response,
);
throw new Agents.HttpError(err);
throw new HttpError(err);
}
/* Pass all responses through the error interceptor */
@ -83,7 +82,7 @@ instance.interceptors.request.use((config) => {
}
config.validateStatus = () => false;
const agents = Agents.getGlobalAgents();
const agents = getGlobalAgents();
// if (config.skipSslVerify) {
// logger.info('跳过SSL验证');
// agents = createAgent({ rejectUnauthorized: false } as any);
@ -100,7 +99,7 @@ instance.interceptors.response.use(null, async (error) => {
const { config, response } = error;
if (!config) {
return Promise.reject(new Agents.HttpError(error));
return Promise.reject(new HttpError(error));
}
/* Pick up errors we want to retry */
@ -120,7 +119,7 @@ instance.interceptors.response.use(null, async (error) => {
const waitMinutes = (headerRetryAfter / 60).toFixed(1);
log(`Found retry-after response header with value: ${response.headers['retry-after']}, waiting ${waitMinutes} minutes`);
log(JSON.stringify(response.data));
return Promise.reject(new Agents.HttpError(error));
return Promise.reject(new HttpError(error));
}
log(`waiting ${retryAfter} seconds`);
@ -132,7 +131,7 @@ instance.interceptors.response.use(null, async (error) => {
}
if (!response) {
return Promise.reject(new Agents.HttpError(error));
return Promise.reject(new HttpError(error));
}
/* Validate and return response */
return validateStatus(response);

View File

@ -32,14 +32,13 @@ export const directory = {
*/
export * as crypto from './crypto/index.js'
export * from './crypto/forge.js'
export * as forge from './crypto/forge.js'
/**
* Axios
*/
export * from './axios.js'
export * as agents from './agents.js'
/**
* Logger
*/

View File

@ -7,7 +7,6 @@ import { BaseSettings, SysInstallInfo, SysPrivateSettings, SysPublicSettings, Sy
import * as _ from 'lodash-es';
import { BaseService } from '../../../basic/index.js';
import { logger, setGlobalProxy } from '@certd/basic';
import { agents } from '@certd/acme-client';
/**
*
*/
@ -132,7 +131,6 @@ export class SysSettingsService extends BaseService<SysSettingsEntity> {
httpsProxy: bean.httpsProxy,
};
setGlobalProxy(opts);
agents.setGlobalProxy(opts);
}
async updateByKey(key: string, setting: any) {