mirror of https://github.com/certd/certd
perf: 优化腾讯云 cloudflare 重复解析记录时的返回值
parent
c7c4318c11
commit
90d1b68bd6
|
@ -178,7 +178,10 @@ export function createAxiosService({ logger }: { logger: Logger }) {
|
|||
);
|
||||
logger.error('返回数据:', JSON.stringify(error.response?.data));
|
||||
if (error.response?.data) {
|
||||
error.message = error.response.data.message || error.response.data.msg || error.response.data.error || error.response.data;
|
||||
const message = error.response.data.message || error.response.data.msg || error.response.data.error;
|
||||
if (typeof message === 'string') {
|
||||
error.message = message;
|
||||
}
|
||||
}
|
||||
if (error instanceof AggregateError) {
|
||||
logger.error('AggregateError', error);
|
||||
|
|
|
@ -49,19 +49,31 @@ export class CloudflareDnsProvider extends AbstractDnsProvider<CloudflareRecord>
|
|||
}
|
||||
|
||||
private async doRequestApi(url: string, data: any = null, method = 'post') {
|
||||
const res = await this.http.request<any, any>({
|
||||
url,
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this.access.apiToken}`,
|
||||
},
|
||||
data,
|
||||
});
|
||||
if (!res.success) {
|
||||
throw new Error(`${JSON.stringify(res.errors)}`);
|
||||
try {
|
||||
const res = await this.http.request<any, any>({
|
||||
url,
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${this.access.apiToken}`,
|
||||
},
|
||||
data,
|
||||
});
|
||||
|
||||
if (!res.success) {
|
||||
throw new Error(`${JSON.stringify(res.errors)}`);
|
||||
}
|
||||
return res;
|
||||
} catch (e: any) {
|
||||
const data = e.response?.data;
|
||||
if (data && data.success === false && data.errors && data.errors.length > 0) {
|
||||
if (data.errors[0].code === 81058) {
|
||||
this.logger.info('dns解析记录重复');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,14 +100,30 @@ export class CloudflareDnsProvider extends AbstractDnsProvider<CloudflareRecord>
|
|||
type: type,
|
||||
ttl: 60,
|
||||
});
|
||||
const record = res.result as CloudflareRecord;
|
||||
this.logger.info(`添加域名解析成功:fullRecord=${fullRecord},value=${value}`);
|
||||
this.logger.info(`dns解析记录:${JSON.stringify(record)}`);
|
||||
|
||||
let record: any = null;
|
||||
if (res == null) {
|
||||
//重复的记录
|
||||
this.logger.info(`dns解析记录重复,无需重复添加`);
|
||||
record = await this.findRecord(zoneId, options);
|
||||
} else {
|
||||
record = res.result as CloudflareRecord;
|
||||
this.logger.info(`添加域名解析成功:fullRecord=${fullRecord},value=${value}`);
|
||||
this.logger.info(`dns解析记录:${JSON.stringify(record)}`);
|
||||
}
|
||||
//本接口需要返回本次创建的dns解析记录,这个记录会在删除的时候用到
|
||||
return record;
|
||||
}
|
||||
|
||||
async findRecord(zoneId: string, options: CreateRecordOptions): Promise<CloudflareRecord | null> {
|
||||
const { fullRecord, value } = options;
|
||||
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records?type=TXT&name=${fullRecord}&content=${value}`;
|
||||
const res = await this.doRequestApi(url, null, 'get');
|
||||
if (res.result.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return res.result[0] as CloudflareRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除dns解析记录,清理申请痕迹
|
||||
* @param options
|
||||
|
@ -105,7 +133,7 @@ export class CloudflareDnsProvider extends AbstractDnsProvider<CloudflareRecord>
|
|||
const record = options.recordRes;
|
||||
this.logger.info('删除域名解析:', fullRecord, value);
|
||||
if (!record) {
|
||||
this.logger.info('record不存在');
|
||||
this.logger.info('record为空,不执行删除');
|
||||
return;
|
||||
}
|
||||
//这里调用删除txt dns解析记录接口
|
||||
|
|
|
@ -105,6 +105,10 @@ export class HuaweiDnsProvider extends AbstractDnsProvider {
|
|||
async removeRecord(options: RemoveRecordOptions<any>): Promise<any> {
|
||||
const { fullRecord, value } = options.recordReq;
|
||||
const record = options.recordRes;
|
||||
if (!record) {
|
||||
this.logger.info('解析记录recordId为空,不执行删除', fullRecord, value);
|
||||
return;
|
||||
}
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${record.zone_id}/recordsets/${record.id}`,
|
||||
method: 'DELETE',
|
||||
|
|
|
@ -65,15 +65,33 @@ export class TencentDnsProvider extends AbstractDnsProvider {
|
|||
} catch (e: any) {
|
||||
if (e?.code === 'InvalidParameter.DomainRecordExist') {
|
||||
this.logger.info('域名解析已存在,无需重复添加:', fullRecord, value);
|
||||
return {};
|
||||
return await this.findRecord(options);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async findRecord(options: CreateRecordOptions): Promise<any> {
|
||||
const params = {
|
||||
Domain: options.domain,
|
||||
RecordType: [options.type],
|
||||
Keyword: options.hostRecord,
|
||||
RecordValue: options.value,
|
||||
};
|
||||
const ret = await this.client.DescribeRecordFilterList(params);
|
||||
if (ret.RecordList && ret.RecordList.length > 0) {
|
||||
this.logger.info('已存在解析记录:', ret.RecordList);
|
||||
return ret.RecordList[0];
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
async removeRecord(options: RemoveRecordOptions<any>) {
|
||||
const { fullRecord, value, domain } = options.recordReq;
|
||||
const record = options.recordRes;
|
||||
if (!record) {
|
||||
this.logger.info('解析记录recordId为空,不执行删除', fullRecord, value);
|
||||
}
|
||||
const params = {
|
||||
Domain: domain,
|
||||
RecordId: record.RecordId,
|
||||
|
|
Loading…
Reference in New Issue