mirror of
https://github.com/certd/certd.git
synced 2026-05-18 06:17:31 +08:00
perf: 自定义插件支持使用_ctx.import("/@/xxx.js")以绝对路径引用模块
This commit is contained in:
@@ -14,7 +14,7 @@ export class BuiltInPluginService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
if(Plugin.define?.type && Plugin.define?.type !== 'builtin'){
|
if(Plugin.define?.type && Plugin.define?.type.toLowerCase() !== 'builtin'){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
list.push({ ...Plugin.define, key });
|
list.push({ ...Plugin.define, key });
|
||||||
|
|||||||
@@ -19,6 +19,19 @@ export type PluginImportReq = {
|
|||||||
override?: boolean;
|
override?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function importer(modulePath: string) {
|
||||||
|
if (!modulePath) {
|
||||||
|
throw new Error("modules path 不能为空")
|
||||||
|
}
|
||||||
|
if (!modulePath.startsWith("/@/")) {
|
||||||
|
return await import(modulePath)
|
||||||
|
}
|
||||||
|
modulePath = modulePath.replace("/@/", "")
|
||||||
|
//替换@为相对地址
|
||||||
|
modulePath = `../../../${modulePath}`
|
||||||
|
return await import(modulePath)
|
||||||
|
}
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
||||||
export class PluginService extends BaseService<PluginEntity> {
|
export class PluginService extends BaseService<PluginEntity> {
|
||||||
@@ -336,8 +349,8 @@ export class PluginService extends BaseService<PluginEntity> {
|
|||||||
}).constructor;
|
}).constructor;
|
||||||
// const script = await this.compile(plugin.content);
|
// const script = await this.compile(plugin.content);
|
||||||
const script = plugin.content;
|
const script = plugin.content;
|
||||||
const getPluginClass = new AsyncFunction(script);
|
const getPluginClass = new AsyncFunction("_ctx", script);
|
||||||
return await getPluginClass({logger: logger});
|
return await getPluginClass({ logger: logger, import: importer });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error("编译插件失败:", e);
|
logger.error("编译插件失败:", e);
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
import { HttpClient, HttpRequestConfig, ILogger } from "@certd/basic";
|
|
||||||
import { MaoyunAccess } from "./access.js";
|
|
||||||
|
|
||||||
export class MaoyunClient {
|
|
||||||
privateKeyPem = "";
|
|
||||||
http: HttpClient;
|
|
||||||
logger: ILogger;
|
|
||||||
access: MaoyunAccess;
|
|
||||||
token: string;
|
|
||||||
|
|
||||||
constructor(opts: { logger: ILogger; http: HttpClient; access: MaoyunAccess }) {
|
|
||||||
this.logger = opts.logger;
|
|
||||||
this.http = opts.http;
|
|
||||||
this.access = opts.access;
|
|
||||||
this.privateKeyPem =
|
|
||||||
"\n-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAt83xKlUSU0i09/pwwQ0MQQ0v71IULdVGJ3AFo+anwLX1TRCp\nxmY5i+xmT9tshHqiPGN8qeg+lDaqA+iwmS6zqi+KlNmmKJc3kUx/h24MI3nff0xy\nz605ZfDgJhwBkJpTI6Sk4+OLX+lZxOiET0nOT7jrhKiFCKX8+0ZXjTJ1cmdifKaj\nqXmjD+XYZzBwA2fCr1kPq2xKvU097Ksu6QvM+La5X/tt+FJOuedmuqZmsb6YQ+6O\n6mJ0bcY0kFDGNkoeY6dEyeJAkIDJbda3n0I71KwRR2J0CSN3TF+w1hSQa7Hp1rXw\n+zQvR6p7O2VY8zQeZZKRKGl7OGdKW5F79iz2fQIDAQABAoIBAEN0BaRGciI0VY2H\n0CdY1X1uDIBke9lSIpvIhZlfxYJ4hFxS2CtiSo4qJGX8HbgElVNaI17rR0P3R6+F\njoG43OCA7/euZEcTL6ZYD5kw7q16RWYfNSc36A+cNXZm4sAhko9LFeQ4FmcNaQ9V\nUXEToe4p6+zUN3Y0DEJezzSXJvjjjodT5L03i2HCW+/xZIHi6oh1DuXdy7h1Ah8s\nS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|
||||||
|
|
||||||
async sign(data: string) {
|
|
||||||
const { KJUR, KEYUTIL, hextob64 } = await import("jsrsasign");
|
|
||||||
const privateKey = KEYUTIL.getKey(this.privateKeyPem);
|
|
||||||
// 创建签名实例
|
|
||||||
const signature = new KJUR.crypto.Signature({
|
|
||||||
alg: "SHA256withRSA",
|
|
||||||
});
|
|
||||||
|
|
||||||
// 初始化私钥
|
|
||||||
signature.init(privateKey);
|
|
||||||
|
|
||||||
// 更新待签名数据(假设原文是字符串)
|
|
||||||
signature.updateString(data);
|
|
||||||
|
|
||||||
// 生成签名(默认返回十六进制字符串)
|
|
||||||
const hexSignature = signature.sign();
|
|
||||||
|
|
||||||
// 转换为 Base64(假设 Ix 是 Base64 编码)
|
|
||||||
return hextob64(hexSignature);
|
|
||||||
}
|
|
||||||
|
|
||||||
async doRequest(req: HttpRequestConfig) {
|
|
||||||
const timestamp = Date.now();
|
|
||||||
|
|
||||||
let data = "";
|
|
||||||
if (req.method.toLowerCase() === "get") {
|
|
||||||
// area_codes=&channel_type=0,1,2&domain_name=&https_status=&nonce=1747242446238&order=&page=1&page_size=10&status=×tamp=1747242446238
|
|
||||||
let queryList = [];
|
|
||||||
for (const key in req.params) {
|
|
||||||
queryList.push(`${key}=${req.params[key]}`);
|
|
||||||
}
|
|
||||||
queryList.push(`nonce=${timestamp}`);
|
|
||||||
queryList.push(`timestamp=${timestamp}`);
|
|
||||||
//sort
|
|
||||||
queryList = queryList.sort();
|
|
||||||
data = queryList.join("&");
|
|
||||||
} else {
|
|
||||||
data = `body=${JSON.stringify(req.data || {})}&nonce=${timestamp}×tamp=${timestamp}`;
|
|
||||||
}
|
|
||||||
const sign = await this.sign(data);
|
|
||||||
const headers: any = {
|
|
||||||
sign: sign,
|
|
||||||
timestamp: timestamp,
|
|
||||||
nonce: timestamp,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.token) {
|
|
||||||
headers.Token = this.token;
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await this.http.request({
|
|
||||||
...req,
|
|
||||||
headers,
|
|
||||||
baseURL: "https://testaa.5678.jp",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.success && res.code !== 200) {
|
|
||||||
throw new Error(`请求失败:${res.msg}`);
|
|
||||||
}
|
|
||||||
return res.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async login() {
|
|
||||||
const req = {
|
|
||||||
email: this.access.username,
|
|
||||||
password: this.access.password,
|
|
||||||
accountType: 1,
|
|
||||||
};
|
|
||||||
const res = await this.doRequest({
|
|
||||||
url: "/api/vcloud/v1/userApi/noAuth/login",
|
|
||||||
method: "post",
|
|
||||||
data: req,
|
|
||||||
logRes: false,
|
|
||||||
logParams: false,
|
|
||||||
});
|
|
||||||
const { token } = res;
|
|
||||||
this.logger.info(`登录成功`);
|
|
||||||
this.token = token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +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
-1
@@ -3,7 +3,7 @@ import { CertApplyPluginNames, CertInfo } from "@certd/plugin-cert";
|
|||||||
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
import { createCertDomainGetterInputDefine, createRemoteSelectInputDefine } from "@certd/plugin-lib";
|
||||||
import { AbstractPlusTaskPlugin } from "@certd/plugin-plus";
|
import { AbstractPlusTaskPlugin } from "@certd/plugin-plus";
|
||||||
import { MaoyunAccess } from "../access.js";
|
import { MaoyunAccess } from "../access.js";
|
||||||
import { MaoyunClient } from "../client.js";
|
import { MaoyunClient } from "@certd/plugin-plus";
|
||||||
|
|
||||||
@IsTaskPlugin({
|
@IsTaskPlugin({
|
||||||
//命名规范,插件类型+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
|
//命名规范,插件类型+功能(就是目录plugin-demo中的demo),大写字母开头,驼峰命名
|
||||||
|
|||||||
Generated
+3
@@ -885,6 +885,9 @@ importers:
|
|||||||
form-data:
|
form-data:
|
||||||
specifier: ^4.0.0
|
specifier: ^4.0.0
|
||||||
version: 4.0.2
|
version: 4.0.2
|
||||||
|
jsrsasign:
|
||||||
|
specifier: ^11.1.0
|
||||||
|
version: 11.1.0
|
||||||
querystring:
|
querystring:
|
||||||
specifier: ^0.2.1
|
specifier: ^0.2.1
|
||||||
version: 0.2.1
|
version: 0.2.1
|
||||||
|
|||||||
Reference in New Issue
Block a user