Files
certd/packages/ui/certd-server/src/modules/auto/auto-print.ts
T

106 lines
3.5 KiB
TypeScript
Raw Normal View History

import { App, Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
2024-11-04 16:39:02 +08:00
import { getPlusInfo, isPlus } from '@certd/plus-core';
2024-12-23 13:27:04 +08:00
import { isDev, logger } from '@certd/basic';
2024-11-04 16:39:02 +08:00
2024-10-14 10:57:12 +08:00
import { SysInstallInfo, SysSettingsService } from '@certd/lib-server';
import { getVersion } from '../../utils/version.js';
2024-10-14 11:41:34 +08:00
import dayjs from 'dayjs';
2024-10-26 16:36:57 +08:00
import { Application } from '@midwayjs/koa';
2024-10-26 18:01:06 +08:00
import { httpsServer, HttpsServerOptions } from './https/server.js';
import { UserService } from '../sys/authority/service/user-service.js';
import { UserSettingsService } from '../mine/service/user-settings-service.js';
2026-02-03 18:28:41 +08:00
import { startProxyServer } from './proxy/server.js';
2024-10-14 10:57:12 +08:00
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class AutoPrint {
2024-10-14 10:57:12 +08:00
@Inject()
sysSettingsService: SysSettingsService;
2024-10-26 16:36:57 +08:00
@App()
app: Application;
@Config('https')
httpsConfig: HttpsServerOptions;
2025-10-11 16:59:28 +08:00
@Config('koa')
koaConfig: any;
2024-10-26 16:36:57 +08:00
@Inject()
userService: UserService;
@Inject()
userSettingsService: UserSettingsService;
@Config('system.resetAdminPasswd')
private resetAdminPasswd: boolean;
2024-10-14 10:57:12 +08:00
async init() {
2024-10-26 16:36:57 +08:00
//监听https
this.startHttpsServer();
2026-02-03 18:28:41 +08:00
// this.startProxyServer();
2025-12-12 23:39:09 +08:00
logger.info("ENV:", process.env.NODE_ENV);
2024-12-23 23:33:13 +08:00
if (isDev()) {
this.startHeapLog();
}
2024-10-14 10:57:12 +08:00
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
logger.info('=========================================');
logger.info('当前站点ID:', installInfo.siteId);
const version = await getVersion();
logger.info(`当前版本:${version}`);
2024-10-14 11:41:34 +08:00
const plusInfo = getPlusInfo();
if (isPlus()) {
2026-01-16 12:57:14 +08:00
logger.info(`授权信息:${plusInfo.vipType},${plusInfo.expireTime === -1 ? '永久' : dayjs(plusInfo.expireTime).format('YYYY-MM-DD')}`);
2024-10-14 11:41:34 +08:00
}
2024-10-14 11:52:37 +08:00
logger.info('Certd已启动');
2024-10-14 10:57:12 +08:00
logger.info('=========================================');
await this.resetPasswd();
}
async resetPasswd(){
if (this.resetAdminPasswd === true) {
logger.info('开始重置1号管理员用户的密码');
const newPasswd = '123456';
await this.userService.resetPassword(1, newPasswd);
await this.userService.updateStatus(1, 1);
await this.userSettingsService.deleteWhere({
userId: 1,
key:"user.two.factor"
})
const publicSettings = await this.sysSettingsService.getPublicSettings()
publicSettings.captchaEnabled = false
await this.sysSettingsService.savePublicSettings(publicSettings);
const user = await this.userService.info(1);
logger.info(`重置1号管理员用户的密码完成,2FA设置已删除,验证码登录已禁用,用户名:${user.username},新密码:${newPasswd},请在登录进去之后尽快修改密码`);
}
2024-12-23 13:27:04 +08:00
}
startHeapLog() {
function format(bytes: any) {
return (bytes / 1024 / 1024).toFixed(2) + ' MB';
}
2025-12-30 11:09:50 +08:00
function printHeapLog() {
2025-01-02 17:48:54 +08:00
const mu = process.memoryUsage();
2025-12-29 16:44:00 +08:00
logger.info(`rss:${format(mu.rss)},heapUsed: ${format(mu.heapUsed)},heapTotal: ${format(mu.heapTotal)},external: ${format(mu.external)},arrayBuffers: ${format(mu.arrayBuffers)}`);
2025-12-30 11:09:50 +08:00
}
setInterval(printHeapLog, 20000);
printHeapLog();
2024-10-14 10:57:12 +08:00
}
2024-10-26 16:36:57 +08:00
2024-12-23 23:33:13 +08:00
startHttpsServer() {
2024-10-26 16:36:57 +08:00
if (!this.httpsConfig.enabled) {
logger.info('Https server is not enabled');
return;
}
2024-10-26 18:01:06 +08:00
httpsServer.start({
2024-10-26 16:36:57 +08:00
...this.httpsConfig,
app: this.app,
2025-10-11 16:59:28 +08:00
hostname: this.httpsConfig.hostname || this.koaConfig.hostname,
2024-10-26 16:36:57 +08:00
});
}
2026-02-03 18:28:41 +08:00
startProxyServer() {
startProxyServer({port: 7003});
}
2024-10-14 10:57:12 +08:00
}