Merge branch 'certd:v2' into v2

This commit is contained in:
origami
2024-11-02 01:53:20 +08:00
committed by GitHub
144 changed files with 1716 additions and 602 deletions
+2 -1
View File
@@ -1 +1,2 @@
./tools/**/* filter=lfs diff=lfs merge=lfs -text
tools/** filter=lfs diff=lfs merge=lfs -text
+3
View File
@@ -19,3 +19,6 @@ run/
.clinic
.env.pgpl.yaml
tools/windows/*
!tools/windows/*.zip
+15
View File
@@ -3,6 +3,21 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [1.27.0](https://github.com/certd/certd/compare/v1.26.16...v1.27.0) (2024-10-31)
### Bug Fixes
* 修复历史记录不能按名称查询的bug ([6113c38](https://github.com/certd/certd/commit/6113c388b7fc58b11ca19ff05cc1286d096c8d28))
### Features
* 首页全新改版 ([63ec5b5](https://github.com/certd/certd/commit/63ec5b5519c760a3330569c0da6dac157302a330))
### Performance Improvements
* 管理控制台数据统计 ([babd589](https://github.com/certd/certd/commit/babd5897ae013ff7c04ebfcbfac8a00d84dd627c))
* lego 升级到 4.19.2 ([129bf53](https://github.com/certd/certd/commit/129bf53edc9bbb001fe49fbd7e239bd1d09cc128))
## [1.26.16](https://github.com/certd/certd/compare/v1.26.15...v1.26.16) (2024-10-30)
### Performance Improvements
+14 -16
View File
@@ -1,6 +1,6 @@
{
"name": "@certd/ui-server",
"version": "1.26.16",
"version": "1.27.0",
"description": "fast-server base midway",
"private": true,
"type": "module",
@@ -27,17 +27,17 @@
},
"dependencies": {
"@alicloud/pop-core": "^1.7.10",
"@certd/acme-client": "^1.26.16",
"@certd/commercial-core": "^1.26.16",
"@certd/lib-huawei": "^1.26.16",
"@certd/lib-jdcloud": "^1.26.16",
"@certd/lib-k8s": "^1.26.16",
"@certd/lib-server": "^1.26.16",
"@certd/midway-flyway-js": "^1.26.16",
"@certd/pipeline": "^1.26.16",
"@certd/plugin-cert": "^1.26.16",
"@certd/plugin-plus": "^1.26.16",
"@certd/plus-core": "^1.26.16",
"@certd/acme-client": "^1.27.0",
"@certd/commercial-core": "^1.27.0",
"@certd/lib-huawei": "^1.27.0",
"@certd/lib-jdcloud": "^1.27.0",
"@certd/lib-k8s": "^1.27.0",
"@certd/lib-server": "^1.27.0",
"@certd/midway-flyway-js": "^1.27.0",
"@certd/pipeline": "^1.27.0",
"@certd/plugin-cert": "^1.27.0",
"@certd/plugin-plus": "^1.27.0",
"@certd/plus-core": "^1.27.0",
"@huaweicloud/huaweicloud-sdk-cdn": "^3.1.120",
"@huaweicloud/huaweicloud-sdk-core": "^3.1.120",
"@koa/cors": "^5.0.0",
@@ -60,6 +60,7 @@
"cache-manager": "^6.1.0",
"cos-nodejs-sdk-v5": "^2.14.6",
"cron-parser": "^4.9.0",
"cross-env": "^7.0.3",
"dayjs": "^1.11.7",
"form-data": "^4.0.0",
"glob": "^11.0.0",
@@ -73,7 +74,7 @@
"lodash-es": "^4.17.21",
"log4js": "^6.7.1",
"lru-cache": "^11.0.1",
"md5": "^2.3.0",
"mwts": "^1.3.0",
"mwtsc": "^1.4.0",
"nanoid": "^5.0.7",
"node-forge": "^1.3.1",
@@ -89,7 +90,6 @@
"ssh2": "^1.15.0",
"strip-ansi": "^7.1.0",
"svg-captcha": "^1.4.0",
"syno": "^2.2.0",
"tencentcloud-sdk-nodejs": "^4.0.44",
"typeorm": "^0.3.20",
"uuid": "^10.0.0"
@@ -106,9 +106,7 @@
"@types/nodemailer": "^6.4.8",
"@types/ssh2": "^1.15.0",
"c8": "^10.1.2",
"cross-env": "^7.0.3",
"mocha": "^10.2.0",
"mwts": "^1.3.0",
"prettier": "^3.3.3",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
@@ -0,0 +1,23 @@
import { Controller, Get, Provide } from '@midwayjs/core';
import { BaseController, Constants } from '@certd/lib-server';
import { http, logger } from '@certd/pipeline';
/**
*/
@Provide()
@Controller('/api/app/')
export class AppController extends BaseController {
@Get('/latest', { summary: Constants.per.authOnly })
async latest(): Promise<any> {
const res = await http.request({
url: 'https://registry.npmmirror.com/@certd/pipeline',
method: 'get',
});
try {
const latest = res['dist-tags'].latest;
return this.ok(latest);
} catch (e: any) {
logger.error(e);
return this.ok('');
}
}
}
@@ -1,5 +1,4 @@
import { MidwayEnvironmentService } from '@midwayjs/core';
import { Controller, Get, Inject, Provide } from '@midwayjs/core';
import { Controller, Get, Inject, MidwayEnvironmentService, Provide } from '@midwayjs/core';
import { logger } from '@certd/pipeline';
import { Constants } from '@certd/lib-server';
@@ -0,0 +1,47 @@
import { Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController, Constants } from '@certd/lib-server';
import { UserService } from '../../modules/sys/authority/service/user-service.js';
import { RoleService } from '../../modules/sys/authority/service/role-service.js';
import { PipelineService } from '../../modules/pipeline/service/pipeline-service.js';
import { HistoryService } from '../../modules/pipeline/service/history-service.js';
export type ChartItem = {
name: string;
value: number;
};
export type UserStatisticCount = {
pipelineCount?: number;
pipelineStatusCount?: ChartItem[];
historyCountPerDay: ChartItem[];
expiringList: any[];
};
/**
*/
@Provide()
@Controller('/api/statistic/')
export class StatisticController extends BaseController {
@Inject()
userService: UserService;
@Inject()
roleService: RoleService;
@Inject()
pipelineService: PipelineService;
@Inject()
historyService: HistoryService;
@Post('/count', { summary: Constants.per.authOnly })
public async count() {
const pipelineCount = await this.pipelineService.count({ userId: this.getUserId() });
const pipelineStatusCount = await this.pipelineService.statusCount({ userId: this.getUserId() });
const historyCount = await this.historyService.countPerDay({ userId: this.getUserId(), days: 7 });
const expiringList = await this.pipelineService.latestExpiringList({ userId: this.getUserId(), count: 5 });
const count: UserStatisticCount = {
pipelineCount,
pipelineStatusCount,
historyCountPerDay: historyCount,
expiringList,
};
return this.ok(count);
}
}
@@ -1,7 +1,6 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { CrudController } from '@certd/lib-server';
import { Constants, CrudController } from '@certd/lib-server';
import { AccessService } from '../../modules/pipeline/service/access-service.js';
import { Constants } from '@certd/lib-server';
/**
* 授权
@@ -78,9 +77,6 @@ export class AccessController extends CrudController<AccessService> {
const list = this.service.getDefineList();
const dict = [];
for (const item of list) {
if (item?.deprecated) {
continue;
}
dict.push({
value: item.name,
label: item.title,
@@ -47,6 +47,7 @@ export class HistoryController extends CrudController<HistoryService> {
let pipelineIds: any = null;
const pipelineTitle = body.query?.pipelineTitle;
delete body.query.pipelineTitle;
if (pipelineTitle) {
const pipelines = await this.pipelineService.list({
query: pipelineQuery,
@@ -103,4 +103,10 @@ export class PipelineController extends CrudController<PipelineService> {
await this.service.cancel(historyId);
return this.ok({});
}
@Post('/count', { summary: Constants.per.authOnly })
async count() {
const count = await this.service.count({ userId: this.getUserId() });
return this.ok({ count });
}
}
@@ -0,0 +1,51 @@
import { Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController } from '@certd/lib-server';
import { UserService } from '../../../modules/sys/authority/service/user-service.js';
import { RoleService } from '../../../modules/sys/authority/service/role-service.js';
import { PipelineService } from '../../../modules/pipeline/service/pipeline-service.js';
import { HistoryService } from '../../../modules/pipeline/service/history-service.js';
export type ChartItem = {
name: string;
value: number;
};
export type SysStatisticCount = {
userCount: number;
pipelineCount?: number;
historyCountPerDay: ChartItem[];
userRegisterCountPerDay: ChartItem[];
pipelineCreateCountPerDay: ChartItem[];
};
/**
*/
@Provide()
@Controller('/api/sys/statistic/')
export class SysStatisticController extends BaseController {
@Inject()
userService: UserService;
@Inject()
roleService: RoleService;
@Inject()
pipelineService: PipelineService;
@Inject()
historyService: HistoryService;
@Post('/count', { summary: 'sys:settings:view' })
public async count() {
const userCount = await this.userService.count();
const userRegisterCountPerDay = await this.userService.registerCountPerDay({ days: 7 });
const pipelineCreateCountPerDay = await this.pipelineService.createCountPerDay({ days: 7 });
const pipelineCount = await this.pipelineService.count({});
const historyCountPerDay = await this.historyService.countPerDay({ days: 7 });
const count: SysStatisticCount = {
userCount,
userRegisterCountPerDay,
pipelineCount,
pipelineCreateCountPerDay,
historyCountPerDay,
};
return this.ok(count);
}
}
@@ -0,0 +1,3 @@
export interface SqlAdapter {
date(columnName: string): string;
}
@@ -0,0 +1,34 @@
import { SqliteAdapter } from './sqlite.js';
import { PostgresqlAdapter } from './postgresql.js';
import { Config, Init, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { SqlAdapter } from './d.js';
@Provide()
@Scope(ScopeEnum.Singleton)
export class DbAdapter implements SqlAdapter {
adapter: SqlAdapter;
@Config('typeorm.dataSource.default.type')
dbType: string;
@Init()
async init() {
if (this.isSqlite()) {
this.adapter = new SqliteAdapter();
} else if (this.isPostgresql()) {
this.adapter = new PostgresqlAdapter();
} else {
throw new Error(`dbType ${this.dbType} not support`);
}
}
isSqlite() {
return this.dbType === 'better-sqlite3';
}
isPostgresql() {
return this.dbType === 'postgres';
}
date(columnName: string) {
return this.adapter.date(columnName);
}
}
@@ -0,0 +1,7 @@
import { SqlAdapter } from './d.js';
export class PostgresqlAdapter implements SqlAdapter {
date(columnName: string) {
return `to_char(${columnName}, 'YYYY-MM-DD')`;
}
}
@@ -0,0 +1,7 @@
import { SqlAdapter } from './d.js';
export class SqliteAdapter implements SqlAdapter {
date(columnName: string) {
return `date(${columnName}, 'localtime')`;
}
}
@@ -1,14 +1,14 @@
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { In, MoreThan, Repository } from 'typeorm';
import { BaseService, PageReq } from '@certd/lib-server';
import { HistoryEntity } from '../entity/history.js';
import { PipelineEntity } from '../entity/pipeline.js';
import { HistoryDetail } from '../entity/vo/history-detail.js';
import { HistoryLogService } from './history-log-service.js';
import { FileItem, Pipeline, RunnableCollection } from '@certd/pipeline';
import { FileStore } from '@certd/pipeline';
import { logger } from '@certd/pipeline';
import { FileItem, FileStore, logger, Pipeline, RunnableCollection } from '@certd/pipeline';
import dayjs from 'dayjs';
import { DbAdapter } from '../../db/index.js';
/**
* 证书申请
@@ -24,6 +24,9 @@ export class HistoryService extends BaseService<HistoryEntity> {
@Inject()
logService: HistoryLogService;
@Inject()
dbAdapter: DbAdapter;
@Config('certd')
private certdConfig: any;
@@ -174,4 +177,23 @@ export class HistoryService extends BaseService<HistoryEntity> {
logger.error('删除文件失败', e);
}
}
async countPerDay(param: { days: number; userId?: any }) {
const todayEnd = dayjs().endOf('day');
const where: any = {
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
};
if (param.userId > 0) {
where.userId = param.userId;
}
const result = await this.getRepository()
.createQueryBuilder('main')
.select(`${this.dbAdapter.date('main.createTime')} AS date`) // 将UNIX时间戳转换为日期
.addSelect('COUNT(1) AS count')
.where(where)
.groupBy('date')
.getRawMany();
return result;
}
}
@@ -1,6 +1,6 @@
import { Config, Inject, Provide, Scope, ScopeEnum, sleep } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { In, MoreThan, Repository } from 'typeorm';
import { BaseService, NeedVIPException, PageReq, SysPublicSettings, SysSettingsService } from '@certd/lib-server';
import { PipelineEntity } from '../entity/pipeline.js';
import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
@@ -19,6 +19,8 @@ import { AccessGetter } from './access-getter.js';
import { CnameRecordService } from '../../cname/service/cname-record-service.js';
import { CnameProxyService } from './cname-proxy-service.js';
import { PluginConfigGetter } from '../../plugin/service/plugin-config-getter.js';
import dayjs from 'dayjs';
import { DbAdapter } from '../../db/index.js';
const runningTasks: Map<string | number, Executor> = new Map();
const freeCount = 10;
@@ -59,6 +61,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
@Config('certd')
private certdConfig: any;
@Inject()
dbAdapter: DbAdapter;
//@ts-ignore
getRepository() {
return this.repository;
@@ -71,11 +76,18 @@ export class PipelineService extends BaseService<PipelineEntity> {
async page(pageReq: PageReq<PipelineEntity>) {
const result = await super.page(pageReq);
await this.fillLastVars(result.records);
return result;
}
private async fillLastVars(records: PipelineEntity[]) {
const pipelineIds: number[] = [];
const recordMap = {};
for (const record of result.records) {
for (const record of records) {
pipelineIds.push(record.id);
recordMap[record.id] = record;
record.title = record.title + '';
}
if (pipelineIds?.length > 0) {
const vars = await this.storageService.findPipelineVars(pipelineIds);
@@ -87,8 +99,6 @@ export class PipelineService extends BaseService<PipelineEntity> {
}
}
}
return result;
}
public async registerTriggerById(pipelineId) {
@@ -467,4 +477,64 @@ export class PipelineService extends BaseService<PipelineEntity> {
logEntity.logs = JSON.stringify(history.logs);
await this.historyLogService.addOrUpdate(logEntity);
}
async count(param: { userId?: any }) {
const count = await this.repository.count({
where: {
userId: param.userId,
},
});
return count;
}
async statusCount(param: { userId?: any } = {}) {
const statusCount = await this.repository
.createQueryBuilder()
.select('status')
.addSelect('count(1)', 'count')
.where({
userId: param.userId,
})
.groupBy('status')
.getRawMany();
return statusCount;
}
async latestExpiringList({ userId }: any) {
let list = await this.repository.find({
select: {
id: true,
title: true,
status: true,
},
where: {
userId,
},
});
await this.fillLastVars(list);
list = list.filter(item => {
return item.lastVars?.certExpiresTime != null;
});
list = list.sort((a, b) => {
return a.lastVars.certExpiresTime - b.lastVars.certExpiresTime;
});
return list.slice(0, 5);
}
async createCountPerDay(param: { days: number } = { days: 7 }) {
const todayEnd = dayjs().endOf('day');
const result = await this.getRepository()
.createQueryBuilder('main')
.select(`${this.dbAdapter.date('main.createTime')} AS date`) // 将UNIX时间戳转换为日期
.addSelect('COUNT(1) AS count')
.where({
// 0点
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
})
.groupBy('date')
.getRawMany();
return result;
}
}
@@ -49,6 +49,9 @@ export class RoleService extends BaseService<RoleEntity> {
}
async getPermissionByRoleIds(roleIds: any) {
if (!roleIds || roleIds.length === 0) {
return [];
}
return await this.permissionService.repository
.createQueryBuilder('permission')
.innerJoinAndSelect(RolePermissionEntity, 'rp', 'rp.permissionId = permission.id and rp.roleId in (:...roleIds)', { roleIds })
@@ -1,21 +1,18 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { MoreThan, Repository } from 'typeorm';
import { UserEntity } from '../entity/user.js';
import * as _ from 'lodash-es';
import md5 from 'md5';
import { CommonException, FileService } from '@certd/lib-server';
import { BaseService } from '@certd/lib-server';
import { BaseService, CommonException, Constants, FileService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
import { RoleService } from './role-service.js';
import { PermissionService } from './permission-service.js';
import { UserRoleService } from './user-role-service.js';
import { Constants } from '@certd/lib-server';
import { UserRoleEntity } from '../entity/user-role.js';
import bcrypt from 'bcryptjs';
import { SysSettingsService } from '@certd/lib-server';
import { SysInstallInfo } from '@certd/lib-server';
import { RandomUtil } from '../../../../utils/random.js';
import dayjs from 'dayjs';
import { DbAdapter } from '../../../db/index.js';
import { utils } from '@certd/pipeline';
/**
* 系统用户
*/
@@ -36,6 +33,8 @@ export class UserService extends BaseService<UserEntity> {
@Inject()
fileService: FileService;
@Inject()
dbAdapter: DbAdapter;
//@ts-ignore
getRepository() {
@@ -116,7 +115,7 @@ export class UserService extends BaseService<UserEntity> {
private async genPassword(rawPassword: any, passwordVersion: number) {
if (passwordVersion == null || passwordVersion <= 1) {
return md5(rawPassword);
return utils.hash.md5(rawPassword);
}
const salt = bcrypt.genSaltSync(10);
const plainPassword = await this.buildPlainPassword(rawPassword);
@@ -245,4 +244,29 @@ export class UserService extends BaseService<UserEntity> {
status,
});
}
async count(param: { userId?: any } = {}) {
const count = await this.repository.count({
where: {
id: param.userId,
},
});
return count;
}
async registerCountPerDay(param: { days: number } = { days: 7 }) {
const todayEnd = dayjs().endOf('day');
const result = await this.getRepository()
.createQueryBuilder('main')
.select(`${this.dbAdapter.date('main.createTime')} AS date`) // 将UNIX时间戳转换为日期
.addSelect('COUNT(1) AS count')
.where({
// 0点
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
})
.groupBy('date')
.getRawMany();
return result;
}
}
@@ -18,7 +18,8 @@ export class CloudflareAccess extends BaseAccess {
component: {
placeholder: 'api token,用户 API 令牌',
},
helper: '前往 https://dash.cloudflare.com/profile/api-tokens 获取API令牌, token权限必须包含:[Zone区域-Zone区域-Edit编辑], [Zone区域-DNS-Edit编辑]',
helper:
'前往 [获取API令牌](https://dash.cloudflare.com/profile/api-tokens),注意是令牌,不是密钥。\n token权限必须包含:[Zone区域-Zone区域-Edit编辑], [Zone区域-DNS-Edit编辑]',
required: true,
encrypt: true,
})
@@ -1,5 +1,5 @@
import { AccessInput, BaseAccess, IsAccess } from '@certd/pipeline';
import { isDev } from "../../utils/env.js";
import { isDev } from '../../utils/env.js';
/**
* 这个注解将注册一个授权配置
@@ -6,6 +6,7 @@ import { SshClient } from '../../lib/ssh.js';
title: '执行远程主机脚本命令',
icon: 'tabler:brand-powershell',
group: pluginGroups.host.key,
desc: '可以执行重启nginx等操作让证书生效',
input: {},
default: {
strategy: {
@@ -6,7 +6,7 @@ import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from
@IsTaskPlugin({
name: 'HauweiDeployCertToCDN',
title: '部署证书至华为云CDN',
icon: 'ant-design:huawei-outlined',
icon: 'svg:icon-huawei',
group: pluginGroups.huawei.key,
desc: '',
default: {
+1 -3
View File
@@ -1,3 +1 @@
export function isDev() {
return process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'local';
}
export { isDev } from '@certd/pipeline';
Binary file not shown.
Binary file not shown.