mirror of https://github.com/certd/certd
perf: 支持雨云dns解析
parent
434b259525
commit
83543487e7
|
@ -60,10 +60,24 @@ export class RainyunAccess extends BaseAccess {
|
||||||
"domains.Domain": req.query??""
|
"domains.Domain": req.query??""
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return await this.doRequest({
|
const res = await this.doRequest({
|
||||||
url: `/product/domain/?options=${encodeURIComponent(JSON.stringify(options))}`,
|
url: `/product/domain/?options=${encodeURIComponent(JSON.stringify(options))}`,
|
||||||
method: "GET",
|
method: "GET",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
total: res.TotalRecords,
|
||||||
|
list: res.Records || []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async getDomainId(domain:string){
|
||||||
|
const res = await this.getDomainList({query: domain,size:1});
|
||||||
|
if (res.list.length === 0) {
|
||||||
|
throw new Error(`域名${domain}不存在 ` );
|
||||||
|
}
|
||||||
|
return res.list[0].ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
async doRequest(req:HttpRequestConfig){
|
async doRequest(req:HttpRequestConfig){
|
||||||
|
|
|
@ -19,69 +19,48 @@ export class RainyunDnsProvider extends AbstractDnsProvider {
|
||||||
async createRecord(options: CreateRecordOptions): Promise<any> {
|
async createRecord(options: CreateRecordOptions): Promise<any> {
|
||||||
|
|
||||||
const access: RainyunAccess = this.ctx.access as RainyunAccess
|
const access: RainyunAccess = this.ctx.access as RainyunAccess
|
||||||
console.log(access)
|
|
||||||
|
const domainId = await access.getDomainId(options.domain);
|
||||||
|
if (!domainId) {
|
||||||
|
throw new Error(`域名${options.domain}未找到`);
|
||||||
|
}
|
||||||
|
|
||||||
const { fullRecord,hostRecord, value, type, domain } = options;
|
const { fullRecord,hostRecord, value, type, domain } = options;
|
||||||
this.logger.info('添加域名解析:', fullRecord, value, domain);
|
this.logger.info('添加域名解析:', fullRecord, value, domain);
|
||||||
// const domain = await this.matchDomain(fullRecord);
|
|
||||||
const params = {
|
|
||||||
RegionId: 'cn-hangzhou',
|
|
||||||
DomainName: domain,
|
|
||||||
RR: hostRecord,
|
|
||||||
Type: type,
|
|
||||||
Value: value,
|
|
||||||
// Line: 'oversea' // 海外
|
|
||||||
};
|
|
||||||
|
|
||||||
const requestOption = {
|
const ret = await access.doRequest({
|
||||||
method: 'POST',
|
url: `/product/domain/${domainId}/dns`,
|
||||||
};
|
method: 'POST',
|
||||||
try {
|
data: {
|
||||||
const ret = await this.client.request('AddDomainRecord', params, requestOption);
|
host: hostRecord,
|
||||||
this.logger.info('添加域名解析成功:', JSON.stringify(options), ret.RecordId);
|
value: value,
|
||||||
return ret.RecordId;
|
type: type,
|
||||||
} catch (e: any) {
|
line: 'DEFAULT',
|
||||||
if (e.code === 'DomainRecordDuplicate') {
|
ttl: 360,
|
||||||
return;
|
},
|
||||||
}
|
});
|
||||||
if(e.code === "LastOperationNotFinished"){
|
this.logger.info('添加域名解析成功:', JSON.stringify(options), ret.ID);
|
||||||
this.logger.info('上一个操作还未完成,5s后重试')
|
return {
|
||||||
await this.ctx.utils.sleep(5000)
|
recordId:ret.ID,
|
||||||
return this.createRecord(options)
|
domainId: domainId
|
||||||
}
|
};
|
||||||
this.logger.info('添加域名解析出错', e);
|
|
||||||
this.resolveError(e, options);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolveError(e: any, req: CreateRecordOptions) {
|
|
||||||
if (e.message?.indexOf('The specified domain name does not exist') > -1) {
|
|
||||||
throw new Error(`阿里云账号中不存在此域名:${req.domain}`);
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
async removeRecord(options: RemoveRecordOptions<any>): Promise<any> {
|
async removeRecord(options: RemoveRecordOptions<any>): Promise<any> {
|
||||||
const { fullRecord, value } = options.recordReq;
|
const { fullRecord, value } = options.recordReq;
|
||||||
|
const access: RainyunAccess = this.ctx.access as RainyunAccess
|
||||||
const record = options.recordRes;
|
const record = options.recordRes;
|
||||||
const params = {
|
const ret = await access.doRequest({
|
||||||
RegionId: 'cn-hangzhou',
|
url: `/product/domain/${record.domainId}/dns`,
|
||||||
RecordId: record,
|
method: 'DELETE',
|
||||||
};
|
data:{
|
||||||
|
record_id: record.recordId
|
||||||
const requestOption = {
|
|
||||||
method: 'POST',
|
|
||||||
};
|
|
||||||
try{
|
|
||||||
const ret = await this.client.request('DeleteDomainRecord', params, requestOption);
|
|
||||||
this.logger.info('删除域名解析成功:', fullRecord, value, ret.RecordId);
|
|
||||||
return ret.RecordId;
|
|
||||||
}catch (e) {
|
|
||||||
if(e.code === "LastOperationNotFinished"){
|
|
||||||
this.logger.info('上一个操作还未完成,5s后重试')
|
|
||||||
await this.ctx.utils.sleep(5000)
|
|
||||||
return this.removeRecord(options)
|
|
||||||
}
|
}
|
||||||
throw e
|
});
|
||||||
}
|
this.logger.info('删除域名解析成功:', fullRecord, value, ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue