mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
feat: 升级midway,支持esm
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from "@certd/plugin-cert";
|
||||
import { Autowire, HttpClient, ILogger } from "@certd/pipeline";
|
||||
import { CloudflareAccess } from "./access";
|
||||
import {
|
||||
AbstractDnsProvider,
|
||||
CreateRecordOptions,
|
||||
IsDnsProvider,
|
||||
RemoveRecordOptions,
|
||||
} from '@certd/plugin-cert';
|
||||
import { Autowire, HttpClient, ILogger } from '@certd/pipeline';
|
||||
import { CloudflareAccess } from './access.js';
|
||||
|
||||
export type CloudflareRecord = {
|
||||
id: string;
|
||||
@@ -15,7 +20,6 @@ export type CloudflareRecord = {
|
||||
modified_on: string;
|
||||
};
|
||||
|
||||
|
||||
// 这里通过IsDnsProvider注册一个dnsProvider
|
||||
@IsDnsProvider({
|
||||
name: 'cloudflare',
|
||||
@@ -24,94 +28,96 @@ export type CloudflareRecord = {
|
||||
// 这里是对应的 cloudflare的access类型名称
|
||||
accessType: 'cloudflare',
|
||||
})
|
||||
export class CloudflareDnsProvider extends AbstractDnsProvider<CloudflareRecord>{
|
||||
export class CloudflareDnsProvider extends AbstractDnsProvider<CloudflareRecord> {
|
||||
// 通过Autowire传递context
|
||||
@Autowire()
|
||||
logger! : ILogger;
|
||||
logger!: ILogger;
|
||||
access!: CloudflareAccess;
|
||||
http!: HttpClient;
|
||||
async onInstance() {
|
||||
//一些初始化的操作
|
||||
// 也可以通过ctx成员变量传递context, 与Autowire效果一样
|
||||
this.access = this.ctx.access as CloudflareAccess;
|
||||
this.http = this.ctx.http
|
||||
this.http = this.ctx.http;
|
||||
}
|
||||
|
||||
|
||||
async getZoneId(domain:string){
|
||||
async getZoneId(domain: string) {
|
||||
const url = `https://api.cloudflare.com/client/v4/zones?name=${domain}`;
|
||||
const res = await this.doRequestApi(url,null,"get");
|
||||
return res.result[0].id
|
||||
const res = await this.doRequestApi(url, null, 'get');
|
||||
return res.result[0].id;
|
||||
}
|
||||
|
||||
|
||||
private async doRequestApi(url: string,data:any = null,method:string="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
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建dns解析记录,用于验证域名所有权
|
||||
* 创建dns解析记录,用于验证域名所有权
|
||||
*/
|
||||
async createRecord(options: CreateRecordOptions): Promise<CloudflareRecord> {
|
||||
|
||||
/**
|
||||
* 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 { fullRecord, value, type, domain } = options;
|
||||
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
|
||||
|
||||
const zoneId = await this.getZoneId(domain);
|
||||
this.logger.info('获取zoneId成功:',zoneId)
|
||||
this.logger.info('获取zoneId成功:', zoneId);
|
||||
|
||||
// 给domain下创建txt类型的dns解析记录,fullRecord
|
||||
let url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`;
|
||||
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/dns_records`;
|
||||
const res = await this.doRequestApi(url, {
|
||||
content: value,
|
||||
name: fullRecord,
|
||||
type: type,
|
||||
ttl: 60,
|
||||
})
|
||||
content: value,
|
||||
name: fullRecord,
|
||||
type: type,
|
||||
ttl: 60,
|
||||
});
|
||||
const record = res.result as CloudflareRecord;
|
||||
this.logger.info(`添加域名解析成功:fullRecord=${fullRecord},value=${value}`);
|
||||
this.logger.info(`dns解析记录:${JSON.stringify(record)}`,)
|
||||
this.logger.info(
|
||||
`添加域名解析成功:fullRecord=${fullRecord},value=${value}`
|
||||
);
|
||||
this.logger.info(`dns解析记录:${JSON.stringify(record)}`);
|
||||
|
||||
//本接口需要返回本次创建的dns解析记录,这个记录会在删除的时候用到
|
||||
return record
|
||||
return record;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除dns解析记录,清理申请痕迹
|
||||
* @param options
|
||||
*/
|
||||
async removeRecord(options: RemoveRecordOptions<CloudflareRecord>): Promise<void> {
|
||||
async removeRecord(
|
||||
options: RemoveRecordOptions<CloudflareRecord>
|
||||
): Promise<void> {
|
||||
const { fullRecord, value, record } = options;
|
||||
this.logger.info('删除域名解析:', fullRecord, value);
|
||||
if(!record){
|
||||
if (!record) {
|
||||
this.logger.info('record不存在');
|
||||
return
|
||||
return;
|
||||
}
|
||||
//这里调用删除txt dns解析记录接口
|
||||
const zoneId = record.zone_id;
|
||||
const recordId = record.id;
|
||||
let 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}`);
|
||||
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}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './dns-provider';
|
||||
export * from './plugins';
|
||||
export * from './access';
|
||||
export * from './dns-provider.js';
|
||||
export * from './plugins/index.js';
|
||||
export * from './access.js';
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from './plugin-deploy-to-cdn';
|
||||
export * from './plugin-deploy-to-cdn.js';
|
||||
|
||||
Reference in New Issue
Block a user