mirror of https://github.com/certd/certd
perf: 又拍云支持云存储
parent
0948c5bc69
commit
8449f8580d
|
@ -0,0 +1,77 @@
|
||||||
|
import { UpyunAccess } from "./access.js";
|
||||||
|
import { HttpClient, ILogger } from "@certd/basic";
|
||||||
|
import { CertInfo } from "@certd/plugin-cert";
|
||||||
|
|
||||||
|
export type UpyunClientOptions = {
|
||||||
|
access: UpyunAccess
|
||||||
|
logger: ILogger;
|
||||||
|
http: HttpClient
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UpyunClient {
|
||||||
|
opts: UpyunClientOptions;
|
||||||
|
|
||||||
|
constructor(opts: UpyunClientOptions) {
|
||||||
|
this.opts = opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
async uploadCert(cookie: string,cert:CertInfo) {
|
||||||
|
// https://console.upyun.com/api/https/certificate/
|
||||||
|
const res = await this.doRequest({
|
||||||
|
cookie: cookie,
|
||||||
|
url: "https://console.upyun.com/api/https/certificate/",
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
certificate: cert.crt,
|
||||||
|
private_key: cert.key
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.data.result.certificate_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getLoginToken() {
|
||||||
|
const access = this.opts.access
|
||||||
|
const http = this.opts.http;
|
||||||
|
const res = await http.request({
|
||||||
|
url: "https://console.upyun.com/accounts/signin/",
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
username: access.username,
|
||||||
|
password: access.password
|
||||||
|
},
|
||||||
|
logRes: false,
|
||||||
|
returnResponse: true
|
||||||
|
});
|
||||||
|
if (res.data?.errors?.length > 0) {
|
||||||
|
throw new Error(JSON.stringify(res.data.msg));
|
||||||
|
}
|
||||||
|
const cookie = res.headers["set-cookie"];
|
||||||
|
return cookie;
|
||||||
|
}
|
||||||
|
|
||||||
|
async doRequest(req: {
|
||||||
|
cookie: string,
|
||||||
|
url: string,
|
||||||
|
method: string,
|
||||||
|
data: any
|
||||||
|
}) {
|
||||||
|
|
||||||
|
const res = await this.opts.http.request({
|
||||||
|
url: req.url,
|
||||||
|
method: req.method,
|
||||||
|
data: req.data,
|
||||||
|
headers: {
|
||||||
|
Cookie: req.cookie
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (res.msg.errors.length > 0) {
|
||||||
|
throw new Error(JSON.stringify(res.msg));
|
||||||
|
}
|
||||||
|
if(res.data?.error_code){
|
||||||
|
throw new Error(res.data?.message);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,2 +1,3 @@
|
||||||
export * from './plugins/index.js';
|
export * from './plugins/index.js';
|
||||||
export * from './access.js';
|
export * from './access.js';
|
||||||
|
export * from './client.js';
|
||||||
|
|
|
@ -1,35 +1,38 @@
|
||||||
import { IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from '@certd/pipeline';
|
import { IsTaskPlugin, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
|
||||||
import { CertInfo } from '@certd/plugin-cert';
|
import { CertInfo } from "@certd/plugin-cert";
|
||||||
import { AbstractPlusTaskPlugin } from '@certd/plugin-plus';
|
import { AbstractPlusTaskPlugin } from "@certd/plugin-plus";
|
||||||
import { UpyunAccess } from '../access.js';
|
import { UpyunAccess } from "../access.js";
|
||||||
import {createCertDomainGetterInputDefine, createRemoteSelectInputDefine} from '@certd/plugin-lib';
|
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
||||||
import { CertApplyPluginNames} from '@certd/plugin-cert';
|
import { CertApplyPluginNames } from "@certd/plugin-cert";
|
||||||
import {optionsUtils} from "@certd/basic/dist/utils/util.options.js";
|
import { optionsUtils } from "@certd/basic/dist/utils/util.options.js";
|
||||||
|
import { UpyunClient } from "../client.js";
|
||||||
|
|
||||||
@IsTaskPlugin({
|
@IsTaskPlugin({
|
||||||
//命名规范,插件名称+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
|
//命名规范,插件名称+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
|
||||||
name: 'UpyunDeployToCdn',
|
name: "UpyunDeployToCdn",
|
||||||
title: '又拍云-部署证书到CDN',
|
title: "又拍云-部署证书到CDN",
|
||||||
icon: 'svg:icon-upyun',
|
icon: "svg:icon-upyun",
|
||||||
|
desc:"支持又拍云CDN,云存储",
|
||||||
//插件分组
|
//插件分组
|
||||||
group: pluginGroups.cdn.key,
|
group: pluginGroups.cdn.key,
|
||||||
needPlus: true,
|
needPlus: true,
|
||||||
default: {
|
default: {
|
||||||
//默认值配置照抄即可
|
//默认值配置照抄即可
|
||||||
strategy: {
|
strategy: {
|
||||||
runStrategy: RunStrategy.SkipWhenSucceed,
|
runStrategy: RunStrategy.SkipWhenSucceed
|
||||||
},
|
}
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
//类名规范,跟上面插件名称(name)一致
|
//类名规范,跟上面插件名称(name)一致
|
||||||
export class UpyunDeployToCdn extends AbstractPlusTaskPlugin {
|
export class UpyunDeployToCdn extends AbstractPlusTaskPlugin {
|
||||||
//证书选择,此项必须要有
|
//证书选择,此项必须要有
|
||||||
@TaskInput({
|
@TaskInput({
|
||||||
title: '域名证书',
|
title: "域名证书",
|
||||||
helper: '请选择前置任务输出的域名证书',
|
helper: "请选择前置任务输出的域名证书",
|
||||||
component: {
|
component: {
|
||||||
name: 'output-selector',
|
name: "output-selector",
|
||||||
from: [...CertApplyPluginNames],
|
from: [...CertApplyPluginNames]
|
||||||
},
|
}
|
||||||
// required: true, // 必填
|
// required: true, // 必填
|
||||||
})
|
})
|
||||||
cert!: CertInfo;
|
cert!: CertInfo;
|
||||||
|
@ -38,142 +41,99 @@ export class UpyunDeployToCdn extends AbstractPlusTaskPlugin {
|
||||||
certDomains!: string[];
|
certDomains!: string[];
|
||||||
//授权选择框
|
//授权选择框
|
||||||
@TaskInput({
|
@TaskInput({
|
||||||
title: 'Upyun授权',
|
title: "Upyun授权",
|
||||||
component: {
|
component: {
|
||||||
name: 'access-selector',
|
name: "access-selector",
|
||||||
type: 'upyun', //固定授权类型
|
type: "upyun" //固定授权类型
|
||||||
},
|
},
|
||||||
required: true, //必填
|
required: true //必填
|
||||||
})
|
})
|
||||||
accessId!: string;
|
accessId!: string;
|
||||||
//
|
//
|
||||||
|
|
||||||
@TaskInput(
|
@TaskInput(
|
||||||
createRemoteSelectInputDefine({
|
createRemoteSelectInputDefine({
|
||||||
title: 'CDN加速域名',
|
title: "加速域名",
|
||||||
helper: '选择CDN加速域名,可以手动输入',
|
helper: "选择加速域名,可以手动输入",
|
||||||
typeName: 'UpyunDeployToCdn',
|
typeName: "UpyunDeployToCdn",
|
||||||
action: UpyunDeployToCdn.prototype.onGetCdnList.name,
|
action: UpyunDeployToCdn.prototype.onGetCdnList.name,
|
||||||
watches: ['accessId'],
|
watches: ["accessId"]
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
cdnList!: string[];
|
cdnList!: string[];
|
||||||
|
|
||||||
//插件实例化时执行的方法
|
//插件实例化时执行的方法
|
||||||
async onInstance() {}
|
async onInstance() {
|
||||||
|
}
|
||||||
|
|
||||||
//插件执行方法
|
//插件执行方法
|
||||||
async execute(): Promise<void> {
|
async execute(): Promise<void> {
|
||||||
|
const access = await this.accessService.getById<UpyunAccess>(this.accessId);
|
||||||
|
|
||||||
const cookie = await this.getLoginToken();
|
const upyunClient = new UpyunClient({
|
||||||
|
access,
|
||||||
|
logger: this.logger,
|
||||||
|
http: this.ctx.http
|
||||||
|
});
|
||||||
|
const cookie = await upyunClient.getLoginToken();
|
||||||
this.logger.info(`登录成功`);
|
this.logger.info(`登录成功`);
|
||||||
const certId = await this.uploadCert(cookie);
|
const certId = await upyunClient.uploadCert(cookie, this.cert);
|
||||||
this.logger.info(`上传证书成功:${certId}`);
|
this.logger.info(`上传证书成功:${certId}`);
|
||||||
for (const item of this.cdnList) {
|
for (const item of this.cdnList) {
|
||||||
this.logger.info(`开始部署证书:${item}`);
|
this.logger.info(`开始部署证书:${item}`);
|
||||||
const res = await this.doRequest({
|
const res = await upyunClient.doRequest({
|
||||||
cookie:cookie,
|
cookie: cookie,
|
||||||
url: 'https://console.upyun.com/api/https/migrate/domain',
|
url: "https://console.upyun.com/api/https/migrate/domain",
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
data:{
|
data: {
|
||||||
crt_id: certId,
|
crt_id: certId,
|
||||||
domain_name : item
|
domain_name: item
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
this.logger.info(`部署成功:${JSON.stringify(res)}`);
|
this.logger.info(`部署成功:${JSON.stringify(res)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.info('部署成功');
|
this.logger.info("部署成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
async uploadCert(cookie:string){
|
|
||||||
// https://console.upyun.com/api/https/certificate/
|
|
||||||
const res = await this.doRequest({
|
|
||||||
cookie:cookie,
|
|
||||||
url: 'https://console.upyun.com/api/https/certificate/',
|
|
||||||
method: 'POST',
|
|
||||||
data:{
|
|
||||||
certificate: this.cert.crt,
|
|
||||||
private_key: this.cert.key
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return res.data.result.certificate_id
|
|
||||||
}
|
|
||||||
|
|
||||||
async getLoginToken(){
|
|
||||||
const access = await this.accessService.getById<UpyunAccess>(this.accessId)
|
|
||||||
const res = await this.http.request({
|
|
||||||
url: 'https://console.upyun.com/accounts/signin/',
|
|
||||||
method: 'POST',
|
|
||||||
data:{
|
|
||||||
username: access.username,
|
|
||||||
password: access.password
|
|
||||||
},
|
|
||||||
logRes:false,
|
|
||||||
returnResponse:true
|
|
||||||
});
|
|
||||||
if (res.data?.errors?.length>0) {
|
|
||||||
throw new Error(JSON.stringify(res.data.msg));
|
|
||||||
}
|
|
||||||
const cookie = res.headers['set-cookie'];
|
|
||||||
return cookie;
|
|
||||||
}
|
|
||||||
|
|
||||||
async doRequest(req:{
|
|
||||||
cookie:string,
|
|
||||||
url:string,
|
|
||||||
method:string,
|
|
||||||
data:any
|
|
||||||
}){
|
|
||||||
|
|
||||||
const res = await this.http.request({
|
|
||||||
url: req.url,
|
|
||||||
method: req.method,
|
|
||||||
data:req.data,
|
|
||||||
headers:{
|
|
||||||
Cookie: req.cookie
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if (res.msg.errors.length>0) {
|
|
||||||
throw new Error(JSON.stringify(res.msg));
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
async onGetCdnList() {
|
async onGetCdnList() {
|
||||||
if(!this.accessId){
|
if (!this.accessId) {
|
||||||
throw new Error('accessId不能为空');
|
throw new Error("accessId不能为空");
|
||||||
}
|
}
|
||||||
|
const access = await this.accessService.getById<UpyunAccess>(this.accessId);
|
||||||
|
|
||||||
const cookie = await this.getLoginToken();
|
const upyunClient = new UpyunClient({
|
||||||
|
access,
|
||||||
|
logger: this.logger,
|
||||||
|
http: this.ctx.http
|
||||||
|
});
|
||||||
|
const cookie = await upyunClient.getLoginToken();
|
||||||
const req = {
|
const req = {
|
||||||
cookie,
|
cookie,
|
||||||
url: 'https://console.upyun.com/api/v2/buckets/?bucket_name=&with_domains=true&business_type=file&perPage=100&page=1&tag=all&state=all&type=ucdn&security_cdn=false',
|
url: "https://console.upyun.com/api/account/domains/?limit=15&business_type=file&security_cdn=false&websocket=false&key=&domain=",
|
||||||
method: 'GET',
|
method: "GET",
|
||||||
data:{}
|
data: {}
|
||||||
}
|
};
|
||||||
const res = await this.doRequest(req);
|
const res = await upyunClient.doRequest(req);
|
||||||
|
|
||||||
const buckets = res.data?.buckets;
|
const domains = res.data?.domains;
|
||||||
if(!buckets || buckets.length === 0){
|
if (!domains || domains.length === 0) {
|
||||||
throw new Error('没有找到CDN加速域名');
|
throw new Error("没有找到加速域名");
|
||||||
}
|
}
|
||||||
const list= []
|
const list = [];
|
||||||
for (const item of buckets) {
|
for (const domain of domains) {
|
||||||
for (const domain of item.domains) {
|
list.push({
|
||||||
list.push({
|
domain: domain.domain,
|
||||||
domain:domain.domain,
|
bucket: domain.bucket_name
|
||||||
bucket:item.bucket_name
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = list.map((item: any) => {
|
const options = list.map((item: any) => {
|
||||||
return {
|
return {
|
||||||
value: item.domain,
|
value: item.domain,
|
||||||
label: `${item.domain}<${item.bucket}>`,
|
label: `${item.domain}<${item.bucket}>`,
|
||||||
domain: item.domain,
|
domain: item.domain
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
return optionsUtils.buildGroupOptions(options, this.certDomains);
|
return optionsUtils.buildGroupOptions(options, this.certDomains);
|
||||||
|
@ -181,5 +141,6 @@ export class UpyunDeployToCdn extends AbstractPlusTaskPlugin {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//实例化一下,注册插件
|
//实例化一下,注册插件
|
||||||
new UpyunDeployToCdn();
|
new UpyunDeployToCdn();
|
||||||
|
|
Loading…
Reference in New Issue