2024-10-07 03:21:16 +08:00
|
|
|
|
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
|
2024-11-04 16:39:02 +08:00
|
|
|
|
import { Autowire } from '@certd/pipeline';
|
|
|
|
|
|
import { HttpClient, ILogger } from '@certd/basic';
|
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
|
import { CloudflareAccess } from './access.js';
|
2024-06-11 01:55:29 +08:00
|
|
|
|
|
2024-06-15 02:17:34 +08:00
|
|
|
|
export type CloudflareRecord = {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
type: string;
|
|
|
|
|
|
name: string;
|
|
|
|
|
|
content: string;
|
|
|
|
|
|
ttl: number;
|
|
|
|
|
|
proxied: boolean;
|
|
|
|
|
|
zone_id: string;
|
|
|
|
|
|
zone_name: string;
|
|
|
|
|
|
created_on: string;
|
|
|
|
|
|
modified_on: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 这里通过IsDnsProvider注册一个dnsProvider
|
2024-06-11 01:55:29 +08:00
|
|
|
|
@IsDnsProvider({
|
|
|
|
|
|
name: 'cloudflare',
|
2024-06-14 01:26:50 +08:00
|
|
|
|
title: 'cloudflare',
|
2024-06-15 02:17:34 +08:00
|
|
|
|
desc: 'cloudflare dns provider',
|
|
|
|
|
|
// 这里是对应的 cloudflare的access类型名称
|
2024-06-11 01:55:29 +08:00
|
|
|
|
accessType: 'cloudflare',
|
|
|
|
|
|
})
|
2024-07-15 00:30:33 +08:00
|
|
|
|
export class CloudflareDnsProvider extends AbstractDnsProvider<CloudflareRecord> {
|
2024-06-15 02:17:34 +08:00
|
|
|
|
// 通过Autowire传递context
|
2024-06-11 01:55:29 +08:00
|
|
|
|
@Autowire()
|
2024-07-15 00:30:33 +08:00
|
|
|
|
logger!: ILogger;
|
2024-06-11 01:55:29 +08:00
|
|
|
|
access!: CloudflareAccess;
|
2024-06-14 01:22:07 +08:00
|
|
|
|
http!: HttpClient;
|
2024-06-11 01:55:29 +08:00
|
|
|
|
async onInstance() {
|
2024-06-14 01:22:07 +08:00
|
|
|
|
//一些初始化的操作
|
2024-06-15 02:17:34 +08:00
|
|
|
|
// 也可以通过ctx成员变量传递context, 与Autowire效果一样
|
2024-06-14 01:22:07 +08:00
|
|
|
|
this.access = this.ctx.access as CloudflareAccess;
|
2024-07-15 00:30:33 +08:00
|
|
|
|
this.http = this.ctx.http;
|
2024-06-11 01:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
|
async getZoneId(domain: string) {
|
2024-10-22 16:21:35 +08:00
|
|
|
|
this.logger.info('获取zoneId:', domain);
|
2024-06-15 02:17:34 +08:00
|
|
|
|
const url = `https://api.cloudflare.com/client/v4/zones?name=${domain}`;
|
2024-07-15 00:30:33 +08:00
|
|
|
|
const res = await this.doRequestApi(url, null, 'get');
|
2024-10-22 16:21:35 +08:00
|
|
|
|
if (res.result.length === 0) {
|
|
|
|
|
|
throw new Error(`未找到域名${domain}的zoneId`);
|
|
|
|
|
|
}
|
2024-07-15 00:30:33 +08:00
|
|
|
|
return res.result[0].id;
|
2024-06-15 02:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-15 00:30:33 +08:00
|
|
|
|
private async doRequestApi(url: string, data: any = null, method = 'post') {
|
2024-11-17 02:01:44 +08:00
|
|
|
|
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;
|
2024-07-15 00:30:33 +08:00
|
|
|
|
}
|
2024-06-15 02:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-14 01:22:07 +08:00
|
|
|
|
/**
|
2024-07-15 00:30:33 +08:00
|
|
|
|
* 创建dns解析记录,用于验证域名所有权
|
2024-06-14 01:22:07 +08:00
|
|
|
|
*/
|
2024-06-15 02:17:34 +08:00
|
|
|
|
async createRecord(options: CreateRecordOptions): Promise<CloudflareRecord> {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* fullRecord: '_acme-challenge.test.example.com',
|
|
|
|
|
|
* value: 一串uuid
|
|
|
|
|
|
* type: 'TXT',
|
|
|
|
|
|
* domain: 'example.com'
|
|
|
|
|
|
*/
|
2024-07-15 00:30:33 +08:00
|
|
|
|
const { fullRecord, value, type, domain } = options;
|
|
|
|
|
|
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
|
2024-06-14 01:22:07 +08:00
|
|
|
|
|
2024-06-15 02:17:34 +08:00
|
|
|
|
const zoneId = await this.getZoneId(domain);
|
2024-07-15 00:30:33 +08:00
|
|
|
|
this.logger.info('获取zoneId成功:', zoneId);
|
2024-06-15 02:17:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 给domain下创建txt类型的dns解析记录,fullRecord
|
2024-07-15 00:30:33 +08:00
|
|
|
|
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`;
|
2024-06-15 02:17:34 +08:00
|
|
|
|
const res = await this.doRequestApi(url, {
|
2024-07-15 00:30:33 +08:00
|
|
|
|
content: value,
|
|
|
|
|
|
name: fullRecord,
|
|
|
|
|
|
type: type,
|
|
|
|
|
|
ttl: 60,
|
|
|
|
|
|
});
|
2024-11-17 02:01:44 +08:00
|
|
|
|
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)}`);
|
|
|
|
|
|
}
|
2024-06-15 02:17:34 +08:00
|
|
|
|
//本接口需要返回本次创建的dns解析记录,这个记录会在删除的时候用到
|
2024-07-15 00:30:33 +08:00
|
|
|
|
return record;
|
2024-06-11 01:55:29 +08:00
|
|
|
|
}
|
2024-06-15 02:17:34 +08:00
|
|
|
|
|
2024-11-17 02:01:44 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-15 02:17:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 删除dns解析记录,清理申请痕迹
|
|
|
|
|
|
* @param options
|
|
|
|
|
|
*/
|
2024-10-07 03:21:16 +08:00
|
|
|
|
async removeRecord(options: RemoveRecordOptions<CloudflareRecord>): Promise<void> {
|
|
|
|
|
|
const { fullRecord, value } = options.recordReq;
|
|
|
|
|
|
const record = options.recordRes;
|
2024-06-15 02:17:34 +08:00
|
|
|
|
this.logger.info('删除域名解析:', fullRecord, value);
|
2024-07-15 00:30:33 +08:00
|
|
|
|
if (!record) {
|
2024-11-17 02:01:44 +08:00
|
|
|
|
this.logger.info('record为空,不执行删除');
|
2024-07-15 00:30:33 +08:00
|
|
|
|
return;
|
2024-06-15 02:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
//这里调用删除txt dns解析记录接口
|
|
|
|
|
|
const zoneId = record.zone_id;
|
|
|
|
|
|
const recordId = record.id;
|
2024-07-15 00:30:33 +08:00
|
|
|
|
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records/${recordId}`;
|
|
|
|
|
|
await this.doRequestApi(url, null, 'delete');
|
2024-10-07 03:21:16 +08:00
|
|
|
|
this.logger.info(`删除域名解析成功:fullRecord=${fullRecord},value=${value}`);
|
2024-06-11 01:55:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-15 02:17:34 +08:00
|
|
|
|
//实例化这个provider,将其自动注册到系统中
|
2024-06-11 01:55:29 +08:00
|
|
|
|
new CloudflareDnsProvider();
|