Files
certd/packages/plugins/plugin-cert/src/plugin/cert-plugin/cert-reader.ts

84 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { CertInfo } from "./acme.js";
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";
export type CertReaderHandleContext = { reader: CertReader; tmpCrtPath: string; tmpKeyPath: string };
export type CertReaderHandle = (ctx: CertReaderHandleContext) => Promise<void>;
export type HandleOpts = { logger: ILogger; handle: CertReaderHandle };
export class CertReader implements CertInfo {
crt: string;
key: string;
csr: string;
detail: any;
expires: number;
constructor(certInfo: CertInfo) {
this.crt = certInfo.crt;
this.key = certInfo.key;
this.csr = certInfo.csr;
const { detail, expires } = this.getCrtDetail(this.crt);
this.detail = detail;
this.expires = expires.getTime();
}
toCertInfo(): CertInfo {
return {
crt: this.crt,
key: this.key,
csr: this.csr,
};
}
2024-09-05 15:36:35 +08:00
getCrtDetail(crt: string = this.crt) {
2024-08-25 11:56:15 +08:00
const detail = crypto.readCertificateInfo(crt.toString());
const expires = detail.notAfter;
return { detail, expires };
}
saveToFile(type: "crt" | "key", filepath?: string) {
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 });
}
fs.writeFileSync(filepath, this[type]);
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");
logger.info("本地文件写入成功");
try {
await opts.handle({
reader: this,
tmpCrtPath: tmpCrtPath,
tmpKeyPath: tmpKeyPath,
});
} finally {
//删除临时文件
logger.info("删除临时文件");
fs.unlinkSync(tmpCrtPath);
fs.unlinkSync(tmpKeyPath);
}
}
buildCertFileName(suffix: string, applyTime: number, prefix = "cert") {
const detail = this.getCrtDetail();
let domain = detail.detail.domains.commonName;
domain = domain.replace(".", "_").replace("*", "_");
return `${prefix}_${domain}_${applyTime}.${suffix}`;
}
}