mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
chore: 优化https server重启
This commit is contained in:
@@ -4,7 +4,7 @@ import { SysInstallInfo, SysSettingsService } from '@certd/lib-server';
|
||||
import { getVersion } from '../../utils/version.js';
|
||||
import dayjs from 'dayjs';
|
||||
import { Application } from '@midwayjs/koa';
|
||||
import { HttpsServerOptions, startHttpsServer } from './https/server.js';
|
||||
import { httpsServer, HttpsServerOptions } from './https/server.js';
|
||||
|
||||
@Autoload()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
@@ -41,7 +41,7 @@ export class AutoZPrint {
|
||||
logger.info('Https server is not enabled');
|
||||
return;
|
||||
}
|
||||
await startHttpsServer({
|
||||
httpsServer.start({
|
||||
...this.httpsConfig,
|
||||
app: this.app,
|
||||
});
|
||||
|
||||
@@ -12,38 +12,67 @@ export type HttpsServerOptions = {
|
||||
cert: string;
|
||||
};
|
||||
|
||||
export async function startHttpsServer(opts: HttpsServerOptions) {
|
||||
logger.info('=========================================');
|
||||
if (!opts.key || !opts.cert) {
|
||||
logger.error('证书路径未配置,无法启动https服务,请先配置:koa.https.key和koa.https.cert');
|
||||
return;
|
||||
export class HttpsServer {
|
||||
server: https.Server;
|
||||
opts: HttpsServerOptions;
|
||||
constructor() {}
|
||||
|
||||
async restart() {
|
||||
await this.close();
|
||||
return this.start(this.opts);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(opts.key) || !fs.existsSync(opts.cert)) {
|
||||
logger.info('证书文件不存在,将生成自签名证书');
|
||||
createSelfCertificate({
|
||||
crtPath: opts.cert,
|
||||
keyPath: opts.key,
|
||||
async close() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.server.close(() => {
|
||||
resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
logger.info('准备启动https服务');
|
||||
const httpServer = https.createServer(
|
||||
{
|
||||
cert: fs.readFileSync(opts.cert),
|
||||
key: fs.readFileSync(opts.key),
|
||||
},
|
||||
opts.app.callback()
|
||||
);
|
||||
const hostname = '0.0.0.0';
|
||||
// A function that runs in the context of the http server
|
||||
// and reports what type of server listens on which port
|
||||
function listeningReporter() {
|
||||
// `this` refers to the http server here
|
||||
logger.info(`Https server is listening on https://${hostname}:${opts.port}`);
|
||||
}
|
||||
try {
|
||||
httpServer.listen(opts.port, hostname, listeningReporter);
|
||||
} catch (e) {
|
||||
logger.error('启动https服务失败', e);
|
||||
|
||||
start(opts: HttpsServerOptions) {
|
||||
if (!opts) {
|
||||
logger.error('https配置不能为空');
|
||||
return;
|
||||
}
|
||||
this.opts = opts;
|
||||
logger.info('=========================================');
|
||||
if (!opts.key || !opts.cert) {
|
||||
logger.error('证书路径未配置,无法启动https服务,请先配置:koa.https.key和koa.https.cert');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fs.existsSync(opts.key) || !fs.existsSync(opts.cert)) {
|
||||
logger.info('证书文件不存在,将生成自签名证书');
|
||||
createSelfCertificate({
|
||||
crtPath: opts.cert,
|
||||
keyPath: opts.key,
|
||||
});
|
||||
}
|
||||
logger.info('准备启动https服务');
|
||||
const httpServer = https.createServer(
|
||||
{
|
||||
cert: fs.readFileSync(opts.cert),
|
||||
key: fs.readFileSync(opts.key),
|
||||
},
|
||||
opts.app.callback()
|
||||
);
|
||||
this.server = httpServer;
|
||||
const hostname = '0.0.0.0';
|
||||
// A function that runs in the context of the http server
|
||||
// and reports what type of server listens on which port
|
||||
function listeningReporter() {
|
||||
// `this` refers to the http server here
|
||||
logger.info(`Https server is listening on https://${hostname}:${opts.port}`);
|
||||
}
|
||||
|
||||
try {
|
||||
httpServer.listen(opts.port, hostname, listeningReporter);
|
||||
return httpServer;
|
||||
} catch (e) {
|
||||
logger.error('启动https服务失败', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const httpsServer = new HttpsServer();
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy } from '@certd/pipeline';
|
||||
import { httpsServer } from '../../../modules/auto/https/server.js';
|
||||
|
||||
@IsTaskPlugin({
|
||||
name: 'RestartCertd',
|
||||
title: '重启Certd',
|
||||
title: '重启 Certd',
|
||||
icon: 'mdi:restart',
|
||||
desc: '【仅管理员】延迟一定时间后自动杀死自己,然后通过Docker来自动重启',
|
||||
desc: '【仅管理员可用】 重启 certd的https服务,用于更新 Certd 的 ssl 证书',
|
||||
group: pluginGroups.other.key,
|
||||
default: {
|
||||
strategy: {
|
||||
@@ -13,26 +14,14 @@ import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput
|
||||
},
|
||||
})
|
||||
export class RestartCertdPlugin extends AbstractTaskPlugin {
|
||||
@TaskInput({
|
||||
title: '延迟时间',
|
||||
value: 30,
|
||||
component: {
|
||||
placeholder: '30',
|
||||
},
|
||||
helper: '延迟多少秒后执行',
|
||||
required: true,
|
||||
})
|
||||
delay = 30;
|
||||
async onInstance() {}
|
||||
async execute(): Promise<void> {
|
||||
if (!this.isAdmin()) {
|
||||
throw new Error('只有管理员才能运行此任务');
|
||||
}
|
||||
this.logger.info(`Certd 将在 ${this.delay} 秒后关闭`);
|
||||
setTimeout(() => {
|
||||
this.logger.info('关闭 Certd');
|
||||
process.exit(1);
|
||||
}, this.delay * 1000);
|
||||
this.logger.info('Certd https server 将在 3 秒后重启');
|
||||
await this.ctx.utils.sleep(3000);
|
||||
await httpsServer.restart();
|
||||
}
|
||||
}
|
||||
new RestartCertdPlugin();
|
||||
|
||||
Reference in New Issue
Block a user