mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
fix: webui Confi&webui Login
This commit is contained in:
@@ -4,6 +4,7 @@ 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) => {
|
||||
let WebUiConfigData = await WebUiConfig.GetWebUIConfig();
|
||||
const { token } = req.body;
|
||||
if (isEmpty(token)) {
|
||||
res.json({
|
||||
@@ -12,7 +13,7 @@ export const LoginHandler: RequestHandler = async (req, res) => {
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (!await DataRuntime.checkLoginRate(WebUiConfig.loginRate)) {
|
||||
if (!await DataRuntime.checkLoginRate(WebUiConfigData.loginRate)) {
|
||||
res.json({
|
||||
code: -1,
|
||||
message: 'login rate limit'
|
||||
@@ -20,14 +21,14 @@ export const LoginHandler: RequestHandler = async (req, res) => {
|
||||
return;
|
||||
}
|
||||
//验证config.token是否等于token
|
||||
if (WebUiConfig.token !== token) {
|
||||
if (WebUiConfigData.token !== token) {
|
||||
res.json({
|
||||
code: -1,
|
||||
message: 'token is invalid'
|
||||
});
|
||||
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({
|
||||
code: 0,
|
||||
message: 'success',
|
||||
@@ -46,11 +47,12 @@ export const LogoutHandler: RequestHandler = (req, res) => {
|
||||
return;
|
||||
};
|
||||
export const checkHandler: RequestHandler = async (req, res) => {
|
||||
let WebUiConfigData = await WebUiConfig.GetWebUIConfig();
|
||||
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)
|
||||
await AuthHelper.validateCredentialWithinOneHour(WebUiConfigData.token,Credential)
|
||||
res.json({
|
||||
code: 0,
|
||||
message: 'success'
|
||||
|
@@ -41,34 +41,40 @@ export interface WebUiConfigType {
|
||||
token: string;
|
||||
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 的配置文件,如果不存在则创建初始化配置文件
|
||||
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();
|
@@ -1,7 +1,7 @@
|
||||
import { Router } from 'express';
|
||||
import { QQCheckLoginStatusHandler } from '../api/QQLogin';
|
||||
import { QQCheckLoginStatusHandler, QQGetQuickLoginListHandler } from '../api/QQLogin';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.all('/GetQuickLoginList', QQGetQuickLoginListHandler)
|
||||
router.post('/CheckLoginStatus', QQCheckLoginStatusHandler);
|
||||
export { router as QQLoginRouter };
|
@@ -150,9 +150,6 @@
|
||||
</div>
|
||||
<div id="quick-login-dropdown" class="login-form">
|
||||
<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>
|
||||
</div>
|
||||
<div id="qrcode" class="qrcode" style="display: none;">
|
||||
@@ -161,6 +158,7 @@
|
||||
<p id="message"></p>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
async function CheckQQLoginStatus(retCredential) {
|
||||
let QQLoginResponse = await fetch('/api/QQLogin/CheckLoginStatus', {
|
||||
method: 'POST',
|
||||
@@ -182,7 +180,7 @@
|
||||
return false;
|
||||
}
|
||||
async function GetQQQucickLoginList(retCredential) {
|
||||
let QQLoginResponse = await fetch('/api/QQLogin/GetQQQucickLoginList', {
|
||||
let QQLoginResponse = await fetch('/api/QQLogin/GetQuickLoginList', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': "Bearer " + retCredential,
|
||||
@@ -197,23 +195,15 @@
|
||||
}
|
||||
return [];
|
||||
}
|
||||
async function GetQQQucickLoginList(retCredential) {
|
||||
let QQLoginResponse = await fetch('/api/QQLogin/GetQQLoginQrcode', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': "Bearer " + retCredential,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
async function InitPages() {
|
||||
let QuickLists = GetQQQucickLoginList(localStorage.getItem('auth'));
|
||||
let QuickListSelect = document.querySelector("#quick-login-select");
|
||||
QuickLists.forEach(QuickUin => {
|
||||
let optionUinEle = document.createElement('option');
|
||||
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 () {
|
||||
let quickLoginOptions = document.querySelector('#quick-login-dropdown');
|
||||
let qrcode = document.querySelector('#qrcode');
|
||||
|
Reference in New Issue
Block a user