MCSManager/app.js

281 lines
7.8 KiB
JavaScript
Raw Normal View History

2018-02-13 15:07:06 +08:00
//运行时环境检测
try {
let versionNum = parseInt(process.version.replace(/v/igm, "").split(".")[0]);
2018-02-15 16:37:30 +08:00
if (versionNum < 8) {
2018-02-13 15:07:06 +08:00
console.log("[ ERROR ] 您的 Node 运行环境版本似乎低于我们要求的版本.");
console.log("[ ERROR ] 可能会出现未知异常,请立即更新 Node 版本 (>=8.0.0)");
}
} catch (err) {
//忽略任何版本检测导致的错误
}
2018-02-24 16:00:15 +08:00
//总全局变量
global.MCSERVER = {};
//全局仅限本地配置
2018-04-18 20:08:09 +08:00
MCSERVER.localProperty = {};
2018-02-24 16:00:15 +08:00
//加载配置
require('./property');
2017-11-13 12:26:31 +08:00
const express = require('express');
const fs = require('fs');
2018-04-18 10:47:57 +08:00
const session = require('express-session');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const querystring = require('querystring');
2018-02-24 14:32:25 +08:00
//gzip压缩
2018-04-18 10:47:57 +08:00
const compression = require('compression');
2017-11-13 12:26:31 +08:00
2018-04-18 10:47:57 +08:00
//各类层装载
2018-04-18 20:32:32 +08:00
const ServerModel = require('./model/ServerModel');
2017-11-13 12:26:31 +08:00
const UserModel = require('./model/UserModel');
const permission = require('./helper/Permission');
const response = require('./helper/Response');
2017-11-15 14:24:20 +08:00
const {
randomString
} = require('./core/User/CryptoMine');
2017-11-13 12:26:31 +08:00
const counter = require('./core/counter');
const DataModel = require('./core/DataModel');
const ftpServerInterface = require('./ftpd/ftpserver');
const VarCenter = require('./model/VarCenter');
2018-04-18 10:47:57 +08:00
2017-11-13 12:26:31 +08:00
//控制台颜色
const colors = require('colors');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'red',
info: 'green',
data: 'blue',
help: 'cyan',
warn: 'yellow',
debug: 'magenta',
error: 'red'
});
//logo输出
const LOGO_FILE_PATH = './core/logo.txt';
let data = fs.readFileSync(LOGO_FILE_PATH, 'utf-8');
console.log(data);
2018-02-24 16:00:15 +08:00
2017-11-13 12:26:31 +08:00
//全局数据中心 记录 CPU 内存
MCSERVER.dataCenter = {};
//装载log记录器
require('./core/log');
//全局设置
MCSERVER.softConfig = new DataModel('McserverConfig');
if (fs.existsSync('./McserverConfig.json')) {
MCSERVER.softConfig.load();
} else {
//初始化
MCSERVER.softConfig.ip = '';
MCSERVER.softConfig.port = 23333;
MCSERVER.softConfig.FTP_port = 10021;
MCSERVER.softConfig.FTP_ip = '';
MCSERVER.softConfig.save();
}
//全局登陆记录器
MCSERVER.login = {};
//全局 在线用户监视器
MCSERVER.onlineUser = {};
//全局 在线 Websocket 监视器
MCSERVER.allSockets = {};
//全局 token 记录器 /
MCSERVER.tokens = {};
//全局 数据内存记录器
MCSERVER.logCenter = {};
2018-04-13 10:00:45 +08:00
// 过期的数据对象
2017-11-13 12:26:31 +08:00
// MCSERVER.logCenter.CPU = [];
// MCSERVER.logCenter.RAM = [];
2018-04-13 10:00:45 +08:00
2017-11-13 12:26:31 +08:00
//init
MCSERVER.logCenter.initLogData = (objStr, len, def = null) => {
let tmp = [];
2017-11-15 14:24:20 +08:00
for (let i = 0; i < len; i++) tmp.push(def);
2017-11-13 12:26:31 +08:00
MCSERVER.logCenter[objStr] = tmp;
}
2018-04-13 10:00:45 +08:00
2017-11-13 12:26:31 +08:00
//压入方法
MCSERVER.logCenter.pushLogData = (objStr, k, v) => {
MCSERVER.logCenter[objStr] = MCSERVER.logCenter[objStr].slice(1);
MCSERVER.logCenter[objStr].push({
'key': k,
'val': v
});
}
//获取最新咨询
require('./model/requestNews');
//exp 框架
var app = express();
//web Socket 框架
var expressWs = require('express-ws')(app);
//Cookie and Session 的基础功能
app.use(cookieParser());
2017-11-15 14:24:20 +08:00
app.use(bodyParser.urlencoded({
extended: false
}));
2017-11-13 12:26:31 +08:00
app.use(bodyParser.json());
2018-04-02 23:12:21 +08:00
var UUID = require('uuid');
2017-11-13 12:26:31 +08:00
app.use(session({
2018-04-02 23:12:21 +08:00
secret: UUID.v4(),
2018-04-18 11:38:10 +08:00
name: 'Mcsm-' + UUID.v1(),
2017-11-15 14:24:20 +08:00
cookie: {
2018-04-02 23:23:20 +08:00
maxAge: 1000 * 60 * 60 * 4
},
2017-11-13 12:26:31 +08:00
resave: true,
saveUninitialized: true
2017-11-13 12:26:31 +08:00
}));
2018-02-24 14:32:25 +08:00
//使用 gzip 静态文本压缩,但是如果你使用反向代理或某 HTTP 服务自带的gzip请关闭它
2018-02-24 16:00:15 +08:00
if (MCSERVER.localProperty.is_gzip)
app.use(compression());
2018-02-24 14:32:25 +08:00
2018-04-13 10:00:45 +08:00
//初始化令牌管理器
2017-11-13 12:26:31 +08:00
VarCenter.set('user_token', {});
VarCenter.set('express_app', app);
//基础根目录
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) => {
2018-04-20 10:04:22 +08:00
console.log('[', req.protocol.green, req.method.cyan, ']', req.originalUrl);
2018-02-24 16:00:15 +08:00
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');
}
2017-11-13 12:26:31 +08:00
res.header('X-Soft', 'Mcserver Manager HTTP_SERVER');
res.header('X-Frame-Options', 'DENY');
next();
});
//基础的根目录路由
app.get(['/login', '/l', '/', '/logined'], function (req, res) {
permission.needLogin(req, res, () => {
res.redirect('/public/#welcome');
}, () => {
2018-02-24 16:00:15 +08:00
res.redirect(MCSERVER.localProperty.login_url);
2017-11-13 12:26:31 +08:00
});
});
//自动装载所有路由
let routeList = fs.readdirSync('./route/');
for (let key in routeList) {
let name = routeList[key].replace('.js', '');
app.use('/' + name, require('./route/' + name));
}
process.on("uncaughtException", function (err) {
//打印出错误
MCSERVER.error('UncaughtException 机制错误报告:', err);
});
2017-11-18 15:13:01 +08:00
//初始化目录结构环境
(function initializationRun() {
const USERS_PATH = './users/';
const SERVER_PATH = './server/';
const SERVER_PATH_CORE = './server/server_core/';
const CENTEN_LOG_JSON_PATH = './core/info.json';
try {
if (!fs.existsSync(USERS_PATH)) fs.mkdirSync(USERS_PATH);
if (!fs.existsSync(SERVER_PATH)) {
2017-11-19 11:25:04 +08:00
fs.mkdir(SERVER_PATH, () => fs.mkdirSync(SERVER_PATH_CORE));
2017-11-18 15:13:01 +08:00
}
if (!fs.existsSync(CENTEN_LOG_JSON_PATH)) {
let resetData = fs.readFileSync('./core/info_reset.json', {
encoding: 'UTF-8'
});
fs.writeFileSync('./core/info.json', resetData, {
encoding: 'UTF-8'
});
}
} catch (err) {
MCSERVER.error('初始化文件环境失败,建议重启,请检查以下报错:', err);
2017-11-15 14:24:20 +08:00
}
2017-11-18 15:13:01 +08:00
})();
2017-11-15 14:24:20 +08:00
2018-04-13 10:00:45 +08:00
//开始对 Oneline File Manager 模块进行必要的初始化
2018-01-20 14:56:22 +08:00
MCSERVER.infoLog('Online_Fs', '初始化 Online_Fs 路由与中间件 ');
2018-01-20 20:32:44 +08:00
2018-01-20 14:56:22 +08:00
//必须先进行登陆 且 fs API 请求必须为 Ajax 请求,得以保证跨域阻止
2018-01-23 11:25:47 +08:00
app.use(['/fs/mkdir', '/fs/rm', '/fs/patse', '/fs/cp', '/fs/rename', '/fs/ls'], function (req, res, next) {
2018-01-20 14:56:22 +08:00
if (req.session.fsos && req.xhr) {
next();
2018-01-20 20:32:44 +08:00
return;
2018-01-20 14:56:22 +08:00
}
res.status(403).send('禁止访问:权限不足!您不能直接访问文件在线管理程序 API请通过正常流程');
});
2018-04-18 10:47:57 +08:00
2018-01-20 20:32:44 +08:00
//载入在线文件管理路由
app.use('/fs_auth', require('./onlinefs/controller/auth'));
app.use('/fs', require('./onlinefs/controller/function'));
2018-04-18 10:47:57 +08:00
//初始化模块
2017-11-19 11:23:11 +08:00
(function initializationProm() {
2017-11-13 12:26:31 +08:00
counter.init();
UserModel.userCenter().initUser();
MCSERVER.infoLog('Module', '初始化 UserManager Module ');
2018-04-18 20:32:32 +08:00
ServerModel.ServerManager().loadALLMinecraftServer();
2017-11-13 12:26:31 +08:00
MCSERVER.infoLog('Module', '初始化 ServerManager Module ');
var host = MCSERVER.softConfig.ip;
var port = MCSERVER.softConfig.port;
if (host == '::')
host = '127.0.0.1';
2018-04-18 10:47:57 +08:00
//App Http listen
2017-11-19 11:23:11 +08:00
app.listen(MCSERVER.softConfig.port, MCSERVER.softConfig.ip, () => {
2017-11-13 12:26:31 +08:00
2017-11-19 11:23:11 +08:00
MCSERVER.infoLog('HTTP', 'HTTP 模块监听: [ http://' + (host || '127.0.0.1'.yellow) + ':' + port + ' ]');
2017-11-13 12:26:31 +08:00
2017-11-19 11:23:11 +08:00
//现在执行 FTP 服务器启动过程
ftpServerInterface.initFTPdServerOptions({
host: MCSERVER.softConfig.FTP_ip || '127.0.0.1',
port: MCSERVER.softConfig.FTP_port,
tls: null
});
//执行ftp逻辑
require('./ftpd/index');
});
2017-11-13 12:26:31 +08:00
2018-04-18 20:32:32 +08:00
2018-04-18 20:08:09 +08:00
})();
//退出事件
let _endFlag = false;
process.on('SIGINT', function () {
if (_endFlag) return;
_endFlag = true;
2018-04-18 20:32:32 +08:00
MCSERVER.infoLog('PROCESS', '程序正在结束,请稍等...'.red);
2018-04-18 20:48:30 +08:00
//保存
counter.save();
2018-04-18 20:32:32 +08:00
ServerModel.ServerManager().saveAllMinecraftServer();
UserModel.userCenter().saveAllUser();
2018-04-18 20:48:30 +08:00
2018-04-18 20:08:09 +08:00
setTimeout(() => {
2018-04-18 20:32:32 +08:00
MCSERVER.infoLog('PROCESS', 'EXIT...'.red);
2018-04-18 20:08:09 +08:00
process.exit(0);
2018-04-18 20:32:32 +08:00
}, 1000)
2018-04-18 20:08:09 +08:00
});