优化 路由层代码细节

This commit is contained in:
Suwings 2020-10-08 17:45:01 +08:00
parent 68f8964d84
commit 52a2cc1844
14 changed files with 45 additions and 47 deletions

21
.eslintrc.js Normal file
View File

@ -0,0 +1,21 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
"MCSERVER": true
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-unused-vars": "warn",
"no-use-before-define": "warn",
"no-undef": "warn"
}
};

View File

@ -13,7 +13,7 @@ module.exports.delLogined = (sessionID) => {
};
module.exports.isLogined = (sessionID, username = null) => {
if (Logined.hasOwnProperty(sessionID) && Logined[sessionID]) {
if (Logined[sessionID]) {
if (username) {
return Logined[sessionID][0] === username;
}

View File

@ -7,8 +7,7 @@ const keyManager = require("../helper/KeyManager");
const requestLimit = require("../helper/RequestLimit");
const tools = require("../core/tools");
const fs = require("fs");
const { add } = require("../core/counter");
// 服务端实例状态获取 | 公共性 API 接口
// 无需任何权限判定
@ -103,7 +102,7 @@ router.all("/delete_server/:name", function (req, res) {
// 解析请求参数
const params = req.params.name;
try {
const result = serverModel.deleteServer(params);
serverModel.deleteServer(params);
apiResponse.ok(res);
} catch (err) {
apiResponse.error(res, err);
@ -134,7 +133,7 @@ router.post("/create_user", function (req, res) {
}
try {
// 账号密码判定
const uPattern = /^[a-zA-Z0-9_#\$]{4,18}$/;
const uPattern = /^[a-zA-Z0-9_#$]{4,18}$/;
if (!uPattern.test(req.body.username) || !tools.between(req.body.password, 6, 18)) {
apiResponse.error(res, new Error("用户名或密码格式不正确"));
return;

View File

@ -40,7 +40,7 @@ router.post("/", (req, res) => {
const writeStream = fs.createWriteStream(dstPath);
readStream.pipe(writeStream);
// 删除遗留旧文件
fs.unlink(uploadedPath, (err) => {
fs.unlink(uploadedPath, () => {
/*ignore*/
});
res.send("Done");

View File

@ -1,10 +1,7 @@
//基础的路由定义
const router = require("express")();
var session = require("express-session");
const session = require("express-session");
const { loginUser, userCenter } = require("../model/UserModel");
const response = require("../helper/Response");
const permssion = require("../helper/Permission");
const loginedContainer = require("../helper/LoginedContainer");
const tools = require("../core/tools");
const TokenManager = require("../helper/TokenManager");
@ -104,7 +101,7 @@ router.post("/login", function (req, res) {
//密码错误记录
MCSERVER.login[ip] ? MCSERVER.login[ip]++ : (MCSERVER.login[ip] = 1);
//防止数目过于太大 溢出
MCSERVER.login[ip] > 1000 ? (MCSERVER.login[ip] = 1000) : (MCSERVER.login[ip] = MCSERVER.login[ip]);
MCSERVER.login[ip] > 1000 ? (MCSERVER.login[ip] = 1000) : null;
//passwordError
counter.plus("passwordError");
req.session["login"] = false;

View File

@ -9,7 +9,7 @@ const response = require("../helper/Response");
const loginedContainer = require("../helper/LoginedContainer");
const counter = require("../core/counter");
const expressWs = require("express-ws")(router);
require("express-ws")(router);
//WebSocket 会话类
class WebsocketSession {
@ -154,8 +154,6 @@ router.ws("/ws", function (ws, req) {
let loc = data.indexOf("\n\n");
let reqHeader = data.substr(0, loc);
let reqBody = data.substr(loc + 2);
let obj;
let reqs = req;
//Websocket 自定义协议解析
const reqHeaderObj = JSON.parse(reqHeader);
@ -211,7 +209,7 @@ router.ws("/ws", function (ws, req) {
//再删一次,保险
TokenManager.delToken(token);
delete req.session["token"];
delete WsSession;
WsSession = null;
req.session.save();
//释放全局变量

View File

@ -1,6 +1,5 @@
const { WebSocketObserver } = require("../../model/WebSocketModel");
const { userCenter } = require("../../model/UserModel");
const serverModel = require("../../model/ServerModel");
const permssion = require("../../helper/Permission");
const response = require("../../helper/Response");

View File

@ -1,8 +1,6 @@
const { WebSocketObserver } = require("../../model/WebSocketModel");
const counter = require("../../core/counter");
const response = require("../../helper/Response");
const serverModel = require("../../model/ServerModel");
const userModel = require("../../model/UserModel");
const permssion = require("../../helper/Permission");
const { LogHistory } = require("../../helper/LogHistory");

View File

@ -5,7 +5,6 @@ const permssion = require("../../helper/Permission");
const tools = require("../../core/tools");
const fs = require("fs");
const childProcess = require("child_process");
const iconv = require("iconv-lite");
//Docker 镜像构建结果储存
MCSERVER.PAGE.DockerRes = [];
@ -33,9 +32,9 @@ WebSocketObserver().listener("docker/new", (data) => {
MCSERVER.warning("正在创建 Docker 镜像.");
MCSERVER.warning("镜像名字:", dockerImageName);
dockerfileData = dockerfileData.replace(/\>/gim, ">");
dockerfileData = dockerfileData.replace(/\&lt;/gim, "<");
dockerfileData = dockerfileData.replace(/\&nbsp;/gim, " ");
dockerfileData = dockerfileData.replace(/&gt;/gim, ">");
dockerfileData = dockerfileData.replace(/&lt;/gim, "<");
dockerfileData = dockerfileData.replace(/&nbsp;/gim, " ");
MCSERVER.warning("DockerFile:\n", dockerfileData);
response.wsMsgWindow(data.ws, "镜像正在创建中,请稍等....");
@ -57,7 +56,7 @@ WebSocketObserver().listener("docker/new", (data) => {
pushRes("失败");
}
});
process.on("error", (err) => {
process.on("error", () => {
pushRes("构建出错");
});
// process.stdout.on('data', (data) => console.log(iconv.decode(data, 'utf-8')));

View File

@ -3,7 +3,7 @@ const { userCenter, beliveLogin } = require("../../model/UserModel");
const serverModel = require("../../model/ServerModel");
const response = require("../../helper/Response");
const permssion = require("../../helper/Permission");
const os = require("os");
WebSocketObserver().listener("genuser/home", (data) => {
try {
@ -65,22 +65,17 @@ WebSocketObserver().listener("genuser/re_password", (data) => {
let username = data.WsSession.username.trim();
let config = JSON.parse(data.body);
if (config.newPassword && config.oldPassword) {
let user = userCenter().get(username);
beliveLogin(
username,
config.oldPassword,
() => {
try {
if (config.newPassword.length > 18 || config.newPassword.length < 6) {
response.wsMsgWindow(data.ws, "新的密码长度不正确,需要 6~18 位长度");
return;
}
userCenter().rePassword(username, config.newPassword);
userCenter().initUser();
response.wsMsgWindow(data.ws, "密码修改修改完成,请重新登陆!");
} catch (err) {
throw err;
if (config.newPassword.length > 18 || config.newPassword.length < 6) {
response.wsMsgWindow(data.ws, "新的密码长度不正确,需要 6~18 位长度");
return;
}
userCenter().rePassword(username, config.newPassword);
userCenter().initUser();
response.wsMsgWindow(data.ws, "密码修改修改完成,请重新登陆!");
},
() => {
response.wsMsgWindow(data.ws, "很抱歉,原密码错误,无法修改");

View File

@ -1,5 +1,4 @@
const { WebSocketObserver } = require("../../model/WebSocketModel");
const permssion = require("../../helper/Permission");
const response = require("../../helper/Response");
WebSocketObserver().listener("menu", (data) => {

View File

@ -3,9 +3,6 @@ const permssion = require("../../helper/Permission");
const response = require("../../helper/Response");
const schedulejob = require("../../helper/Schedule");
const tools = require("../../core/tools");
const serverModel = require("../../model/ServerModel");
const UUID = require("uuid");
//每个服务器最大数量计划任务
const MAX_MASK = MCSERVER.localProperty.schedule_max || 10;
@ -13,8 +10,6 @@ const MAX_MASK = MCSERVER.localProperty.schedule_max || 10;
//创建计划任务函数
function CreateScheduleJob(obj) {
let id = tools.randomString(6) + "_" + new Date().getTime();
let thisServer = serverModel.ServerManager().getServer(obj.servername);
schedulejob.createScheduleJobCount(id, obj.time, obj.count, obj.commande, obj.servername);
}
@ -34,11 +29,10 @@ function getMineScheduleList(servername) {
WebSocketObserver().listener("schedule/list", (data) => {
let username = data.WsSession.username;
let servername = data.body;
let list = MCSERVER.Schedule.dataModel.list;
// let list = MCSERVER.Schedule.dataModel.list;
let sendlist = getMineScheduleList(servername);
if (permssion.isCanServer(username, servername)) {
let thisServer = serverModel.ServerManager().getServer(servername);
response.wsSend(data.ws, "schedule/list", {
username: data.WsSession.username,
servername: servername,

View File

@ -2,7 +2,6 @@ const { WebSocketObserver } = require("../../model/WebSocketModel");
const serverModel = require("../../model/ServerModel");
const response = require("../../helper/Response");
const permssion = require("../../helper/Permission");
const os = require("os");
WebSocketObserver().listener("server/view", (data) => {
if (!permssion.isMaster(data.WsSession)) return;

View File

@ -3,7 +3,7 @@ const { userCenter, deleteUser } = require("../../model/UserModel");
const response = require("../../helper/Response");
const permssion = require("../../helper/Permission");
const tools = require("../../core/tools");
const os = require("os");
WebSocketObserver().listener("userset/update", (data) => {
if (!permssion.isMaster(data.WsSession)) return;
@ -34,7 +34,7 @@ WebSocketObserver().listener("userset/create", (data) => {
let password = newUserConfig.password.trim();
// 用户名范围限制
var uPattern = /^[a-zA-Z0-9_#\$]{4,18}$/;
var uPattern = /^[a-zA-Z0-9_#$]{4,18}$/;
if (!uPattern.test(username) || !tools.between(password, 6, 18)) {
response.wsMsgWindow(data.ws, "用户账号或密码格式不正确");
return;
@ -151,7 +151,7 @@ WebSocketObserver().listener("userset/upinfo", (data) => {
//如果需求,则更改用户名以及存储文件
if (username != newUS) {
let uPattern = /^[a-zA-Z0-9_#\$]{4,18}$/;
let uPattern = /^[a-zA-Z0-9_#$]{4,18}$/;
if (!uPattern.test(newUS)) {
response.wsMsgWindow(data.ws, "新的用户名格式不正确,已舍弃用户名的更改");
return;