perf: cname 域名映射记录可读性优化

This commit is contained in:
xiaojunnuo
2024-11-01 18:09:32 +08:00
parent 7ad4b55ee0
commit b1117ed54a
17 changed files with 190 additions and 35 deletions

View File

@@ -41,6 +41,7 @@ export class FileController extends BaseController {
}
const filePath = this.fileService.getFile(key, userId);
this.ctx.response.attachment(filePath);
this.ctx.response.set('Cache-Control', 'public,max-age=2592000');
await send(this.ctx, filePath);
}
}

View File

@@ -21,7 +21,7 @@ export class CnameProviderController extends BaseController {
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body: any) {
body.userId = this.getUserId();
const res = await this.providerService.find({});
const res = await this.providerService.list({});
return this.ok(res);
}
}

View File

@@ -40,7 +40,8 @@ export class CnameRecordController extends CrudController<CnameRecordService> {
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body: any) {
body.userId = this.getUserId();
return super.list(body);
const list = await this.getService().list(body);
return this.ok(list);
}
@Post('/add', { summary: Constants.per.authOnly })

View File

@@ -34,4 +34,6 @@ export class CnameProviderEntity {
default: () => 'CURRENT_TIMESTAMP',
})
updateTime: Date;
title: string;
}

View File

@@ -1,8 +1,9 @@
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService, ValidateException } from '@certd/lib-server';
import { CnameProviderEntity } from '../entity/cname_provider.js';
import { BaseService, ListReq, ValidateException } from '@certd/lib-server';
import { CnameProviderEntity } from '../entity/cname-provider.js';
import { CommonProviders } from './common-provider.js';
/**
* 授权
@@ -19,7 +20,7 @@ export class CnameProviderService extends BaseService<CnameProviderEntity> {
}
async getDefault() {
return await this.repository.findOne({ where: { isDefault: true } });
return await this.repository.findOne({ where: { isDefault: true, disabled: false } });
}
/**
* 新增
@@ -80,10 +81,24 @@ export class CnameProviderService extends BaseService<CnameProviderEntity> {
if (def) {
return def;
}
const founds = await this.repository.find({ take: 1, order: { createTime: 'DESC' } });
const founds = await this.repository.find({ take: 1, order: { createTime: 'DESC' }, where: { disabled: false } });
if (founds && founds.length > 0) {
return founds[0];
}
return null;
return CommonProviders[0] as CnameProviderEntity;
}
async list(req: ListReq): Promise<any[]> {
const list = await super.list(req);
return [...list, ...CommonProviders];
}
async info(id: any, infoIgnoreProperty?: any): Promise<any | null> {
if (id < 0) {
//使用公共provider
return CommonProviders.find(p => p.id === id);
}
return await super.info(id, infoIgnoreProperty);
}
}

View File

@@ -1,16 +1,16 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService, ValidateException } from '@certd/lib-server';
import { BaseService, PlusService, ValidateException } from '@certd/lib-server';
import { CnameRecordEntity, CnameRecordStatusType } from '../entity/cname-record.js';
import { v4 as uuidv4 } from 'uuid';
import { createDnsProvider, IDnsProvider, parseDomain } from '@certd/plugin-cert';
import { cache, CnameProvider, http, logger, utils } from '@certd/pipeline';
import { AccessService } from '../../pipeline/service/access-service.js';
import { isDev } from '../../../utils/env.js';
import { walkTxtRecord } from '@certd/acme-client';
import { CnameProviderService } from './cname-provider-service.js';
import { CnameProviderEntity } from '../entity/cname_provider.js';
import { CnameProviderEntity } from '../entity/cname-provider.js';
import { CommonDnsProvider } from './common-provider.js';
type CnameCheckCacheValue = {
validating: boolean;
@@ -34,6 +34,10 @@ export class CnameRecordService extends BaseService<CnameRecordEntity> {
@Inject()
accessService: AccessService;
@Inject()
plusService: PlusService;
//@ts-ignore
getRepository() {
return this.repository;
@@ -85,8 +89,8 @@ export class CnameRecordService extends BaseService<CnameRecordEntity> {
}
param.hostRecord = hostRecord;
const cnameKey = uuidv4().replaceAll('-', '');
param.recordValue = `${cnameKey}.${cnameProvider.domain}`;
const cnameKey = utils.id.simpleNanoId();
param.recordValue = `${param.domain}.${cnameKey}.${cnameProvider.domain}`;
}
async update(param: any) {
@@ -189,6 +193,15 @@ export class CnameRecordService extends BaseService<CnameRecordEntity> {
if (cnameProvider == null) {
throw new ValidateException(`CNAME服务:${bean.cnameProviderId} 已被删除请修改CNAME记录重新选择CNAME服务`);
}
if (cnameProvider.id < 0) {
//公共CNAME
return new CommonDnsProvider({
config: cnameProvider,
plusService: this.plusService,
});
}
const access = await this.accessService.getById(cnameProvider.accessId, cnameProvider.userId);
const context = { access, logger, http, utils };
const dnsProvider: IDnsProvider = await createDnsProvider({

View File

@@ -0,0 +1,61 @@
import { CreateRecordOptions, DnsProviderContext, IDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
import { PlusService } from '@certd/lib-server';
export type CnameProvider = {
id: number;
domain: string;
title: string;
};
export const CommonProviders = [
{
id: -1,
domain: 'cname.certd.com.cn',
title: '公共CNAME服务',
},
];
export class CommonDnsProvider implements IDnsProvider {
ctx: DnsProviderContext;
config: CnameProvider;
plusService: PlusService;
constructor(opts: { config: CnameProvider; plusService: PlusService }) {
this.config = opts.config;
this.plusService = opts.plusService;
}
async onInstance() {}
async createRecord(options: CreateRecordOptions) {
if (!this.config.domain.endsWith(options.domain)) {
throw new Error('cname服务域名不匹配');
}
const res = await this.plusService.requestWithoutSign({
url: '/activation/certd/cname/recordCreate',
data: {
subjectId: this.plusService.getSubjectId(),
domain: options.domain,
hostRecord: options.hostRecord,
recordValue: options.value,
providerId: this.config.id,
},
});
return res;
}
async removeRecord(options: RemoveRecordOptions<any>) {
const res = await this.plusService.requestWithoutSign({
url: '/activation/certd/cname/recordRemove',
data: {
subjectId: this.plusService.getSubjectId(),
domain: options.recordReq.domain,
hostRecord: options.recordReq.hostRecord,
recordValue: options.recordReq.value,
recordId: options.recordRes.id,
providerId: this.config.id,
},
});
return res;
}
setCtx(ctx: DnsProviderContext): void {
this.ctx = ctx;
}
}