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

124 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-09-04 15:49:15 +08:00
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
2024-11-04 16:39:02 +08:00
2024-09-03 15:40:45 +08:00
import { WestAccess } from './access.js';
type westRecord = {
// 这里定义Record记录的数据结构,跟对应云平台接口返回值一样即可,一般是拿到id就行,用于删除txt解析记录,清理申请痕迹
code: number;
msg: string;
body: {
record_id: number;
2024-09-04 15:49:15 +08:00
};
2024-09-03 15:40:45 +08:00
};
// 这里通过IsDnsProvider注册一个dnsProvider
@IsDnsProvider({
name: 'west',
title: '西部数码',
desc: 'west dns provider',
2025-03-24 21:27:31 +08:00
icon: 'svg:icon-xibushuma',
2024-09-03 15:40:45 +08:00
// 这里是对应的云平台的access类型名称
accessType: 'west',
})
2024-09-04 15:49:15 +08:00
export class WestDnsProvider extends AbstractDnsProvider<westRecord> {
2024-09-03 15:40:45 +08:00
access!: WestAccess;
async onInstance() {
2025-04-28 21:55:23 +08:00
this.access = this.ctx.access as WestAccess
// 也可以通过ctx成员变量传递context
2024-09-03 15:40:45 +08:00
this.logger.debug('access:', this.access);
//初始化的操作
//...
}
private async doRequestApi(url: string, data: any = null, method = 'post') {
2024-09-04 15:49:15 +08:00
if (this.access.scope === 'account') {
data.apikey = this.ctx.utils.hash.md5(this.access.apikey);
2024-09-04 15:49:15 +08:00
data.username = this.access.username;
} else {
data.apidomainkey = this.access.apidomainkey;
}
const res = await this.ctx.http.request<any, any>({
2024-09-03 15:40:45 +08:00
url,
method,
data,
headers: {
2024-09-04 15:49:15 +08:00
'Content-Type': 'application/x-www-form-urlencoded',
},
2024-09-03 15:40:45 +08:00
});
if (res.msg !== 'success') {
throw new Error(`${JSON.stringify(res.msg)}`);
}
return res;
}
/**
* 创建dns解析记录,用于验证域名所有权
*/
async createRecord(options: CreateRecordOptions): Promise<any> {
/**
* 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 = {
2024-09-04 15:49:15 +08:00
act: 'dnsrec.add', // API动作类型
domain: domain, // 域名
record_type: 'TXT', // DNS记录类型
hostname: fullRecord, // 完整的记录名
record_value: value, // 记录的值
record_line: '', // 记录线路
record_ttl: 60, // TTL (生存时间),设置为60秒
2024-09-03 15:40:45 +08:00
};
2024-09-04 15:49:15 +08:00
const url = 'https://api.west.cn/API/v2/domain/dns/';
2024-09-03 15:40:45 +08:00
const res = await this.doRequestApi(url, requestBody);
2024-09-04 15:49:15 +08:00
const record = res as westRecord;
this.logger.info(`添加域名解析成功:fullRecord=${fullRecord},value=${value}`);
2024-09-03 15:40:45 +08:00
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<westRecord>): Promise<void> {
const { fullRecord, value, domain } = options.recordReq;
const record = options.recordRes;
2024-09-03 15:40:45 +08:00
this.logger.info('删除域名解析:', fullRecord, value, record);
if (!record) {
this.logger.info('record不存在');
return;
}
//这里调用删除txt dns解析记录接口
// 准备要发送到API的请求体
const requestBody = {
2024-09-04 15:49:15 +08:00
act: 'dnsrec.remove', // API动作类型
domain: domain, // 域名
2024-09-03 15:40:45 +08:00
record_id: record.body.record_id,
2024-09-04 15:49:15 +08:00
hostname: fullRecord, // 完整的记录名
record_type: 'TXT', // DNS记录类型
record_line: '', // 记录线路
2024-09-03 15:40:45 +08:00
};
2024-09-04 15:49:15 +08:00
const url = 'https://api.west.cn/API/v2/domain/dns/';
2024-09-03 15:40:45 +08:00
const res = await this.doRequestApi(url, requestBody);
2024-09-04 15:49:15 +08:00
const result = res.result;
2024-09-03 15:40:45 +08:00
this.logger.info('删除域名解析成功:', fullRecord, value, JSON.stringify(result));
}
}
//TODO 实例化这个provider,将其自动注册到系统中
2024-09-04 15:49:15 +08:00
new WestDnsProvider();