add deploy plugin cachefly

pull/244/head
origami-owo 2024-10-31 13:36:49 +08:00
parent ceb4b76cdb
commit 6946279f03
5 changed files with 132 additions and 0 deletions

View File

@ -11,3 +11,4 @@ export * from './plugin-doge/index.js';
export * from './plugin-qiniu/index.js';
export * from './plugin-jdcloud/index.js';
export * from './plugin-woai/index.js';
export * from './plugin-cachefly/index.js';

View File

@ -0,0 +1,37 @@
import { AccessInput, BaseAccess, IsAccess } from '@certd/pipeline';
@IsAccess({
name: 'CacheFly',
title: 'CacheFly 授权',
desc: 'CacheFly',
})
export class CacheflyAccess extends BaseAccess {
@AccessInput({
title: 'username',
component: {
placeholder: 'username',
},
required: true,
})
username = '';
@AccessInput({
title: 'password',
component: {
placeholder: 'password',
},
required: true,
encrypt: true,
})
password = '';
@AccessInput({
title: 'totp key',
component: {
placeholder: 'totp key',
},
required: true,
encrypt: true,
})
otpkey = '';
}
new CacheflyAccess();

View File

@ -0,0 +1,2 @@
export * from './plugins/index.js';
export * from './access.js';

View File

@ -0,0 +1 @@
export * from './plugin-deploy-to-cdn.js';

View File

@ -0,0 +1,91 @@
import { AbstractTaskPlugin, HttpClient, IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
import { CertInfo } from '@certd/plugin-cert';
import { CacheflyAccess } from '../access.js';
@IsTaskPlugin({
name: 'CacheFly',
title: '部署证书到 CacheFly',
desc: '部署证书到 CacheFly',
icon: 'clarity:plugin-line',
group: pluginGroups.cdn.key,
default: {
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
})
export class CacheFlyPlugin extends AbstractTaskPlugin {
@TaskInput({
title: '域名证书',
helper: '请选择前置任务输出的域名证书',
component: {
name: 'output-selector',
from: ['CertApply', 'CertApplyLego'],
},
required: true,
})
cert!: CertInfo;
@TaskInput({
title: 'Access授权',
helper: 'CacheFly 的授权',
component: {
name: 'access-selector',
type: 'CacheFly',
},
required: true,
})
accessId!: string;
http!: HttpClient;
private readonly baseApi = 'https://api.cachefly.com';
async onInstance() {
this.http = this.ctx.http;
}
private async doRequestApi(url: string, data: any = null, method = 'post', token: string | null = null) {
const headers = {
'Content-Type': 'application/json',
...(token ? { 'x-cf-authorization': `Bearer ${token}` } : {}),
};
const res = await this.http.request<any, any>({
url,
method,
data,
headers,
});
return res;
}
async execute(): Promise<void> {
const { cert, accessId } = this;
const access = (await this.accessService.getById(accessId)) as CacheflyAccess;
const otpkey = access.otpkey;
const response = await this.http.request<any, any>({
url: `https://cn-api.my-api.cn/api/totp/?key=${otpkey}`,
method: 'get',
});
const otp = response;
this.logger.info('获取到otp:', otp);
const loginResponse = await this.doRequestApi(`${this.baseApi}/api/2.6/auth/login`, {
username: access.username,
password: access.password,
otp: otp,
});
const token = loginResponse.token;
this.logger.info('获取到Token:', token);
// 更新证书
const editCertResponse = await this.doRequestApi(
`${this.baseApi}/api/2.6/certificates`,
{
certificate: cert.crt,
certificateKey: cert.key,
},
'post',
token
);
this.logger.info('证书更新成功:', editCertResponse.message);
}
}
new CacheFlyPlugin();