From d41eb582e467559fbb7a8adbdf92fc182214a4e2 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 22 Mar 2021 23:41:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E7=99=BB=E5=BD=95=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=94=9F=E6=88=90=E9=9A=8F=E6=9C=BA=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/auth.ts | 18 ++++++++ back/api/cookie.ts | 6 +-- back/config/util.ts | 89 ++++++++++++++++++++++++++++++++++++++- src/pages/login/index.tsx | 8 +++- 4 files changed, 114 insertions(+), 7 deletions(-) diff --git a/back/api/auth.ts b/back/api/auth.ts index 004f2721..d31f2679 100644 --- a/back/api/auth.ts +++ b/back/api/auth.ts @@ -4,6 +4,7 @@ import { Logger } from 'winston'; import * as fs from 'fs'; import config from '../config'; import jwt from 'jsonwebtoken'; +import { createPassword } from '../config/util'; const route = Router(); export default (app: Router) => { @@ -19,6 +20,23 @@ export default (app: Router) => { if (err) console.log(err); const authInfo = JSON.parse(data); if (username && password) { + if ( + authInfo.username === 'admin' && + authInfo.password === 'adminadmin' + ) { + const newPassword = createPassword(16, 22); + fs.writeFileSync( + config.authConfigFile, + JSON.stringify({ + username: authInfo.username, + password: newPassword, + }), + ); + return res.send({ + code: 100, + msg: '已初始化密码,请前往auth.json查看并重新登录', + }); + } if ( username == authInfo.username && password == authInfo.password diff --git a/back/api/cookie.ts b/back/api/cookie.ts index ef1bc2b3..cf502294 100644 --- a/back/api/cookie.ts +++ b/back/api/cookie.ts @@ -65,7 +65,7 @@ export default (app: Router) => { const cookieService = Container.get(CookieService); const data = await cookieService.addCookie(req.body.cookies); if (data) { - return res.send({ code: 500, data }); + return res.send({ code: 400, data }); } else { return res.send({ code: 200, data: '新建成功' }); } @@ -84,7 +84,7 @@ export default (app: Router) => { const cookieService = Container.get(CookieService); const data = await cookieService.updateCookie(req.body); if (data) { - return res.send({ code: 500, data }); + return res.send({ code: 400, data }); } else { return res.send({ code: 200, data: '新建成功' }); } @@ -105,7 +105,7 @@ export default (app: Router) => { req.body.cookie as string, ); if (data) { - return res.send({ code: 500, data }); + return res.send({ code: 400, data }); } else { return res.send({ code: 200, data: '新建成功' }); } diff --git a/back/config/util.ts b/back/config/util.ts index 5c7d2886..bc87ee91 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -15,8 +15,8 @@ export function getLastModifyFilePath(dir: string) { const arr = fs.readdirSync(dir); arr.forEach((item) => { - var fullpath = path.join(dir, item); - var stats = fs.statSync(fullpath); + const fullpath = path.join(dir, item); + const stats = fs.statSync(fullpath); if (stats.isFile()) { if (stats.mtimeMs >= 0) { filePath = fullpath; @@ -26,3 +26,88 @@ export function getLastModifyFilePath(dir: string) { } return filePath; } + +export function createPassword(min: number, max: number): string { + const num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + const english = [ + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + ]; + const ENGLISH = [ + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + ]; + const special = ['-', '_', '#']; + const config = num.concat(english).concat(ENGLISH).concat(special); + + const arr = []; + arr.push(getOne(num)); + arr.push(getOne(english)); + arr.push(getOne(ENGLISH)); + arr.push(getOne(special)); + + const len = min + Math.floor(Math.random() * (max - min + 1)); + + for (let i = 4; i < len; i++) { + arr.push(config[Math.floor(Math.random() * config.length)]); + } + + const newArr = []; + for (let j = 0; j < len; j++) { + newArr.push(arr.splice(Math.random() * arr.length, 1)[0]); + } + + function getOne(arr) { + return arr[Math.floor(Math.random() * arr.length)]; + } + + return newArr.join(''); +} diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 3b60ec78..442fb993 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -18,11 +18,15 @@ const Login = () => { }, }) .then((data) => { - if (data.code == 200) { + if (data.code === 200) { localStorage.setItem(config.authKey, data.token); history.push('/cookie'); + } else if (data.code === 100) { + notification.warn({ + message: data.msg, + }); } else { - notification.open({ + notification.error({ message: data.msg, }); }