2024-07-15 00:30:33 +08:00
|
|
|
|
import {
|
|
|
|
|
|
AbstractDnsProvider,
|
|
|
|
|
|
CreateRecordOptions,
|
|
|
|
|
|
IsDnsProvider,
|
|
|
|
|
|
RemoveRecordOptions,
|
|
|
|
|
|
} from '@certd/plugin-cert';
|
|
|
|
|
|
import { Autowire, HttpClient, ILogger } from '@certd/pipeline';
|
|
|
|
|
|
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-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');
|
|
|
|
|
|
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') {
|
|
|
|
|
|
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;
|
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-06-15 02:17:34 +08:00
|
|
|
|
const record = res.result as CloudflareRecord;
|
2024-07-15 00:30:33 +08:00
|
|
|
|
this.logger.info(
|
|
|
|
|
|
`添加域名解析成功:fullRecord=${fullRecord},value=${value}`
|
|
|
|
|
|
);
|
|
|
|
|
|
this.logger.info(`dns解析记录:${JSON.stringify(record)}`);
|
2024-06-14 01:22:07 +08:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 删除dns解析记录,清理申请痕迹
|
|
|
|
|
|
* @param options
|
|
|
|
|
|
*/
|
2024-07-15 00:30:33 +08:00
|
|
|
|
async removeRecord(
|
|
|
|
|
|
options: RemoveRecordOptions<CloudflareRecord>
|
|
|
|
|
|
): Promise<void> {
|
2024-06-11 01:55:29 +08:00
|
|
|
|
const { fullRecord, value, record } = options;
|
2024-06-15 02:17:34 +08:00
|
|
|
|
this.logger.info('删除域名解析:', fullRecord, value);
|
2024-07-15 00:30:33 +08:00
|
|
|
|
if (!record) {
|
2024-06-15 02:17:34 +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');
|
|
|
|
|
|
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();
|