From 21e80a8a88d17301aa9b4f984b229300c90822eb Mon Sep 17 00:00:00 2001 From: suwings Date: Sat, 24 Feb 2018 16:00:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=AC=E5=9C=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 27 +++++++++---- core/Process/BaseMcserver.js | 7 +--- core/Process/Mcserver.js | 2 +- property.js | 64 ++++++++++++++++++++++++++++++ route/websocket/center.js | 22 +++++----- route/websocket/console.js | 2 +- route/websocket/console/history.js | 6 ++- 7 files changed, 102 insertions(+), 28 deletions(-) create mode 100644 property.js diff --git a/app.js b/app.js index 9b349912..ac0a91dc 100644 --- a/app.js +++ b/app.js @@ -9,6 +9,15 @@ try { //忽略任何版本检测导致的错误 } +//总全局变量 +global.MCSERVER = {}; + +//全局仅限本地配置 +MCSERVER.localProperty = {} +//加载配置 +require('./property'); + + const express = require('express'); const fs = require('fs'); var session = require('express-session'); @@ -51,8 +60,7 @@ const LOGO_FILE_PATH = './core/logo.txt'; let data = fs.readFileSync(LOGO_FILE_PATH, 'utf-8'); console.log(data); -//总全局变量 -global.MCSERVER = {}; + //全局数据中心 记录 CPU 内存 MCSERVER.dataCenter = {}; @@ -126,7 +134,8 @@ app.use(session({ })); //使用 gzip 静态文本压缩,但是如果你使用反向代理或某 HTTP 服务自带的gzip,请关闭它 -//app.use(compression()); +if (MCSERVER.localProperty.is_gzip) + app.use(compression()); //初始化令牌管理器 已弃用 向下兼容 VarCenter.set('user_token', {}); @@ -139,10 +148,12 @@ app.use('/public', express.static('./public')); // console 中间件挂载 app.use((req, res, next) => { console.log('[', req.protocol.green, req.httpVersion.green, req.method.cyan, ']', req.originalUrl); - // res.header("Access-Control-Allow-Origin", "*"); - // res.header('Access-Control-Allow-Methods', 'GET, POST'); - // res.header("Access-Control-Allow-Headers", "X-Requested-With"); - // res.header('Access-Control-Allow-Headers', 'Content-Type'); + if (MCSERVER.localProperty.is_allow_csrf) { + res.header("Access-Control-Allow-Origin", "*"); + res.header('Access-Control-Allow-Methods', 'GET, POST'); + res.header("Access-Control-Allow-Headers", "X-Requested-With"); + res.header('Access-Control-Allow-Headers', 'Content-Type'); + } res.header('X-Soft', 'Mcserver Manager HTTP_SERVER'); res.header('X-Frame-Options', 'DENY'); next(); @@ -154,7 +165,7 @@ app.get(['/login', '/l', '/', '/logined'], function (req, res) { permission.needLogin(req, res, () => { res.redirect('/public/#welcome'); }, () => { - res.redirect('/public/login/'); + res.redirect(MCSERVER.localProperty.login_url); }); }); diff --git a/core/Process/BaseMcserver.js b/core/Process/BaseMcserver.js index 71c00de9..43d6b3bd 100644 --- a/core/Process/BaseMcserver.js +++ b/core/Process/BaseMcserver.js @@ -5,12 +5,7 @@ const EventEmitter = require('events'); const DataModel = require('../DataModel'); const os = require('os'); -var CODE_CONSOLE = 'GBK'; -if (os.platform() == 'win32') { - CODE_CONSOLE = 'GBK'; -} else { - CODE_CONSOLE = 'UTF-8'; -} +var CODE_CONSOLE = MCSERVER.localProperty.console_encode; //https://github.com/Gagle/Node-Properties const properties = require("properties"); diff --git a/core/Process/Mcserver.js b/core/Process/Mcserver.js index e945cef6..51c46641 100644 --- a/core/Process/Mcserver.js +++ b/core/Process/Mcserver.js @@ -82,7 +82,7 @@ class MinecraftServer extends ServerProcess { terminalLog(strLine) { this.terminalQueue.push(strLine); - if (this.terminalQueue.length > 512) { + if (this.terminalQueue.length > MCSERVER.localProperty.terminalQueue_max_length) { this.terminalQueue = this.terminalQueue.slice(400); } } diff --git a/property.js b/property.js new file mode 100644 index 00000000..f328f96f --- /dev/null +++ b/property.js @@ -0,0 +1,64 @@ +//仅限本地配置文件 +//你可以根据这些说明进行一些相关的配置,修改。 + +/** + * ----------- 无计算机语言基础者请阅读 ----------- + * + * 文本值,双引号之间是文本描述(字符串),类似于 "UTF-8" ,"Hello" 等等 + * 真假值, true 代表准许 false 代表禁止 + * 数字值,直接书写即可 列如 1,2,3,5.565,20000,5555, + * 注释,纯属用来看的,毫无作用 // 代表单行注释 + * + * 请放心,这不是要你书写计算机语言,而是进行十分简单的编辑; + * 当然你可以选择不改动此文件。 + * + * + * 绝大部分设置,需要重启面板生效! + */ + + +/* --------------- 功能代码区,请勿修改 ---------------*/ +//控制台默认编码代码 +const os = require("os"); +let auto_console_coding; +if (os.platform() == "win32") + auto_console_coding = 'GBK'; +else + auto_console_coding = 'UTF-8'; +/* --------------- 功能代码区,请勿修改 ---------------*/ + + + + +/*--------------- 配置开始,这与 Minecraft 服务器配置几乎差不多 --------------- */ + +//控制台实时刷新频率 单位毫秒 默认1100 毫秒 +MCSERVER.localProperty.console_send_times = 1100; + + +//控制台默认编码,默认自动选择,你可以去除变量 auto_console_coding 自己写,如 "UFT-8" 或 "GBXXXX" +//如果控制台中文乱码,你可以尝试更改此选项 +MCSERVER.localProperty.console_encode = auto_console_coding; + + +//是否开启 gzip 静态文件压缩,但是如果你使用反向代理或某 HTTP 服务自带的gzip,请关闭它 +MCSERVER.localProperty.is_gzip = true; + + +//是否准许跨域请求,如果准许,将失去一部分安全性,但是你二次开发可能需要 +MCSERVER.localProperty.is_allow_csrf = false; + + +//登录页面 URL,我们有两个登录页面,你可以选择其一,或自己选择 +MCSERVER.localProperty.login_url = "/public/login/"; + + +//控制台历史记录最大缓存长度 +//缓存长度越高,能存下的日志越多,但是对服务器内存牺牲更大 +//只建议调小,而不建议调大 +MCSERVER.localProperty.terminalQueue_max_length = 512; + + +//控制数据中心 数据刷新频率 单位毫秒 +//默认 2000 毫秒 +MCSERVER.localProperty.data_center_times = 2000; \ No newline at end of file diff --git a/route/websocket/center.js b/route/websocket/center.js index 3d0d8dcf..38263c30 100644 --- a/route/websocket/center.js +++ b/route/websocket/center.js @@ -1,4 +1,6 @@ -const { WebSocketObserver } = require('../../model/WebSocketModel'); +const { + WebSocketObserver +} = require('../../model/WebSocketModel'); const counter = require('../../core/counter'); const tools = require('../../core/tools'); const response = require('../../helper/Response'); @@ -40,12 +42,12 @@ let cacheSystemInfo = null; let usage = process.memoryUsage(); //init 记录器 -MCSERVER.logCenter.initLogData('CPU',16); -MCSERVER.logCenter.initLogData('RAM',16); +MCSERVER.logCenter.initLogData('CPU', 16); +MCSERVER.logCenter.initLogData('RAM', 16); //实不相瞒,其实我是弄的缓存 -setInterval(function() { - osUtils.cpuUsage(function(v) { +setInterval(function () { + osUtils.cpuUsage(function (v) { // console.log('CPU Usage (%): ' + v * 100); cacheCPU = (v * 100).toFixed(2); MCSERVER.dataCenter.cacheCPU = cacheCPU; @@ -81,15 +83,15 @@ setInterval(function() { } - let useMemBai = ((os.freemem() / 1024) / (os.totalmem() / 1024) * 100).toFixed(0); + let useMemBai = ((os.freemem() / 1024) / (os.totalmem() / 1024) * 100).toFixed(0); //压入记录器 - MCSERVER.logCenter.pushLogData('CPU',tools.getMineTime(),parseInt(cacheCPU)); - MCSERVER.logCenter.pushLogData('RAM',tools.getMineTime(),100 - useMemBai); + MCSERVER.logCenter.pushLogData('CPU', tools.getMineTime(), parseInt(cacheCPU)); + MCSERVER.logCenter.pushLogData('RAM', tools.getMineTime(), 100 - useMemBai); // console.log(MCSERVER.logCenter.RAM) - + setTimeout(() => counter.save(), 0); //让其异步的去保存 -}, 2000); +}, MCSERVER.localProperty.data_center_times); //数据中心 diff --git a/route/websocket/console.js b/route/websocket/console.js index 06caa405..a84c424e 100644 --- a/route/websocket/console.js +++ b/route/websocket/console.js @@ -115,7 +115,7 @@ setInterval(() => { delete consoleBuffer[serverName] consoleBuffer[serverName] = ""; } -}, 1100); +}, MCSERVER.localProperty.console_send_times); //控制台标准输出流 serverModel.ServerManager().on('console', (data) => { let server = serverModel.ServerManager().getServer(data.serverName); diff --git a/route/websocket/console/history.js b/route/websocket/console/history.js index 320d5214..90117f97 100644 --- a/route/websocket/console/history.js +++ b/route/websocket/console/history.js @@ -2,7 +2,9 @@ const response = require('../../../helper/Response'); var serverModel = require('../../../model/ServerModel'); var userModel = require('../../../model/UserModel'); const permssion = require('../../../helper/Permission'); -const { WebSocketObserver } = require('../../../model/WebSocketModel'); +const { + WebSocketObserver +} = require('../../../model/WebSocketModel'); //前端请求加载历史缓存 const HISTORY_SIZE = 64; @@ -22,7 +24,7 @@ WebSocketObserver().listener('server/console/history', (data) => { stringHistory += dataHistory[i]; } if (stringHistory == '' || stringHistory.length <= 2) - stringHistory = '------- 暂无历史记录 -------
'; + stringHistory = '------- 暂无记录历史记录,更多历史记录请查看在线文件管理的 Log 文本文件 -------
'; response.wsSend(data.ws, 'server/console/history', 'terminalBack', stringHistory); }