From b84dbbcacc51c6e204639da4e8eb59a7455892ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=A5=88=E5=8D=83=E7=A5=81?= <2632139786@qq.com> Date: Sun, 14 Apr 2024 10:00:13 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BB=8E=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=8E=B7=E5=8F=96=E9=BB=98=E8=AE=A4restart=20port?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ksr.js | 10 +++++++++- plugins/other/restart.js | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ksr.js b/ksr.js index 7a1bf55..9093833 100644 --- a/ksr.js +++ b/ksr.js @@ -1,6 +1,8 @@ import { spawn } from 'child_process'; import log4js from 'log4js'; import http from 'http' +import YAML from 'yaml' +import fs from 'fs' /* keep ssh run */ @@ -47,5 +49,11 @@ const serverHttpexit = http.createServer(async (req, res) => { res.end('Not Found\n'); } }) +let restart_port +try { + restart_port = YAML.parse(fs.readFileSync(`./config/config/bot.yaml`, `utf-8`)) + restart_port = restart_port.restart_port || 27881 +} catch {} -serverHttpexit.listen(27881, () => { }); +logger.info(`restart_api run on port ${restart_port || 27881}`) +serverHttpexit.listen(restart_port || 27881, () => { }); diff --git a/plugins/other/restart.js b/plugins/other/restart.js index 191540f..25bdbb3 100644 --- a/plugins/other/restart.js +++ b/plugins/other/restart.js @@ -2,6 +2,8 @@ import plugin from '../../lib/plugins/plugin.js' import { createRequire } from 'module' import fetch from 'node-fetch' import net from 'net' +import fs from 'fs' +import YAML from 'yaml' const require = createRequire(import.meta.url) const { exec } = require('child_process') @@ -62,6 +64,11 @@ export class Restart extends plugin { } async restart () { + let restart_port + try { + restart_port = YAML.parse(fs.readFileSync(`./config/config/bot.yaml`, `utf-8`)) + restart_port = restart_port.restart_port || 27881 + } catch { } await this.e.reply('开始执行重启,请稍等...') logger.mark(`${this.e.logFnc} 开始执行重启,请稍等...`) @@ -74,9 +81,9 @@ export class Restart extends plugin { let npm = await this.checkPnpm() await redis.set(this.key, data, { EX: 120 }) - if(await isPortTaken(27881)) { + if(await isPortTaken(restart_port || 27881)) { try { - let result = await fetch(`http://localhost:27881/restart`) + let result = await fetch(`http://localhost:${restart_port || 27881}/restart`) result = await result.text() if(result !== `OK`) { redis.del(this.key) @@ -132,11 +139,16 @@ export class Restart extends plugin { } async stop () { - if(await isPortTaken(27881)) { + let restart_port + try { + restart_port = YAML.parse(fs.readFileSync(`./config/config/bot.yaml`, `utf-8`)) + restart_port = restart_port.restart_port || 27881 + } catch { } + if(await isPortTaken(restart_port || 27881)) { try { logger.mark('关机成功,已停止运行') await this.e.reply(`关机成功,已停止运行`) - await fetch(`http://localhost:27881/exit`) + await fetch(`http://localhost:${restart_port || 27881}/exit`) return } catch(error) { this.e.reply(`操作失败!\n${error}`)