chore: 优化https server重启

pull/229/head
xiaojunnuo 2024-10-26 18:01:06 +08:00
parent 513a5b49c1
commit be2f0aa435
7 changed files with 78 additions and 58 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -17,20 +17,22 @@ CERTD_HTTPS_port=7002
参考Certd顶部的创建证书流水线教程 参考Certd顶部的创建证书流水线教程
### 2、配置复制到本机任务 ### 2、配置复制到本机任务
将证书复制到certd的证书安装位置
![](./images/1.png) ![](./images/1.png)
![](./images/2.png) ![](./images/2.png)
### 3、配置重启Certd任务 ### 3、配置重启Certd任务
:::warning 重启certd的https server让证书生效
1. 重启Certd插件并不具备重启自己的能力而是`杀死自己`,然后靠`Docker`来重启自己
2. 源码部署情况下,需要通过`执行主机脚本`插件来自己写脚本重启`certd`
3. 建议将本流水线的触发时间与其他流水线时间错开,避免重启时影响其他流水线的执行
:::
![img.png](./images/3.png) ![img.png](./images/3.png)
### 4、配置定时任务 ### 4、配置定时任务
每天定时执行,最终效果如下 每天定时执行,最终效果如下
![](./images/ok.png) ![](./images/ok.png)
:::warning
建议将本流水线的触发时间与其他流水线时间错开,避免重启时影响其他流水线的执行
:::

View File

@ -86,7 +86,7 @@ export function createAxiosService({ logger }: { logger: Logger }) {
service.interceptors.request.use( service.interceptors.request.use(
(config: any) => { (config: any) => {
logger.info(`http request:${config.url}method:${config.method}`); logger.info(`http request:${config.url}method:${config.method}`);
if (config.logParams !== false) { if (config.logParams !== false && config.params) {
logger.info(`params:${JSON.stringify(config.params)}`); logger.info(`params:${JSON.stringify(config.params)}`);
} }
if (config.timeout == null) { if (config.timeout == null) {
@ -182,7 +182,7 @@ export function createAxiosService({ logger }: { logger: Logger }) {
export const http = createAxiosService({ logger }) as HttpClient; export const http = createAxiosService({ logger }) as HttpClient;
export type HttpClientResponse<R> = any; export type HttpClientResponse<R> = any;
export type HttpRequestConfig<D=any> = { export type HttpRequestConfig<D = any> = {
skipSslVerify?: boolean; skipSslVerify?: boolean;
skipCheckRes?: boolean; skipCheckRes?: boolean;
logParams?: boolean; logParams?: boolean;

View File

@ -4,7 +4,7 @@ import { SysInstallInfo, SysSettingsService } from '@certd/lib-server';
import { getVersion } from '../../utils/version.js'; import { getVersion } from '../../utils/version.js';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { Application } from '@midwayjs/koa'; import { Application } from '@midwayjs/koa';
import { HttpsServerOptions, startHttpsServer } from './https/server.js'; import { httpsServer, HttpsServerOptions } from './https/server.js';
@Autoload() @Autoload()
@Scope(ScopeEnum.Singleton) @Scope(ScopeEnum.Singleton)
@ -41,7 +41,7 @@ export class AutoZPrint {
logger.info('Https server is not enabled'); logger.info('Https server is not enabled');
return; return;
} }
await startHttpsServer({ httpsServer.start({
...this.httpsConfig, ...this.httpsConfig,
app: this.app, app: this.app,
}); });

View File

@ -12,7 +12,30 @@ export type HttpsServerOptions = {
cert: string; cert: string;
}; };
export async function startHttpsServer(opts: HttpsServerOptions) { export class HttpsServer {
server: https.Server;
opts: HttpsServerOptions;
constructor() {}
async restart() {
await this.close();
return this.start(this.opts);
}
async close() {
return new Promise((resolve, reject) => {
this.server.close(() => {
resolve(true);
});
});
}
start(opts: HttpsServerOptions) {
if (!opts) {
logger.error('https配置不能为空');
return;
}
this.opts = opts;
logger.info('========================================='); logger.info('=========================================');
if (!opts.key || !opts.cert) { if (!opts.key || !opts.cert) {
logger.error('证书路径未配置无法启动https服务请先配置koa.https.key和koa.https.cert'); logger.error('证书路径未配置无法启动https服务请先配置koa.https.key和koa.https.cert');
@ -34,6 +57,7 @@ export async function startHttpsServer(opts: HttpsServerOptions) {
}, },
opts.app.callback() opts.app.callback()
); );
this.server = httpServer;
const hostname = '0.0.0.0'; const hostname = '0.0.0.0';
// A function that runs in the context of the http server // A function that runs in the context of the http server
// and reports what type of server listens on which port // and reports what type of server listens on which port
@ -41,9 +65,14 @@ export async function startHttpsServer(opts: HttpsServerOptions) {
// `this` refers to the http server here // `this` refers to the http server here
logger.info(`Https server is listening on https://${hostname}:${opts.port}`); logger.info(`Https server is listening on https://${hostname}:${opts.port}`);
} }
try { try {
httpServer.listen(opts.port, hostname, listeningReporter); httpServer.listen(opts.port, hostname, listeningReporter);
return httpServer;
} catch (e) { } catch (e) {
logger.error('启动https服务失败', e); logger.error('启动https服务失败', e);
} }
}
} }
export const httpsServer = new HttpsServer();

View File

@ -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({ @IsTaskPlugin({
name: 'RestartCertd', name: 'RestartCertd',
title: '重启Certd', title: '重启 Certd',
icon: 'mdi:restart', icon: 'mdi:restart',
desc: '【仅管理员】延迟一定时间后自动杀死自己然后通过Docker来自动重启', desc: '【仅管理员可用】 重启 certd的https服务用于更新 Certd 的 ssl 证书',
group: pluginGroups.other.key, group: pluginGroups.other.key,
default: { default: {
strategy: { strategy: {
@ -13,26 +14,14 @@ import { AbstractTaskPlugin, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput
}, },
}) })
export class RestartCertdPlugin extends AbstractTaskPlugin { export class RestartCertdPlugin extends AbstractTaskPlugin {
@TaskInput({
title: '延迟时间',
value: 30,
component: {
placeholder: '30',
},
helper: '延迟多少秒后执行',
required: true,
})
delay = 30;
async onInstance() {} async onInstance() {}
async execute(): Promise<void> { async execute(): Promise<void> {
if (!this.isAdmin()) { if (!this.isAdmin()) {
throw new Error('只有管理员才能运行此任务'); throw new Error('只有管理员才能运行此任务');
} }
this.logger.info(`Certd 将在 ${this.delay} 秒后关闭`); this.logger.info('Certd https server 将在 3 秒后重启');
setTimeout(() => { await this.ctx.utils.sleep(3000);
this.logger.info('关闭 Certd'); await httpsServer.restart();
process.exit(1);
}, this.delay * 1000);
} }
} }
new RestartCertdPlugin(); new RestartCertdPlugin();