From 37e9888d23d9ea0e0eea2ae3455328a17652acde 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 09:44:54 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20node=20ksr=20=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=90=8E=E4=B8=8D=E8=BF=9B=E5=85=A5=E5=90=8E=E5=8F=B0=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ksr.js | 51 +++++++++++++++++++++++++ plugins/other/restart.js | 81 +++++++++++++++++++++++++++++----------- 2 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 ksr.js diff --git a/ksr.js b/ksr.js new file mode 100644 index 0000000..7a1bf55 --- /dev/null +++ b/ksr.js @@ -0,0 +1,51 @@ +import { spawn } from 'child_process'; +import log4js from 'log4js'; +import http from 'http' + +/* keep ssh run */ + +log4js.configure({ + appenders: { console: { type: 'console' } }, + categories: { default: { appenders: ['console'], level: 'debug' } } +}); +const logger = log4js.getLogger('app'); + +let serverProcess; +const startServer = async () => { + logger.info('Starting Bot...'); + serverProcess = spawn('node', ['app.js'], { stdio: 'inherit' }); + serverProcess.on('close', (code) => { + logger.info(`Bot process exited with code ${code}`); + if (code == null) return + process.exit() + }); +}; +startServer(); + +const serverHttpexit = http.createServer(async (req, res) => { + let remoteIP = req.socket.remoteAddress; + if (remoteIP.startsWith('::ffff:')) { + remoteIP = remoteIP.slice(7); + } + if (remoteIP !== `::1` && remoteIP !== `127.0.0.1`) { + console.log(remoteIP) + res.writeHead(403, { 'Content-Type': 'text/plain' }); + res.end('Access Forbidden\n'); + return + } + if (req.url === `/restart`) { + await serverProcess.kill(); + await startServer(); + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('OK\n'); + } else if (req.url === `/exit`) { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('OK\n'); + process.exit() + } else { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not Found\n'); + } +}) + +serverHttpexit.listen(27881, () => { }); diff --git a/plugins/other/restart.js b/plugins/other/restart.js index fb7d077..191540f 100644 --- a/plugins/other/restart.js +++ b/plugins/other/restart.js @@ -1,9 +1,20 @@ import plugin from '../../lib/plugins/plugin.js' import { createRequire } from 'module' +import fetch from 'node-fetch' +import net from 'net' const require = createRequire(import.meta.url) const { exec } = require('child_process') +const isPortTaken = async (port) => { + return new Promise((resolve) => { + const tester = net.createServer() + .once('error', () => resolve(true)) + .once('listening', () => tester.once('close', () => resolve(false)).close()) + .listen(port); + }); +}; + export class Restart extends plugin { constructor (e = '') { super({ @@ -62,30 +73,44 @@ export class Restart extends plugin { }) let npm = await this.checkPnpm() - - try { - await redis.set(this.key, data, { EX: 120 }) - let cm = `${npm} start` - if (process.argv[1].includes('pm2')) { - cm = `${npm} run restart` - } - - exec(cm, { windowsHide: true }, (error, stdout, stderr) => { - if (error) { + await redis.set(this.key, data, { EX: 120 }) + if(await isPortTaken(27881)) { + try { + let result = await fetch(`http://localhost:27881/restart`) + result = await result.text() + if(result !== `OK`) { redis.del(this.key) - this.e.reply(`操作失败!\n${error.stack}`) - logger.error(`重启失败\n${error.stack}`) - } else if (stdout) { - logger.mark('重启成功,运行已由前台转为后台') - logger.mark(`查看日志请用命令:${npm} run log`) - logger.mark(`停止后台运行命令:${npm} stop`) - process.exit() + this.e.reply(`操作失败!`) + logger.error(`重启失败`) } - }) - } catch (error) { - redis.del(this.key) - let e = error.stack ?? error - this.e.reply(`操作失败!\n${e}`) + } catch(error) { + redis.del(this.key) + this.e.reply(`操作失败!\n${error}`) + } + } else { + try { + let cm = `${npm} start` + if (process.argv[1].includes('pm2')) { + cm = `${npm} run restart` + } + + exec(cm, { windowsHide: true }, (error, stdout, stderr) => { + if (error) { + redis.del(this.key) + this.e.reply(`操作失败!\n${error.stack}`) + logger.error(`重启失败\n${error.stack}`) + } else if (stdout) { + logger.mark('重启成功,运行已由前台转为后台') + logger.mark(`查看日志请用命令:${npm} run log`) + logger.mark(`停止后台运行命令:${npm} stop`) + process.exit() + } + }) + } catch (error) { + redis.del(this.key) + let e = error.stack ?? error + this.e.reply(`操作失败!\n${e}`) + } } return true @@ -107,6 +132,18 @@ export class Restart extends plugin { } async stop () { + if(await isPortTaken(27881)) { + try { + logger.mark('关机成功,已停止运行') + await this.e.reply(`关机成功,已停止运行`) + await fetch(`http://localhost:27881/exit`) + return + } catch(error) { + this.e.reply(`操作失败!\n${error}`) + logger.error(`关机失败\n${error}`) + } + } + if (!process.argv[1].includes('pm2')) { logger.mark('关机成功,已停止运行') await this.e.reply('关机成功,已停止运行') 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 2/5] =?UTF-8?q?chore:=20=E4=BB=8E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=8E=B7=E5=8F=96=E9=BB=98=E8=AE=A4restart?= =?UTF-8?q?=20port=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}`) From bdb09132f2ab8a2ad1b991b1c1c89845cd1c353b 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:03:43 +0800 Subject: [PATCH 3/5] . --- config/default_config/bot.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/default_config/bot.yaml b/config/default_config/bot.yaml index 8dc3d92..e725c0e 100644 --- a/config/default_config/bot.yaml +++ b/config/default_config/bot.yaml @@ -7,6 +7,8 @@ ignore_self: true resend: false # 发送消息错误时是否通知主人 sendmsg_error: false +# 重启API端口 仅ksr.js生效 +restart_port: 27882 # ffmpeg ffmpeg_path: ffprobe_path: From af7bb4bfbe07d26f3b2a59a2be35da2f524d9bb2 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 17:26:31 +0800 Subject: [PATCH 4/5] =?UTF-8?q?chore:=20=E6=9B=B4=E6=94=B9restart=5Fapi?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=AB=AF=E5=8F=A3=E4=B8=BA27881?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/default_config/bot.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/default_config/bot.yaml b/config/default_config/bot.yaml index e725c0e..2d54c6b 100644 --- a/config/default_config/bot.yaml +++ b/config/default_config/bot.yaml @@ -8,7 +8,7 @@ resend: false # 发送消息错误时是否通知主人 sendmsg_error: false # 重启API端口 仅ksr.js生效 -restart_port: 27882 +restart_port: 27881 # ffmpeg ffmpeg_path: ffprobe_path: From 1822400b1fe47d474b89d30ff6fd1f0a1c47f395 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: Mon, 15 Apr 2024 21:54:06 +0800 Subject: [PATCH 5/5] =?UTF-8?q?chore:=20`node=20ksr`=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E4=B8=BA`pnpm=20ksr`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ksr.js => lib/tools/ksr.js | 0 package.json | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) rename ksr.js => lib/tools/ksr.js (100%) diff --git a/ksr.js b/lib/tools/ksr.js similarity index 100% rename from ksr.js rename to lib/tools/ksr.js diff --git a/package.json b/package.json index 82cd594..a21ecf7 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "start": "pm2 start ./config/pm2/pm2.json", "stop": "pm2 stop ./config/pm2/pm2.json", "restart": "pm2 restart ./config/pm2/pm2.json", - "log": "node ./lib/tools/log.js" + "log": "node ./lib/tools/log.js", + "ksr": "node ./lib/tools/ksr.js" }, "dependencies": { "art-template": "^4.13.2",