perf: 支持dns.la

pull/361/head
xiaojunnuo 2025-03-14 00:53:31 +08:00
parent 27386ea04d
commit ee8af18d0a
4 changed files with 216 additions and 0 deletions

View File

@ -14,3 +14,4 @@ export * from './plugin-cachefly/index.js';
export * from './plugin-gcore/index.js';
export * from './plugin-qnap/index.js';
export * from './plugin-aws/index.js';
export * from './plugin-dnsla/index.js';

View File

@ -0,0 +1,41 @@
import { IsAccess, AccessInput, BaseAccess } from '@certd/pipeline';
/**
*
* certd
*/
@IsAccess({
name: 'dnsla',
title: 'dns.la授权',
icon: 'arcticons:dns-changer-3',
desc: '',
})
export class DnslaAccess extends BaseAccess {
/**
*
*/
@AccessInput({
title: 'APIID',
component: {
placeholder: 'APIID',
},
helper:"从我的账户->API密钥中获取 APIID APISecret",
required: true,
encrypt: false,
})
apiId = '';
@AccessInput({
title: 'APISecret',
component: {
placeholder: '',
},
helper:
'',
required: false,
encrypt: true,
})
apiSecret = '';
}
new DnslaAccess();

View File

@ -0,0 +1,172 @@
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from "@certd/plugin-cert";
import { DnslaAccess } from "./access.js";
export type DnslaRecord = {
id: string;
};
// 这里通过IsDnsProvider注册一个dnsProvider
@IsDnsProvider({
name: 'dnsla',
title: 'dnsla',
desc: 'dns.la',
icon: 'arcticons:dns-changer-3',
// 这里是对应的 cloudflare的access类型名称
accessType: 'dnsla',
})
export class DnslaDnsProvider extends AbstractDnsProvider<DnslaRecord> {
// 通过Autowire传递context
access!: DnslaAccess;
async onInstance() {
//一些初始化的操作
// 也可以通过ctx成员变量传递context 与Autowire效果一样
this.access = this.ctx.access as DnslaAccess;
}
private async doRequestApi(url: string, data: any = null, method = 'post') {
/**
* Basic
* API APIID APISecret
* APIID=myApiId
* APISecret=mySecret
* Basic
* # APIID APISecret
* str = "myApiId:mySecret"
* token = base64Encode(str)
* Basic
* Authorization: Basic {token}
*
* application/json
* {
* "code":200,
* "msg":"",
* "data":{}
* }
*/
const token = Buffer.from(`${this.access.apiId}:${this.access.apiSecret}`).toString('base64');
const res = await this.http.request<any, any>({
url:"https://api.dns.la"+url,
method,
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${token}`,
},
data,
});
if (res.code !== 200) {
throw new Error(res.msg);
}
return res;
}
async getDomainDetail(domain:string){
/**
*
* GET /api/domain?id=85371689655342080&domain=test.com HTTP/1.1
* Authorization: Basic {token}
*
*
* id id string iddomain
* domain string iddomain
* Response
*
* {
* "code": 200,
* "msg": "",
* "data": {
* "id": "85371689655342080",
* "createdAt": 1692856597,
* "updatedAt": 1692856598,
* "userId": "85068081529119744",
* "userAccount": "foo@foo.com",
* "assetId": "",
* "groupId": "",
*/
const url = `/api/domain?domain=${domain}`;
const res = await this.doRequestApi(url, null, 'get');
return res.data
}
/**
* dns
*/
async createRecord(options: CreateRecordOptions): Promise<DnslaRecord> {
/**
* fullRecord: '_acme-challenge.test.example.com',
* value: uuid
* type: 'TXT',
* domain: 'example.com'
*/
const { fullRecord, value, type, domain } = options;
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
const domainDetail = await this.getDomainDetail(domain);
const domainId = domainDetail.id;
this.logger.info('获取domainId成功:', domainId);
// 给domain下创建txt类型的dns解析记录fullRecord
/**
* POST /api/record HTTP/1.1
* Authorization: Basic {token}
* Content-Type: application/json; charset=utf-8
*
* {
* "domainId": "85369994254488576",
* "type": 1,
* "host": "www",
* "data": "1.1.1.1",
* "ttl": 600,
* "groupId": "",
* "lineId": "",
* "preference": 10,
* "weight": 1,
* "dominant": false
* }
*/
const url = `/api/record`;
const res = await this.doRequestApi(url, {
domainId: domainId,
type: type,
host: fullRecord,
data: value,
ttl: 60,
});
return res.data;
}
/**
* dns,
* @param options
*/
async removeRecord(options: RemoveRecordOptions<DnslaRecord>): Promise<void> {
const { fullRecord, value } = options.recordReq;
const record = options.recordRes;
this.logger.info('删除域名解析:', fullRecord, value);
if (!record) {
this.logger.info('record为空不执行删除');
return;
}
//这里调用删除txt dns解析记录接口
/**
*
* DELETE /api/record?id=85371689655342080 HTTP/1.1
* Authorization: Basic {token}
*
*/
const recordId = record.id;
const url = `/api/record?id=${recordId}`;
await this.doRequestApi(url, null, 'delete');
this.logger.info(`删除域名解析成功:fullRecord=${fullRecord},value=${value}`);
}
}
//实例化这个provider将其自动注册到系统中
new DnslaDnsProvider();

View File

@ -0,0 +1,2 @@
export * from './dns-provider.js';
export * from './access.js';