MCSManager/app.js

310 lines
9.6 KiB
JavaScript
Raw Normal View History

2021-06-18 17:45:08 +08:00
/*
* ==================================
* MCSManager
* Copyright(c) 2021 Suwings.
* MIT Licensed
* ==================================
*/
// Logo 输出
console.log(`______ _______________________ ___
___ |/ /_ ____/_ ___/__ |/ /_____ _____________ _______ _____________
__ /|_/ /_ / _____ \\__ /|_/ /_ __ /_ __ \\ __ /_ __ / _ \\_ ___/
_ / / / / /___ ____/ /_ / / / / /_/ /_ / / / /_/ /_ /_/ // __/ /
/_/ /_/ \\____/ /____/ /_/ /_/ \\__,_/ /_/ /_/\\__,_/ _\\__, / \\___//_/
/____/
+ Copyright 2021 Suwings All rights reserved.
+ Version 8.6
`);
// 运行时环境检测
2018-02-13 15:07:06 +08:00
try {
2020-10-08 11:59:48 +08:00
let versionNum = parseInt(process.version.replace(/v/gim, "").split(".")[0]);
2021-06-18 17:45:08 +08:00
// 尽管我们建议最低版本为 v10 版本
2020-10-08 11:59:48 +08:00
if (versionNum < 10) {
console.log("[ WARN ] 您的 Node 运行环境版本似乎低于我们要求的版本.");
console.log("[ WARN ] 可能会出现未知情况,建议您更新 Node 版本 (>=10.0.0)");
}
2018-02-13 15:07:06 +08:00
} catch (err) {
2021-06-18 17:45:08 +08:00
// 忽略任何版本检测导致的错误
2018-02-13 15:07:06 +08:00
}
2021-06-18 17:45:08 +08:00
// 全局变量 MCSERVER
2018-06-03 10:43:49 +08:00
global.MCSERVER = {};
2021-06-18 17:45:08 +08:00
// 测试时检测
2018-06-03 10:43:49 +08:00
MCSERVER.allError = 0;
2021-06-18 17:45:08 +08:00
// 自动化部署测试
2018-05-30 13:19:40 +08:00
setTimeout(() => {
2020-10-08 11:59:48 +08:00
let arg2 = process.argv[2] || "";
if (arg2 == "--test") {
MCSERVER.infoLog("Test", "测试过程结束...");
if (MCSERVER.allError > 0) {
MCSERVER.infoLog("Test", "测试未通过!");
process.exit(500);
2018-05-30 13:19:40 +08:00
}
2020-10-08 11:59:48 +08:00
MCSERVER.infoLog("Test", "测试通过!");
process.exit(0);
}
2018-05-30 13:19:40 +08:00
}, 10000);
2020-10-08 11:59:48 +08:00
const fs = require("fs");
2018-04-29 20:19:06 +08:00
2021-06-18 17:45:08 +08:00
// 全局仅限本地配置
2018-04-18 20:08:09 +08:00
MCSERVER.localProperty = {};
2018-04-29 20:19:06 +08:00
2020-10-08 11:59:48 +08:00
const tools = require("./core/tools");
2018-04-29 20:19:06 +08:00
2021-06-18 17:45:08 +08:00
// 生成第一次配置文件
2020-10-08 11:59:48 +08:00
const INIT_CONFIG_PATH = "./model/init_config/";
const PRO_CONFIG = "./property.js";
2020-10-08 12:13:27 +08:00
if (!fs.existsSync(PRO_CONFIG)) tools.mCopyFileSync(INIT_CONFIG_PATH + "property.js", PRO_CONFIG);
2018-04-29 20:19:06 +08:00
2021-06-18 17:45:08 +08:00
// 加载配置
2020-10-08 11:59:48 +08:00
require("./property");
2018-02-24 16:00:15 +08:00
2020-10-08 11:59:48 +08:00
const express = require("express");
const session = require("express-session");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const querystring = require("querystring");
2021-06-18 17:45:08 +08:00
// gzip压缩
2020-10-08 11:59:48 +08:00
const compression = require("compression");
2017-11-13 12:26:31 +08:00
2021-06-18 17:45:08 +08:00
// 各类层装载 与 初始化
2020-10-08 11:59:48 +08:00
const ServerModel = require("./model/ServerModel");
const UserModel = require("./model/UserModel");
const permission = require("./helper/Permission");
const response = require("./helper/Response");
const { randomString } = require("./core/User/CryptoMine");
const counter = require("./core/counter");
const DataModel = require("./core/DataModel");
const tokenManger = require("./helper/TokenManager");
const nodeSchedule = require("node-schedule");
2020-10-08 11:59:48 +08:00
const Schedule = require("./helper/Schedule");
const NewsCenter = require("./model/NewsCenter");
2018-04-18 10:47:57 +08:00
2021-06-18 17:45:08 +08:00
// 控制台颜色
2020-10-08 11:59:48 +08:00
const colors = require("colors");
2017-11-13 12:26:31 +08:00
colors.setTheme({
2020-10-08 11:59:48 +08:00
silly: "rainbow",
input: "grey",
verbose: "cyan",
prompt: "red",
info: "green",
data: "blue",
help: "cyan",
warn: "yellow",
debug: "magenta",
2020-10-08 12:14:30 +08:00
error: "red"
2017-11-13 12:26:31 +08:00
});
2021-06-18 17:45:08 +08:00
// 全局数据中心 记录 CPU 内存
2017-11-13 12:26:31 +08:00
MCSERVER.dataCenter = {};
2021-06-18 17:45:08 +08:00
// 装载log记录器
2020-10-08 11:59:48 +08:00
require("./core/log");
MCSERVER.info("控制面板正在启动中...");
2017-11-13 12:26:31 +08:00
2021-06-18 17:45:08 +08:00
// 全局登陆记录器
2017-11-13 12:26:31 +08:00
MCSERVER.login = {};
2021-06-18 17:45:08 +08:00
// 全局 在线用户监视器
2017-11-13 12:26:31 +08:00
MCSERVER.onlineUser = {};
2021-06-18 17:45:08 +08:00
// 全局 在线 Websocket 监视器
2017-11-13 12:26:31 +08:00
MCSERVER.allSockets = {};
2021-06-18 17:45:08 +08:00
// 全局 数据内存记录器
2017-11-13 12:26:31 +08:00
MCSERVER.logCenter = {};
2021-06-18 17:45:08 +08:00
// PAGE 页面数据储存器
MCSERVER.PAGE = {};
2017-11-13 12:26:31 +08:00
2021-06-18 17:45:08 +08:00
// 计数数据:初始化方法
2017-11-13 12:26:31 +08:00
MCSERVER.logCenter.initLogData = (objStr, len, def = null) => {
2020-10-08 11:59:48 +08:00
let tmp = [];
for (let i = 0; i < len; i++) tmp.push(def);
MCSERVER.logCenter[objStr] = tmp;
};
2018-04-13 10:00:45 +08:00
2021-06-18 17:45:08 +08:00
// 计数数据:压入方法
2017-11-13 12:26:31 +08:00
MCSERVER.logCenter.pushLogData = (objStr, k, v) => {
2020-10-08 11:59:48 +08:00
MCSERVER.logCenter[objStr] = MCSERVER.logCenter[objStr].slice(1);
MCSERVER.logCenter[objStr].push({
key: k,
2020-10-08 12:14:30 +08:00
val: v
2020-10-08 11:59:48 +08:00
});
};
2017-11-13 12:26:31 +08:00
2021-06-18 17:45:08 +08:00
// 初始化 Express 框架
2017-11-13 12:26:31 +08:00
var app = express();
2021-06-18 17:45:08 +08:00
// 初始化 Express-WebSocket 框架
2020-10-08 11:59:48 +08:00
var expressWs = require("express-ws")(app);
2017-11-13 12:26:31 +08:00
2021-06-18 17:45:08 +08:00
// 初始化 Cookie and Session 的基础功能
2017-11-13 12:26:31 +08:00
app.use(cookieParser());
2020-10-08 11:59:48 +08:00
app.use(
bodyParser.urlencoded({
2020-10-08 12:14:30 +08:00
extended: false
2020-10-08 11:59:48 +08:00
})
);
2017-11-13 12:26:31 +08:00
app.use(bodyParser.json());
2021-06-18 17:45:08 +08:00
// 初始化 Session 功能
2020-10-08 11:59:48 +08:00
var UUID = require("uuid");
app.use(
session({
2018-04-02 23:12:21 +08:00
secret: UUID.v4(),
2020-10-08 11:59:48 +08:00
name: "MCSM_SESSION_ID",
2017-11-15 14:24:20 +08:00
cookie: {
2020-10-08 12:14:30 +08:00
maxAge: MCSERVER.localProperty.session_max_age * 1000 * 60
2018-04-02 23:23:20 +08:00
},
2018-04-20 10:06:19 +08:00
resave: false,
2020-10-08 12:14:30 +08:00
saveUninitialized: false
2020-10-08 11:59:48 +08:00
})
);
2017-11-13 12:26:31 +08:00
2021-06-18 17:45:08 +08:00
// 使用 gzip 静态文本压缩,但是如果你使用反向代理或某 HTTP 服务自带的gzip请关闭它
2020-10-08 11:59:48 +08:00
if (MCSERVER.localProperty.is_gzip) app.use(compression());
2018-02-24 14:32:25 +08:00
2021-06-18 17:45:08 +08:00
// 设置静态文件基础根目录
2020-10-08 11:59:48 +08:00
app.use("/public", express.static("./public"));
2018-01-20 20:32:44 +08:00
2017-11-13 12:26:31 +08:00
// console 中间件挂载
app.use((req, res, next) => {
2020-10-08 11:59:48 +08:00
// 部分请求不必显示
2020-10-08 12:13:27 +08:00
if (req.originalUrl.indexOf("/api/") == -1 && req.originalUrl.indexOf("/fs/") == -1 && req.originalUrl.indexOf("/fs_auth/") == -1 && req.originalUrl.indexOf("/fs_auth/") == -1) {
2020-10-08 11:59:48 +08:00
// MCSERVER.log('[', req.method.cyan, ']', '[', req.ip, ']', req.originalUrl);
}
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-Frame-Options", "DENY");
next();
2017-11-13 12:26:31 +08:00
});
2021-06-18 17:45:08 +08:00
// 初始化基础的根目录路由
2020-10-08 11:59:48 +08:00
app.get(["/login", "/l", "/", "/logined"], function (req, res) {
permission.needLogin(
req,
res,
() => {
res.redirect("/public/#welcome");
},
() => {
res.redirect(MCSERVER.localProperty.login_url);
}
);
2017-11-13 12:26:31 +08:00
});
2021-06-18 17:45:08 +08:00
// 自动装载所有路由
2020-10-08 11:59:48 +08:00
let routeList = fs.readdirSync("./route/");
2017-11-13 12:26:31 +08:00
for (let key in routeList) {
2020-10-08 11:59:48 +08:00
let name = routeList[key].replace(".js", "");
app.use("/" + name, require("./route/" + name));
2017-11-13 12:26:31 +08:00
}
process.on("uncaughtException", function (err) {
2021-06-18 17:45:08 +08:00
// 是否出过错误,本变量用于自动化测试
2020-10-08 11:59:48 +08:00
MCSERVER.allError++;
2021-06-18 17:45:08 +08:00
// 打印出错误
2020-10-08 11:59:48 +08:00
MCSERVER.error("错误报告:", err);
2017-11-13 12:26:31 +08:00
});
2020-10-08 11:59:48 +08:00
process.on("unhandledRejection", (reason, p) => {
MCSERVER.error("错误报告:", reason);
2020-03-04 18:28:31 +08:00
});
2021-06-18 17:45:08 +08:00
// 初始化目录结构环境
2017-11-18 15:13:01 +08:00
(function initializationRun() {
2020-10-08 11:59:48 +08:00
const USERS_PATH = "./users/";
const SERVER_PATH = "./server/";
const SERVER_PATH_CORE = "./server/server_core/";
const SERVER_PATH_SCH = "./server/schedule/";
const CENTEN_LOG_JSON_PATH = "./core/info.json";
const PUBLIC_URL_PATH = "./public/common/URL.js";
const RECORD_PARH = "./server/record_tmp/";
try {
if (!fs.existsSync(USERS_PATH)) fs.mkdirSync(USERS_PATH);
if (!fs.existsSync(SERVER_PATH)) fs.mkdirSync(SERVER_PATH);
if (!fs.existsSync(SERVER_PATH_CORE)) fs.mkdirSync(SERVER_PATH_CORE);
if (!fs.existsSync(SERVER_PATH_SCH)) fs.mkdirSync(SERVER_PATH_SCH);
if (!fs.existsSync(RECORD_PARH)) fs.mkdirSync(RECORD_PARH);
// 生成不 git 同步的文件
2020-10-08 12:13:27 +08:00
if (!fs.existsSync(CENTEN_LOG_JSON_PATH)) tools.mCopyFileSync(INIT_CONFIG_PATH + "info_reset.json", CENTEN_LOG_JSON_PATH);
if (!fs.existsSync(PUBLIC_URL_PATH)) tools.mCopyFileSync(INIT_CONFIG_PATH + "INIT_URL.js", PUBLIC_URL_PATH);
2020-10-08 11:59:48 +08:00
} catch (err) {
MCSERVER.error("初始化文件环境失败,建议重启,请检查以下报错:", err);
}
2017-11-18 15:13:01 +08:00
})();
2017-11-15 14:24:20 +08:00
2021-06-18 17:45:08 +08:00
// 开始对 Oneline File Manager 模块进行必要的初始化
2020-10-08 11:59:48 +08:00
MCSERVER.infoLog("OnlineFs", "正在初始化文件管理路由与中间件 ");
2018-01-20 20:32:44 +08:00
2021-06-18 17:45:08 +08:00
// 必须先进行登陆 且 fs API 请求必须为 Ajax 请求,得以保证跨域阻止
2020-10-08 12:13:27 +08:00
app.use(["/fs/mkdir", "/fs/rm", "/fs/patse", "/fs/cp", "/fs/rename", "/fs/ls"], function (req, res, next) {
if (req.session.fsos && req.xhr) {
next();
return;
2020-10-08 11:59:48 +08:00
}
2020-10-08 12:13:27 +08:00
res.status(403).send("禁止访问:权限不足!您不能直接访问文件在线管理程序 API请通过正常流程");
});
2018-04-18 10:47:57 +08:00
2021-06-18 17:45:08 +08:00
// 载入在线文件管理路由
2020-10-08 11:59:48 +08:00
app.use("/fs_auth", require("./onlinefs/controller/auth"));
app.use("/fs", require("./onlinefs/controller/function"));
2018-01-20 20:32:44 +08:00
2021-06-18 17:45:08 +08:00
// 初始化各个模块
2017-11-19 11:23:11 +08:00
(function initializationProm() {
2020-10-08 11:59:48 +08:00
MCSERVER.infoLog("Module", "正在初始化用户管理模块");
counter.init();
UserModel.userCenter().initUser();
MCSERVER.infoLog("Module", "正在初始化服务端管理模块");
ServerModel.ServerManager().loadALLMinecraftServer();
MCSERVER.infoLog("Module", "正在初始化计划任务模块");
Schedule.init();
var host = MCSERVER.localProperty.http_ip;
var port = MCSERVER.localProperty.http_port;
if (host == "::") host = "127.0.0.1";
2021-06-18 17:45:08 +08:00
// Express HTTP 服务监听启动
2020-10-08 12:13:27 +08:00
app.listen(MCSERVER.localProperty.http_port, MCSERVER.localProperty.http_ip, () => {
MCSERVER.infoLog("HTTP", "HTTP 模块监听: [ http://" + (host || "127.0.0.1".yellow) + ":" + port + " ]");
MCSERVER.infoLog("INFO", "配置文件: property.js 文件");
MCSERVER.infoLog("INFO", "文档参阅: https://github.com/suwings/mcsmanager");
if (MCSERVER.allError <= 0) {
MCSERVER.infoLog("INFO", "控制面板已经启动");
2021-06-23 10:04:53 +08:00
// 执行自启动任务
require("./helper/AutoStartTask").startAutoTask();
2020-10-08 12:13:27 +08:00
} else {
MCSERVER.infoLog("INFO", "控制面板启动异常");
2020-10-08 11:59:48 +08:00
}
2020-10-08 12:13:27 +08:00
});
2018-04-18 20:08:09 +08:00
})();
2021-06-18 17:45:08 +08:00
// 用于捕捉前方所有路由都未经过的请求,则可为 404 页面
2020-10-08 11:59:48 +08:00
app.get("*", function (req, res) {
2021-06-18 17:45:08 +08:00
// 重定向到 404 页面
2020-10-08 11:59:48 +08:00
res.redirect("/public/template/404_page.html");
res.end();
});
2021-06-18 17:45:08 +08:00
// 设置定时获取最新新闻动态
2020-10-12 13:32:35 +08:00
nodeSchedule.scheduleJob("59 59 23 * * *", function () {
2020-10-08 11:59:48 +08:00
MCSERVER.infoLog("INFO", "自动更新新闻动态与最新消息");
NewsCenter.requestNews();
});
2021-06-18 17:45:08 +08:00
// 重启自动获取一次
2019-10-03 11:20:32 +08:00
NewsCenter.requestNews();
2021-06-18 17:45:08 +08:00
// 程序退出信号处理
2020-10-08 11:59:48 +08:00
require("./core/procexit");