mirror of
https://github.com/certd/certd.git
synced 2026-07-06 20:37:34 +08:00
chore: format
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { logger } from '@certd/basic';
|
||||
import fs from 'fs';
|
||||
import { logger } from "@certd/basic";
|
||||
import fs from "fs";
|
||||
// @ts-ignore
|
||||
import forge from 'node-forge';
|
||||
import path from 'path';
|
||||
import forge from "node-forge";
|
||||
import path from "path";
|
||||
|
||||
export function createSelfCertificate(opts: { crtPath: string; keyPath: string }) {
|
||||
// 生成密钥对
|
||||
@@ -11,14 +11,14 @@ export function createSelfCertificate(opts: { crtPath: string; keyPath: string }
|
||||
// 创建自签名证书
|
||||
const cert = forge.pki.createCertificate();
|
||||
cert.publicKey = keypair.publicKey;
|
||||
cert.serialNumber = '01';
|
||||
cert.serialNumber = "01";
|
||||
cert.validFrom = new Date(Date.now() - 1000 * 60 * 60 * 24).toISOString(); // 1天前
|
||||
cert.validTo = new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 10).toISOString(); // 10年后
|
||||
// 创建主题
|
||||
const attrs = [
|
||||
{
|
||||
name: 'commonName',
|
||||
value: 'self-certificate.certd', // 或者你的域名
|
||||
name: "commonName",
|
||||
value: "self-certificate.certd", // 或者你的域名
|
||||
},
|
||||
];
|
||||
cert.setSubject(attrs);
|
||||
@@ -30,7 +30,7 @@ export function createSelfCertificate(opts: { crtPath: string; keyPath: string }
|
||||
const pemKey = forge.pki.privateKeyToPem(keypair.privateKey);
|
||||
|
||||
// 写入文件
|
||||
logger.info('生成自签名证书成功');
|
||||
logger.info("生成自签名证书成功");
|
||||
logger.info(`自签证书保存路径: ${opts.crtPath}`);
|
||||
logger.info(`自签私钥保存路径: ${opts.keyPath}`);
|
||||
const crtDir = path.dirname(opts.crtPath);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import https from 'node:https';
|
||||
import fs from 'fs';
|
||||
import { Application } from '@midwayjs/koa';
|
||||
import { createSelfCertificate } from './self-certificate.js';
|
||||
import {logger, safePromise} from '@certd/basic';
|
||||
import https from "node:https";
|
||||
import fs from "fs";
|
||||
import { Application } from "@midwayjs/koa";
|
||||
import { createSelfCertificate } from "./self-certificate.js";
|
||||
import { logger, safePromise } from "@certd/basic";
|
||||
|
||||
export type HttpsServerOptions = {
|
||||
enabled: boolean;
|
||||
@@ -33,24 +33,24 @@ export class HttpsServer {
|
||||
|
||||
start(opts: HttpsServerOptions) {
|
||||
if (!opts) {
|
||||
logger.error('https配置不能为空');
|
||||
logger.error("https配置不能为空");
|
||||
return;
|
||||
}
|
||||
this.opts = opts;
|
||||
logger.info('=========================================');
|
||||
logger.info("=========================================");
|
||||
if (!opts.key || !opts.cert) {
|
||||
logger.error('证书路径未配置,无法启动https服务,请先配置:koa.https.key和koa.https.cert');
|
||||
logger.error("证书路径未配置,无法启动https服务,请先配置:koa.https.key和koa.https.cert");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fs.existsSync(opts.key) || !fs.existsSync(opts.cert)) {
|
||||
logger.info('证书文件不存在,将生成自签名证书');
|
||||
logger.info("证书文件不存在,将生成自签名证书");
|
||||
createSelfCertificate({
|
||||
crtPath: opts.cert,
|
||||
keyPath: opts.key,
|
||||
});
|
||||
}
|
||||
logger.info('准备启动https服务');
|
||||
logger.info("准备启动https服务");
|
||||
const httpServer = https.createServer(
|
||||
{
|
||||
cert: fs.readFileSync(opts.cert),
|
||||
@@ -59,7 +59,7 @@ export class HttpsServer {
|
||||
opts.app.callback()
|
||||
);
|
||||
this.server = httpServer;
|
||||
let hostname = opts.hostname || '::';
|
||||
let hostname = opts.hostname || "::";
|
||||
// A function that runs in the context of the http server
|
||||
// and reports what type of server listens on which port
|
||||
function listeningReporter() {
|
||||
@@ -71,19 +71,18 @@ export class HttpsServer {
|
||||
httpServer.listen(opts.port, hostname, listeningReporter);
|
||||
return httpServer;
|
||||
} catch (e) {
|
||||
if ( e.message?.includes("address family not supported")) {
|
||||
hostname = "0.0.0.0"
|
||||
if (e.message?.includes("address family not supported")) {
|
||||
hostname = "0.0.0.0";
|
||||
logger.error(`${e.message},尝试监听${hostname}`, e);
|
||||
try{
|
||||
try {
|
||||
httpServer.listen(opts.port, hostname, listeningReporter);
|
||||
return httpServer;
|
||||
}catch (e) {
|
||||
logger.error('启动https服务失败', e);
|
||||
} catch (e) {
|
||||
logger.error("启动https服务失败", e);
|
||||
}
|
||||
}else{
|
||||
logger.error('启动https服务失败', e);
|
||||
} else {
|
||||
logger.error("启动https服务失败", e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user