From e1005876cbd2dab0fe922503216ff41c5399cedb Mon Sep 17 00:00:00 2001 From: gsd Date: Thu, 7 Nov 2024 18:21:57 +0300 Subject: [PATCH] ratelimits --- .../annotations/impl/WaitAfterNextAspect.java | 11 ++++++++-- .../controllers/other/PulseController.java | 21 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/app/annotations/impl/WaitAfterNextAspect.java b/src/main/java/app/annotations/impl/WaitAfterNextAspect.java index 4ba5644..0b4798a 100644 --- a/src/main/java/app/annotations/impl/WaitAfterNextAspect.java +++ b/src/main/java/app/annotations/impl/WaitAfterNextAspect.java @@ -1,6 +1,5 @@ package app.annotations.impl; -import app.annotations.exceptions.NeedCookie; import app.annotations.exceptions.WaitRateLimit; import app.annotations.interfaces.WaitAfterNext; import jakarta.servlet.http.HttpServletRequest; @@ -57,9 +56,17 @@ public class WaitAfterNextAspect { public String getIp(HttpServletRequest request) { if (request.getHeader("X-Forwarded-For") == null) { - return ""; + return request.getRemoteAddr(); } return request.getHeader("X-Forwarded-For").split(",")[0]; } + + public HashSet getRateLimits() { + return wait_order; + } + + public void clearRateLimits() { + wait_order.clear(); + } } diff --git a/src/main/java/app/controllers/other/PulseController.java b/src/main/java/app/controllers/other/PulseController.java index f641413..d25408d 100644 --- a/src/main/java/app/controllers/other/PulseController.java +++ b/src/main/java/app/controllers/other/PulseController.java @@ -1,16 +1,22 @@ package app.controllers.other; +import app.annotations.impl.WaitAfterNextAspect; +import app.annotations.interfaces.CheckPermitionFlag; +import app.annotations.interfaces.CheckWebAccess; import app.services.StatsService; import app.services.db.DBService; import app.updates.OnlineUpdater; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; +import java.util.HashSet; /** * контролллер для проверки жива ли эта хуйня и че там по бд @@ -22,14 +28,17 @@ public class PulseController { DBService dbService; OnlineUpdater onlineUpdater; StatsService statsService; + WaitAfterNextAspect waitAfterNextAspect; @Autowired public PulseController(DBService dbService, OnlineUpdater onlineUpdater, - StatsService statsService) { + StatsService statsService, + WaitAfterNextAspect waitAfterNextAspect) { this.dbService = dbService; this.onlineUpdater = onlineUpdater; this.statsService = statsService; + this.waitAfterNextAspect = waitAfterNextAspect; } @GetMapping("/db") @@ -50,4 +59,14 @@ public class PulseController { public ResponseEntity> getServices() { return new ResponseEntity<>(statsService.getServices(), HttpStatus.OK); } + + @GetMapping("/ratelimit") + @CheckWebAccess + @CheckPermitionFlag(flag = "z") + public HashSet getRateLimitOrder(HttpServletRequest request, @RequestParam(defaultValue = "false") boolean clear) { + if (clear) { + waitAfterNextAspect.clearRateLimits(); + } + return waitAfterNextAspect.getRateLimits(); + } }