Browse Source

logging

master
gsd 1 year ago
parent
commit
2af59c621e
  1. 6
      src/main/java/app/annotations/enums/CollectStages.java
  2. 35
      src/main/java/app/annotations/impl/CollectStatisticAspect.java
  3. 41
      src/main/java/app/annotations/impl/WebAccessAspect.java
  4. 3
      src/main/java/app/annotations/interfaces/CollectStatistic.java
  5. 1
      src/main/java/app/controllers/StatsController.java
  6. 5
      src/main/java/app/controllers/admin/BanController.java
  7. 5
      src/main/java/app/controllers/admin/DBController.java
  8. 3
      src/main/java/app/controllers/admin/KickController.java
  9. 5
      src/main/java/app/controllers/admin/MuteContoller.java
  10. 3
      src/main/java/app/controllers/admin/RconController.java
  11. 5
      src/main/java/app/controllers/admin/VIPController.java
  12. 28
      src/main/java/app/entities/db/CollectableStatistic.java
  13. 3
      src/main/java/app/services/db/CollectStatisticService.java

6
src/main/java/app/annotations/enums/CollectStages.java

@ -0,0 +1,6 @@
package app.annotations.enums;
public enum CollectStages {
COMBINED, BEFORE, AFTER;
}

35
src/main/java/app/annotations/impl/CollectStatisticAspect.java

@ -1,39 +1,60 @@
package app.annotations.impl; package app.annotations.impl;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.CollectStatistic;
import app.entities.db.CollectableStatistic; import app.entities.db.CollectableStatistic;
import app.services.db.CollectStatisticService; import app.services.db.CollectStatisticService;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.Arrays; import java.sql.Timestamp;
import java.time.Instant;
@Aspect @Aspect
@Configuration @Configuration
public class CollectStatisticAspect { public class CollectStatisticAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
private CollectStatisticService collectStatisticService; private CollectStatisticService collectStatisticService;
private HttpServletRequest request; private HttpServletRequest request;
private HttpServletResponse response;
@Autowired @Autowired
public CollectStatisticAspect(CollectStatisticService collectStatisticService, HttpServletRequest request) { public CollectStatisticAspect(CollectStatisticService collectStatisticService,
HttpServletRequest request,
HttpServletResponse response) {
this.collectStatisticService = collectStatisticService; this.collectStatisticService = collectStatisticService;
this.request = request; this.request = request;
this.response = response;
} }
@Before("@annotation(app.annotations.interfaces.CollectStatistic)") @Before("@annotation(app.annotations.interfaces.CollectStatistic)")
public void before() { public void before(JoinPoint joinPoint) {
collectStatisticService.add(new CollectableStatistic(request)); CollectStages collectStages = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(CollectStatistic.class).stage();
if (collectStages.equals(CollectStages.COMBINED) || collectStages.equals(CollectStages.BEFORE))
if (request != null)
collectStatisticService.add(new CollectableStatistic(request));
else return;
}
@After("@annotation(app.annotations.interfaces.CollectStatistic)")
public void after(JoinPoint joinPoint) {
CollectStages collectStages = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(CollectStatistic.class).stage();
if (collectStages.equals(CollectStages.COMBINED) || collectStages.equals(CollectStages.AFTER))
if (response != null)
logger.info("[RES] [{}] {}", Timestamp.from(Instant.now()).toString(), response.getStatus());
else
logger.warn("[RES] [{}] response in null", Timestamp.from(Instant.now()).toString());
else return;
} }
} }

41
src/main/java/app/annotations/impl/WebAccessAspect.java

