feat: 站点证书监控

pull/330/head
xiaojunnuo 2024-12-22 14:01:10 +08:00
parent a019956698
commit 9c8c7a7812
4 changed files with 150 additions and 7 deletions

View File

@ -0,0 +1,54 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('cd_cert_info')
export class CertInfoEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'user_id', comment: '用户id' })
userId: number;
@Column({ name: 'domain', comment: '主域名' })
domain: string;
@Column({ name: 'domains', comment: '域名' })
domains: string;
@Column({ name: 'domain_count', comment: '域名数量' })
domainCount: number;
@Column({ name: 'pipeline_id', comment: '关联流水线id' })
pipelineId: number;
@Column({ name: 'apply_time', comment: '申请时间' })
applyTime: string;
@Column({ name: 'from_type', comment: '来源' })
fromType: string;
@Column({ name: 'cert_provider', comment: '证书颁发机构' })
certProvider: string;
@Column({ name: 'expires_time', comment: '过期时间' })
expiresTime: number;
@Column({ name: 'cert_info', comment: '证书详情' })
certInfo: string;
@Column({ name: 'cert_file', comment: '证书下载' })
certFile: string;
@Column({
name: 'create_time',
comment: '创建时间',
default: () => 'CURRENT_TIMESTAMP',
})
createTime: Date;
@Column({
name: 'update_time',
comment: '修改时间',
default: () => 'CURRENT_TIMESTAMP',
})
updateTime: Date;
}

View File

@ -1,8 +1,8 @@
import { Column, PrimaryGeneratedColumn } from 'typeorm'; import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
/** /**
*/ */
// @Entity('cd_site_info') @Entity('cd_site_info')
export class SiteInfoEntity { export class SiteInfoEntity {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -12,16 +12,33 @@ export class SiteInfoEntity {
name: string; name: string;
@Column({ comment: '域名', length: 100 }) @Column({ comment: '域名', length: 100 })
domain: string; domain: string;
@Column({ name: 'cert_info', comment: '证书详情', length: 1000 }) @Column({ comment: '其他域名', length: 4096 })
domains: string;
@Column({ name: 'cert_info', comment: '证书详情', length: 4096 })
certInfo: string; certInfo: string;
@Column({ name: 'cert_status', comment: '证书状态', length: 100 }) @Column({ name: 'cert_status', comment: '证书状态', length: 100 })
certStatus: string; certStatus: string;
@Column({ name: 'cert_valid_to', comment: '证书到期时间', length: 100 })
certValidTo: Date; @Column({ name: 'cert_provider', comment: '证书颁发机构', length: 100 })
@Column({ name: 'last_time', comment: '上次检查时间' }) certProvider: string;
lastTime: Date;
@Column({ name: 'cert_expires_time', comment: '证书到期时间' })
certExpiresTime: number;
@Column({ name: 'last_check_time', comment: '上次检查时间' })
lastCheckTime: number;
@Column({ name: 'check_status', comment: '检查状态' })
checkStatus: string;
@Column({ name: 'pipeline_id', comment: '关联流水线id' }) @Column({ name: 'pipeline_id', comment: '关联流水线id' })
pipelineId: number; pipelineId: number;
@Column({ name: 'cert_info_id', comment: '证书id' })
certInfoId: number;
@Column({ name: 'disabled', comment: '禁用启用' })
disabled: boolean;
@Column({ name: 'create_time', comment: '创建时间', default: () => 'CURRENT_TIMESTAMP' }) @Column({ name: 'create_time', comment: '创建时间', default: () => 'CURRENT_TIMESTAMP' })
createTime: Date; createTime: Date;
@Column({ name: 'update_time', comment: '修改时间', default: () => 'CURRENT_TIMESTAMP' }) @Column({ name: 'update_time', comment: '修改时间', default: () => 'CURRENT_TIMESTAMP' })

View File

@ -0,0 +1,47 @@
import { Provide } from '@midwayjs/core';
import { BaseService } from '@certd/lib-server';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { CertInfoEntity } from '../entity/cert-info.js';
@Provide()
export class CertInfoService extends BaseService<CertInfoEntity> {
@InjectEntityModel(CertInfoEntity)
repository: Repository<CertInfoEntity>;
//@ts-ignore
getRepository() {
return this.repository;
}
async getUserDomainCount(userId: number) {
if (!userId) {
throw new Error('userId is required');
}
return await this.repository.sum('domainCount', {
userId,
});
}
async updateDomains(pipelineId: number, domains: string[]) {
const found = await this.repository.findOne({
where: {
pipelineId,
},
});
const bean = new CertInfoEntity();
if (found) {
//bean
bean.id = found.id;
} else {
//create
bean.pipelineId = pipelineId;
}
bean.domain = domains[0];
bean.domains = domains.join(',');
bean.domainCount = domains.length;
await this.addOrUpdate(bean);
}
}

View File

@ -0,0 +1,25 @@
import { Provide } from '@midwayjs/core';
import { BaseService } from '@certd/lib-server';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { SiteInfoEntity } from '../entity/site-info.js';
@Provide()
export class SiteInfoService extends BaseService<SiteInfoEntity> {
@InjectEntityModel(SiteInfoEntity)
repository: Repository<SiteInfoEntity>;
//@ts-ignore
getRepository() {
return this.repository;
}
async getUserMonitorCount(userId: number) {
if (!userId) {
throw new Error('userId is required');
}
return await this.repository.count({
where: { userId },
});
}
}