2024-07-15 00:30:33 +08:00
|
|
|
import { CertInfo } from "./acme.js";
|
2023-05-23 18:01:20 +08:00
|
|
|
import fs from "fs";
|
|
|
|
|
import os from "os";
|
|
|
|
|
import path from "path";
|
2024-08-25 11:56:15 +08:00
|
|
|
import { crypto } from "@certd/acme-client";
|
2024-09-05 15:36:35 +08:00
|
|
|
import { ILogger } from "@certd/pipeline";
|
2024-09-05 18:00:45 +08:00
|
|
|
import dayjs from "dayjs";
|
2024-09-05 15:36:35 +08:00
|
|
|
|
2024-09-06 00:13:21 +08:00
|
|
|
export type CertReaderHandleContext = { reader: CertReader; tmpCrtPath: string; tmpKeyPath: string; tmpPfxPath?: string; tmpDerPath?: string };
|
2024-09-05 15:36:35 +08:00
|
|
|
export type CertReaderHandle = (ctx: CertReaderHandleContext) => Promise<void>;
|
|
|
|
|
export type HandleOpts = { logger: ILogger; handle: CertReaderHandle };
|
2024-09-06 00:13:21 +08:00
|
|
|
export class CertReader {
|
|
|
|
|
cert: CertInfo;
|
2023-05-23 18:01:20 +08:00
|
|
|
crt: string;
|
|
|
|
|
key: string;
|
|
|
|
|
csr: string;
|
|
|
|
|
|
|
|
|
|
detail: any;
|
|
|
|
|
expires: number;
|
|
|
|
|
constructor(certInfo: CertInfo) {
|
2024-09-06 00:13:21 +08:00
|
|
|
this.cert = certInfo;
|
2023-05-23 18:01:20 +08:00
|
|
|
this.crt = certInfo.crt;
|
|
|
|
|
this.key = certInfo.key;
|
|
|
|
|
this.csr = certInfo.csr;
|
|
|
|
|
|
2024-09-06 00:13:21 +08:00
|
|
|
const { detail, expires } = this.getCrtDetail(this.cert.crt);
|
2023-05-23 18:01:20 +08:00
|
|
|
this.detail = detail;
|
|
|
|
|
this.expires = expires.getTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toCertInfo(): CertInfo {
|
2024-09-06 00:13:21 +08:00
|
|
|
return this.cert;
|
2023-05-23 18:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-06 00:13:21 +08:00
|
|
|
getCrtDetail(crt: string = this.cert.crt) {
|
2024-08-25 11:56:15 +08:00
|
|
|
const detail = crypto.readCertificateInfo(crt.toString());
|
|
|
|
|
const expires = detail.notAfter;
|
2023-05-23 18:01:20 +08:00
|
|
|
return { detail, expires };
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 00:13:21 +08:00
|
|
|
saveToFile(type: "crt" | "key" | "pfx" | "der", filepath?: string) {
|
|
|
|
|
if (!this.cert[type]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 18:01:20 +08:00
|
|
|
if (filepath == null) {
|
|
|
|
|
//写入临时目录
|
|
|
|
|
filepath = path.join(os.tmpdir(), "/certd/tmp/", Math.floor(Math.random() * 1000000) + "", `cert.${type}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dir = path.dirname(filepath);
|
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
}
|
2024-09-06 00:13:21 +08:00
|
|
|
if (type === "crt" || type === "key") {
|
|
|
|
|
fs.writeFileSync(filepath, this.cert[type]);
|
|
|
|
|
} else {
|
|
|
|
|
fs.writeFileSync(filepath, Buffer.from(this.cert[type], "base64"));
|
|
|
|
|
}
|
2023-05-23 18:01:20 +08:00
|
|
|
return filepath;
|
|
|
|
|
}
|
2024-09-05 15:36:35 +08:00
|
|
|
|
|
|
|
|
async readCertFile(opts: HandleOpts) {
|
|
|
|
|
const logger = opts.logger;
|
|
|
|
|
logger.info("将证书写入本地缓存文件");
|
|
|
|
|
const tmpCrtPath = this.saveToFile("crt");
|
|
|
|
|
const tmpKeyPath = this.saveToFile("key");
|
2024-09-06 00:13:21 +08:00
|
|
|
const tmpPfxPath = this.saveToFile("pfx");
|
|
|
|
|
const tmpDerPath = this.saveToFile("der");
|
2024-09-05 15:36:35 +08:00
|
|
|
logger.info("本地文件写入成功");
|
|
|
|
|
try {
|
|
|
|
|
await opts.handle({
|
|
|
|
|
reader: this,
|
|
|
|
|
tmpCrtPath: tmpCrtPath,
|
|
|
|
|
tmpKeyPath: tmpKeyPath,
|
2024-09-06 00:13:21 +08:00
|
|
|
tmpPfxPath: tmpPfxPath,
|
|
|
|
|
tmpDerPath: tmpDerPath,
|
2024-09-05 15:36:35 +08:00
|
|
|
});
|
|
|
|
|
} finally {
|
|
|
|
|
//删除临时文件
|
|
|
|
|
logger.info("删除临时文件");
|
|
|
|
|
fs.unlinkSync(tmpCrtPath);
|
|
|
|
|
fs.unlinkSync(tmpKeyPath);
|
2024-09-06 00:13:21 +08:00
|
|
|
fs.unlinkSync(tmpPfxPath);
|
|
|
|
|
fs.unlinkSync(tmpDerPath);
|
2024-09-05 15:36:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buildCertFileName(suffix: string, applyTime: number, prefix = "cert") {
|
|
|
|
|
const detail = this.getCrtDetail();
|
|
|
|
|
let domain = detail.detail.domains.commonName;
|
|
|
|
|
domain = domain.replace(".", "_").replace("*", "_");
|
2024-09-05 18:00:45 +08:00
|
|
|
const timeStr = dayjs(applyTime).format("YYYYMMDDHHmmss");
|
|
|
|
|
return `${prefix}_${domain}_${timeStr}.${suffix}`;
|
2024-09-05 15:36:35 +08:00
|
|
|
}
|
2023-05-23 18:01:20 +08:00
|
|
|
}
|