From 579b9dc0c29658e9e29a54256432b15aa273d2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Tue, 7 May 2024 22:44:55 +0800 Subject: [PATCH] fix: WebUiConfig --- src/webui/index.ts | 4 ++-- src/webui/src/api/Auth.ts | 30 ++++++++++++++++++++++++------ src/webui/src/helper/config.ts | 17 +++++++++-------- static/login.html | 3 +-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/webui/index.ts b/src/webui/index.ts index 61e8d9ad..4ea2bad9 100644 --- a/src/webui/index.ts +++ b/src/webui/index.ts @@ -3,7 +3,7 @@ import { NextFunction, Request, Response } from 'express'; import { AuthHelper } from './src/helper/SignToken'; import { resolve } from 'node:path'; import { ALLRouter } from './src/router'; -import { WebUIConfig } from './src/helper/config'; +import { WebUiConfig } from './src/helper/config'; const app = express(); /** * 初始化并启动WebUI服务。 @@ -12,7 +12,7 @@ const app = express(); * @returns {Promise} 无返回值。 */ export async function InitWebUi() { - let config = await WebUIConfig(); + let config = WebUiConfig; app.use(express.json()); // 初始服务 app.all('/', (_req, res) => { diff --git a/src/webui/src/api/Auth.ts b/src/webui/src/api/Auth.ts index 8552a8d7..709d9d4e 100644 --- a/src/webui/src/api/Auth.ts +++ b/src/webui/src/api/Auth.ts @@ -1,6 +1,6 @@ import { RequestHandler } from "express"; import { AuthHelper } from "../helper/SignToken"; -import { WebUIConfig } from "../helper/config"; +import { WebUiConfig } from "../helper/config"; import { DataRuntime } from "../helper/Data"; const isEmpty = (data: any) => data === undefined || data === null || data === ''; export const LoginHandler: RequestHandler = async (req, res) => { @@ -11,9 +11,8 @@ export const LoginHandler: RequestHandler = async (req, res) => { message: 'token is empty' }); return; - } - let config = await WebUIConfig(); - if (!await DataRuntime.checkLoginRate(config.loginRate)) { + } + if (!await DataRuntime.checkLoginRate(WebUiConfig.loginRate)) { res.json({ code: -1, message: 'login rate limit' @@ -21,14 +20,14 @@ export const LoginHandler: RequestHandler = async (req, res) => { return; } //验证config.token是否等于token - if (config.token !== token) { + if (WebUiConfig.token !== token) { res.json({ code: -1, message: 'token is invalid' }); return; } - let signCredential = Buffer.from(JSON.stringify(await AuthHelper.signCredential(config.token))).toString('base64'); + let signCredential = Buffer.from(JSON.stringify(await AuthHelper.signCredential(WebUiConfig.token))).toString('base64'); res.json({ code: 0, message: 'success', @@ -46,3 +45,22 @@ export const LogoutHandler: RequestHandler = (req, res) => { }); return; }; +export const checkHandler: RequestHandler = async (req, res) => { + const authorization = req.headers.authorization; + try { + let CredentialBase64:string = authorization?.split(' ')[1] as string; + let Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString()); + await AuthHelper.validateCredentialWithinOneHour(WebUiConfig.token,Credential) + res.json({ + code: 0, + message: 'success' + }); + } catch (e) { + res.json({ + code: -1, + message: 'failed' + }); + } + + return; +}; diff --git a/src/webui/src/helper/config.ts b/src/webui/src/helper/config.ts index bac01bd5..a9701f7b 100644 --- a/src/webui/src/helper/config.ts +++ b/src/webui/src/helper/config.ts @@ -36,17 +36,15 @@ async function tryUsePort(port: number, tryCount: number = 0): Promise { }); } -export interface WebUiConfig { +export interface WebUiConfigType { port: number; token: string; loginRate: number } - -// 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件 -export async function WebUIConfig(): Promise { +async function WebUIConfig(): Promise { try { let configPath = resolve(__dirname, "./config/webui.json"); - let config: WebUiConfig = { + let config: WebUiConfigType = { port: 6099, token: Math.random().toString(36).slice(2),//生成随机密码 loginRate: 3 @@ -57,7 +55,7 @@ export async function WebUIConfig(): Promise { } let fileContent = readFileSync(configPath, "utf-8"); - let parsedConfig = JSON.parse(fileContent) as WebUiConfig; + let parsedConfig = JSON.parse(fileContent) as WebUiConfigType; // 修正端口占用情况 const [err, data] = await tryUsePort(parsedConfig.port).then(data => [null, data as number]).catch(err => [err, null]); @@ -69,5 +67,8 @@ export async function WebUIConfig(): Promise { } catch (e) { console.error("读取配置文件失败", e); } - return {} as WebUiConfig; // 理论上这行代码到不了,为了保持函数完整性而保留 -} \ No newline at end of file + return {} as WebUiConfigType; // 理论上这行代码到不了,为了保持函数完整性而保留 +} + +// 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件 +export const WebUiConfig = await WebUIConfig(); \ No newline at end of file diff --git a/static/login.html b/static/login.html index 98214c22..4da656b5 100644 --- a/static/login.html +++ b/static/login.html @@ -71,6 +71,7 @@