2024-10-09 02:34:28 +08:00
|
|
|
import { CreateRecordOptions, DnsProviderContext, DnsProviderDefine, IDnsProvider, RemoveRecordOptions } from "./api.js";
|
|
|
|
|
import { dnsProviderRegistry } from "./registry.js";
|
2024-11-20 11:36:39 +08:00
|
|
|
import { HttpClient, ILogger } from "@certd/basic";
|
2025-06-05 11:25:16 +08:00
|
|
|
import punycode from "punycode.js";
|
2024-06-15 02:17:34 +08:00
|
|
|
export abstract class AbstractDnsProvider<T = any> implements IDnsProvider<T> {
|
2024-06-14 01:22:07 +08:00
|
|
|
ctx!: DnsProviderContext;
|
2024-11-20 11:36:39 +08:00
|
|
|
http!: HttpClient;
|
|
|
|
|
logger!: ILogger;
|
2024-06-14 01:22:07 +08:00
|
|
|
|
2025-04-24 11:54:54 +08:00
|
|
|
usePunyCode(): boolean {
|
2025-05-11 10:22:10 +08:00
|
|
|
//是否使用punycode来添加解析记录
|
|
|
|
|
//默认都使用原始中文域名来添加
|
2025-04-24 11:54:54 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-05 11:25:16 +08:00
|
|
|
/**
|
|
|
|
|
* 中文转英文
|
|
|
|
|
* @param domain
|
|
|
|
|
*/
|
|
|
|
|
punyCodeEncode(domain: string) {
|
|
|
|
|
return punycode.toASCII(domain);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转中文域名
|
|
|
|
|
* @param domain
|
|
|
|
|
*/
|
|
|
|
|
punyCodeDecode(domain: string) {
|
|
|
|
|
return punycode.toUnicode(domain);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 01:22:07 +08:00
|
|
|
setCtx(ctx: DnsProviderContext) {
|
|
|
|
|
this.ctx = ctx;
|
2024-11-20 11:36:39 +08:00
|
|
|
this.logger = ctx.logger;
|
|
|
|
|
this.http = ctx.http;
|
2024-06-14 01:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 12:13:57 +08:00
|
|
|
async parseDomain(fullDomain: string) {
|
|
|
|
|
return await this.ctx.domainParser.parse(fullDomain);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 02:17:34 +08:00
|
|
|
abstract createRecord(options: CreateRecordOptions): Promise<T>;
|
2024-06-14 01:22:07 +08:00
|
|
|
|
|
|
|
|
abstract onInstance(): Promise<void>;
|
|
|
|
|
|
2024-06-15 02:17:34 +08:00
|
|
|
abstract removeRecord(options: RemoveRecordOptions<T>): Promise<void>;
|
2024-06-14 01:22:07 +08:00
|
|
|
}
|
2024-10-07 03:21:16 +08:00
|
|
|
|
2024-10-09 02:34:28 +08:00
|
|
|
export async function createDnsProvider(opts: { dnsProviderType: string; context: DnsProviderContext }): Promise<IDnsProvider> {
|
|
|
|
|
const { dnsProviderType, context } = opts;
|
|
|
|
|
const dnsProviderPlugin = dnsProviderRegistry.get(dnsProviderType);
|
2025-04-27 15:50:38 +08:00
|
|
|
const DnsProviderClass = await dnsProviderPlugin.target();
|
2024-10-09 02:34:28 +08:00
|
|
|
const dnsProviderDefine = dnsProviderPlugin.define as DnsProviderDefine;
|
|
|
|
|
if (dnsProviderDefine.deprecated) {
|
2024-10-26 11:20:50 +08:00
|
|
|
context.logger.warn(dnsProviderDefine.deprecated);
|
2024-10-09 02:34:28 +08:00
|
|
|
}
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const dnsProvider: IDnsProvider = new DnsProviderClass();
|
|
|
|
|
dnsProvider.setCtx(context);
|
|
|
|
|
await dnsProvider.onInstance();
|
|
|
|
|
return dnsProvider;
|
|
|
|
|
}
|