From 52a2cc1844de59e7db0bad4a1b640add7f6a0414 Mon Sep 17 00:00:00 2001 From: Suwings Date: Thu, 8 Oct 2020 17:45:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=B1=82=E4=BB=A3=E7=A0=81=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .eslintrc.js | 21 +++++++++++++++++++++ helper/LoginedContainer.js | 2 +- route/api.js | 7 +++---- route/fileupload.js | 2 +- route/user.js | 7 ++----- route/websocket.js | 6 ++---- route/websocket/apikey.js | 1 - route/websocket/console.js | 2 -- route/websocket/docker.js | 9 ++++----- route/websocket/genuser.js | 19 +++++++------------ route/websocket/meum.js | 1 - route/websocket/schedule.js | 8 +------- route/websocket/server.js | 1 - route/websocket/user.js | 6 +++--- 14 files changed, 45 insertions(+), 47 deletions(-) create mode 100644 .eslintrc.js diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..edd341d --- /dev/null +++ b/.eslintrc.js @@ -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" + } +}; \ No newline at end of file diff --git a/helper/LoginedContainer.js b/helper/LoginedContainer.js index 27a367b..36786fa 100644 --- a/helper/LoginedContainer.js +++ b/helper/LoginedContainer.js @@ -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; } diff --git a/route/api.js b/route/api.js index 4432080..606b139 100644 --- a/route/api.js +++ b/route/api.js @@ -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; diff --git a/route/fileupload.js b/route/fileupload.js index 9942435..9794f15 100644 --- a/route/fileupload.js +++ b/route/fileupload.js @@ -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"); diff --git a/route/user.js b/route/user.js index 5b0621e..6410acc 100644 --- a/route/user.js +++ b/route/user.js @@ -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; diff --git a/route/websocket.js b/route/websocket.js index 54ef7fd..13b964f 100644 --- a/route/websocket.js +++ b/route/websocket.js @@ -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(); //释放全局变量 diff --git a/route/websocket/apikey.js b/route/websocket/apikey.js index 0471fe5..69f35c5 100644 --- a/route/websocket/apikey.js +++ b/route/websocket/apikey.js @@ -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"); diff --git a/route/websocket/console.js b/route/websocket/console.js index 60aac5d..d1e8644 100644 --- a/route/websocket/console.js +++ b/route/websocket/console.js @@ -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"); diff --git a/route/websocket/docker.js b/route/websocket/docker.js index 9f5c873..2ba0e80 100644 --- a/route/websocket/docker.js +++ b/route/websocket/docker.js @@ -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(/\</gim, "<"); - dockerfileData = dockerfileData.replace(/\ /gim, " "); + dockerfileData = dockerfileData.replace(/>/gim, ">"); + dockerfileData = dockerfileData.replace(/</gim, "<"); + dockerfileData = dockerfileData.replace(/ /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'))); diff --git a/route/websocket/genuser.js b/route/websocket/genuser.js index 6caefd9..862ecf6 100644 --- a/route/websocket/genuser.js +++ b/route/websocket/genuser.js @@ -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, "很抱歉,原密码错误,无法修改"); diff --git a/route/websocket/meum.js b/route/websocket/meum.js index 93b25cb..51ec981 100644 --- a/route/websocket/meum.js +++ b/route/websocket/meum.js @@ -1,5 +1,4 @@ const { WebSocketObserver } = require("../../model/WebSocketModel"); -const permssion = require("../../helper/Permission"); const response = require("../../helper/Response"); WebSocketObserver().listener("menu", (data) => { diff --git a/route/websocket/schedule.js b/route/websocket/schedule.js index b6b3201..cfd5cae 100644 --- a/route/websocket/schedule.js +++ b/route/websocket/schedule.js @@ -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, diff --git a/route/websocket/server.js b/route/websocket/server.js index c1273eb..6535474 100644 --- a/route/websocket/server.js +++ b/route/websocket/server.js @@ -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; diff --git a/route/websocket/user.js b/route/websocket/user.js index 5973f0b..89b4fd7 100644 --- a/route/websocket/user.js +++ b/route/websocket/user.js @@ -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;