chore: format

This commit is contained in:
xiaojunnuo
2026-05-31 01:41:33 +08:00
parent acd440106b
commit 4b57a0d729
557 changed files with 12530 additions and 14039 deletions
@@ -1,42 +1,42 @@
import { HttpRequestConfig } from '@certd/basic';
import { IsAccess, AccessInput, BaseAccess, PageSearch, Pager } from '@certd/pipeline';
import qs from 'qs';
import iconv from 'iconv-lite';
import { HttpRequestConfig } from "@certd/basic";
import { IsAccess, AccessInput, BaseAccess, PageSearch, Pager } from "@certd/pipeline";
import qs from "qs";
import iconv from "iconv-lite";
/**
* 这个注解将注册一个授权配置
* 在certd的后台管理系统中,用户可以选择添加此类型的授权
*/
@IsAccess({
name: 'west',
title: '西部数码授权',
desc: '',
icon: 'tabler:map-west',
name: "west",
title: "西部数码授权",
desc: "",
icon: "tabler:map-west",
})
export class WestAccess extends BaseAccess {
/**
* 授权属性配置
*/
@AccessInput({
title: '权限范围',
title: "权限范围",
component: {
name: 'a-select',
vModel: 'value',
name: "a-select",
vModel: "value",
options: [
{ value: 'account', label: '账户级别,对所有域名都有权限管理' },
{ value: 'domain', label: '域名级别,仅能管理单个域名' },
{ value: "account", label: "账户级别,对所有域名都有权限管理" },
{ value: "domain", label: "域名级别,仅能管理单个域名" },
],
},
helper: '选择权限范围',
helper: "选择权限范围",
required: true,
})
scope = '';
scope = "";
/**
* 授权属性配置
*/
@AccessInput({
title: '账号',
helper: '你的登录账号',
title: "账号",
helper: "你的登录账号",
encrypt: false,
required: false,
mergeScript: `
@@ -47,17 +47,17 @@ export class WestAccess extends BaseAccess {
}
`,
})
username = '';
username = "";
/**
* 授权属性配置
*/
@AccessInput({
title: 'ApiKey',
title: "ApiKey",
component: {
placeholder: '账户级别的key,对整个账户都有管理权限',
placeholder: "账户级别的key,对整个账户都有管理权限",
},
helper: '账户级别的key,对整个账户都有管理权限\n前往[API接口配置](https://www.west.cn/manager/API/APIconfig.asp),手动设置“api连接密码”',
helper: "账户级别的key,对整个账户都有管理权限\n前往[API接口配置](https://www.west.cn/manager/API/APIconfig.asp),手动设置“api连接密码”",
encrypt: true,
required: false,
mergeScript: `
@@ -68,17 +68,17 @@ export class WestAccess extends BaseAccess {
}
`,
})
apikey = '';
apikey = "";
/**
* 授权属性配置
*/
@AccessInput({
title: 'apidomainkey',
title: "apidomainkey",
component: {
placeholder: '域名级别的key,仅对单个域名有权限',
placeholder: "域名级别的key,仅对单个域名有权限",
},
helper: '域名级别的key,仅对单个域名有权限。 \n前往[西部数据域名管理](https://www.west.cn/manager/domain/),点击域名,右上方点击ApiKey获取密钥',
helper: "域名级别的key,仅对单个域名有权限。 \n前往[西部数据域名管理](https://www.west.cn/manager/domain/),点击域名,右上方点击ApiKey获取密钥",
encrypt: true,
required: false,
mergeScript: `
@@ -89,16 +89,15 @@ export class WestAccess extends BaseAccess {
}
`,
})
apidomainkey = '';
apidomainkey = "";
/**
/**
* 授权属性配置
*/
@AccessInput({
title: '域名',
title: "域名",
component: {
placeholder: '域名级别的key对应的域名',
placeholder: "域名级别的key对应的域名",
},
encrypt: false,
required: false,
@@ -110,55 +109,52 @@ export class WestAccess extends BaseAccess {
}
`,
})
domain = '';
domain = "";
@AccessInput({
title: "测试",
component: {
name: "api-test",
action: "TestRequest"
action: "TestRequest",
},
helper: "点击测试接口是否正常"
helper: "点击测试接口是否正常",
})
testRequest = true;
async onTestRequest() {
if(this.scope === 'domain'){
if(!this.domain){
throw new Error('domain 必填');
if (this.scope === "domain") {
if (!this.domain) {
throw new Error("domain 必填");
}
await this.getDomainRecordList({limit:1});
await this.getDomainRecordList({ limit: 1 });
return "ok";
}
await this.getDomainList({pageNo:1,pageSize:1});
await this.getDomainList({ pageNo: 1, pageSize: 1 });
return "ok";
}
async getDomainRecordList(req:{limit:number}){
// 获取域名解析记录列表
return await this.doDoimainApiRequest('https://api.west.cn/API/v2/domain/dns/',{
act:'dnsrec.list',
domain:this.domain,
async getDomainRecordList(req: { limit: number }) {
// 获取域名解析记录列表
return await this.doDoimainApiRequest("https://api.west.cn/API/v2/domain/dns/", {
act: "dnsrec.list",
domain: this.domain,
limit: req.limit || 10,
})
});
}
async doDoimainApiRequest(url: string, data: any = null, method = 'post') {
async doDoimainApiRequest(url: string, data: any = null, method = "post") {
data.apidomainkey = this.apidomainkey;
const res = await this.ctx.http.request<any, any>({
url,
method,
data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
"Content-Type": "application/x-www-form-urlencoded",
},
});
if (res.msg !== 'success') {
if(res.msg.includes('500 already exists')){
if (res.msg !== "success") {
if (res.msg.includes("500 already exists")) {
return res;
}
throw new Error(`${JSON.stringify(res.msg)}`);
@@ -167,31 +163,29 @@ export class WestAccess extends BaseAccess {
}
async getDomainList(req: PageSearch) {
const pager = new Pager(req)
const pager = new Pager(req);
const res = await this.doRequest({
url: '/v2/domain',
method: 'GET',
data:{
act:'getdomains',
url: "/v2/domain",
method: "GET",
data: {
act: "getdomains",
limit: pager.pageSize,
page: pager.pageNo,
}
},
});
return res;
}
public async doRequest(req: HttpRequestConfig) {
let { url, method, data } = req;
if (data == null) {
data = {};
}
if (!method) {
method = 'POST';
method = "POST";
}
if (this.scope === 'account') {
if (this.scope === "account") {
/**
* token text 身份验证字符串,取值为:md5(username+api_password+timestamp),其中:
username:您在我司注册的用户名。
@@ -214,58 +208,56 @@ token=md5(zhangsan + 5dh232kfg!* + 1554691950854)=cfcd208495d565ef66e7dff9f98764
data.apidomainkey = this.apidomainkey;
}
const headers = {}
const body: any = {}
if (method.toUpperCase() === 'POST') {
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
body.data = data
} else if (method.toUpperCase() === 'GET') {
let queryString = '';
if (method.toUpperCase() === 'GET') {
const headers = {};
const body: any = {};
if (method.toUpperCase() === "POST") {
headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
body.data = data;
} else if (method.toUpperCase() === "GET") {
let queryString = "";
if (method.toUpperCase() === "GET") {
queryString = qs.stringify(data);
}
url = `${url}?${queryString}`
url = `${url}?${queryString}`;
}
const res = await this.ctx.http.request<any, any>({
baseURL: 'https://api.west.cn/api',
baseURL: "https://api.west.cn/api",
url,
method,
...body,
headers,
responseType: 'arraybuffer', // 关键:获取原始二进制数据
responseType: "arraybuffer", // 关键:获取原始二进制数据
transformResponse: [
function(data, headers) { // headers 参数包含响应头
function (data, headers) {
// headers 参数包含响应头
try {
const contentType = headers['content-type'] || '';
const contentType = headers["content-type"] || "";
// 判断是否是 GB2312/GBK 编码
//@ts-ignore
if (contentType.includes('gb2312') || contentType.includes('gbk')) {
if (contentType.includes("gb2312") || contentType.includes("gbk")) {
// 使用 iconv-lite 解码
data = iconv.decode(data, 'gb2312');
}else{
data = iconv.decode(data, "gb2312");
} else {
// 默认按 UTF-8 处理
data = data.toString('utf-8');
data = data.toString("utf-8");
}
} catch (error) {
console.error('解码失败:', error);
console.error("解码失败:", error);
}
return JSON.parse(data);
}
]
},
],
});
this.ctx.logger.info(`request ${url} ${method} res:${JSON.stringify(res)}`);
if (res.msg !== 'success' && res.result!= 200) {
if(res.msg.includes('500 already exists')){
if (res.msg !== "success" && res.result != 200) {
if (res.msg.includes("500 already exists")) {
return res;
}
throw new Error(`${JSON.stringify(res.msg)}`);
}
return res;
}
}
new WestAccess();
@@ -1,6 +1,6 @@
import { AbstractDnsProvider, CreateRecordOptions, RemoveRecordOptions } from '@certd/plugin-cert';
import { AbstractDnsProvider, CreateRecordOptions, RemoveRecordOptions } from "@certd/plugin-cert";
import { WestAccess } from './access.js';
import { WestAccess } from "./access.js";
type westRecord = {
// 这里定义Record记录的数据结构,跟对应云平台接口返回值一样即可,一般是拿到id就行,用于删除txt解析记录,清理申请痕迹
@@ -11,14 +11,13 @@ type westRecord = {
};
};
export class WestDnsProviderDomain extends AbstractDnsProvider<westRecord> {
access!: WestAccess;
async onInstance() {
this.access = this.ctx.access as WestAccess
this.access = this.ctx.access as WestAccess;
// 也可以通过ctx成员变量传递context
this.logger.debug('access:', this.access);
this.logger.debug("access:", this.access);
//初始化的操作
//...
}
@@ -35,26 +34,26 @@ export class WestDnsProviderDomain extends AbstractDnsProvider<westRecord> {
* domain: 'example.com'
*/
const { fullRecord, value, type, domain } = options;
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
this.logger.info("添加域名解析:", fullRecord, value, type, domain);
// 准备要发送到API的请求体
const requestBody = {
act: 'dnsrec.add', // API动作类型
act: "dnsrec.add", // API动作类型
domain: domain, // 域名
record_type: 'TXT', // DNS记录类型
record_type: "TXT", // DNS记录类型
hostname: fullRecord, // 完整的记录名
record_value: value, // 记录的值
record_line: '', // 记录线路
record_line: "", // 记录线路
record_ttl: 60, // TTL (生存时间),设置为60秒
};
const url = 'https://api.west.cn/API/v2/domain/dns/';
const url = "https://api.west.cn/API/v2/domain/dns/";
const res = await this.access.doDoimainApiRequest(url, requestBody);
const record = res as westRecord;
this.logger.info(`添加域名解析成功:fullRecord=${fullRecord},value=${value}`);
this.logger.info(`dns解析记录:${JSON.stringify(record)}`);
// 西部数码生效较慢 增加90秒等待 提高成功率
this.logger.info('等待解析生效:wait 90s');
this.logger.info("等待解析生效:wait 90s");
await new Promise(resolve => setTimeout(resolve, 90000));
return record;
}
@@ -66,31 +65,31 @@ export class WestDnsProviderDomain extends AbstractDnsProvider<westRecord> {
async removeRecord(options: RemoveRecordOptions<westRecord>): Promise<void> {
const { fullRecord, value, domain } = options.recordReq;
const record = options.recordRes;
this.logger.info('删除域名解析:', fullRecord, value, record);
this.logger.info("删除域名解析:", fullRecord, value, record);
if (!record) {
this.logger.info('record不存在');
this.logger.info("record不存在");
return;
}
//这里调用删除txt dns解析记录接口
const record_id = record?.body?.record_id;
if (!record_id) {
this.logger.info('record_id不存在');
this.logger.info("record_id不存在");
return;
}
// 准备要发送到API的请求体
const requestBody = {
act: 'dnsrec.remove', // API动作类型
act: "dnsrec.remove", // API动作类型
domain: domain, // 域名
record_id: record_id,
hostname: fullRecord, // 完整的记录名
record_type: 'TXT', // DNS记录类型
record_line: '', // 记录线路
record_type: "TXT", // DNS记录类型
record_line: "", // 记录线路
};
const url = 'https://api.west.cn/API/v2/domain/dns/';
const url = "https://api.west.cn/API/v2/domain/dns/";
const res = await this.access.doDoimainApiRequest(url, requestBody);
const result = res.result;
this.logger.info('删除域名解析成功:', fullRecord, value, JSON.stringify(result));
this.logger.info("删除域名解析成功:", fullRecord, value, JSON.stringify(result));
}
}
@@ -1,7 +1,7 @@
import { PageRes, PageSearch } from '@certd/pipeline';
import { AbstractDnsProvider, CreateRecordOptions, DomainRecord, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
import { WestAccess } from './access.js';
import { WestDnsProviderDomain } from './dns-provider-domain.js';
import { PageRes, PageSearch } from "@certd/pipeline";
import { AbstractDnsProvider, CreateRecordOptions, DomainRecord, IsDnsProvider, RemoveRecordOptions } from "@certd/plugin-cert";
import { WestAccess } from "./access.js";
import { WestDnsProviderDomain } from "./dns-provider-domain.js";
type westRecord = {
// 这里定义Record记录的数据结构,跟对应云平台接口返回值一样即可,一般是拿到id就行,用于删除txt解析记录,清理申请痕迹
@@ -14,25 +14,25 @@ type westRecord = {
// 这里通过IsDnsProvider注册一个dnsProvider
@IsDnsProvider({
name: 'west',
title: '西部数码',
desc: 'west dns provider',
icon: 'svg:icon-xibushuma',
name: "west",
title: "西部数码",
desc: "west dns provider",
icon: "svg:icon-xibushuma",
// 这里是对应的云平台的access类型名称
accessType: 'west',
accessType: "west",
})
export class WestDnsProvider extends AbstractDnsProvider<westRecord> {
access!: WestAccess;
async onInstance() {
this.access = this.ctx.access as WestAccess
this.access = this.ctx.access as WestAccess;
// 也可以通过ctx成员变量传递context
this.logger.debug('access:', this.access);
this.logger.debug("access:", this.access);
//初始化的操作
//...
}
getDomainProvider(){
getDomainProvider() {
const provider = new WestDnsProviderDomain();
provider.access = this.access;
provider.logger = this.logger;
@@ -45,8 +45,7 @@ export class WestDnsProvider extends AbstractDnsProvider<westRecord> {
* 创建dns解析记录,用于验证域名所有权
*/
async createRecord(options: CreateRecordOptions): Promise<any> {
if(this.access.scope === 'domain'){
if (this.access.scope === "domain") {
//如果是域名级别的,走老接口
const provider = this.getDomainProvider();
return provider.createRecord(options);
@@ -59,31 +58,31 @@ export class WestDnsProvider extends AbstractDnsProvider<westRecord> {
* type: 'TXT',
* domain: 'example.com'
*/
const { fullRecord, value, type, domain,hostRecord } = options;
this.logger.info('添加域名解析:', fullRecord, value, type, domain);
const { fullRecord, value, type, domain, hostRecord } = options;
this.logger.info("添加域名解析:", fullRecord, value, type, domain);
// 准备要发送到API的请求体
const requestBody = {
act: 'adddnsrecord', // API动作类型
act: "adddnsrecord", // API动作类型
domain: domain, // 域名
type: 'TXT', // DNS记录类型
type: "TXT", // DNS记录类型
host: hostRecord, // 完整的记录名
value: value, // 记录的值
line: '', // 记录线路
line: "", // 记录线路
ttl: 60, // TTL (生存时间),设置为60秒
};
const url = '/v2/domain/';
const url = "/v2/domain/";
const res = await this.access.doRequest({
url,
method:'POST',
method: "POST",
data: requestBody,
});
const record = res as westRecord;
this.logger.info(`添加域名解析成功:fullRecord=${fullRecord},value=${value}`);
this.logger.info(`dns解析记录:${JSON.stringify(record)}`);
// 西部数码生效较慢 增加90秒等待 提高成功率
this.logger.info('等待解析生效:wait 90s');
this.logger.info("等待解析生效:wait 90s");
await new Promise(resolve => setTimeout(resolve, 90000));
return record;
}
@@ -94,7 +93,7 @@ export class WestDnsProvider extends AbstractDnsProvider<westRecord> {
* @param options
*/
async removeRecord(options: RemoveRecordOptions<westRecord>): Promise<void> {
if(this.access.scope === 'domain'){
if (this.access.scope === "domain") {
//如果是域名级别的,走老接口
const provider = this.getDomainProvider();
return provider.removeRecord(options as any);
@@ -102,32 +101,32 @@ export class WestDnsProvider extends AbstractDnsProvider<westRecord> {
const { fullRecord, value, domain } = options.recordReq;
const record = options.recordRes;
this.logger.info('删除域名解析:', fullRecord, value, record);
this.logger.info("删除域名解析:", fullRecord, value, record);
if (!record) {
this.logger.info('record不存在');
this.logger.info("record不存在");
return;
}
//这里调用删除txt dns解析记录接口
const record_id = record.data?.id;
if (!record_id) {
this.logger.info('record_id不存在');
this.logger.info("record_id不存在");
return;
}
// 准备要发送到API的请求体
const requestBody = {
act: 'deldnsrecord', // API动作类型
act: "deldnsrecord", // API动作类型
domain: domain, // 域名
id: record_id,
};
const url = '/v2/domain/';
const url = "/v2/domain/";
const res = await this.access.doRequest({
url,
method:'POST',
method: "POST",
data: requestBody,
});
const result = res.result;
this.logger.info('删除域名解析成功:', fullRecord, value, JSON.stringify(result));
this.logger.info("删除域名解析成功:", fullRecord, value, JSON.stringify(result));
}
async getDomainListPage(req: PageSearch): Promise<PageRes<DomainRecord>> {
@@ -1,3 +1,3 @@
export * from './dns-provider.js';
export * from './access.js';
export * from './plugins/deploy-to-vhost.js';
export * from "./dns-provider.js";
export * from "./access.js";
export * from "./plugins/deploy-to-vhost.js";
@@ -1,11 +1,4 @@
import {
AbstractTaskPlugin,
IsTaskPlugin,
PageSearch,
pluginGroups,
RunStrategy,
TaskInput
} from "@certd/pipeline";
import { AbstractTaskPlugin, IsTaskPlugin, PageSearch, pluginGroups, RunStrategy, TaskInput } from "@certd/pipeline";
import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
import { WestAccess } from "../access.js";
@@ -22,9 +15,9 @@ import { WestAccess } from "../access.js";
default: {
//默认值配置照抄即可
strategy: {
runStrategy: RunStrategy.SkipWhenSucceed
}
}
runStrategy: RunStrategy.SkipWhenSucceed,
},
},
})
//类名规范,跟上面插件名称(name)一致
export class WestDeployToVhost extends AbstractTaskPlugin {
@@ -34,8 +27,8 @@ export class WestDeployToVhost extends AbstractTaskPlugin {
helper: "请选择前置任务输出的域名证书",
component: {
name: "output-selector",
from: [...CertApplyPluginNames]
}
from: [...CertApplyPluginNames],
},
// required: true, // 必填
})
cert!: CertInfo;
@@ -43,27 +36,25 @@ export class WestDeployToVhost extends AbstractTaskPlugin {
@TaskInput(createCertDomainGetterInputDefine({ props: { required: false } }))
certDomains!: string[];
//授权选择框
@TaskInput({
title: "西数授权",
component: {
name: "access-selector",
type: "west" //固定授权类型
type: "west", //固定授权类型
},
required: true //必填
required: true, //必填
})
accessId!: string;
//
@TaskInput(
createRemoteSelectInputDefine({
title: "虚拟主机列表",
helper: "虚拟主机列表",
action: WestDeployToVhost.prototype.onGetVhostList.name,
pager: false,
search: false
search: false,
})
)
vhostList!: string[];
@@ -80,8 +71,7 @@ export class WestDeployToVhost extends AbstractTaskPlugin {
// certList!: string[];
//插件实例化时执行的方法
async onInstance() {
}
async onInstance() {}
//插件执行方法
async execute(): Promise<void> {
@@ -91,9 +81,9 @@ export class WestDeployToVhost extends AbstractTaskPlugin {
this.logger.info(`----------- 开始更新证书到虚拟主机:${item}`);
const arr = item.split("_");
const sitename = arr[1];
await this.uploadCert({access,sitename});
await this.uploadCert({ access, sitename });
await this.ctx.utils.sleep(2000);
const res = await this.getVhostSslInfo({access,sitename});
const res = await this.getVhostSslInfo({ access, sitename });
this.logger.info(`----------- 虚拟主机${sitename}证书信息:${JSON.stringify(res)}`);
this.logger.info(`----------- 更新证书${item}成功`);
}
@@ -131,10 +121,10 @@ export class WestDeployToVhost extends AbstractTaskPlugin {
// };
// }
async uploadCert(req:{access:any,sitename:string}){
const {access,sitename} = req;
const data = {
/**
async uploadCert(req: { access: any; sitename: string }) {
const { access, sitename } = req;
const data = {
/**
* act
vhostssl
@@ -154,50 +144,50 @@ String
私匙
certcontent
*/
act:"vhostssl",
sitename:sitename,
westly:"1",
cmd:"import",
opensslclosessl:"openssl",
keycontent:this.cert.key,
certcontent:this.cert.crt,
}
act: "vhostssl",
sitename: sitename,
westly: "1",
cmd: "import",
opensslclosessl: "openssl",
keycontent: this.cert.key,
certcontent: this.cert.crt,
};
const res = await access.doRequest({
url: `/v2/vhost/`,
method:"POST",
data:data
});
return res;
}
async getVhostSslInfo(req:{access:any,sitename:string}){
const {access,sitename} = req;
const data = {
act:"vhostssl",
sitename:sitename,
cmd:"info",
}
const res = await access.doRequest({
url: `/v2/vhost/`,
method:"POST",
data:data
method: "POST",
data: data,
});
return res;
}
async onGetVhostList(data: PageSearch = {}) {
async getVhostSslInfo(req: { access: any; sitename: string }) {
const { access, sitename } = req;
const data = {
act: "vhostssl",
sitename: sitename,
cmd: "info",
};
const res = await access.doRequest({
url: `/v2/vhost/`,
method: "POST",
data: data,
});
return res;
}
async onGetVhostList(data: PageSearch = {}) {
const access = await this.getAccess<WestAccess>(this.accessId);
const res = await access.doRequest({
url: `/v2/vhost/`,
method:"POST",
data:{
act:"sync",
method: "POST",
data: {
act: "sync",
westid: 1,
}
},
});
const list = res.data
const list = res.data;
if (!list || list.length === 0) {
throw new Error("没有找到虚拟主机");
}
@@ -211,14 +201,14 @@ certcontent
return {
label: `${item.sitename}<${item.westid}-${item.bindings}>`,
value: `${item.westid}_${item.sitename}`,
domain: item.bindings.split(",")
domain: item.bindings.split(","),
};
});
return {
list: this.ctx.utils.options.buildGroupOptions(options, this.certDomains),
total: list.length,
pageNo: 1,
pageSize: list.length
pageSize: list.length,
};
}
}