diff --git a/src/main/java/app/controllers/AdminController.java b/src/main/java/app/controllers/AdminController.java new file mode 100644 index 0000000..73bb72b --- /dev/null +++ b/src/main/java/app/controllers/AdminController.java @@ -0,0 +1,51 @@ +package app.controllers; + +import app.entities.PlayerProfile; +import app.entities.db.Permition; +import app.services.ProfileService; +import app.services.ServerService; +import app.utils.SaltedCookie; +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") +public class AdminController { + ServerService serverService; + SaltedCookie saltedCookie; + ProfileService profileService; + + @Autowired + public AdminController(ServerService serverService, + SaltedCookie saltedCookie, + ProfileService profileService) { + this.serverService = serverService; + this.saltedCookie = saltedCookie; + this.profileService = profileService; + } + + public boolean ValidateAdmin(String steam64, String flag) { + Permition permition = profileService.GetProfile(steam64, List.of("permition")).getPermition(); + if (permition == null) return false; + return permition.getFlags().contains(flag); + } + + @GetMapping("kick") + public ResponseEntity kickPlayer( + @CookieValue(value = "steam64", defaultValue = "") String steam64, + @CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured, + @RequestParam(value = "steam64", required = true) String kicked_steam64 + ) { + if(!saltedCookie.Validate(steam64, steam64_secured)) return new ResponseEntity<>(HttpStatus.FORBIDDEN); + if(!ValidateAdmin(steam64, "z")) return new ResponseEntity<>(HttpStatus.FORBIDDEN); + if(serverService.kickPlayer(profileService.GetProfile(kicked_steam64,List.of()))){ + return new ResponseEntity(HttpStatus.OK); + } else { + return new ResponseEntity(HttpStatus.NOT_FOUND); + } + } +} diff --git a/src/main/java/app/controllers/ProfileController.java b/src/main/java/app/controllers/ProfileController.java index 3e01c3e..93d8f57 100644 --- a/src/main/java/app/controllers/ProfileController.java +++ b/src/main/java/app/controllers/ProfileController.java @@ -18,15 +18,12 @@ import java.util.List; public class ProfileController { private SaltedCookie saltedCookie; private ProfileService profileService; - private Stats stats; @Autowired public ProfileController(SaltedCookie saltedCookie, - ProfileService profileService, - Stats stats) { + ProfileService profileService) { this.saltedCookie = saltedCookie; this.profileService = profileService; - this.stats = stats; } @GetMapping("/current") @@ -38,15 +35,4 @@ public class ProfileController { return new ResponseEntity(profileService.GetProfile(steam64), HttpStatus.OK); } - - @GetMapping("/current/kick") - public ResponseEntity KickCurrentUserFromServers( - @CookieValue(value = "steam64", defaultValue = "") String steam64, - @CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured - ){ - if(!saltedCookie.Validate(steam64, steam64_secured)) return new ResponseEntity<>(HttpStatus.FORBIDDEN); - - //return new ResponseEntity(stats.kickPlayer(profileService.GetProfile(steam64, List.of()).getPlay_on()), HttpStatus.OK); - return new ResponseEntity<>(HttpStatus.FORBIDDEN); - } } diff --git a/src/main/java/app/entities/Stats.java b/src/main/java/app/entities/Stats.java index dd9d97d..174d76a 100644 --- a/src/main/java/app/entities/Stats.java +++ b/src/main/java/app/entities/Stats.java @@ -78,19 +78,4 @@ public class Stats { return; } } - - /*@JsonIgnore - public PlayOn searchPlayer(SteamID steamID){ - for (Map.Entry stringServerEntry : servers.entrySet()) { - RCONPlayer player = stringServerEntry.getValue().searchPlayer(steamID); - if (player != null) return new PlayOn(stringServerEntry.getKey(), player.getId()); - } - return null; - }*/ - - /*@JsonIgnore - public String kickPlayer(PlayOn playOn){ - if (playOn == null) return ""; - return servers.get(playOn.getServer_id()).ExecuteRCON(applicationContext, "sm_kick #%d kicked from backend".formatted(playOn.getPlayer_id())); - }*/ } diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index e6f97e2..218c285 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -19,16 +19,16 @@ public class ProfileService { SteamWebApi steamWebApi; UsertimeService usertimeService; PermitionService permitionService; - Stats stats; + StatsService statsService; @Autowired public ProfileService(SteamWebApi steamWebApi, UsertimeService usertimeService, PermitionService permitionService, - Stats stats) { + StatsService statsService) { this.steamWebApi = steamWebApi; this.usertimeService = usertimeService; this.permitionService = permitionService; - this.stats = stats; + this.statsService = statsService; } public PlayerProfile GetProfile(String steam64, List requests) { @@ -40,7 +40,7 @@ public class ProfileService { start_time = Instant.now().toEpochMilli(); - //profile.setPlay_on(stats.searchPlayer(steamID)); + profile.setPlay_on(statsService.searchPlayer(steamID)); end_time = Instant.now().toEpochMilli() - start_time; profile.getResponse_time().put("play_on", Double.valueOf(end_time) / 1000); diff --git a/src/main/java/app/services/ServerService.java b/src/main/java/app/services/ServerService.java new file mode 100644 index 0000000..071a5ae --- /dev/null +++ b/src/main/java/app/services/ServerService.java @@ -0,0 +1,27 @@ +package app.services; + +import app.entities.PlayerProfile; +import app.entities.Stats; +import app.entities.server.PlayOn; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +@Service +public class ServerService { + Stats stats; + ApplicationContext applicationContext; + + @Autowired + public ServerService(Stats stats, + ApplicationContext applicationContext) { + this.stats = stats; + this.applicationContext = applicationContext; + } + + public boolean kickPlayer(PlayerProfile playerProfile){ + if (playerProfile.getPlay_on() == null) return false; + return stats.getServers().get(playerProfile.getPlay_on().getServer_id()) + .ExecuteRCON(applicationContext, "sm_kick #%d kicked from backend".formatted(playerProfile.getPlay_on().getPlayer_id())).contains("kicked"); + } +} diff --git a/src/main/java/app/services/StatsService.java b/src/main/java/app/services/StatsService.java new file mode 100644 index 0000000..d1bd299 --- /dev/null +++ b/src/main/java/app/services/StatsService.java @@ -0,0 +1,30 @@ +package app.services; + +import app.entities.Stats; +import app.entities.other.SteamID; +import app.entities.server.PlayOn; +import app.entities.server.Server; +import app.entities.server.players.RCONPlayer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Map; + +@Service +public class StatsService { + Stats stats; + + @Autowired + public StatsService (Stats stats) { + this.stats = stats; + } + + 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()); + } + return null; + } + +}