From 1c17b41e160944b073e1849e6f9467c3659a4bfc Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Wed, 4 Sep 2024 15:49:15 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=A5=BF=E9=83=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=94=A8=E6=88=B7=E7=BA=A7=E7=9A=84apikey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/plugin-west/dns-provider.ts | 61 +++++++++---------- 1 file changed, 29 insertions(+), 32 deletions(-) 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 index 2e0c522a..9b5be555 100644 --- a/packages/ui/certd-server/src/plugins/plugin-west/dns-provider.ts +++ b/packages/ui/certd-server/src/plugins/plugin-west/dns-provider.ts @@ -1,9 +1,4 @@ -import { - AbstractDnsProvider, - CreateRecordOptions, - IsDnsProvider, - RemoveRecordOptions, -} from '@certd/plugin-cert'; +import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert'; import { Autowire, HttpClient, ILogger } from '@certd/pipeline'; import { WestAccess } from './access.js'; @@ -13,7 +8,7 @@ type westRecord = { msg: string; body: { record_id: number; - } + }; }; // 这里通过IsDnsProvider注册一个dnsProvider @@ -24,7 +19,7 @@ type westRecord = { // 这里是对应的云平台的access类型名称 accessType: 'west', }) -export class westDnsProvider extends AbstractDnsProvider { +export class WestDnsProvider extends AbstractDnsProvider { // 通过Autowire注入工具对象 @Autowire() access!: WestAccess; @@ -41,13 +36,19 @@ export class westDnsProvider extends AbstractDnsProvider { } private async doRequestApi(url: string, data: any = null, method = 'post') { + if (this.access.scope === 'account') { + data.apikey = this.access.apikey; + data.username = this.access.username; + } else { + data.apidomainkey = this.access.apidomainkey; + } const res = await this.http.request({ url, method, data, headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - } + 'Content-Type': 'application/x-www-form-urlencoded', + }, }); if (res.msg !== 'success') { throw new Error(`${JSON.stringify(res.msg)}`); @@ -71,22 +72,19 @@ export class westDnsProvider extends AbstractDnsProvider { // 准备要发送到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 + act: 'dnsrec.add', // API动作类型 + domain: domain, // 域名 + record_type: 'TXT', // DNS记录类型 + hostname: fullRecord, // 完整的记录名 + record_value: value, // 记录的值 + record_line: '', // 记录线路 + record_ttl: 60, // TTL (生存时间),设置为60秒 }; - const url = `https://api.west.cn/API/v2/domain/dns/`; + 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}` - ); + 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'); @@ -109,21 +107,20 @@ export class westDnsProvider extends AbstractDnsProvider { // 准备要发送到API的请求体 const requestBody = { - act: 'dnsrec.remove', // API动作类型 - domain: domain, // 域名 + act: 'dnsrec.remove', // API动作类型 + domain: domain, // 域名 record_id: record.body.record_id, - hostname: fullRecord, // 完整的记录名 - record_type: 'TXT', // DNS记录类型 - record_line: '', // 记录线路 - apidomainkey: this.access.apidomainkey + hostname: fullRecord, // 完整的记录名 + record_type: 'TXT', // DNS记录类型 + record_line: '', // 记录线路 }; - const url = `https://api.west.cn/API/v2/domain/dns/`; + const url = 'https://api.west.cn/API/v2/domain/dns/'; const res = await this.doRequestApi(url, requestBody); - const result = res.result + const result = res.result; this.logger.info('删除域名解析成功:', fullRecord, value, JSON.stringify(result)); } } //TODO 实例化这个provider,将其自动注册到系统中 -new westDnsProvider(); +new WestDnsProvider();