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

139 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-09-02 18:36:12 +08:00
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
2024-07-15 00:30:33 +08:00
import { Autowire, ILogger } from '@certd/pipeline';
2024-09-05 18:00:45 +08:00
import { AliyunAccess, AliyunClient } from '@certd/plugin-plus';
2022-11-08 22:10:42 +08:00
@IsDnsProvider({
name: 'aliyun',
title: '阿里云',
desc: '阿里云DNS解析提供商',
accessType: 'aliyun',
2022-11-08 22:10:42 +08:00
})
2024-07-15 00:30:33 +08:00
export class AliyunDnsProvider extends AbstractDnsProvider {
2022-11-08 22:10:42 +08:00
client: any;
2023-01-11 20:39:48 +08:00
@Autowire()
access!: AliyunAccess;
@Autowire()
logger!: ILogger;
2023-05-09 10:16:49 +08:00
async onInstance() {
2022-11-08 22:10:42 +08:00
const access: any = this.access;
2024-09-05 18:00:45 +08:00
this.client = new AliyunClient({ logger: this.logger });
2024-09-05 18:00:45 +08:00
await this.client.init({
2022-11-08 22:10:42 +08:00
accessKeyId: access.accessKeyId,
accessKeySecret: access.accessKeySecret,
endpoint: 'https://alidns.aliyuncs.com',
apiVersion: '2015-01-09',
});
2022-11-08 22:10:42 +08:00
}
//
// async getDomainList() {
// const params = {
// RegionId: 'cn-hangzhou',
// PageSize: 100,
// };
//
// const requestOption = {
// method: 'POST',
// };
//
// const ret = await this.client.request(
// 'DescribeDomains',
// params,
// requestOption
// );
// return ret.Domains.Domain;
// }
//
// async matchDomain(dnsRecord: string) {
// const list = await this.getDomainList();
// let domain = null;
// const domainList = [];
// for (const item of list) {
// domainList.push(item.DomainName);
// if (_.endsWith(dnsRecord, item.DomainName)) {
// domain = item.DomainName;
// break;
// }
// }
// if (!domain) {
// throw new Error(
// `can not find Domain :${dnsRecord} ,list: ${JSON.stringify(domainList)}`
// );
// }
// return domain;
// }
//
// async getRecords(domain: string, rr: string, value: string) {
// const params: any = {
// RegionId: 'cn-hangzhou',
// DomainName: domain,
// RRKeyWord: rr,
// ValueKeyWord: undefined,
// };
// if (value) {
// params.ValueKeyWord = value;
// }
//
// const requestOption = {
// method: 'POST',
// };
//
// const ret = await this.client.request(
// 'DescribeDomainRecords',
// params,
// requestOption
// );
// return ret.DomainRecords.Record;
// }
2022-11-08 22:10:42 +08:00
async createRecord(options: CreateRecordOptions): Promise<any> {
2024-07-15 00:30:33 +08:00
const { fullRecord, value, type, domain } = options;
this.logger.info('添加域名解析:', fullRecord, value, domain);
// const domain = await this.matchDomain(fullRecord);
const rr = fullRecord.replace('.' + domain, '');
2022-11-08 22:10:42 +08:00
const params = {
RegionId: 'cn-hangzhou',
2022-11-08 22:10:42 +08:00
DomainName: domain,
RR: rr,
Type: type,
Value: value,
// Line: 'oversea' // 海外
};
const requestOption = {
method: 'POST',
2022-11-08 22:10:42 +08:00
};
try {
2024-09-02 18:36:12 +08:00
const ret = await this.client.request('AddDomainRecord', params, requestOption);
this.logger.info('添加域名解析成功:', value, value, ret.RecordId);
2022-11-08 22:10:42 +08:00
return ret.RecordId;
} catch (e: any) {
if (e.code === 'DomainRecordDuplicate') {
2022-11-08 22:10:42 +08:00
return;
}
this.logger.info('添加域名解析出错', e);
2022-11-08 22:10:42 +08:00
throw e;
}
}
2024-06-15 02:17:34 +08:00
async removeRecord(options: RemoveRecordOptions<any>): Promise<any> {
const { fullRecord, value } = options.recordReq;
const record = options.recordRes;
2022-11-08 22:10:42 +08:00
const params = {
RegionId: 'cn-hangzhou',
2022-11-08 22:10:42 +08:00
RecordId: record,
};
const requestOption = {
method: 'POST',
2022-11-08 22:10:42 +08:00
};
2024-09-02 18:36:12 +08:00
const ret = await this.client.request('DeleteDomainRecord', params, requestOption);
this.logger.info('删除域名解析成功:', fullRecord, value, ret.RecordId);
2022-11-08 22:10:42 +08:00
return ret.RecordId;
}
}
2023-05-09 09:56:31 +08:00
new AliyunDnsProvider();