From 8b8039f42bbce10a4d0e737cdeeeef9bb17bee5a Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 20 Sep 2024 19:29:16 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=BE=A4=E6=99=96=E6=94=AF=E6=8C=81OTP?= =?UTF-8?q?=E5=8F=8C=E9=87=8D=E9=AA=8C=E8=AF=81=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/pipeline/src/core/handler.ts | 33 +++++++++ packages/core/pipeline/src/core/index.ts | 1 + .../core/pipeline/src/utils/util.request.ts | 17 +++-- .../ui/certd-client/src/components/index.ts | 2 + .../src/components/plugins/index.ts | 6 ++ .../plugins/synology/device-id-getter.vue | 73 +++++++++++++++++++ .../pipeline/controller/handle-controller.ts | 36 +++++++++ 7 files changed, 160 insertions(+), 8 deletions(-) create mode 100644 packages/core/pipeline/src/core/handler.ts create mode 100644 packages/ui/certd-client/src/components/plugins/index.ts create mode 100644 packages/ui/certd-client/src/components/plugins/synology/device-id-getter.vue create mode 100644 packages/ui/certd-server/src/modules/pipeline/controller/handle-controller.ts diff --git a/packages/core/pipeline/src/core/handler.ts b/packages/core/pipeline/src/core/handler.ts new file mode 100644 index 00000000..8180a627 --- /dev/null +++ b/packages/core/pipeline/src/core/handler.ts @@ -0,0 +1,33 @@ +import _ from "lodash-es"; +import { HttpClient, ILogger } from "../utils"; + +export type PluginRequest = { + type: "plugin" | "access"; + typeName: string; + action: string; + input: any; + data: any; +}; + +export type RequestHandleContext = { + http: HttpClient; + logger: ILogger; +}; + +export class RequestHandler { + async onRequest(req: PluginRequest, ctx: RequestHandleContext) { + if (!req.action) { + throw new Error("action is required"); + } + + const methodName = `on${_.upperFirst(req.action)}`; + + // @ts-ignore + const method = this[methodName]; + if (method) { + // @ts-ignore + return await this[methodName](req.data, ctx); + } + throw new Error(`action ${req.action} not found`); + } +} diff --git a/packages/core/pipeline/src/core/index.ts b/packages/core/pipeline/src/core/index.ts index ea038be6..6059e858 100644 --- a/packages/core/pipeline/src/core/index.ts +++ b/packages/core/pipeline/src/core/index.ts @@ -4,3 +4,4 @@ export * from "./context.js"; export * from "./storage.js"; export * from "./file-store.js"; export * from "./license.js"; +export * from "./handler.js"; diff --git a/packages/core/pipeline/src/utils/util.request.ts b/packages/core/pipeline/src/utils/util.request.ts index 12e841fd..ca0c11b9 100644 --- a/packages/core/pipeline/src/utils/util.request.ts +++ b/packages/core/pipeline/src/utils/util.request.ts @@ -45,7 +45,7 @@ export function createAxiosService({ logger }: { logger: Logger }) { // 创建一个 axios 实例 const service = axios.create(); - const defaultAgents = createAgent(); + // const defaultAgents = createAgent(); // 请求拦截 service.interceptors.request.use( (config: any) => { @@ -53,13 +53,14 @@ export function createAxiosService({ logger }: { logger: Logger }) { if (config.timeout == null) { config.timeout = 15000; } - let agents = defaultAgents; - if (config.skipSslVerify) { - agents = createAgent({ rejectUnauthorized: config.rejectUnauthorized }); - } - - config.httpsAgent = agents.httpsAgent; - config.httpAgent = agents.httpAgent; + // let agents = defaultAgents; + // if (config.skipSslVerify) { + // logger.info("跳过SSL验证"); + // agents = createAgent({ rejectUnauthorized: config.rejectUnauthorized }); + // } + // delete config.skipSslVerify; + // config.httpsAgent = agents.httpsAgent; + // config.httpAgent = agents.httpAgent; return config; }, diff --git a/packages/ui/certd-client/src/components/index.ts b/packages/ui/certd-client/src/components/index.ts index 3df02964..7e003452 100644 --- a/packages/ui/certd-client/src/components/index.ts +++ b/packages/ui/certd-client/src/components/index.ts @@ -8,6 +8,7 @@ import { CheckCircleOutlined, InfoCircleOutlined, UndoOutlined } from "@ant-desi import CronEditor from "./cron-editor/index.vue"; import { CronLight } from "@vue-js-cron/light"; import "@vue-js-cron/light/dist/light.css"; +import Plugins from "./plugins/index"; export default { install(app: any) { app.component("PiContainer", PiContainer); @@ -24,5 +25,6 @@ export default { app.component("UndoOutlined", UndoOutlined); app.use(vip); + app.use(Plugins); } }; diff --git a/packages/ui/certd-client/src/components/plugins/index.ts b/packages/ui/certd-client/src/components/plugins/index.ts new file mode 100644 index 00000000..d999d992 --- /dev/null +++ b/packages/ui/certd-client/src/components/plugins/index.ts @@ -0,0 +1,6 @@ +import PiSynologyIdDeviceGetter from "./synology/device-id-getter.vue"; +export default { + install(app: any) { + app.component("PiSynologyDeviceIdGetter", PiSynologyIdDeviceGetter); + } +}; diff --git a/packages/ui/certd-client/src/components/plugins/synology/device-id-getter.vue b/packages/ui/certd-client/src/components/plugins/synology/device-id-getter.vue new file mode 100644 index 00000000..f58149bb --- /dev/null +++ b/packages/ui/certd-client/src/components/plugins/synology/device-id-getter.vue @@ -0,0 +1,73 @@ + + + diff --git a/packages/ui/certd-server/src/modules/pipeline/controller/handle-controller.ts b/packages/ui/certd-server/src/modules/pipeline/controller/handle-controller.ts new file mode 100644 index 00000000..ccd23826 --- /dev/null +++ b/packages/ui/certd-server/src/modules/pipeline/controller/handle-controller.ts @@ -0,0 +1,36 @@ +import { ALL, Body, Controller, Post, Provide } from '@midwayjs/core'; +import { Constants } from '../../../basic/constants.js'; +import { accessRegistry, http, logger, PluginRequest, RequestHandleContext } from '@certd/pipeline'; +import { merge } from 'lodash-es'; +import { BaseController } from '../../../basic/base-controller.js'; +@Provide() +@Controller('/api/pi/handle') +export class HandleController extends BaseController { + @Post('/', { summary: Constants.per.authOnly }) + async request(@Body(ALL) body: PluginRequest) { + const type = body.type; + if (type === 'access') { + const accessItem = accessRegistry.get(body.typeName); + const accessCls = accessItem.target; + if (accessCls == null) { + throw new Error(`access ${body.typeName} not found`); + } + //实例化access + //@ts-ignore + const access = new accessCls(); + //注入input + merge(access, body.input); + const ctx: RequestHandleContext = { + http: http, + logger: logger, + }; + const res = await access.onRequest(body, ctx); + + return this.ok(res); + } else if (type === 'plugin') { + throw new Error(`plugin:${body.typeName} not support`); + } else { + throw new Error(`type:${type} not support`); + } + } +}