2022-11-08 22:10:42 +08:00
|
|
|
import Core from "@alicloud/pop-core";
|
|
|
|
|
import _ from "lodash";
|
2023-01-11 20:39:48 +08:00
|
|
|
import { CreateRecordOptions, IDnsProvider, IsDnsProvider, RemoveRecordOptions } from "@certd/plugin-cert";
|
|
|
|
|
import { Autowire, ILogger } from "@certd/pipeline";
|
|
|
|
|
import { AliyunAccess } from "../access";
|
2022-11-08 22:10:42 +08:00
|
|
|
|
|
|
|
|
@IsDnsProvider({
|
|
|
|
|
name: "aliyun",
|
|
|
|
|
title: "阿里云",
|
|
|
|
|
desc: "阿里云DNS解析提供商",
|
|
|
|
|
accessType: "aliyun",
|
|
|
|
|
})
|
2022-12-29 23:52:51 +08:00
|
|
|
export class AliyunDnsProvider implements IDnsProvider {
|
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;
|
|
|
|
|
this.client = new Core({
|
|
|
|
|
accessKeyId: access.accessKeyId,
|
|
|
|
|
accessKeySecret: access.accessKeySecret,
|
|
|
|
|
endpoint: "https://alidns.aliyuncs.com",
|
|
|
|
|
apiVersion: "2015-01-09",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDomainList() {
|
|
|
|
|
const params = {
|
|
|
|
|
RegionId: "cn-hangzhou",
|
2024-01-03 14:11:21 +08:00
|
|
|
PageSize: 100,
|
2022-11-08 22:10:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
2024-01-03 11:45:58 +08:00
|
|
|
const domainList = [];
|
2022-11-08 22:10:42 +08:00
|
|
|
for (const item of list) {
|
2024-01-03 11:45:58 +08:00
|
|
|
domainList.push(item.DomainName);
|
2022-11-08 22:10:42 +08:00
|
|
|
if (_.endsWith(dnsRecord, item.DomainName)) {
|
|
|
|
|
domain = item.DomainName;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!domain) {
|
2024-01-03 11:45:58 +08:00
|
|
|
throw new Error(`can not find Domain :${dnsRecord} ,list: ${JSON.stringify(domainList)}`);
|
2022-11-08 22:10:42 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async createRecord(options: CreateRecordOptions): Promise<any> {
|
|
|
|
|
const { fullRecord, value, type } = options;
|
|
|
|
|
this.logger.info("添加域名解析:", fullRecord, value);
|
|
|
|
|
const domain = await this.matchDomain(fullRecord);
|
|
|
|
|
const rr = fullRecord.replace("." + domain, "");
|
|
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
|
RegionId: "cn-hangzhou",
|
|
|
|
|
DomainName: domain,
|
|
|
|
|
RR: rr,
|
|
|
|
|
Type: type,
|
|
|
|
|
Value: value,
|
|
|
|
|
// Line: 'oversea' // 海外
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ret = await this.client.request("AddDomainRecord", params, requestOption);
|
|
|
|
|
this.logger.info("添加域名解析成功:", value, value, ret.RecordId);
|
|
|
|
|
return ret.RecordId;
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
if (e.code === "DomainRecordDuplicate") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.logger.info("添加域名解析出错", e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
async removeRecord(options: RemoveRecordOptions): Promise<any> {
|
|
|
|
|
const { fullRecord, value, record } = options;
|
|
|
|
|
const params = {
|
|
|
|
|
RegionId: "cn-hangzhou",
|
|
|
|
|
RecordId: record,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const requestOption = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ret = await this.client.request("DeleteDomainRecord", params, requestOption);
|
|
|
|
|
this.logger.info("删除域名解析成功:", fullRecord, value, ret.RecordId);
|
|
|
|
|
return ret.RecordId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-09 09:56:31 +08:00
|
|
|
|
|
|
|
|
new AliyunDnsProvider();
|