From 05fd258afd7a31bc23e8ac5f784ad9c26383f37a Mon Sep 17 00:00:00 2001 From: linyuchen Date: Wed, 13 Mar 2024 21:51:17 +0800 Subject: [PATCH] feat: config api --- src/main/main.ts | 74 +++----------------------- src/main/setConfig.ts | 63 ++++++++++++++++++++++ src/onebot11/action/index.ts | 4 ++ src/onebot11/action/llonebot/Config.ts | 20 +++++++ src/onebot11/action/types.ts | 2 + src/onebot11/server/http.ts | 10 ++-- 6 files changed, 103 insertions(+), 70 deletions(-) create mode 100644 src/main/setConfig.ts create mode 100644 src/onebot11/action/llonebot/Config.ts diff --git a/src/main/main.ts b/src/main/main.ts index 27442c6..67a8e25 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -13,34 +13,29 @@ import { import {ob11WebsocketServer} from "../onebot11/server/ws/WebsocketServer"; import {checkFfmpeg, DATA_DIR, getConfigUtil, log} from "../common/utils"; import { - friendRequests, getFriend, + friendRequests, + getFriend, getGroup, getGroupMember, - llonebotError, refreshGroupMembers, + llonebotError, + refreshGroupMembers, selfInfo } from "../common/data"; import {hookNTQQApiCall, hookNTQQApiReceive, ReceiveCmd, registerReceiveHook} from "../ntqqapi/hook"; import {OB11Constructor} from "../onebot11/constructor"; import {NTQQApi} from "../ntqqapi/ntcall"; -import { - ChatType, - FriendRequestNotify, - GroupMember, - GroupNotifies, - GroupNotifyTypes, - RawMessage -} from "../ntqqapi/types"; +import {ChatType, FriendRequestNotify, GroupNotifies, GroupNotifyTypes, RawMessage} from "../ntqqapi/types"; import {ob11HTTPServer} from "../onebot11/server/http"; import {OB11FriendRecallNoticeEvent} from "../onebot11/event/notice/OB11FriendRecallNoticeEvent"; import {OB11GroupRecallNoticeEvent} from "../onebot11/event/notice/OB11GroupRecallNoticeEvent"; import {postOB11Event} from "../onebot11/server/postOB11Event"; import {ob11ReverseWebsockets} from "../onebot11/server/ws/ReverseWebsocket"; import {OB11GroupAdminNoticeEvent} from "../onebot11/event/notice/OB11GroupAdminNoticeEvent"; -import {OB11GroupDecreaseEvent} from "../onebot11/event/notice/OB11GroupDecreaseEvent"; import {OB11GroupRequestEvent} from "../onebot11/event/request/OB11GroupRequest"; import {OB11FriendRequestEvent} from "../onebot11/event/request/OB11FriendRequest"; import * as path from "node:path"; import {dbUtil} from "../common/db"; +import {setConfig} from "./setConfig"; let running = false; @@ -90,61 +85,8 @@ function onLoad() { const config = getConfigUtil().getConfig() return config; }) - ipcMain.on(CHANNEL_SET_CONFIG, (event, arg: Config) => { - let oldConfig = getConfigUtil().getConfig(); - getConfigUtil().setConfig(arg) - if (arg.ob11.httpPort != oldConfig.ob11.httpPort && arg.ob11.enableHttp) { - ob11HTTPServer.restart(arg.ob11.httpPort); - } - // 判断是否启用或关闭HTTP服务 - if (!arg.ob11.enableHttp) { - ob11HTTPServer.stop(); - } else { - ob11HTTPServer.start(arg.ob11.httpPort); - } - // 正向ws端口变化,重启服务 - if (arg.ob11.wsPort != oldConfig.ob11.wsPort) { - ob11WebsocketServer.restart(arg.ob11.wsPort); - } - // 判断是否启用或关闭正向ws - if (arg.ob11.enableWs != oldConfig.ob11.enableWs) { - if (arg.ob11.enableWs) { - ob11WebsocketServer.start(arg.ob11.wsPort); - } else { - ob11WebsocketServer.stop(); - } - } - // 判断是否启用或关闭反向ws - if (arg.ob11.enableWsReverse != oldConfig.ob11.enableWsReverse) { - if (arg.ob11.enableWsReverse) { - ob11ReverseWebsockets.start(); - } else { - ob11ReverseWebsockets.stop(); - } - } - if (arg.ob11.enableWsReverse) { - // 判断反向ws地址有变化 - if (arg.ob11.wsHosts.length != oldConfig.ob11.wsHosts.length) { - ob11ReverseWebsockets.restart(); - } else { - for (const newHost of arg.ob11.wsHosts) { - if (!oldConfig.ob11.wsHosts.includes(newHost)) { - ob11ReverseWebsockets.restart(); - break; - } - } - } - } - - // 检查ffmpeg - if (arg.ffmpeg) { - checkFfmpeg(arg.ffmpeg).then(success => { - if (success) { - llonebotError.ffmpegError = '' - } - }) - } - + ipcMain.on(CHANNEL_SET_CONFIG, (event, config: Config) => { + setConfig(config).then(); }) ipcMain.on(CHANNEL_LOG, (event, arg) => { diff --git a/src/main/setConfig.ts b/src/main/setConfig.ts new file mode 100644 index 0000000..45f0006 --- /dev/null +++ b/src/main/setConfig.ts @@ -0,0 +1,63 @@ +import {Config} from "../common/types"; +import {checkFfmpeg, getConfigUtil} from "../common/utils"; +import {ob11HTTPServer} from "../onebot11/server/http"; +import {ob11WebsocketServer} from "../onebot11/server/ws/WebsocketServer"; +import {ob11ReverseWebsockets} from "../onebot11/server/ws/ReverseWebsocket"; +import {llonebotError} from "../common/data"; + +export async function setConfig(config: Config) { + let oldConfig = getConfigUtil().getConfig(); + getConfigUtil().setConfig(config) + if (config.ob11.httpPort != oldConfig.ob11.httpPort && config.ob11.enableHttp) { + ob11HTTPServer.restart(config.ob11.httpPort); + } + // 判断是否启用或关闭HTTP服务 + if (!config.ob11.enableHttp) { + ob11HTTPServer.stop(); + } else { + ob11HTTPServer.start(config.ob11.httpPort); + } + // 正向ws端口变化,重启服务 + if (config.ob11.wsPort != oldConfig.ob11.wsPort) { + ob11WebsocketServer.restart(config.ob11.wsPort); + } + // 判断是否启用或关闭正向ws + if (config.ob11.enableWs != oldConfig.ob11.enableWs) { + if (config.ob11.enableWs) { + ob11WebsocketServer.start(config.ob11.wsPort); + } else { + ob11WebsocketServer.stop(); + } + } + // 判断是否启用或关闭反向ws + if (config.ob11.enableWsReverse != oldConfig.ob11.enableWsReverse) { + if (config.ob11.enableWsReverse) { + ob11ReverseWebsockets.start(); + } else { + ob11ReverseWebsockets.stop(); + } + } + if (config.ob11.enableWsReverse) { + // 判断反向ws地址有变化 + if (config.ob11.wsHosts.length != oldConfig.ob11.wsHosts.length) { + ob11ReverseWebsockets.restart(); + } else { + for (const newHost of config.ob11.wsHosts) { + if (!oldConfig.ob11.wsHosts.includes(newHost)) { + ob11ReverseWebsockets.restart(); + break; + } + } + } + } + + // 检查ffmpeg + if (config.ffmpeg) { + checkFfmpeg(config.ffmpeg).then(success => { + if (success) { + llonebotError.ffmpegError = '' + } + }) + } + +} \ No newline at end of file diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index ac63ef1..c94a55b 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -32,9 +32,13 @@ import GetImage from "./GetImage"; import GetRecord from "./GetRecord"; import GoCQHTTPMarkMsgAsRead from "./MarkMsgAsRead"; import CleanCache from "./CleanCache"; +import {GetConfigAction, SetConfigAction} from "./llonebot/Config"; export const actionHandlers = [ new Debug(), + new GetConfigAction(), + new SetConfigAction(), + // onebot11 new SendLike(), new GetMsg(), new GetLoginInfo(), diff --git a/src/onebot11/action/llonebot/Config.ts b/src/onebot11/action/llonebot/Config.ts new file mode 100644 index 0000000..60fea1c --- /dev/null +++ b/src/onebot11/action/llonebot/Config.ts @@ -0,0 +1,20 @@ +import BaseAction from "../BaseAction"; +import {Config} from "../../../common/types"; +import {getConfigUtil} from "../../../common/utils"; +import {ActionName} from "../types"; +import {setConfig} from "../../../main/setConfig"; + + +export class GetConfigAction extends BaseAction { + actionName = ActionName.GetConfig + protected async _handle(payload: null): Promise { + return getConfigUtil().getConfig() + } +} + +export class SetConfigAction extends BaseAction { + actionName = ActionName.SetConfig + protected async _handle(payload: Config): Promise { + setConfig(payload).then(); + } +} \ No newline at end of file diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index 9ab5f2c..d75ab10 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -14,6 +14,8 @@ export interface InvalidCheckResult { } export enum ActionName { + GetConfig = "get_config", + SetConfig = "set_config", Debug = "llonebot_debug", SendLike = "send_like", GetLoginInfo = "get_login_info", diff --git a/src/onebot11/server/http.ts b/src/onebot11/server/http.ts index 8c44805..31cfff8 100644 --- a/src/onebot11/server/http.ts +++ b/src/onebot11/server/http.ts @@ -20,8 +20,10 @@ class OB11HTTPServer extends HttpServerBase { export const ob11HTTPServer = new OB11HTTPServer(); -for (const action of actionHandlers) { - for (const method of ["post", "get"]) { - ob11HTTPServer.registerRouter(method, action.actionName, (res, payload) => action.handle(payload)) +setTimeout(() => { + for (const action of actionHandlers) { + for (const method of ["post", "get"]) { + ob11HTTPServer.registerRouter(method, action.actionName, (res, payload) => action.handle(payload)) + } } -} +}, 0) \ No newline at end of file