mirror of
https://github.com/certd/certd.git
synced 2026-07-09 06:07:37 +08:00
chore: format
This commit is contained in:
@@ -1,58 +1,55 @@
|
||||
import { IsAccess, AccessInput, BaseAccess } from '@certd/pipeline';
|
||||
import { DogeClient } from './lib/index.js';
|
||||
import { IsAccess, AccessInput, BaseAccess } from "@certd/pipeline";
|
||||
import { DogeClient } from "./lib/index.js";
|
||||
|
||||
/**
|
||||
* 这个注解将注册一个授权配置
|
||||
* 在certd的后台管理系统中,用户可以选择添加此类型的授权
|
||||
*/
|
||||
@IsAccess({
|
||||
name: 'dogecloud',
|
||||
title: '多吉云',
|
||||
desc: '',
|
||||
icon: 'svg:icon-dogecloud',
|
||||
name: "dogecloud",
|
||||
title: "多吉云",
|
||||
desc: "",
|
||||
icon: "svg:icon-dogecloud",
|
||||
})
|
||||
export class DogeCloudAccess extends BaseAccess {
|
||||
/**
|
||||
* 授权属性配置
|
||||
*/
|
||||
@AccessInput({
|
||||
title: 'AccessKey',
|
||||
title: "AccessKey",
|
||||
component: {
|
||||
placeholder: 'AccessKey',
|
||||
placeholder: "AccessKey",
|
||||
},
|
||||
helper: '请前往[多吉云-密钥管理](https://console.dogecloud.com/user/keys)获取',
|
||||
helper: "请前往[多吉云-密钥管理](https://console.dogecloud.com/user/keys)获取",
|
||||
required: true,
|
||||
encrypt: false,
|
||||
})
|
||||
accessKey = '';
|
||||
accessKey = "";
|
||||
|
||||
@AccessInput({
|
||||
title: 'SecretKey',
|
||||
title: "SecretKey",
|
||||
component: {
|
||||
placeholder: 'SecretKey',
|
||||
placeholder: "SecretKey",
|
||||
},
|
||||
helper: '请前往[多吉云-密钥管理](https://console.dogecloud.com/user/keys)获取',
|
||||
helper: "请前往[多吉云-密钥管理](https://console.dogecloud.com/user/keys)获取",
|
||||
required: true,
|
||||
encrypt: true,
|
||||
})
|
||||
secretKey = '';
|
||||
secretKey = "";
|
||||
|
||||
@AccessInput({
|
||||
title: "测试",
|
||||
component: {
|
||||
name: "api-test",
|
||||
action: "TestRequest"
|
||||
action: "TestRequest",
|
||||
},
|
||||
helper: "测试授权是否正确"
|
||||
helper: "测试授权是否正确",
|
||||
})
|
||||
testRequest = true;
|
||||
|
||||
async onTestRequest() {
|
||||
const dogeClient = new DogeClient(this, this.ctx.http, this.ctx.logger);
|
||||
await dogeClient.request(
|
||||
'/cdn/domain/list.json',
|
||||
{},
|
||||
);
|
||||
await dogeClient.request("/cdn/domain/list.json", {});
|
||||
return "ok";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export * from './access.js';
|
||||
export * from './lib/index.js';
|
||||
export * from './plugins/index.js';
|
||||
export * from "./access.js";
|
||||
export * from "./lib/index.js";
|
||||
export * from "./plugins/index.js";
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import crypto from 'crypto';
|
||||
import querystring from 'querystring';
|
||||
import { DogeCloudAccess } from '../access.js';
|
||||
import { HttpClient, ILogger } from '@certd/basic';
|
||||
import crypto from "crypto";
|
||||
import querystring from "querystring";
|
||||
import { DogeCloudAccess } from "../access.js";
|
||||
import { HttpClient, ILogger } from "@certd/basic";
|
||||
|
||||
export class DogeClient {
|
||||
accessKey: string;
|
||||
secretKey: string;
|
||||
http: HttpClient;
|
||||
logger: ILogger;
|
||||
constructor(access: DogeCloudAccess, http: HttpClient,logger: ILogger) {
|
||||
constructor(access: DogeCloudAccess, http: HttpClient, logger: ILogger) {
|
||||
this.accessKey = access.accessKey;
|
||||
this.secretKey = access.secretKey;
|
||||
this.http = http;
|
||||
@@ -21,26 +21,26 @@ export class DogeClient {
|
||||
|
||||
const body = jsonMode ? JSON.stringify(data) : querystring.encode(data);
|
||||
const sign = crypto
|
||||
.createHmac('sha1', this.secretKey)
|
||||
.update(Buffer.from(apiPath + '\n' + body, 'utf8'))
|
||||
.digest('hex');
|
||||
const authorization = 'TOKEN ' + this.accessKey + ':' + sign;
|
||||
.createHmac("sha1", this.secretKey)
|
||||
.update(Buffer.from(apiPath + "\n" + body, "utf8"))
|
||||
.digest("hex");
|
||||
const authorization = "TOKEN " + this.accessKey + ":" + sign;
|
||||
const res: any = await this.http.request({
|
||||
url: 'https://api.dogecloud.com' + apiPath,
|
||||
method: 'POST',
|
||||
url: "https://api.dogecloud.com" + apiPath,
|
||||
method: "POST",
|
||||
data: body,
|
||||
responseType: 'json',
|
||||
responseType: "json",
|
||||
headers: {
|
||||
'Content-Type': jsonMode ? 'application/json' : 'application/x-www-form-urlencoded',
|
||||
"Content-Type": jsonMode ? "application/json" : "application/x-www-form-urlencoded",
|
||||
Authorization: authorization,
|
||||
},
|
||||
});
|
||||
|
||||
if (res.code == null && ignoreResNullCode) {
|
||||
//ignore
|
||||
this.logger.warn('执行出错:', res);
|
||||
this.logger.warn("执行出错:", res);
|
||||
} else if (res.code !== 200) {
|
||||
throw new Error('API Error: ' + res.msg);
|
||||
throw new Error("API Error: " + res.msg);
|
||||
}
|
||||
return res.data;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, PageSearch, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
||||
import { CertInfo } from '@certd/plugin-cert';
|
||||
import { DogeClient } from '../../lib/index.js';
|
||||
import dayjs from 'dayjs';
|
||||
import { CertApplyPluginNames } from '@certd/plugin-cert';
|
||||
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from '@certd/plugin-lib';
|
||||
import { AbstractTaskPlugin, IsTaskPlugin, PageSearch, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
|
||||
import { CertInfo } from "@certd/plugin-cert";
|
||||
import { DogeClient } from "../../lib/index.js";
|
||||
import dayjs from "dayjs";
|
||||
import { CertApplyPluginNames } from "@certd/plugin-cert";
|
||||
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
||||
@IsTaskPlugin({
|
||||
name: 'DogeCloudDeployToCDN',
|
||||
title: '多吉云-部署到多吉云CDN',
|
||||
icon: 'svg:icon-dogecloud',
|
||||
name: "DogeCloudDeployToCDN",
|
||||
title: "多吉云-部署到多吉云CDN",
|
||||
icon: "svg:icon-dogecloud",
|
||||
group: pluginGroups.cdn.key,
|
||||
default: {
|
||||
strategy: {
|
||||
@@ -18,10 +18,10 @@ import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from
|
||||
export class DogeCloudDeployToCDNPlugin extends AbstractTaskPlugin {
|
||||
//证书选择,此项必须要有
|
||||
@TaskInput({
|
||||
title: '证书',
|
||||
helper: '请选择前置任务输出的域名证书',
|
||||
title: "证书",
|
||||
helper: "请选择前置任务输出的域名证书",
|
||||
component: {
|
||||
name: 'output-selector',
|
||||
name: "output-selector",
|
||||
from: [...CertApplyPluginNames],
|
||||
},
|
||||
required: true,
|
||||
@@ -33,33 +33,35 @@ export class DogeCloudDeployToCDNPlugin extends AbstractTaskPlugin {
|
||||
|
||||
//授权选择框
|
||||
@TaskInput({
|
||||
title: '多吉云授权',
|
||||
helper: '多吉云AccessKey',
|
||||
title: "多吉云授权",
|
||||
helper: "多吉云AccessKey",
|
||||
component: {
|
||||
name: 'access-selector',
|
||||
type: 'dogecloud',
|
||||
name: "access-selector",
|
||||
type: "dogecloud",
|
||||
},
|
||||
rules: [{ required: true, message: '此项必填' }],
|
||||
rules: [{ required: true, message: "此项必填" }],
|
||||
})
|
||||
accessId!: string;
|
||||
|
||||
@TaskInput(createRemoteSelectInputDefine({
|
||||
title: 'CDN域名',
|
||||
helper: '请选择CDN域名,可以选择多个,一次性部署',
|
||||
required: true,
|
||||
action: DogeCloudDeployToCDNPlugin.prototype.onGetDomainList.name,
|
||||
pager: false,
|
||||
search: false
|
||||
}))
|
||||
@TaskInput(
|
||||
createRemoteSelectInputDefine({
|
||||
title: "CDN域名",
|
||||
helper: "请选择CDN域名,可以选择多个,一次性部署",
|
||||
required: true,
|
||||
action: DogeCloudDeployToCDNPlugin.prototype.onGetDomainList.name,
|
||||
pager: false,
|
||||
search: false,
|
||||
})
|
||||
)
|
||||
domain!: string | string[];
|
||||
|
||||
@TaskInput({
|
||||
title: '忽略部署接口报错',
|
||||
helper: '当该域名部署后报错,但是实际上已经部署成功时,可以勾选',
|
||||
title: "忽略部署接口报错",
|
||||
helper: "当该域名部署后报错,但是实际上已经部署成功时,可以勾选",
|
||||
value: false,
|
||||
component: {
|
||||
name: 'a-switch',
|
||||
type: 'checked',
|
||||
name: "a-switch",
|
||||
type: "checked",
|
||||
},
|
||||
})
|
||||
ignoreDeployNullCode = false;
|
||||
@@ -73,13 +75,13 @@ export class DogeCloudDeployToCDNPlugin extends AbstractTaskPlugin {
|
||||
async execute(): Promise<void> {
|
||||
const certId: number = await this.updateCert();
|
||||
|
||||
let domains = this.domain
|
||||
if (typeof domains === 'string'){
|
||||
domains = [domains]
|
||||
let domains = this.domain;
|
||||
if (typeof domains === "string") {
|
||||
domains = [domains];
|
||||
}
|
||||
for (const domain of domains) {
|
||||
this.ctx.logger.info(`绑定证书${certId}到域名${domain}`);
|
||||
await this.bindCert(certId,domain);
|
||||
await this.bindCert(certId, domain);
|
||||
}
|
||||
this.logger.info("执行完成,3秒后删除过期证书");
|
||||
|
||||
@@ -88,17 +90,17 @@ export class DogeCloudDeployToCDNPlugin extends AbstractTaskPlugin {
|
||||
}
|
||||
|
||||
async updateCert() {
|
||||
const data = await this.dogeClient.request('/cdn/cert/upload.json', {
|
||||
note: 'certd-' + dayjs().format('YYYYMMDDHHmmss'),
|
||||
const data = await this.dogeClient.request("/cdn/cert/upload.json", {
|
||||
note: "certd-" + dayjs().format("YYYYMMDDHHmmss"),
|
||||
cert: this.cert.crt,
|
||||
private: this.cert.key,
|
||||
});
|
||||
return data.id;
|
||||
}
|
||||
|
||||
async bindCert(certId: number,domain: string) {
|
||||
async bindCert(certId: number, domain: string) {
|
||||
await this.dogeClient.request(
|
||||
'/cdn/cert/bind.json',
|
||||
"/cdn/cert/bind.json",
|
||||
{
|
||||
id: certId,
|
||||
domain: domain,
|
||||
@@ -108,34 +110,24 @@ export class DogeCloudDeployToCDNPlugin extends AbstractTaskPlugin {
|
||||
}
|
||||
|
||||
async clearExpiredCert() {
|
||||
const res = await this.dogeClient.request(
|
||||
'/cdn/cert/list.json',
|
||||
{},
|
||||
);
|
||||
const list = res.certs?.filter((item: any) => item.expire < dayjs().unix() && item.domainCount === 0) || [];
|
||||
const res = await this.dogeClient.request("/cdn/cert/list.json", {});
|
||||
const list = res.certs?.filter((item: any) => item.expire < dayjs().unix() && item.domainCount === 0) || [];
|
||||
for (const item of list) {
|
||||
this.ctx.logger.info(`删除过期证书${item.id}->${item.domain}`);
|
||||
try{
|
||||
await this.dogeClient.request(
|
||||
'/cdn/cert/delete.json',
|
||||
{
|
||||
id: item.id,
|
||||
},
|
||||
);
|
||||
}catch(err){
|
||||
try {
|
||||
await this.dogeClient.request("/cdn/cert/delete.json", {
|
||||
id: item.id,
|
||||
});
|
||||
} catch (err) {
|
||||
this.ctx.logger.warn(`删除过期证书${item.id}->${item.domain}失败`, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async onGetDomainList(data: PageSearch = {}) {
|
||||
const res = await this.dogeClient.request(
|
||||
'/cdn/domain/list.json',
|
||||
{},
|
||||
);
|
||||
const res = await this.dogeClient.request("/cdn/domain/list.json", {});
|
||||
|
||||
const list = res.domains
|
||||
const list = res.domains;
|
||||
if (!list || list.length === 0) {
|
||||
throw new Error("没有找到CDN域名");
|
||||
}
|
||||
@@ -144,7 +136,7 @@ export class DogeCloudDeployToCDNPlugin extends AbstractTaskPlugin {
|
||||
return {
|
||||
label: `${item.name}`,
|
||||
value: item.name,
|
||||
domain: item.name
|
||||
domain: item.name,
|
||||
};
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from './deploy-to-cdn/index.js';
|
||||
export * from "./deploy-to-cdn/index.js";
|
||||
|
||||
Reference in New Issue
Block a user