Files
certd/packages/ui/certd-server/src/plugins/plugin-demo/dns-provider.ts
T

107 lines
3.2 KiB
TypeScript
Raw Normal View History

import { AbstractDnsProvider, CreateRecordOptions, DomainRecord, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
2024-11-04 16:39:02 +08:00
import { PageRes, PageSearch } from '@certd/pipeline';
2024-11-04 16:39:02 +08:00
import { isDev } from '../../utils/env.js';
import { DemoAccess } from './access.js';
2024-06-15 02:17:34 +08:00
type DemoRecord = {
// 这里定义Record记录的数据结构,跟对应云平台接口返回值一样即可,一般是拿到id就行,用于删除txt解析记录,清理申请痕迹
// id:string
2024-07-15 00:30:33 +08:00
};
2024-06-15 02:17:34 +08:00
// 这里通过IsDnsProvider注册一个dnsProvider
2024-03-22 00:50:02 +08:00
@IsDnsProvider({
2024-07-15 00:30:33 +08:00
name: 'demo',
title: 'Dns提供商Demo',
desc: 'dns provider示例',
2024-11-30 01:57:09 +08:00
icon: 'clarity:plugin-line',
2024-06-15 02:17:34 +08:00
// 这里是对应的云平台的access类型名称
2024-07-15 00:30:33 +08:00
accessType: 'demo',
order: 99,
2024-03-22 00:50:02 +08:00
})
2024-06-15 02:17:34 +08:00
export class DemoDnsProvider extends AbstractDnsProvider<DemoRecord> {
2024-03-22 00:50:02 +08:00
access!: DemoAccess;
async onInstance() {
2025-04-28 21:55:23 +08:00
this.access = this.ctx.access as DemoAccess
// 也可以通过ctx成员变量传递context
2024-07-15 00:30:33 +08:00
this.logger.debug('access', this.access);
2024-03-22 00:50:02 +08:00
//初始化的操作
//...
}
2024-06-15 02:17:34 +08:00
/**
* 创建dns解析记录,用于验证域名所有权
*/
2024-03-22 00:50:02 +08:00
async createRecord(options: CreateRecordOptions): Promise<any> {
2024-06-15 02:17:34 +08:00
/**
* options 参数说明
* fullRecord: '_acme-challenge.example.com',
* value: 一串uuid
* type: 'TXT',
* domain: 'example.com'
*/
const { fullRecord, value, type, domain } = options;
2024-07-15 00:30:33 +08:00
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
2024-06-15 02:17:34 +08:00
// 调用创建dns解析记录的对应的云端接口,创建txt类型的dns解析记录
// 请根据实际接口情况调用,例如:
// const createDnsRecordUrl = "xxx"
// const record = this.http.post(createDnsRecordUrl,{
// // 授权参数
// // 创建dns解析记录的参数
// })
// //返回本次创建的dns解析记录,这个记录会在删除的时候用到
// return record
2024-03-22 00:50:02 +08:00
}
2024-06-15 02:17:34 +08:00
/**
* 删除dns解析记录,清理申请痕迹
* @param options
*/
async removeRecord(options: RemoveRecordOptions<DemoRecord>): Promise<void> {
const { fullRecord, value, domain } = options.recordReq;
const record = options.recordRes;
this.logger.info('删除域名解析:', domain, fullRecord, value, record);
2024-06-15 02:17:34 +08:00
//这里调用删除txt dns解析记录接口
//请根据实际接口情况调用,例如:
// const deleteDnsRecordUrl = "xxx"
// const res = this.http.delete(deleteDnsRecordUrl,{
// // 授权参数
// // 删除dns解析记录的参数
// })
//
2024-07-15 00:30:33 +08:00
this.logger.info('删除域名解析成功:', fullRecord, value);
2024-03-22 00:50:02 +08:00
}
/**
*
* @param req 实现获取域名列表
* @returns
*/
async getDomainListPage(req: PageSearch): Promise<PageRes<DomainRecord>> {
const res = await this.http.request({
// 请求接口获取域名列表
})
const list = []
// const list = res.Domains?.map(item => ({
// id: item.Id,
// domain: item.DomainName,
// })) || []
return {
list,
total: res.Total,
}
}
2024-03-22 00:50:02 +08:00
}
//TODO 实例化这个provider,将其自动注册到系统中
2024-10-10 02:15:05 +08:00
if (isDev()) {
2024-09-29 15:00:17 +08:00
//你的实现 要去掉这个if,不然生产环境将不会显示
new DemoDnsProvider();
}