mirror of
https://github.com/certd/certd.git
synced 2026-05-16 21:27:34 +08:00
perf: 站点监控支持监控IP
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
import {logger, safePromise, utils} from '@certd/basic';
|
||||
import { merge } from 'lodash-es';
|
||||
import https from 'https';
|
||||
import { PeerCertificate } from 'tls';
|
||||
import { logger, safePromise, utils } from "@certd/basic";
|
||||
import { merge } from "lodash-es";
|
||||
import https from "https";
|
||||
import { PeerCertificate } from "tls";
|
||||
|
||||
export type SiteTestReq = {
|
||||
host: string; // 只用域名部分
|
||||
port?: number;
|
||||
method?: string;
|
||||
retryTimes?: number;
|
||||
ipAddress?: string;
|
||||
};
|
||||
|
||||
export type SiteTestRes = {
|
||||
certificate?: PeerCertificate;
|
||||
};
|
||||
|
||||
export class SiteTester {
|
||||
async test(req: SiteTestReq): Promise<SiteTestRes> {
|
||||
logger.info('测试站点:', JSON.stringify(req));
|
||||
logger.info("测试站点:", JSON.stringify(req));
|
||||
const maxRetryTimes = req.retryTimes ?? 3;
|
||||
let tryCount = 0;
|
||||
let result: SiteTestRes = {};
|
||||
@@ -37,17 +40,34 @@ export class SiteTester {
|
||||
}
|
||||
|
||||
async doTestOnce(req: SiteTestReq): Promise<SiteTestRes> {
|
||||
const agent = new https.Agent({ keepAlive: false });
|
||||
|
||||
const options: any = merge(
|
||||
{
|
||||
port: 443,
|
||||
method: 'GET',
|
||||
rejectUnauthorized: false,
|
||||
method: "GET",
|
||||
rejectUnauthorized: false
|
||||
},
|
||||
req
|
||||
);
|
||||
options.agent = agent;
|
||||
|
||||
const agentOptions:any = {}
|
||||
if (req.ipAddress) {
|
||||
//使用固定的ip
|
||||
const ipAddress = req.ipAddress;
|
||||
agentOptions.lookup = (hostname: string, options: any, callback: any) => {
|
||||
//判断ip是v4 还是v6
|
||||
console.log("options",options)
|
||||
console.log("ipaddress",ipAddress)
|
||||
if (ipAddress.indexOf(":") > -1) {
|
||||
callback(null, [ipAddress], 6);
|
||||
} else {
|
||||
callback(null, [ipAddress], 4);
|
||||
}
|
||||
};
|
||||
options.lookup = agentOptions.lookup;
|
||||
}
|
||||
|
||||
options.agent = new https.Agent({ keepAlive: false, ...agentOptions });
|
||||
|
||||
// 创建 HTTPS 请求
|
||||
const requestPromise = safePromise((resolve, reject) => {
|
||||
const req = https.request(options, res => {
|
||||
@@ -56,20 +76,20 @@ export class SiteTester {
|
||||
const certificate = res.socket.getPeerCertificate();
|
||||
// logger.info('证书信息', certificate);
|
||||
if (certificate.subject == null) {
|
||||
logger.warn('证书信息为空');
|
||||
logger.warn("证书信息为空");
|
||||
resolve({
|
||||
certificate: null,
|
||||
certificate: null
|
||||
});
|
||||
}
|
||||
resolve({
|
||||
certificate,
|
||||
certificate
|
||||
});
|
||||
res.socket.end();
|
||||
// 关闭响应
|
||||
res.destroy();
|
||||
});
|
||||
|
||||
req.on('error', e => {
|
||||
req.on("error", e => {
|
||||
reject(e);
|
||||
});
|
||||
req.end();
|
||||
|
||||
Reference in New Issue
Block a user