mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
perf: openapi返回证书时挑选匹配范围最小的那一个;增加format参数,增加返回值p7b格式,增加detail返回
This commit is contained in:
@@ -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}){
|
||||
|
||||
@@ -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') {
|
||||
|
||||
Reference in New Issue
Block a user