From b2088824cc4f6b5bb44936cdd5a78bda019f9126 Mon Sep 17 00:00:00 2001 From: idranme Date: Wed, 4 Sep 2024 17:15:41 +0800 Subject: [PATCH] feat --- package.json | 4 ++-- src/common/config.ts | 3 ++- src/common/types.ts | 1 + src/onebot11/adapter.ts | 10 ++++++---- src/onebot11/connect/http.ts | 6 ++++-- src/onebot11/connect/ws.ts | 10 ++++++++-- src/renderer/index.ts | 7 ++++++- 7 files changed, 29 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index aa734f0..81a0bbb 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "cors": "^2.8.5", "cosmokit": "^1.6.2", "express": "^4.19.2", - "fast-xml-parser": "^4.4.1", + "fast-xml-parser": "^4.5.0", "file-type": "^19.4.1", "fluent-ffmpeg": "^2.1.3", "minato": "^3.5.1", @@ -38,7 +38,7 @@ "electron": "^31.4.0", "electron-vite": "^2.3.0", "typescript": "^5.5.4", - "vite": "^5.4.2", + "vite": "^5.4.3", "vite-plugin-cp": "^4.0.8" }, "packageManager": "yarn@4.4.1" diff --git a/src/common/config.ts b/src/common/config.ts index c57d2a9..1922aad 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -33,7 +33,8 @@ export class ConfigUtil { enableWsReverse: false, messagePostFormat: 'array', enableHttpHeart: false, - enableQOAutoQuote: false + enableQOAutoQuote: false, + listenLocalhost: false } const defaultConfig: Config = { enableLLOB: true, diff --git a/src/common/types.ts b/src/common/types.ts index b00a879..7379733 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -11,6 +11,7 @@ export interface OB11Config { messagePostFormat?: 'array' | 'string' enableHttpHeart?: boolean enableQOAutoQuote: boolean // 快速操作回复自动引用原消息 + listenLocalhost: boolean } export interface CheckVersion { diff --git a/src/onebot11/adapter.ts b/src/onebot11/adapter.ts index 6af8cf3..152b225 100644 --- a/src/onebot11/adapter.ts +++ b/src/onebot11/adapter.ts @@ -50,7 +50,8 @@ class OneBot11Adapter extends Service { this.ob11Http = new OB11Http(ctx, { port: config.httpPort, token: config.token, - actionMap + actionMap, + listenLocalhost: config.listenLocalhost }) this.ob11HttpPost = new OB11HttpPost(ctx, { hosts: config.httpHosts, @@ -62,7 +63,8 @@ class OneBot11Adapter extends Service { port: config.wsPort, heartInterval: config.heartInterval, token: config.token, - actionMap + actionMap, + listenLocalhost: config.listenLocalhost }) this.ob11WebSocketReverseManager = new OB11WebSocketReverseManager(ctx, { hosts: config.wsHosts, @@ -292,7 +294,7 @@ class OneBot11Adapter extends Service { } } // HTTP 端口变化,重启服务 - if (config.ob11.httpPort !== old.httpPort) { + if ((config.ob11.httpPort !== old.httpPort || config.ob11.listenLocalhost !== old.listenLocalhost) && config.ob11.enableHttp) { await this.ob11Http.stop() this.ob11Http.start() } @@ -305,7 +307,7 @@ class OneBot11Adapter extends Service { } } // 正向 WebSocket 端口变化,重启服务 - if (config.ob11.wsPort !== old.wsPort) { + if ((config.ob11.wsPort !== old.wsPort || config.ob11.listenLocalhost !== old.listenLocalhost) && config.ob11.enableWs) { await this.ob11WebSocket.stop() this.ob11WebSocket.start() llonebotError.wsServerError = '' diff --git a/src/onebot11/connect/http.ts b/src/onebot11/connect/http.ts index d24c0c6..75896e2 100644 --- a/src/onebot11/connect/http.ts +++ b/src/onebot11/connect/http.ts @@ -51,8 +51,9 @@ class OB11Http { this.expressAPP.get('/', (req: Request, res: Response) => { res.send(`LLOneBot server 已启动`) }) - this.server = this.expressAPP.listen(this.config.port, '0.0.0.0', () => { - this.ctx.logger.info(`HTTP server started 0.0.0.0:${this.config.port}`) + const host = this.config.listenLocalhost ? '127.0.0.1' : '0.0.0.0' + this.server = this.expressAPP.listen(this.config.port, host, () => { + this.ctx.logger.info(`HTTP server started ${host}:${this.config.port}`) }) llonebotError.httpServerError = '' } catch (e: any) { @@ -136,6 +137,7 @@ namespace OB11Http { port: number token?: string actionMap: Map> + listenLocalhost: boolean } } diff --git a/src/onebot11/connect/ws.ts b/src/onebot11/connect/ws.ts index 690ec8d..00c10d9 100644 --- a/src/onebot11/connect/ws.ts +++ b/src/onebot11/connect/ws.ts @@ -21,9 +21,14 @@ class OB11WebSocket { public start() { if (this.wsServer) return - this.ctx.logger.info(`WebSocket server started 0.0.0.0:${this.config.port}`) + const host = this.config.listenLocalhost ? '127.0.0.1' : '0.0.0.0' + this.ctx.logger.info(`WebSocket server started ${host}:${this.config.port}`) try { - this.wsServer = new WebSocketServer({ port: this.config.port, maxPayload: 1024 * 1024 * 1024 }) + this.wsServer = new WebSocketServer({ + host, + port: this.config.port, + maxPayload: 1024 * 1024 * 1024 + }) llonebotError.wsServerError = '' } catch (e: any) { llonebotError.wsServerError = '正向 WebSocket 服务启动失败, ' + e.toString() @@ -165,6 +170,7 @@ namespace OB11WebSocket { heartInterval: number token?: string actionMap: Map> + listenLocalhost: boolean } } diff --git a/src/renderer/index.ts b/src/renderer/index.ts index 4623977..f783a98 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -166,7 +166,12 @@ async function onSettingWindowCreated(view: Element) { SettingItem( '快速操作回复自动引用原消息', null, - SettingSwitch('ob11.enableQOAutoQuote', config.ob11.enableQOAutoQuote, { 'control-display-id': 'config-ob11-enableQOAutoQuote' }), + SettingSwitch('ob11.enableQOAutoQuote', config.ob11.enableQOAutoQuote), + ), + SettingItem( + 'HTTP、正向 WebSocket 服务仅监听 127.0.0.1', + '而不是 0.0.0.0', + SettingSwitch('ob11.listenLocalhost', config.ob11.listenLocalhost), ), SettingItem('', null, SettingButton('保存', 'config-ob11-save', 'primary')), ]),