2025-01-04 20:10:00 +08:00
|
|
|
import { logger, utils } from '@certd/basic';
|
2024-12-23 18:11:06 +08:00
|
|
|
import { merge } from 'lodash-es';
|
|
|
|
|
import https from 'https';
|
|
|
|
|
import { PeerCertificate } from 'tls';
|
|
|
|
|
export type SiteTestReq = {
|
|
|
|
|
host: string; // 只用域名部分
|
|
|
|
|
port?: number;
|
|
|
|
|
method?: string;
|
2025-01-04 20:10:00 +08:00
|
|
|
retryTimes?: number;
|
2024-12-23 18:11:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type SiteTestRes = {
|
|
|
|
|
certificate?: PeerCertificate;
|
|
|
|
|
};
|
|
|
|
|
export class SiteTester {
|
|
|
|
|
async test(req: SiteTestReq): Promise<SiteTestRes> {
|
|
|
|
|
logger.info('测试站点:', JSON.stringify(req));
|
2025-01-04 20:10:00 +08:00
|
|
|
const maxRetryTimes = req.retryTimes ?? 3;
|
|
|
|
|
let tryCount = 0;
|
|
|
|
|
let result: SiteTestRes = {};
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
result = await this.doTestOnce(req);
|
|
|
|
|
return result;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
tryCount++;
|
|
|
|
|
if (tryCount > maxRetryTimes) {
|
2025-01-04 20:17:08 +08:00
|
|
|
logger.error(`测试站点出错,重试${maxRetryTimes}次。`, e.message);
|
2025-01-04 20:10:00 +08:00
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
//指数退避
|
|
|
|
|
const time = 2 ** tryCount;
|
|
|
|
|
logger.error(`测试站点出错,${time}s后重试`, e);
|
|
|
|
|
await utils.sleep(time * 1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async doTestOnce(req: SiteTestReq): Promise<SiteTestRes> {
|
2024-12-23 18:11:06 +08:00
|
|
|
const agent = new https.Agent({ keepAlive: false });
|
|
|
|
|
|
|
|
|
|
const options: any = merge(
|
|
|
|
|
{
|
|
|
|
|
port: 443,
|
|
|
|
|
method: 'GET',
|
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
|
},
|
|
|
|
|
req
|
|
|
|
|
);
|
|
|
|
|
options.agent = agent;
|
|
|
|
|
// 创建 HTTPS 请求
|
|
|
|
|
const requestPromise = new Promise((resolve, reject) => {
|
|
|
|
|
const req = https.request(options, res => {
|
|
|
|
|
// 获取证书
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const certificate = res.socket.getPeerCertificate();
|
|
|
|
|
// logger.info('证书信息', certificate);
|
|
|
|
|
if (certificate.subject == null) {
|
|
|
|
|
logger.warn('证书信息为空');
|
|
|
|
|
resolve({
|
|
|
|
|
certificate: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
resolve({
|
|
|
|
|
certificate,
|
|
|
|
|
});
|
|
|
|
|
res.socket.end();
|
|
|
|
|
// 关闭响应
|
|
|
|
|
res.destroy();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('error', e => {
|
|
|
|
|
reject(e);
|
|
|
|
|
});
|
|
|
|
|
req.end();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await requestPromise;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const siteTester = new SiteTester();
|