From bdada6ed1691590b4d4754d1c037dfa7a34b5bcc Mon Sep 17 00:00:00 2001 From: gsd Date: Fri, 10 Feb 2023 23:07:26 +0300 Subject: [PATCH] Ban support and more rested --- .../app/controllers/admin/BanController.java | 59 +++++++++++++++++++ .../KickController.java} | 21 +++---- src/main/java/app/entities/PlayerProfile.java | 3 +- src/main/java/app/entities/Stats.java | 12 ++-- src/main/java/app/entities/db/Ban.java | 18 +++++- src/main/java/app/entities/server/PlayOn.java | 2 + src/main/java/app/entities/server/Server.java | 14 ++++- .../java/app/services/ProfileService.java | 19 ++++-- src/main/java/app/services/StatsService.java | 6 +- src/main/java/app/services/db/BanService.java | 57 ++++++++++++++++++ .../app/services/db/PermitionService.java | 5 +- 11 files changed, 185 insertions(+), 31 deletions(-) create mode 100644 src/main/java/app/controllers/admin/BanController.java rename src/main/java/app/controllers/{AdminController.java => admin/KickController.java} (77%) diff --git a/src/main/java/app/controllers/admin/BanController.java b/src/main/java/app/controllers/admin/BanController.java new file mode 100644 index 0000000..773f2aa --- /dev/null +++ b/src/main/java/app/controllers/admin/BanController.java @@ -0,0 +1,59 @@ +package app.controllers.admin; + +import app.annotations.interfaces.CheckPermitionFlag; +import app.annotations.interfaces.NeedValidCookie; +import app.entities.db.Ban; +import app.services.ProfileService; +import app.services.db.BanService; +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.*; + +import java.util.List; + +@RestController +@RequestMapping("api/admin/ban") +public class BanController { + ProfileService profileService; + BanService banService; + + @Autowired + public BanController(ProfileService profileService, + BanService banService) { + this.profileService = profileService; + this.banService = banService; + } + + @PostMapping + @NeedValidCookie + @CheckPermitionFlag(flag = "z") + public ResponseEntity banPlayer( + HttpServletRequest request, + @CookieValue(value = "steam64") String admin_steam64, + @RequestParam(value = "steam64") String user_steam64, + @RequestParam(value = "ban_length", required = false, defaultValue = "0") int ban_length, + @RequestParam(value = "ban_reason", required = false, defaultValue = "фурриёб") String ban_reason + ){ + boolean result = banService.addBan( + profileService.GetProfile(user_steam64, List.of("steam_data")), + profileService.GetProfile(admin_steam64, List.of("steam_data")), + ban_length, ban_reason); + return result ? new ResponseEntity(result, HttpStatus.CREATED) : new ResponseEntity(result, HttpStatus.NOT_FOUND); + } + + @DeleteMapping + @NeedValidCookie + @CheckPermitionFlag(flag = "z") + public ResponseEntity unbanPlayer( + HttpServletRequest request, + @CookieValue(value = "steam64") String admin_steam64, + @RequestParam(value = "steam64") String user_steam64 + ){ + boolean result = banService.removeBan( + profileService.GetProfile(user_steam64, List.of()), + profileService.GetProfile(admin_steam64, List.of())); + return result ? new ResponseEntity(result, HttpStatus.OK) : new ResponseEntity(result, HttpStatus.NOT_FOUND); + } +} diff --git a/src/main/java/app/controllers/AdminController.java b/src/main/java/app/controllers/admin/KickController.java similarity index 77% rename from src/main/java/app/controllers/AdminController.java rename to src/main/java/app/controllers/admin/KickController.java index 096da94..93909f5 100644 --- a/src/main/java/app/controllers/AdminController.java +++ b/src/main/java/app/controllers/admin/KickController.java @@ -1,11 +1,10 @@ -package app.controllers; +package app.controllers.admin; import app.annotations.interfaces.CheckPermitionFlag; import app.annotations.interfaces.NeedValidCookie; -import app.entities.db.Permition; import app.services.ProfileService; import app.services.ServerService; -import app.utils.SaltedCookie; +import app.services.db.BanService; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -15,27 +14,25 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("api/admin") -public class AdminController { +@RequestMapping("api/admin/kick") +public class KickController { ServerService serverService; - SaltedCookie saltedCookie; ProfileService profileService; + BanService banService; @Autowired - public AdminController(ServerService serverService, - SaltedCookie saltedCookie, - ProfileService profileService) { + public KickController(ServerService serverService, + ProfileService profileService) { this.serverService = serverService; - this.saltedCookie = saltedCookie; this.profileService = profileService; } - @GetMapping("kick") + @PostMapping @NeedValidCookie @CheckPermitionFlag(flag = "z") public ResponseEntity kickPlayer( HttpServletRequest request, - @CookieValue(value = "steam64", defaultValue = "") String steam64, + @CookieValue(value = "steam64") String steam64, @RequestParam(value = "steam64", required = false, defaultValue = "") String kicked_steam64, @RequestParam(value = "player_name", required = false, defaultValue = "") String player_name ) { diff --git a/src/main/java/app/entities/PlayerProfile.java b/src/main/java/app/entities/PlayerProfile.java index 3dec742..bc01599 100644 --- a/src/main/java/app/entities/PlayerProfile.java +++ b/src/main/java/app/entities/PlayerProfile.java @@ -1,5 +1,6 @@ package app.entities; +import app.entities.db.Ban; import app.entities.db.Permition; import app.entities.other.SteamID; import app.entities.server.PlayOn; @@ -11,7 +12,7 @@ import java.util.HashMap; @Data public class PlayerProfile { - Object ban; + Ban ban; HashMap> gametime; HashMap> lastplay; Permition permition; diff --git a/src/main/java/app/entities/Stats.java b/src/main/java/app/entities/Stats.java index 174d76a..6e92473 100644 --- a/src/main/java/app/entities/Stats.java +++ b/src/main/java/app/entities/Stats.java @@ -45,12 +45,10 @@ public class Stats { } public void RefreshServerA2SData(ApplicationContext context, String server_name){ - SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class); - try { + try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) { SourceQueryInfoResponse info = sourceQueryClient.getInfo(getServers().get(server_name).getInetAddress()).join(); getServers().get(server_name).UpdateStatusFromA2S(info); - sourceQueryClient.close(); - } catch (IOException err) { + } catch (IOException err_info) { getServers().get(server_name).SetDownStatus(); return; } @@ -60,12 +58,10 @@ public class Stats { //////////////////////////////////////////////////////////////////////// //If player count > 0 make base player request //////////////////////////////////////////////////////////////////////// - try { - sourceQueryClient = context.getBean(SourceQueryClient.class); + try (SourceQueryClient sourceQueryClient = context.getBean(SourceQueryClient.class)) { SourceQueryPlayerResponse players = sourceQueryClient.getPlayers(getServers().get(server_name).getInetAddress()).join(); getServers().get(server_name).UpdatePlayersFromA2S(players); - sourceQueryClient.close(); - } catch (IOException err) { + } catch (IOException err_players) { return; } /////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/app/entities/db/Ban.java b/src/main/java/app/entities/db/Ban.java index c00ae1a..a51d4cf 100644 --- a/src/main/java/app/entities/db/Ban.java +++ b/src/main/java/app/entities/db/Ban.java @@ -10,7 +10,7 @@ import java.sql.Timestamp; public class Ban { int id; String steam_id; - BigInteger account_id; + Long account_id; String player_name; int ban_length; String ban_reason; @@ -21,4 +21,20 @@ public class Ban { boolean active; String unbanned_by_id; Timestamp unbanned_timestamp; + + public Ban(Object[] obj) { + id = (int) obj[0]; + steam_id = (String) obj[1]; + account_id = (Long) obj[2]; + player_name = (String) obj[3]; + ban_length = (int) obj[4]; + ban_reason = (String) obj[5]; + banned_by = (String) obj[6]; + banned_by_id = (String) obj[7]; + ip = (String) obj[8]; + timestamp = (Timestamp) obj[9]; + active = (boolean) obj[10]; + unbanned_by_id = (String) obj[11]; + unbanned_timestamp = (Timestamp) obj[12]; + } } diff --git a/src/main/java/app/entities/server/PlayOn.java b/src/main/java/app/entities/server/PlayOn.java index 0cd411b..8900dd1 100644 --- a/src/main/java/app/entities/server/PlayOn.java +++ b/src/main/java/app/entities/server/PlayOn.java @@ -8,4 +8,6 @@ import lombok.Data; public class PlayOn { String server_id; int player_id; + String ip; + String name; } diff --git a/src/main/java/app/entities/server/Server.java b/src/main/java/app/entities/server/Server.java index ad0ef1b..67f6c44 100644 --- a/src/main/java/app/entities/server/Server.java +++ b/src/main/java/app/entities/server/Server.java @@ -95,14 +95,24 @@ public class Server { @JsonIgnore public String ExecuteRCON(ApplicationContext context, String command) { - SourceRconClient rconClient = context.getBean(SourceRconClient.class); + try(SourceRconClient rconClient = context.getBean(SourceRconClient.class)) { + SourceRconAuthResponse response = rconClient.authenticate(getInetAddress(), getRcon_password().getBytes()).join(); + if(!response.isAuthenticated()) { + return null; + } + SourceRconCmdResponse res = rconClient.execute(getInetAddress(), command).join(); + return res.getResult(); + } catch (Exception err) { + return ""; + } + /*SourceRconClient rconClient = context.getBean(SourceRconClient.class); SourceRconAuthResponse response = rconClient.authenticate(getInetAddress(), getRcon_password().getBytes()).join(); if(!response.isAuthenticated()) { return null; } SourceRconCmdResponse res = rconClient.execute(getInetAddress(), command).join(); rconClient.cleanup(); - return res.getResult(); + return res.getResult();*/ } public void SetDownStatus() { diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index c10879c..2d39eab 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -3,6 +3,7 @@ package app.services; import app.entities.PlayerProfile; import app.entities.Stats; import app.entities.other.SteamID; +import app.services.db.BanService; import app.services.db.PermitionService; import app.services.db.UsertimeService; import app.services.steam.SteamWebApi; @@ -20,15 +21,18 @@ public class ProfileService { UsertimeService usertimeService; PermitionService permitionService; StatsService statsService; + BanService banService; @Autowired public ProfileService(SteamWebApi steamWebApi, UsertimeService usertimeService, PermitionService permitionService, - StatsService statsService) { + StatsService statsService, + BanService banService) { this.steamWebApi = steamWebApi; this.usertimeService = usertimeService; this.permitionService = permitionService; this.statsService = statsService; + this.banService = banService; } public PlayerProfile GetProfile(SteamID steamID, List requests) { @@ -74,18 +78,25 @@ public class ProfileService { profile.getResponse_time().put("usertime", Double.valueOf(end_time) / 1000); } - if(requests.contains("permition")){ + //if(requests.contains("permition")){ start_time = Instant.now().toEpochMilli(); profile.setPermition(permitionService.getPermition(steamID)); end_time = Instant.now().toEpochMilli() - start_time; profile.getResponse_time().put("permition", Double.valueOf(end_time) / 1000); - } + //} + + //if(requests.contains("ban")){ + start_time = Instant.now().toEpochMilli(); + profile.setBan(banService.getBan(steamID)); + end_time = Instant.now().toEpochMilli() - start_time; + profile.getResponse_time().put("ban", Double.valueOf(end_time) / 1000); + //} return profile; } public PlayerProfile GetProfile(String steam64) { - return GetProfile(steam64, List.of("steam_data,lastplay,usertime,permition".split(","))); + return GetProfile(steam64, List.of("steam_data,lastplay,usertime,permition,ban".split(","))); } public PlayerProfile GetProfileOnPlayerOnServers(String name, List requests) { diff --git a/src/main/java/app/services/StatsService.java b/src/main/java/app/services/StatsService.java index 55563fa..2ae03f5 100644 --- a/src/main/java/app/services/StatsService.java +++ b/src/main/java/app/services/StatsService.java @@ -22,7 +22,11 @@ public class StatsService { public PlayOn searchPlayer(SteamID steamID){ for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { RCONPlayer player = stringServerEntry.getValue().searchPlayer(steamID); - if (player != null) return new PlayOn(stringServerEntry.getKey(), player.getId()); + if (player != null) return new PlayOn( + stringServerEntry.getKey(), + player.getId(), + player.getIp(), + player.getName()); } return null; } diff --git a/src/main/java/app/services/db/BanService.java b/src/main/java/app/services/db/BanService.java index b89ca36..48fb833 100644 --- a/src/main/java/app/services/db/BanService.java +++ b/src/main/java/app/services/db/BanService.java @@ -1,7 +1,64 @@ package app.services.db; +import app.entities.PlayerProfile; +import app.entities.db.Ban; +import app.entities.other.SteamID; +import app.services.ProfileService; +import app.services.ServerService; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.sql.Timestamp; +import java.util.List; @Service public class BanService { + @PersistenceContext + EntityManager entityManager; + ServerService serverService; + + @Autowired + public BanService(ServerService serverService) { + this.serverService = serverService; + } + + public Ban getBan(SteamID steamID) { + List result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE account_id = ?1 AND active = 1") + .setParameter(1, steamID.account_id) + .getResultList(); + return result.stream().map(Ban::new).findFirst().orElse(null); + } + + @Transactional + public boolean removeBan(PlayerProfile user, PlayerProfile admin) { + Ban ban = getBan(user.getSteamids()); + if (ban == null) return false; + return entityManager.createNativeQuery("UPDATE light_bans SET active=?1, unbanned_by_id = ?2, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?3") + .setParameter(1, 0) + .setParameter(2, admin.getSteamids().steam2) + .setParameter(3, ban.getId()) + .executeUpdate() > 0; + } + + @Transactional + public boolean addBan(PlayerProfile user, PlayerProfile admin, int ban_length, String ban_reason) { + Ban ban = getBan(user.getSteamids()); + if (ban != null) return true; + boolean result = entityManager.createNativeQuery("INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)") + .setParameter(1, user.getSteamids().steam2) + .setParameter(2, user.getSteamids().account_id) + .setParameter(3, ban_length) + .setParameter(4, ban_reason) + .setParameter(5, admin.getSteamids().steam2) + .setParameter(6, 1) + .setParameter(7, user.getPlay_on() != null ? user.getPlay_on().getIp() : "") + .setParameter(8, user.getPlay_on() != null ? user.getPlay_on().getName() : user.getSteam_data().getNickname()) + .setParameter(9, admin.getSteam_data().getNickname()) + .executeUpdate() > 0; + serverService.kickPlayer(user); + return result; + } } diff --git a/src/main/java/app/services/db/PermitionService.java b/src/main/java/app/services/db/PermitionService.java index f2b71bf..be2694d 100644 --- a/src/main/java/app/services/db/PermitionService.java +++ b/src/main/java/app/services/db/PermitionService.java @@ -1,5 +1,6 @@ package app.services.db; +import app.entities.db.Ban; import app.entities.db.Permition; import app.entities.other.SteamID; import jakarta.persistence.EntityManager; @@ -14,9 +15,9 @@ public class PermitionService { EntityManager entityManager; public Permition getPermition(SteamID steamID){ - List permitionList = entityManager.createNativeQuery("SELECT id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`) as u_timestamp FROM sm_admins WHERE `identity` LIKE ?1") + List result = entityManager.createNativeQuery("SELECT id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`) as u_timestamp FROM sm_admins WHERE `identity` LIKE ?1") .setParameter(1, steamID.steam2) .getResultList(); - return permitionList.size() > 0 ? new Permition(permitionList.get(0)) : null; + return result.stream().map(Permition::new).findFirst().orElse(null); } }