fix:webui auth ratelimit

This commit is contained in:
手瓜一十雪 2024-05-07 22:04:21 +08:00
parent 09eaa3116a
commit 24658edc45
2 changed files with 4 additions and 2 deletions

View File

@ -13,7 +13,7 @@ export const LoginHandler: RequestHandler = async (req, res) => {
return; return;
} }
let config = await WebUIConfig(); let config = await WebUIConfig();
if (!DataRuntime.checkLoginRate(config.loginRate)) { if (!await DataRuntime.checkLoginRate(config.loginRate)) {
res.json({ res.json({
code: -1, code: -1,
message: 'login rate limit' message: 'login rate limit'

View File

@ -22,12 +22,14 @@ let LoginRuntime: LoginRuntimeType = {
} }
export const DataRuntime = { export const DataRuntime = {
checkLoginRate: async function (RateLimit: number): Promise<boolean> { checkLoginRate: async function (RateLimit: number): Promise<boolean> {
LoginRuntime.LoginCurrentRate++;
//console.log(RateLimit, LoginRuntime.LoginCurrentRate, Date.now() - LoginRuntime.LoginCurrentTime);
if (Date.now() - LoginRuntime.LoginCurrentTime > 1000 * 60) { if (Date.now() - LoginRuntime.LoginCurrentTime > 1000 * 60) {
LoginRuntime.LoginCurrentRate = 0;//超出时间重置限速 LoginRuntime.LoginCurrentRate = 0;//超出时间重置限速
LoginRuntime.LoginCurrentTime = Date.now();
return true; return true;
} }
if (LoginRuntime.LoginCurrentRate <= RateLimit) { if (LoginRuntime.LoginCurrentRate <= RateLimit) {
LoginRuntime.LoginCurrentRate++;
return true; return true;
} }
return false; return false;