2026-01-23 16:56:01 +08:00
|
|
|
import { AbstractDnsProvider, CreateRecordOptions, DomainRecord, IsDnsProvider, RemoveRecordOptions } from "@certd/plugin-cert";
|
2025-09-22 23:30:28 +08:00
|
|
|
import { XinnetAccess } from "./access.js";
|
2026-01-08 15:33:26 +08:00
|
|
|
import { XinnetClient } from "@certd/plugin-plus";
|
2026-01-23 16:56:01 +08:00
|
|
|
import { PageRes, PageSearch } from "@certd/pipeline";
|
2025-09-22 23:30:28 +08:00
|
|
|
|
|
|
|
|
export type XinnetRecord = {
|
2025-09-24 00:55:31 +08:00
|
|
|
recordId: number;
|
|
|
|
|
recordFullName: string;
|
|
|
|
|
recordValue: string;
|
|
|
|
|
type: string;
|
|
|
|
|
serviceCode: string;
|
|
|
|
|
dcpCookie: string;
|
2025-09-22 23:30:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 这里通过IsDnsProvider注册一个dnsProvider
|
|
|
|
|
@IsDnsProvider({
|
2025-09-24 00:55:31 +08:00
|
|
|
name: "xinnet",
|
|
|
|
|
title: "新网",
|
|
|
|
|
desc: "新网域名解析",
|
2025-12-27 02:20:01 +08:00
|
|
|
icon: "svg:icon-xinnet",
|
2025-09-22 23:30:28 +08:00
|
|
|
// 这里是对应的 cloudflare的access类型名称
|
2025-09-24 00:55:31 +08:00
|
|
|
accessType: "xinnet",
|
|
|
|
|
order: 7
|
2025-09-22 23:30:28 +08:00
|
|
|
})
|
|
|
|
|
export class XinnetProvider extends AbstractDnsProvider<XinnetRecord> {
|
|
|
|
|
access!: XinnetAccess;
|
|
|
|
|
|
|
|
|
|
async onInstance() {
|
|
|
|
|
//一些初始化的操作
|
|
|
|
|
// 也可以通过ctx成员变量传递context
|
|
|
|
|
this.access = this.ctx.access as XinnetAccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建dns解析记录,用于验证域名所有权
|
|
|
|
|
*/
|
|
|
|
|
async createRecord(options: CreateRecordOptions): Promise<XinnetRecord> {
|
|
|
|
|
/**
|
|
|
|
|
* fullRecord: '_acme-challenge.test.example.com',
|
|
|
|
|
* value: 一串uuid
|
|
|
|
|
* type: 'TXT',
|
|
|
|
|
* domain: 'example.com'
|
|
|
|
|
*/
|
2025-09-24 00:55:31 +08:00
|
|
|
const { fullRecord, hostRecord, value, type, domain } = options;
|
|
|
|
|
this.logger.info("添加域名解析:", fullRecord, value, type, domain);
|
|
|
|
|
|
|
|
|
|
const client = new XinnetClient({
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
access: this.access,
|
|
|
|
|
http: this.http
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await client.getDomainList({
|
|
|
|
|
searchKey: domain
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res.list || res.list.length == 0) {
|
|
|
|
|
throw new Error("域名不存在");
|
|
|
|
|
}
|
|
|
|
|
const serviceCode = res.list[0].serviceCode;
|
|
|
|
|
|
|
|
|
|
const dcpCookie = await client.getDcpCookie({
|
|
|
|
|
serviceCode
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const recordRes = await client.addDomainDnsRecord({
|
|
|
|
|
recordName: hostRecord,
|
|
|
|
|
type: type,
|
|
|
|
|
recordValue: value
|
|
|
|
|
}, {
|
|
|
|
|
dcpCookie,
|
|
|
|
|
serviceCode
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
...recordRes,
|
|
|
|
|
serviceCode,
|
|
|
|
|
dcpCookie
|
|
|
|
|
};
|
2025-09-22 23:30:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除dns解析记录,清理申请痕迹
|
|
|
|
|
* @param options
|
|
|
|
|
*/
|
2025-09-23 00:56:08 +08:00
|
|
|
async removeRecord(options: RemoveRecordOptions<XinnetRecord>): Promise<void> {
|
2025-09-24 00:55:31 +08:00
|
|
|
const client = new XinnetClient({
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
access: this.access,
|
|
|
|
|
http: this.http
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const recordRes = options.recordRes;
|
|
|
|
|
let dcpCookie = recordRes.dcpCookie;
|
|
|
|
|
if (!dcpCookie) {
|
|
|
|
|
dcpCookie = await client.getDcpCookie({
|
|
|
|
|
serviceCode: recordRes.serviceCode
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await client.deleteDomainDnsRecord(recordRes, {
|
|
|
|
|
dcpCookie,
|
|
|
|
|
serviceCode: recordRes.serviceCode
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2025-09-22 23:30:28 +08:00
|
|
|
}
|
2026-01-23 16:56:01 +08:00
|
|
|
|
|
|
|
|
async getDomainListPage(req: PageSearch): Promise<PageRes<DomainRecord>> {
|
|
|
|
|
|
|
|
|
|
const client = new XinnetClient({
|
|
|
|
|
logger: this.logger,
|
|
|
|
|
access: this.access,
|
|
|
|
|
http: this.http
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await client.getDomainList(req);
|
|
|
|
|
|
|
|
|
|
const list = res.list.map((item) => ({
|
|
|
|
|
domain: item.domainName,
|
|
|
|
|
id: item.domainName
|
|
|
|
|
}));
|
|
|
|
|
return {
|
|
|
|
|
list: list || [],
|
|
|
|
|
total: res.totalRows || 0
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-22 23:30:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//实例化这个provider,将其自动注册到系统中
|
2025-09-23 00:56:08 +08:00
|
|
|
new XinnetProvider();
|