2024-07-15 00:30:33 +08:00
|
|
|
import * as _ from 'lodash-es';
|
|
|
|
|
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
|
|
|
|
|
import { Autowire, ILogger } from '@certd/pipeline';
|
|
|
|
|
import { HuaweiAccess } from '../access/index.js';
|
|
|
|
|
import { ApiRequestOptions, HuaweiYunClient } from '@certd/lib-huawei';
|
2023-05-09 13:52:25 +08:00
|
|
|
|
|
|
|
|
export type SearchRecordOptions = {
|
|
|
|
|
zoneId: string;
|
|
|
|
|
} & CreateRecordOptions;
|
2023-05-09 09:19:17 +08:00
|
|
|
|
|
|
|
|
@IsDnsProvider({
|
2024-05-27 18:38:41 +08:00
|
|
|
name: 'huawei',
|
|
|
|
|
title: '华为云',
|
|
|
|
|
desc: '华为云DNS解析提供商',
|
|
|
|
|
accessType: 'huawei',
|
2023-05-09 09:19:17 +08:00
|
|
|
})
|
2024-07-15 00:30:33 +08:00
|
|
|
export class HuaweiDnsProvider extends AbstractDnsProvider {
|
2023-05-09 13:52:25 +08:00
|
|
|
client!: HuaweiYunClient;
|
2023-05-09 09:19:17 +08:00
|
|
|
@Autowire()
|
|
|
|
|
access!: HuaweiAccess;
|
|
|
|
|
@Autowire()
|
|
|
|
|
logger!: ILogger;
|
2024-05-27 18:38:41 +08:00
|
|
|
domainEndpoint = 'https://domains-external.myhuaweicloud.com';
|
|
|
|
|
dnsEndpoint = 'https://dns.cn-south-1.myhuaweicloud.com';
|
2023-05-09 10:16:49 +08:00
|
|
|
async onInstance() {
|
2023-05-09 09:19:17 +08:00
|
|
|
const access: any = this.access;
|
|
|
|
|
this.client = new HuaweiYunClient(access);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDomainList() {
|
2023-05-09 13:52:25 +08:00
|
|
|
const url = `${this.dnsEndpoint}/v2/zones`;
|
2023-05-09 09:19:17 +08:00
|
|
|
const ret = await this.client.request({
|
|
|
|
|
url,
|
2024-05-27 18:38:41 +08:00
|
|
|
method: 'GET',
|
2023-05-09 09:19:17 +08:00
|
|
|
});
|
2023-05-09 13:52:25 +08:00
|
|
|
return ret.zones;
|
2023-05-09 09:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async matchDomain(dnsRecord: string) {
|
2023-05-09 13:52:25 +08:00
|
|
|
const zoneList = await this.getDomainList();
|
|
|
|
|
let zoneRecord = null;
|
|
|
|
|
for (const item of zoneList) {
|
2024-05-27 18:38:41 +08:00
|
|
|
if (_.endsWith(dnsRecord + '.', item.name)) {
|
2023-05-09 13:52:25 +08:00
|
|
|
zoneRecord = item;
|
2023-05-09 09:19:17 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-09 13:52:25 +08:00
|
|
|
if (!zoneRecord) {
|
2024-05-27 18:38:41 +08:00
|
|
|
throw new Error('can not find Domain ,' + dnsRecord);
|
2023-05-09 09:19:17 +08:00
|
|
|
}
|
2023-05-09 13:52:25 +08:00
|
|
|
return zoneRecord;
|
2023-05-09 09:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
2023-05-09 13:52:25 +08:00
|
|
|
async searchRecord(options: SearchRecordOptions): Promise<any> {
|
|
|
|
|
const req: ApiRequestOptions = {
|
|
|
|
|
url: `${this.dnsEndpoint}/v2/zones/${options.zoneId}/recordsets?name=${options.fullRecord}.`,
|
2024-05-27 18:38:41 +08:00
|
|
|
method: 'GET',
|
2023-05-09 09:19:17 +08:00
|
|
|
};
|
2023-05-09 13:52:25 +08:00
|
|
|
const ret = await this.client.request(req);
|
|
|
|
|
return ret.recordsets;
|
2023-05-09 09:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createRecord(options: CreateRecordOptions): Promise<any> {
|
|
|
|
|
const { fullRecord, value, type } = options;
|
2024-05-27 18:38:41 +08:00
|
|
|
this.logger.info('添加域名解析:', fullRecord, value);
|
2023-05-09 13:52:25 +08:00
|
|
|
const zoneRecord = await this.matchDomain(fullRecord);
|
|
|
|
|
const zoneId = zoneRecord.id;
|
2023-05-09 09:19:17 +08:00
|
|
|
|
2023-05-09 13:52:25 +08:00
|
|
|
const records: any = await this.searchRecord({
|
|
|
|
|
zoneId,
|
|
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
if (records && records.length > 0) {
|
|
|
|
|
for (const record of records) {
|
|
|
|
|
await this.removeRecord({
|
2023-05-23 18:01:20 +08:00
|
|
|
record,
|
2023-05-09 13:52:25 +08:00
|
|
|
...options,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-09 09:19:17 +08:00
|
|
|
|
|
|
|
|
try {
|
2023-05-09 13:52:25 +08:00
|
|
|
const req: ApiRequestOptions = {
|
|
|
|
|
url: `${this.dnsEndpoint}/v2/zones/${zoneId}/recordsets`,
|
2024-05-27 18:38:41 +08:00
|
|
|
method: 'POST',
|
2023-05-09 13:52:25 +08:00
|
|
|
data: {
|
2024-05-27 18:38:41 +08:00
|
|
|
name: fullRecord + '.',
|
2023-05-09 13:52:25 +08:00
|
|
|
type,
|
|
|
|
|
records: [`"${value}"`],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const ret = await this.client.request(req);
|
2024-05-27 18:38:41 +08:00
|
|
|
this.logger.info('添加域名解析成功:', value, ret);
|
2023-05-09 13:52:25 +08:00
|
|
|
return ret;
|
2023-05-09 09:19:17 +08:00
|
|
|
} catch (e: any) {
|
2024-05-27 18:38:41 +08:00
|
|
|
if (e.code === 'DNS.0312') {
|
2023-05-09 09:19:17 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2024-05-27 18:38:41 +08:00
|
|
|
this.logger.info('添加域名解析出错', e);
|
2023-05-09 09:19:17 +08:00
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-15 02:17:34 +08:00
|
|
|
async removeRecord(options: RemoveRecordOptions<any>): Promise<any> {
|
2023-05-09 09:19:17 +08:00
|
|
|
const { fullRecord, value, record } = options;
|
2023-05-09 13:52:25 +08:00
|
|
|
const req: ApiRequestOptions = {
|
|
|
|
|
url: `${this.dnsEndpoint}/v2/zones/${record.zone_id}/recordsets/${record.id}`,
|
2024-05-27 18:38:41 +08:00
|
|
|
method: 'DELETE',
|
2023-05-09 09:19:17 +08:00
|
|
|
};
|
|
|
|
|
|
2023-05-09 13:52:25 +08:00
|
|
|
const ret = await this.client.request(req);
|
2024-05-27 18:38:41 +08:00
|
|
|
this.logger.info('删除域名解析成功:', fullRecord, value, ret.RecordId);
|
2023-05-09 09:19:17 +08:00
|
|
|
return ret.RecordId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-09 09:56:31 +08:00
|
|
|
|
|
|
|
|
new HuaweiDnsProvider();
|