From c59cab1aaeb19f86df8e3e0d8127cbd0a9ef77f3 Mon Sep 17 00:00:00 2001 From: Moeyuuko Date: Tue, 3 Sep 2024 15:40:45 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E8=A5=BF=E9=83=A8?= =?UTF-8?q?=E6=95=B0=E7=A0=81DNS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/certd-server/src/plugins/index.ts | 1 + .../src/plugins/plugin-west/access.ts | 27 ++++ .../src/plugins/plugin-west/dns-provider.ts | 129 ++++++++++++++++++ .../src/plugins/plugin-west/index.ts | 2 + 4 files changed, 159 insertions(+) create mode 100644 packages/ui/certd-server/src/plugins/plugin-west/access.ts create mode 100644 packages/ui/certd-server/src/plugins/plugin-west/dns-provider.ts create mode 100644 packages/ui/certd-server/src/plugins/plugin-west/index.ts diff --git a/packages/ui/certd-server/src/plugins/index.ts b/packages/ui/certd-server/src/plugins/index.ts index 8350d0b4..d2ceab09 100644 --- a/packages/ui/certd-server/src/plugins/index.ts +++ b/packages/ui/certd-server/src/plugins/index.ts @@ -6,3 +6,4 @@ export * from './plugin-host/index.js'; export * from './plugin-huawei/index.js'; export * from './plugin-demo/index.js'; export * from './plugin-other/index.js'; +export * from './plugin-west/index.js'; diff --git a/packages/ui/certd-server/src/plugins/plugin-west/access.ts b/packages/ui/certd-server/src/plugins/plugin-west/access.ts new file mode 100644 index 00000000..61ac2159 --- /dev/null +++ b/packages/ui/certd-server/src/plugins/plugin-west/access.ts @@ -0,0 +1,27 @@ +import { IsAccess, AccessInput } from '@certd/pipeline'; + +/** + * 这个注解将注册一个授权配置 + * 在certd的后台管理系统中,用户可以选择添加此类型的授权 + */ +@IsAccess({ + name: 'west', + title: '西部数码授权', + desc: '', +}) +export class WestAccess { + /** + * 授权属性配置 + */ + @AccessInput({ + title: 'ApiKey', + component: { + placeholder: 'apidomainkey', + }, + helper:'前往https://www.west.cn/manager/domain/ 进入对应域名管理页面,上方点击ApiKey获取密钥', + required: true, + }) + apidomainkey = ''; +} + +new WestAccess(); diff --git a/packages/ui/certd-server/src/plugins/plugin-west/dns-provider.ts b/packages/ui/certd-server/src/plugins/plugin-west/dns-provider.ts new file mode 100644 index 00000000..2e0c522a --- /dev/null +++ b/packages/ui/certd-server/src/plugins/plugin-west/dns-provider.ts @@ -0,0 +1,129 @@ +import { + AbstractDnsProvider, + CreateRecordOptions, + IsDnsProvider, + RemoveRecordOptions, +} from '@certd/plugin-cert'; +import { Autowire, HttpClient, ILogger } from '@certd/pipeline'; +import { WestAccess } from './access.js'; + +type westRecord = { + // 这里定义Record记录的数据结构,跟对应云平台接口返回值一样即可,一般是拿到id就行,用于删除txt解析记录,清理申请痕迹 + code: number; + msg: string; + body: { + record_id: number; + } +}; + +// 这里通过IsDnsProvider注册一个dnsProvider +@IsDnsProvider({ + name: 'west', + title: '西部数码', + desc: 'west dns provider', + // 这里是对应的云平台的access类型名称 + accessType: 'west', +}) +export class westDnsProvider extends AbstractDnsProvider { + // 通过Autowire注入工具对象 + @Autowire() + access!: WestAccess; + @Autowire() + logger!: ILogger; + http!: HttpClient; + + async onInstance() { + // 也可以通过ctx成员变量传递context, 与Autowire效果一样 + this.http = this.ctx.http; + this.logger.debug('access:', this.access); + //初始化的操作 + //... + } + + private async doRequestApi(url: string, data: any = null, method = 'post') { + const res = await this.http.request({ + url, + method, + data, + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + }); + if (res.msg !== 'success') { + throw new Error(`${JSON.stringify(res.msg)}`); + } + return res; + } + + /** + * 创建dns解析记录,用于验证域名所有权 + */ + async createRecord(options: CreateRecordOptions): Promise { + /** + * options 参数说明 + * fullRecord: '_acme-challenge.example.com', + * value: 一串uuid + * type: 'TXT', + * domain: 'example.com' + */ + const { fullRecord, value, type, domain } = options; + this.logger.info('添加域名解析:', fullRecord, value, type, domain); + + // 准备要发送到API的请求体 + const requestBody = { + act: 'dnsrec.add', // API动作类型 + domain: domain, // 域名 + record_type: 'TXT', // DNS记录类型 + hostname: fullRecord, // 完整的记录名 + record_value: value, // 记录的值 + record_line: '', // 记录线路 + record_ttl: 60, // TTL (生存时间),设置为60秒 + apidomainkey: this.access.apidomainkey + }; + + const url = `https://api.west.cn/API/v2/domain/dns/`; + const res = await this.doRequestApi(url, requestBody); + const record = res as westRecord + this.logger.info( + `添加域名解析成功:fullRecord=${fullRecord},value=${value}` + ); + this.logger.info(`dns解析记录:${JSON.stringify(record)}`); + // 西部数码生效较慢 增加90秒等待 提高成功率 + this.logger.info('等待解析生效:wait 90s'); + await new Promise(resolve => setTimeout(resolve, 90000)); + return record; + } + + /** + * 删除dns解析记录,清理申请痕迹 + * @param options + */ + async removeRecord(options: RemoveRecordOptions): Promise { + const { fullRecord, value, record, domain } = options; + this.logger.info('删除域名解析:', fullRecord, value, record); + if (!record) { + this.logger.info('record不存在'); + return; + } + //这里调用删除txt dns解析记录接口 + + // 准备要发送到API的请求体 + const requestBody = { + act: 'dnsrec.remove', // API动作类型 + domain: domain, // 域名 + record_id: record.body.record_id, + hostname: fullRecord, // 完整的记录名 + record_type: 'TXT', // DNS记录类型 + record_line: '', // 记录线路 + apidomainkey: this.access.apidomainkey + }; + + const url = `https://api.west.cn/API/v2/domain/dns/`; + const res = await this.doRequestApi(url, requestBody); + const result = res.result + this.logger.info('删除域名解析成功:', fullRecord, value, JSON.stringify(result)); + } +} + +//TODO 实例化这个provider,将其自动注册到系统中 +new westDnsProvider(); diff --git a/packages/ui/certd-server/src/plugins/plugin-west/index.ts b/packages/ui/certd-server/src/plugins/plugin-west/index.ts new file mode 100644 index 00000000..db899c71 --- /dev/null +++ b/packages/ui/certd-server/src/plugins/plugin-west/index.ts @@ -0,0 +1,2 @@ +export * from './dns-provider.js'; +export * from './access.js';