perf: openapi返回证书时挑选匹配范围最小的那一个;增加format参数,增加返回值p7b格式,增加detail返回

pull/519/head
xiaojunnuo 2025-08-28 22:39:11 +08:00
parent c09c962cb6
commit 2085bcceb6
6 changed files with 70 additions and 18 deletions

View File

@ -21,6 +21,15 @@ export type CertReaderHandleContext = {
};
export type CertReaderHandle = (ctx: CertReaderHandleContext) => Promise<void>;
export type HandleOpts = { logger: ILogger; handle: CertReaderHandle };
const formats = {
pem: ["crt", "key", "ic"],
one: ["one"],
pfx: ["pfx"],
der: ["der"],
jks: ["jks"],
p7b: ["p7b", "key"],
};
export class CertReader {
cert: CertInfo;
@ -74,8 +83,17 @@ export class CertReader {
return arr[0] + endStr;
}
toCertInfo(): CertInfo {
return this.cert;
toCertInfo(format?: string): CertInfo {
if (!format) {
return this.cert;
}
const formatArr = formats[format];
const res: any = {};
formatArr.forEach((key: string) => {
res[key] = this.cert[key];
});
return res;
}
getCrtDetail(crt: string = this.cert.crt) {

View File

@ -119,7 +119,7 @@ export async function SaveCommPluginConfigs(data: CommPluginConfig): Promise<voi
});
}
export async function savePluginSetting(req: { id: number; metadata: any }): Promise<void> {
export async function savePluginSetting(req: { name: string; sysSetting: any }): Promise<void> {
return await request({
url: apiPrefix + "/saveSetting",
method: "post",

View File

@ -1,17 +1,17 @@
import * as api from "/@/views/sys/plugin/api";
import { useFormWrapper } from "@fast-crud/fast-crud";
import { useI18n } from "/@/locales";
import { Modal, notification } from "ant-design-vue";
import { notification } from "ant-design-vue";
import ConfigEditor from "./config-editor.vue";
import { useModal } from "/@/use/use-modal";
import { ref } from "vue";
import { usePluginStore } from "/@/store/plugin";
export function usePluginConfig() {
const { openCrudFormDialog } = useFormWrapper();
const { t } = useI18n();
const pluginStore = usePluginStore();
const modal = useModal();
// @ts-ignore
async function openConfigDialog({ row, crudExpose }) {
const configEditorRef = ref();
function createCrudOptions() {

View File

@ -10,6 +10,7 @@ export type CertGetReq = {
domains?: string;
certId: number;
autoApply?:boolean;
format?:string; //默认是所有,pem,der,p12,pfx,jks,one,p7b
};
/**
@ -38,6 +39,7 @@ export class OpenCertController extends BaseOpenController {
domains: req.domains,
certId: req.certId,
autoApply: req.autoApply??false,
format: req.format
});
return this.ok(res);
}

View File

@ -27,7 +27,7 @@ export class CertInfoFacade {
@Inject()
userSettingsService : UserSettingsService
async getCertInfo(req: { domains?: string; certId?: number; userId: number,autoApply?:boolean }) {
async getCertInfo(req: { domains?: string; certId?: number; userId: number,autoApply?:boolean,format?:string }) {
const { domains, certId, userId } = req;
if (certId) {
return await this.certInfoService.getCertInfoById({ id: certId, userId });
@ -41,7 +41,7 @@ export class CertInfoFacade {
const domainArr = domains.split(',');
const matchedList = await this.certInfoService.getMatchCertList({domains:domainArr,userId})
let matched: CertInfoEntity = null
if (matchedList.length === 0 ) {
if(req.autoApply === true){
//自动申请,先创建自动申请流水线
@ -54,13 +54,7 @@ export class CertInfoFacade {
});
}
}
matched = null;
for (const item of matchedList) {
if (item.expiresTime>0 && item.expiresTime > new Date().getTime()) {
matched = item;
break
}
}
let matched = this.getMinixMatched(matchedList);
if (!matched) {
if(req.autoApply === true){
//如果没有找到有效期内的证书,则自动触发一次申请
@ -75,7 +69,38 @@ export class CertInfoFacade {
}
}
return await this.certInfoService.getCertInfoById({ id: matched.id, userId: userId });
return await this.certInfoService.getCertInfoById({ id: matched.id, userId: userId,format:req.format });
}
public getMinixMatched(matchedList: CertInfoEntity[]) {
let matched: CertInfoEntity = null;
for (const item of matchedList) {
if (item.expiresTime > 0 && item.expiresTime > new Date().getTime()) {
if (matched) {
//如果前面已经有match的值判断范围是否比上一个小
const currentStars = `-${item.domains}`.split("*");
const matchedStars = `-${matched.domains}`.split("*");
const currentLength = item.domains.split(",");
const matchedLength = matched.domains.split(",");
if (currentStars.length < matchedStars.length) {
//如果*的数量比上一个少,则替换为当前
matched = item;
} else if (currentStars.length == matchedStars.length) {
//如果*的数量相同,则比较域名数量
if (currentLength.length < matchedLength.length) {
matched = item;
}
}
} else {
matched = item;
}
}
}
return matched;
}
async createAutoPipeline(req:{domains:string[],userId:number}){

View File

@ -113,7 +113,7 @@ export class CertInfoService extends BaseService<CertInfoEntity> {
});
}
async getCertInfoById(req: { id: number; userId: number }) {
async getCertInfoById(req: { id: number; userId: number,format?:string }) {
const entity = await this.info(req.id);
if (!entity || entity.userId !== req.userId) {
throw new CodeException(Constants.res.openCertNotFound);
@ -124,7 +124,14 @@ export class CertInfoService extends BaseService<CertInfoEntity> {
}
const certInfo = JSON.parse(entity.certInfo) as CertInfo;
const certReader = new CertReader(certInfo);
return certReader.toCertInfo();
return {
...certReader.toCertInfo(req.format),
detail: {
id: entity.id,
domains: entity.domains.split(','),
notAfter: certReader.expires,
},
};
}
async updateCertByPipelineId(pipelineId: number, cert: CertInfo,file?:string,fromType = 'pipeline') {