From b910c954625673c7fd6600a2eaf96c164cc758c3 Mon Sep 17 00:00:00 2001 From: gsd Date: Sun, 12 Mar 2023 18:43:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=8F=20=D0=B7=D0=B0=D0=B5?= =?UTF-8?q?=D0=B1=D0=B0=D0=BB=D0=B8=20=D1=87=D1=82=D0=BE=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=81=D0=B0=D0=B9=D1=82=D0=B5=20=D1=82=D1=8B=D1=87=D1=83=D1=82?= =?UTF-8?q?=20=D0=B2=D0=B8=D0=BF=D0=BA=D1=83=20=D0=B4=D0=B2=D0=B0=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotations/exceptions/WaitRateLimit.java | 4 ++ .../annotations/impl/WaitAfterNextAspect.java | 50 +++++++++++++++++++ .../annotations/interfaces/WaitAfterNext.java | 11 ++++ .../controllers/user/ProfileController.java | 3 ++ .../handler/GlobalExceptionAdvice.java | 5 ++ 5 files changed, 73 insertions(+) create mode 100644 src/main/java/app/annotations/exceptions/WaitRateLimit.java create mode 100644 src/main/java/app/annotations/impl/WaitAfterNextAspect.java create mode 100644 src/main/java/app/annotations/interfaces/WaitAfterNext.java diff --git a/src/main/java/app/annotations/exceptions/WaitRateLimit.java b/src/main/java/app/annotations/exceptions/WaitRateLimit.java new file mode 100644 index 0000000..d80207d --- /dev/null +++ b/src/main/java/app/annotations/exceptions/WaitRateLimit.java @@ -0,0 +1,4 @@ +package app.annotations.exceptions; + +public class WaitRateLimit extends RuntimeException { +} diff --git a/src/main/java/app/annotations/impl/WaitAfterNextAspect.java b/src/main/java/app/annotations/impl/WaitAfterNextAspect.java new file mode 100644 index 0000000..db75081 --- /dev/null +++ b/src/main/java/app/annotations/impl/WaitAfterNextAspect.java @@ -0,0 +1,50 @@ +package app.annotations.impl; + +import app.annotations.exceptions.NeedCookie; +import app.annotations.exceptions.WaitRateLimit; +import jakarta.servlet.http.HttpServletRequest; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.springframework.context.annotation.Configuration; + +import java.util.HashSet; + +@Aspect +@Configuration +public class WaitAfterNextAspect { + HashSet wait_steam64 = new HashSet<>(); + + @Before("@annotation(app.annotations.interfaces.WaitAfterNext) && args(request,..)") + public void before(HttpServletRequest request) { + String steam64 = getSteam64fromCookie(request); + + if (steam64.isEmpty()) return; + if (wait_steam64.contains(steam64)) throw new WaitRateLimit(); + wait_steam64.add(steam64); + } + + @After("@annotation(app.annotations.interfaces.WaitAfterNext) && args(request,..)") + public void after(HttpServletRequest request) { + String steam64 = getSteam64fromCookie(request); + if (steam64.isEmpty()) return; + if (wait_steam64.contains(steam64)) wait_steam64.remove(steam64); + } + + public String getSteam64fromCookie(HttpServletRequest request) { + if(request.getHeader("Cookie") == null) { + return ""; + } + + String[] rawCookieParams = request.getHeader("Cookie").split(";"); + String steam64 = ""; + for(String rawCookie: rawCookieParams) { + if(rawCookie.contains("steam64=")) { + steam64 = rawCookie.split("=")[1]; + continue; + } + } + return steam64; + } +} diff --git a/src/main/java/app/annotations/interfaces/WaitAfterNext.java b/src/main/java/app/annotations/interfaces/WaitAfterNext.java new file mode 100644 index 0000000..e57b771 --- /dev/null +++ b/src/main/java/app/annotations/interfaces/WaitAfterNext.java @@ -0,0 +1,11 @@ +package app.annotations.interfaces; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface WaitAfterNext { +} diff --git a/src/main/java/app/controllers/user/ProfileController.java b/src/main/java/app/controllers/user/ProfileController.java index 4c394ba..000515f 100644 --- a/src/main/java/app/controllers/user/ProfileController.java +++ b/src/main/java/app/controllers/user/ProfileController.java @@ -3,6 +3,7 @@ package app.controllers.user; import app.annotations.enums.AuthMethod; import app.annotations.interfaces.BurstUpdatePlayers; import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.WaitAfterNext; import app.entities.SocialAuth; import app.services.ProfileService; import app.services.ReportService; @@ -52,6 +53,7 @@ public class ProfileController { @PostMapping("/freevip") @CheckWebAccess + @WaitAfterNext public ResponseEntity GetFreeVIP(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String steam64, @RequestBody(required = false) SocialAuth socialAuth, @@ -66,6 +68,7 @@ public class ProfileController { @PostMapping("/report") @CheckWebAccess @BurstUpdatePlayers + @WaitAfterNext public ResponseEntity ReportUser(HttpServletRequest request, @CookieValue(value = "steam64", defaultValue = "") String steam64, @RequestParam(value = "steam64", defaultValue = "") String reported_steam64, diff --git a/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java b/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java index 5b7c7e8..90b29ed 100644 --- a/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java +++ b/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java @@ -39,4 +39,9 @@ public class GlobalExceptionAdvice { public ResponseEntity handFailedAuthCheck() { return new ResponseEntity(HttpStatus.UNAUTHORIZED); } + + @ExceptionHandler(WaitRateLimit.class) + public ResponseEntity handWaitRateLimit(){ + return new ResponseEntity<>("wait current before call this", HttpStatus.FORBIDDEN); + } }