@ -60,7 +60,12 @@ public class WebAccessAspect {
List<String> rawCookie = session.getHandshakeHeaders().get("cookie"); List<String> rawCookie = session.getHandshakeHeaders().get("cookie");
switch (auth_method) { switch (auth_method) {
case SECRET_KEY -> { case SECRET_KEY -> {
String secret_key = rawCookie.stream().filter(s -> s.contains("secretkey=")).map(s -> s.split("=")[1]).findFirst().orElse(null); String secret_key = "";
try {
secret_key = rawCookie.stream().filter(s -> s.contains("secretkey=")).map(s -> s.split("=")[1]).findFirst().orElse(null);
} catch (Exception e) {
session.close(CloseStatus.NOT_ACCEPTABLE);
}
if(!saltedCookie.ValidateSecretKey(secret_key)){ if(!saltedCookie.ValidateSecretKey(secret_key)){
logger.error("Invalid secret key on session {}", session.getId()); logger.error("Invalid secret key on session {}", session.getId());
session.close(CloseStatus.NOT_ACCEPTABLE); session.close(CloseStatus.NOT_ACCEPTABLE);
@ -91,22 +96,26 @@ public class WebAccessAspect {
String steam64_secured = ""; String steam64_secured = "";
String secret_key = ""; String secret_key = "";
for(String rawCookie: rawCookieParams) { try {
if((!steam64.isEmpty() && !steam64_secured.isEmpty() || (!steam64.isEmpty() && !secret_key.isEmpty()))) { for (String rawCookie : rawCookieParams) {
break; if ((!steam64.isEmpty() && !steam64_secured.isEmpty() || (!steam64.isEmpty() && !secret_key.isEmpty()))) {
} break;
if(rawCookie.contains("steam64=")) { }
steam64 = rawCookie.split("=")[1]; if (rawCookie.contains("steam64=")) {
continue; steam64 = rawCookie.split("=")[1];
} continue;
if(rawCookie.contains("steam64_secured=")) { }
steam64_secured = rawCookie.split("=")[1]; if (rawCookie.contains("steam64_secured=")) {
continue; steam64_secured = rawCookie.split("=")[1];
} continue;
if(rawCookie.contains("secretkey=")) { }
secret_key = rawCookie.split("=")[1]; if (rawCookie.contains("secretkey=")) {
continue; secret_key = rawCookie.split("=")[1];
continue;
}
} }
} catch (Exception e) {
throw new InvalidSecretKey();
} }
switch (auth_method){ switch (auth_method){

3
src/main/java/app/annotations/interfaces/CollectStatistic.java

@ -1,5 +1,7 @@
package app.annotations.interfaces; package app.annotations.interfaces;
import app.annotations.enums.CollectStages;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -8,4 +10,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface CollectStatistic { public @interface CollectStatistic {
public CollectStages stage() default CollectStages.BEFORE;
} }

1
src/main/java/app/controllers/StatsController.java

@ -1,5 +1,6 @@
package app.controllers; package app.controllers;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.BurstUpdatePlayers; import app.annotations.interfaces.BurstUpdatePlayers;
import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.CollectStatistic;
import app.entities.Stats; import app.entities.Stats;

5
src/main/java/app/controllers/admin/BanController.java

@ -1,6 +1,7 @@
package app.controllers.admin; package app.controllers.admin;
import app.annotations.enums.AuthMethod; import app.annotations.enums.AuthMethod;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.*; import app.annotations.interfaces.*;
import app.entities.db.Ban; import app.entities.db.Ban;
import app.services.ProfileService; import app.services.ProfileService;
@ -36,7 +37,7 @@ public class BanController {
@CheckPermitionFlag(flag = "d") @CheckPermitionFlag(flag = "d")
@BurstUpdatePlayers @BurstUpdatePlayers
@WaitAfterNext(order = "ban") @WaitAfterNext(order = "ban")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity banPlayer( public ResponseEntity banPlayer(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String admin_steam64, @CookieValue(value = "steam64") String admin_steam64,
@ -62,7 +63,7 @@ public class BanController {
@CheckWebAccess @CheckWebAccess
@CheckPermitionFlag(flag = "e") @CheckPermitionFlag(flag = "e")
@WaitAfterNext(order = "unban") @WaitAfterNext(order = "unban")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity unbanPlayer( public ResponseEntity unbanPlayer(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String admin_steam64, @CookieValue(value = "steam64") String admin_steam64,

5
src/main/java/app/controllers/admin/DBController.java

@ -1,6 +1,7 @@
package app.controllers.admin; package app.controllers.admin;
import app.annotations.enums.AuthMethod; import app.annotations.enums.AuthMethod;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckPermitionFlag;
import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.CheckWebAccess;
import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.CollectStatistic;
@ -27,7 +28,7 @@ public class DBController {
@GetMapping(value = "/alt") @GetMapping(value = "/alt")
@CheckWebAccess @CheckWebAccess
@CheckPermitionFlag(flag = "d") @CheckPermitionFlag(flag = "d")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public List<String> getAltAccountPerAccount(HttpServletRequest request, public List<String> getAltAccountPerAccount(HttpServletRequest request,
@RequestParam(value = "steam64", required = false, defaultValue = "") String steam64) { @RequestParam(value = "steam64", required = false, defaultValue = "") String steam64) {
return detectService.getAccountsPerSteamID(SteamIDConverter.getSteamID(steam64)).stream().map(s -> s.community_url).toList(); return detectService.getAccountsPerSteamID(SteamIDConverter.getSteamID(steam64)).stream().map(s -> s.community_url).toList();
@ -35,7 +36,7 @@ public class DBController {
@RequestMapping(value = "/alt", method = RequestMethod.OPTIONS) @RequestMapping(value = "/alt", method = RequestMethod.OPTIONS)
@CheckWebAccess @CheckWebAccess
@CheckPermitionFlag(flag = "d") @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity check4getAltAccountPerAccount(HttpServletRequest request) { public ResponseEntity check4getAltAccountPerAccount(HttpServletRequest request) {
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }

3
src/main/java/app/controllers/admin/KickController.java

@ -1,5 +1,6 @@
package app.controllers.admin; package app.controllers.admin;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.*; import app.annotations.interfaces.*;
import app.entities.PlayerProfile; import app.entities.PlayerProfile;
import app.services.ProfileService; import app.services.ProfileService;
@ -35,7 +36,7 @@ public class KickController {
@CheckPermitionFlag(flag = "c") @CheckPermitionFlag(flag = "c")
@BurstUpdatePlayers @BurstUpdatePlayers
@WaitAfterNext(order = "kick") @WaitAfterNext(order = "kick")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity kickPlayer( public ResponseEntity kickPlayer(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String steam64, @CookieValue(value = "steam64") String steam64,

5
src/main/java/app/controllers/admin/MuteContoller.java

@ -1,5 +1,6 @@
package app.controllers.admin; package app.controllers.admin;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.*; import app.annotations.interfaces.*;
import app.entities.PlayerProfile; import app.entities.PlayerProfile;
import app.services.ProfileService; import app.services.ProfileService;
@ -35,7 +36,7 @@ public class MuteContoller {
@CheckPermitionFlag(flag = "c") @CheckPermitionFlag(flag = "c")
@BurstUpdatePlayers @BurstUpdatePlayers
@WaitAfterNext(order = "mute") @WaitAfterNext(order = "mute")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity mutePlayer( public ResponseEntity mutePlayer(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String steam64, @CookieValue(value = "steam64") String steam64,
@ -54,7 +55,7 @@ public class MuteContoller {
@CheckPermitionFlag(flag = "c") @CheckPermitionFlag(flag = "c")
@BurstUpdatePlayers @BurstUpdatePlayers
@WaitAfterNext(order = "unmute") @WaitAfterNext(order = "unmute")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity unmutePlayer( public ResponseEntity unmutePlayer(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String steam64, @CookieValue(value = "steam64") String steam64,

3
src/main/java/app/controllers/admin/RconController.java

@ -1,5 +1,6 @@
package app.controllers.admin; package app.controllers.admin;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckPermitionFlag;
import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.CheckWebAccess;
import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.CollectStatistic;
@ -26,7 +27,7 @@ public class RconController {
@CheckWebAccess @CheckWebAccess
@CheckPermitionFlag(flag = "m") @CheckPermitionFlag(flag = "m")
@WaitAfterNext(order = "rcon") @WaitAfterNext(order = "rcon")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity<String> rcon(HttpServletRequest request, public ResponseEntity<String> rcon(HttpServletRequest request,
@RequestParam String srv, @RequestParam String srv,
@RequestParam String command) { @RequestParam String command) {

5
src/main/java/app/controllers/admin/VIPController.java

@ -1,5 +1,6 @@
package app.controllers.admin; package app.controllers.admin;
import app.annotations.enums.CollectStages;
import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.CheckPermitionFlag;
import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.CheckWebAccess;
import app.annotations.interfaces.CollectStatistic; import app.annotations.interfaces.CollectStatistic;
@ -27,7 +28,7 @@ public class VIPController {
@CheckWebAccess @CheckWebAccess
@CheckPermitionFlag(flag = "z") @CheckPermitionFlag(flag = "z")
@WaitAfterNext(order = "givevip") @WaitAfterNext(order = "givevip")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity giveVIP( public ResponseEntity giveVIP(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String admin_steam64, @CookieValue(value = "steam64") String admin_steam64,
@ -49,7 +50,7 @@ public class VIPController {
@CheckWebAccess @CheckWebAccess
@CheckPermitionFlag(flag = "z") @CheckPermitionFlag(flag = "z")
@WaitAfterNext(order = "removevip") @WaitAfterNext(order = "removevip")
@CollectStatistic @CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity removeVIP( public ResponseEntity removeVIP(
HttpServletRequest request, HttpServletRequest request,
@CookieValue(value = "steam64") String admin_steam64, @CookieValue(value = "steam64") String admin_steam64,

28
src/main/java/app/entities/db/CollectableStatistic.java

@ -35,14 +35,22 @@ public class CollectableStatistic {
@Override @Override
public String toString() { public String toString() {
return StringBuilder sb = new StringBuilder();
"Time: " + timestamp.toString() + "\n" + sb.append("[REQ] [");
"IP: " + client_ip + "\n" + sb.append(timestamp.toString());
"Steam64: " + steam64 + "\n" + sb.append("] [");
"Method: " + method + "\n" + sb.append(client_ip);
"Path: " + path + "\n" + sb.append("|");
"Query: " + query + "\n" + sb.append(steam64);
"UA: " + useragent + "\n"; sb.append("] ");
sb.append(method);
sb.append(" ");
sb.append(path);
if (query != null) {
sb.append("?");
sb.append(query);
}
return sb.toString();
} }
private String getCookie(HttpServletRequest request, String cookie_name) { private String getCookie(HttpServletRequest request, String cookie_name) {
@ -58,4 +66,8 @@ public class CollectableStatistic {
public boolean isIgnoreRule() { public boolean isIgnoreRule() {
return getPath() != null && getPath().equals("/api/stats") && getQuery() != null && !getQuery().contains("filter=servers"); return getPath() != null && getPath().equals("/api/stats") && getQuery() != null && !getQuery().contains("filter=servers");
} }
public boolean isStatsRequests() {
return getPath().equals("/api/stats");
}
} }

3
src/main/java/app/services/db/CollectStatisticService.java

@ -33,6 +33,9 @@ public class CollectStatisticService extends BaseUpdater {
if (collectableStatistic.isServerRequest()) return; if (collectableStatistic.isServerRequest()) return;
if (collectableStatistic.isIgnoreRule()) return; if (collectableStatistic.isIgnoreRule()) return;
try { try {
if (!collectableStatistic.isStatsRequests())
logger.info(collectableStatistic.toString());
entityManager.createNativeQuery("INSERT INTO `web_statistic` (`steam64`, `client_ip`, `method`, `path`, `query`, `useragent`) VALUES (?1, ?2, ?3, ?4, ?5, ?6)") entityManager.createNativeQuery("INSERT INTO `web_statistic` (`steam64`, `client_ip`, `method`, `path`, `query`, `useragent`) VALUES (?1, ?2, ?3, ?4, ?5, ?6)")
.setParameter(1, collectableStatistic.getSteam64()) .setParameter(1, collectableStatistic.getSteam64())
.setParameter(2, collectableStatistic.getClient_ip()) .setParameter(2, collectableStatistic.getClient_ip())

Loading…
Cancel
Save