fix: webui Confi&webui Login

This commit is contained in:
手瓜一十雪
2024-05-08 14:29:18 +08:00
parent fc337292bc
commit 0280dcd6a8
4 changed files with 53 additions and 55 deletions

View File

@@ -4,6 +4,7 @@ import { WebUiConfig } from "../helper/config";
import { DataRuntime } from "../helper/Data"; import { DataRuntime } from "../helper/Data";
const isEmpty = (data: any) => data === undefined || data === null || data === ''; const isEmpty = (data: any) => data === undefined || data === null || data === '';
export const LoginHandler: RequestHandler = async (req, res) => { export const LoginHandler: RequestHandler = async (req, res) => {
let WebUiConfigData = await WebUiConfig.GetWebUIConfig();
const { token } = req.body; const { token } = req.body;
if (isEmpty(token)) { if (isEmpty(token)) {
res.json({ res.json({
@@ -12,7 +13,7 @@ export const LoginHandler: RequestHandler = async (req, res) => {
}); });
return; return;
} }
if (!await DataRuntime.checkLoginRate(WebUiConfig.loginRate)) { if (!await DataRuntime.checkLoginRate(WebUiConfigData.loginRate)) {
res.json({ res.json({
code: -1, code: -1,
message: 'login rate limit' message: 'login rate limit'
@@ -20,14 +21,14 @@ export const LoginHandler: RequestHandler = async (req, res) => {
return; return;
} }
//验证config.token是否等于token //验证config.token是否等于token
if (WebUiConfig.token !== token) { if (WebUiConfigData.token !== token) {
res.json({ res.json({
code: -1, code: -1,
message: 'token is invalid' message: 'token is invalid'
}); });
return; return;
} }
let signCredential = Buffer.from(JSON.stringify(await AuthHelper.signCredential(WebUiConfig.token))).toString('base64'); let signCredential = Buffer.from(JSON.stringify(await AuthHelper.signCredential(WebUiConfigData.token))).toString('base64');
res.json({ res.json({
code: 0, code: 0,
message: 'success', message: 'success',
@@ -46,11 +47,12 @@ export const LogoutHandler: RequestHandler = (req, res) => {
return; return;
}; };
export const checkHandler: RequestHandler = async (req, res) => { export const checkHandler: RequestHandler = async (req, res) => {
let WebUiConfigData = await WebUiConfig.GetWebUIConfig();
const authorization = req.headers.authorization; const authorization = req.headers.authorization;
try { try {
let CredentialBase64:string = authorization?.split(' ')[1] as string; let CredentialBase64:string = authorization?.split(' ')[1] as string;
let Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString()); let Credential = JSON.parse(Buffer.from(CredentialBase64, 'base64').toString());
await AuthHelper.validateCredentialWithinOneHour(WebUiConfig.token,Credential) await AuthHelper.validateCredentialWithinOneHour(WebUiConfigData.token,Credential)
res.json({ res.json({
code: 0, code: 0,
message: 'success' message: 'success'

View File

@@ -41,34 +41,40 @@ export interface WebUiConfigType {
token: string; token: string;
loginRate: number loginRate: number
} }
async function GetWebUIConfig(): Promise<WebUiConfigType> {
try {
let configPath = resolve(__dirname, "./config/webui.json");
let config: WebUiConfigType = {
port: 6099,
token: Math.random().toString(36).slice(2),//生成随机密码
loginRate: 3
};
if (!existsSync(configPath)) {
writeFileSync(configPath, JSON.stringify(config, null, 4));
}
let fileContent = readFileSync(configPath, "utf-8");
let parsedConfig = JSON.parse(fileContent) as WebUiConfigType;
// 修正端口占用情况
const [err, data] = await tryUsePort(parsedConfig.port).then(data => [null, data as number]).catch(err => [err, null]);
parsedConfig.port = data;
if (err) {
//一般没那么离谱 如果真有这么离谱 考虑下 向外抛出异常
}
return parsedConfig;
} catch (e) {
console.error("读取配置文件失败", e);
}
return {} as WebUiConfigType; // 理论上这行代码到不了,为了保持函数完整性而保留
}
// 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件 // 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件
export const WebUiConfig = await GetWebUIConfig(); class WebUiConfigWrapper {
WebUiConfigData: WebUiConfigType | undefined = undefined;
async GetWebUIConfig(): Promise<WebUiConfigType> {
if (this.WebUiConfigData) {
return this.WebUiConfigData;
}
try {
let configPath = resolve(__dirname, "./config/webui.json");
let config: WebUiConfigType = {
port: 6099,
token: Math.random().toString(36).slice(2),//生成随机密码
loginRate: 3
};
if (!existsSync(configPath)) {
writeFileSync(configPath, JSON.stringify(config, null, 4));
}
let fileContent = readFileSync(configPath, "utf-8");
let parsedConfig = JSON.parse(fileContent) as WebUiConfigType;
// 修正端口占用情况
const [err, data] = await tryUsePort(parsedConfig.port).then(data => [null, data as number]).catch(err => [err, null]);
parsedConfig.port = data;
if (err) {
//一般没那么离谱 如果真有这么离谱 考虑下 向外抛出异常
}
this.WebUiConfigData = parsedConfig;
return this.WebUiConfigData;
} catch (e) {
console.error("读取配置文件失败", e);
}
return {} as WebUiConfigType; // 理论上这行代码到不了,为了保持函数完整性而保留
}
}
export const WebUiConfig = new WebUiConfigWrapper();

View File

@@ -1,7 +1,7 @@
import { Router } from 'express'; import { Router } from 'express';
import { QQCheckLoginStatusHandler } from '../api/QQLogin'; import { QQCheckLoginStatusHandler, QQGetQuickLoginListHandler } from '../api/QQLogin';
const router = Router(); const router = Router();
router.all('/GetQuickLoginList', QQGetQuickLoginListHandler)
router.post('/CheckLoginStatus', QQCheckLoginStatusHandler); router.post('/CheckLoginStatus', QQCheckLoginStatusHandler);
export { router as QQLoginRouter }; export { router as QQLoginRouter };

View File

@@ -150,9 +150,6 @@
</div> </div>
<div id="quick-login-dropdown" class="login-form"> <div id="quick-login-dropdown" class="login-form">
<select id="quick-login-select" onchange="selectAccount(this.value)"> <select id="quick-login-select" onchange="selectAccount(this.value)">
<option value="Account 1">Account 1</option>
<option value="Account 2">Account 2</option>
<option value="Account 3">Account 3</option>
</select> </select>
</div> </div>
<div id="qrcode" class="qrcode" style="display: none;"> <div id="qrcode" class="qrcode" style="display: none;">
@@ -161,6 +158,7 @@
<p id="message"></p> <p id="message"></p>
</div> </div>
<script> <script>
async function CheckQQLoginStatus(retCredential) { async function CheckQQLoginStatus(retCredential) {
let QQLoginResponse = await fetch('/api/QQLogin/CheckLoginStatus', { let QQLoginResponse = await fetch('/api/QQLogin/CheckLoginStatus', {
method: 'POST', method: 'POST',
@@ -182,7 +180,7 @@
return false; return false;
} }
async function GetQQQucickLoginList(retCredential) { async function GetQQQucickLoginList(retCredential) {
let QQLoginResponse = await fetch('/api/QQLogin/GetQQQucickLoginList', { let QQLoginResponse = await fetch('/api/QQLogin/GetQuickLoginList', {
method: 'POST', method: 'POST',
headers: { headers: {
'Authorization': "Bearer " + retCredential, 'Authorization': "Bearer " + retCredential,
@@ -197,23 +195,15 @@
} }
return []; return [];
} }
async function GetQQQucickLoginList(retCredential) { async function InitPages() {
let QQLoginResponse = await fetch('/api/QQLogin/GetQQLoginQrcode', { let QuickLists = GetQQQucickLoginList(localStorage.getItem('auth'));
method: 'POST', let QuickListSelect = document.querySelector("#quick-login-select");
headers: { QuickLists.forEach(QuickUin => {
'Authorization': "Bearer " + retCredential, let optionUinEle = document.createElement('option');
'Content-Type': 'application/json' optionUinEle.innerHTML = QuickUin;
} optionUinEle.value = QuickUin;
}); });
if (QQLoginResponse.status == 200) {
let QQLoginResponseJson = await QQLoginResponse.json();
if (QQLoginResponseJson.code == 0) {
return QQLoginResponseJson?.data;
}
}
return "";
} }
document.getElementById('quick-login').addEventListener('click', function () { document.getElementById('quick-login').addEventListener('click', function () {
let quickLoginOptions = document.querySelector('#quick-login-dropdown'); let quickLoginOptions = document.querySelector('#quick-login-dropdown');
let qrcode = document.querySelector('#qrcode'); let qrcode = document.querySelector('#qrcode');