chore(cert): add md5 hash naming and duplicate cert handling

1. 新增buildCertName方法的useHash参数,使用域名列表MD5哈希作为证书名后缀避免时间戳重复
2. 为asiaisp上传证书添加重复证书检测逻辑,已存在时直接复用已有证书
This commit is contained in:
xiaojunnuo
2026-06-25 23:12:38 +08:00
parent b48831e60b
commit eeb83f9024
2 changed files with 40 additions and 16 deletions
@@ -280,9 +280,14 @@ export class CertReader {
return `${prefix}_${domain}_${timeStr}.${suffix}`;
}
buildCertName(prefix: string = "") {
buildCertName(prefix: string = "", useHash: boolean = false) {
let domain = this.getMainDomain();
domain = domain.replaceAll(".", "_").replaceAll("*", "_");
if (useHash) {
const domains = JSON.stringify(this.getAllDomains());
const hash = cryptoLib.createHash("md5").update(domains).digest("hex").slice(0, 16);
return `${prefix}_${domain}_${hash}`;
}
return `${prefix}_${domain}_${dayjs().format("YYYYMMDDHHmmssSSS")}`;
}
@@ -293,7 +298,7 @@ export class CertReader {
return name + "_" + dayjs().format("YYYYMMDDHHmmssSSS");
}
static buildCertName(cert: CertInfo) {
return new CertReader(cert).buildCertName();
static buildCertName(cert: CertInfo, useHash: boolean = false) {
return new CertReader(cert).buildCertName("", useHash);
}
}
@@ -188,21 +188,40 @@ export class AsiaIspClient {
*/
async uploadCert(req: { cert: CertInfo; name?: string }): Promise<number> {
const certReader = new CertReader(req.cert);
const certName = req.name || certReader.buildCertName();
const certName = req.name || certReader.buildCertName("", true);
const res = await this.doRequest({
method: "POST",
action: "certificateUpload",
data: {
name: certName,
publicKey: req.cert.crt,
privateKey: req.cert.key,
},
});
try {
const res = await this.doRequest({
method: "POST",
action: "certificateUpload",
data: {
name: certName,
publicKey: req.cert.crt,
privateKey: req.cert.key,
},
});
const certId = res.data;
this.logger.info(`上传证书成功,证书ID: ${certId}`);
return certId;
} catch (e: any) {
const msg = e.message || "";
const isExists = msg.includes("Certificate already exists") || e.code ==='80003' ||
msg.includes("Certificate note name already exists") || e.code ==='80010'
//返回数据: {"code":"80010","msg":"Certificate note name already exists","data":null}
if (!isExists) {
throw e;
}
const certId = res.data;
this.logger.info(`上传证书成功,证书ID: ${certId}`);
return certId;
this.logger.info(`证书已存在,按名称查找: ${certName}`);
const list = await this.getCertList();
const found = list.find((item: any) => item.name === certName);
if (!found) {
throw new Error(`证书已存在但无法查询到: ${certName}`);
}
const certId = Number(found.certId);
this.logger.info(`复用已有证书,证书ID: ${certId}`);
return certId;
}
}
/**