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

195 lines
5.4 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";
2025-01-15 01:05:34 +08:00
import { CertificateInfo, crypto } from "@certd/acme-client";
2024-11-04 15:14:56 +08:00
import { ILogger } from "@certd/basic";
2024-09-05 18:00:45 +08:00
import dayjs from "dayjs";
2025-06-05 16:57:49 +08:00
import { uniq } from "lodash-es";
2024-09-05 15:36:35 +08:00
2024-09-22 23:19:10 +08:00
export type CertReaderHandleContext = {
reader: CertReader;
tmpCrtPath: string;
tmpKeyPath: string;
2024-12-12 16:45:40 +08:00
tmpOcPath?: string;
2024-09-22 23:19:10 +08:00
tmpPfxPath?: string;
tmpDerPath?: string;
tmpIcPath?: string;
2024-10-30 01:44:02 +08:00
tmpJksPath?: string;
2024-12-17 22:50:18 +08:00
tmpOnePath?: string;
2024-09-22 23:19:10 +08:00
};
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;
2025-01-15 01:05:34 +08:00
detail: CertificateInfo;
//毫秒时间戳
expires: number;
constructor(certInfo: CertInfo) {
2024-09-06 00:13:21 +08:00
this.cert = certInfo;
2025-01-15 01:05:34 +08:00
if (!certInfo.ic) {
this.cert.ic = this.getIc();
2024-09-22 23:19:10 +08:00
}
2025-01-15 01:05:34 +08:00
if (!certInfo.oc) {
this.cert.oc = this.getOc();
2024-12-12 16:45:40 +08:00
}
2025-01-15 01:05:34 +08:00
if (!certInfo.one) {
this.cert.one = this.cert.crt + "\n" + this.cert.key;
2024-12-17 22:45:14 +08:00
}
try {
const { detail, expires } = this.getCrtDetail(this.cert.crt);
this.detail = detail;
this.expires = expires.getTime();
} catch (e) {
throw new Error("证书解析失败:" + e.message);
}
}
2024-09-22 23:19:10 +08:00
getIc() {
//中间证书ic 就是crt的第一个 -----END CERTIFICATE----- 之后的内容
const endStr = "-----END CERTIFICATE-----";
2025-01-15 01:05:34 +08:00
const firstBlockEndIndex = this.cert.crt.indexOf(endStr);
2024-09-22 23:19:10 +08:00
const start = firstBlockEndIndex + endStr.length + 1;
2025-01-15 01:05:34 +08:00
if (this.cert.crt.length <= start) {
2024-09-22 23:19:10 +08:00
return "";
}
2025-01-15 01:05:34 +08:00
const ic = this.cert.crt.substring(start);
if (ic == null) {
return "";
}
2025-01-14 14:47:03 +08:00
return ic?.trim();
2024-09-22 23:19:10 +08:00
}
2024-12-12 16:45:40 +08:00
getOc() {
//原始证书 就是crt的第一个 -----END CERTIFICATE----- 之前的内容
const endStr = "-----END CERTIFICATE-----";
2025-01-15 01:05:34 +08:00
const arr = this.cert.crt.split(endStr);
2024-12-12 16:45:40 +08:00
return arr[0] + endStr;
}
toCertInfo(): CertInfo {
2024-09-06 00:13:21 +08:00
return this.cert;
}
2024-09-06 00:13:21 +08:00
getCrtDetail(crt: string = this.cert.crt) {
return CertReader.readCertDetail(crt);
}
static readCertDetail(crt: string) {
2024-08-25 11:56:15 +08:00
const detail = crypto.readCertificateInfo(crt.toString());
const expires = detail.notAfter;
return { detail, expires };
}
2024-09-30 18:00:51 +08:00
getAllDomains() {
const { detail } = this.getCrtDetail();
const domains = [detail.domains.commonName];
domains.push(...detail.domains.altNames);
2025-06-05 16:57:49 +08:00
//去重
return uniq(domains);
2024-09-30 18:00:51 +08:00
}
2025-05-26 22:44:56 +08:00
getAltNames() {
const { detail } = this.getCrtDetail();
return detail.domains.altNames;
}
static getMainDomain(crt: string) {
const { detail } = CertReader.readCertDetail(crt);
return detail.domains.commonName;
}
getMainDomain() {
const { detail } = this.getCrtDetail();
return detail.domains.commonName;
}
2024-12-17 22:50:18 +08:00
saveToFile(type: "crt" | "key" | "pfx" | "der" | "oc" | "one" | "ic" | "jks", filepath?: string) {
2024-09-06 00:13:21 +08:00
if (!this.cert[type]) {
return;
}
if (filepath == null) {
//写入临时目录
2024-10-30 01:44:02 +08:00
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-12-17 22:50:18 +08:00
if (type === "crt" || type === "key" || type === "ic" || type === "oc" || type === "one") {
2024-09-06 00:13:21 +08:00
fs.writeFileSync(filepath, this.cert[type]);
} else {
fs.writeFileSync(filepath, Buffer.from(this.cert[type], "base64"));
}
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");
2024-09-22 23:19:10 +08:00
const tmpIcPath = this.saveToFile("ic");
2024-12-12 16:45:40 +08:00
const tmpOcPath = this.saveToFile("oc");
2024-09-22 23:19:10 +08:00
const tmpDerPath = this.saveToFile("der");
2024-10-30 01:44:02 +08:00
const tmpJksPath = this.saveToFile("jks");
2024-12-17 22:50:18 +08:00
const tmpOnePath = this.saveToFile("one");
2024-12-12 16:45:40 +08:00
logger.info("本地文件写入成功");
2024-09-05 15:36:35 +08:00
try {
2024-09-09 16:01:42 +08:00
return await opts.handle({
2024-09-05 15:36:35 +08:00
reader: this,
tmpCrtPath: tmpCrtPath,
tmpKeyPath: tmpKeyPath,
2024-09-06 00:13:21 +08:00
tmpPfxPath: tmpPfxPath,
tmpDerPath: tmpDerPath,
2024-09-22 23:19:10 +08:00
tmpIcPath: tmpIcPath,
2024-10-30 01:44:02 +08:00
tmpJksPath: tmpJksPath,
2024-12-12 16:45:40 +08:00
tmpOcPath: tmpOcPath,
2024-12-17 22:50:18 +08:00
tmpOnePath,
2024-09-05 15:36:35 +08:00
});
2024-09-09 16:01:42 +08:00
} catch (err) {
logger.error("处理失败", err);
2024-09-09 16:01:42 +08:00
throw err;
2024-09-05 15:36:35 +08:00
} finally {
//删除临时文件
logger.info("清理临时文件");
2024-09-06 10:19:03 +08:00
function removeFile(filepath?: string) {
if (filepath) {
fs.unlinkSync(filepath);
}
}
removeFile(tmpCrtPath);
removeFile(tmpKeyPath);
removeFile(tmpPfxPath);
2024-12-12 16:45:40 +08:00
removeFile(tmpOcPath);
2024-09-06 10:19:03 +08:00
removeFile(tmpDerPath);
2024-09-22 23:19:10 +08:00
removeFile(tmpIcPath);
2024-10-30 01:44:02 +08:00
removeFile(tmpJksPath);
2024-12-17 22:50:18 +08:00
removeFile(tmpOnePath);
2024-09-05 15:36:35 +08:00
}
}
2025-01-20 00:34:33 +08:00
buildCertFileName(suffix: string, applyTime: any, prefix = "cert") {
2024-09-05 15:36:35 +08:00
const detail = this.getCrtDetail();
let domain = detail.detail.domains.commonName;
2025-05-26 22:44:56 +08:00
domain = domain.replaceAll(".", "_").replaceAll("*", "_");
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
}
2025-05-26 22:44:56 +08:00
buildCertName() {
let domain = this.getMainDomain();
domain = domain.replaceAll("*", "_").replaceAll("*", "_");
return `${domain}_${dayjs().format("YYYYMMDDHHmmssSSS")}`;
}
}