diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index 528a93f..5571d5c 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -90,16 +90,16 @@ public class StatsController { return new ResponseEntity<>(serversReader.call(false), HttpStatus.OK); } - @GetMapping("/gc/ip") + @GetMapping("/gc/ip/{port}") @CollectStatistic(stage = CollectStages.COMBINED) - public ResponseEntity getConnectIP(@RequestParam Integer port) { + public ResponseEntity getConnectIP(@PathVariable Integer port) { return new ResponseEntity<>(realIPService.genIPWithPort(port), HttpStatus.OK); } - @GetMapping("/gc/pg") + @GetMapping("/gc/pg/{port}") @ResponseBody @CollectStatistic(stage = CollectStages.COMBINED) - public ResponseEntity getConnectPage(@RequestParam Integer port) { + public ResponseEntity getConnectPage(@PathVariable Integer port) { String route = realIPService.genIPWithPort(port); String page = """ diff --git a/src/main/java/app/controllers/server/ServerUpdaterController.java b/src/main/java/app/controllers/server/ServerUpdaterController.java index d0cb42d..5834247 100644 --- a/src/main/java/app/controllers/server/ServerUpdaterController.java +++ b/src/main/java/app/controllers/server/ServerUpdaterController.java @@ -8,6 +8,7 @@ import app.entities.report.ReportType; import app.entities.server.request.ServerRequestBody; import app.services.ProfileService; import app.services.db.ReportService; +import app.utils.RealIPService; import app.websocket.handlers.ServersHandler; import jakarta.servlet.http.HttpServletRequest; import lombok.Data; @@ -28,6 +29,7 @@ public class ServerUpdaterController { private ServersHandler serversHandler; private ReportService reportService; private ProfileService profileService; + private RealIPService realIPService; @Autowired public ServerUpdaterController(Stats stats, @@ -38,6 +40,7 @@ public class ServerUpdaterController { this.serversHandler = serversHandler; this.reportService = reportService; this.profileService = profileService; + realIPService = new RealIPService(); } @PostMapping(value = "/{srv}") @@ -45,6 +48,7 @@ public class ServerUpdaterController { public ResponseEntity updateServer(HttpServletRequest request, @PathVariable String srv, @RequestBody ServerRequestBody serverRequestBody) { if (!stats.getServers().containsKey(srv)) return new ResponseEntity<>(HttpStatus.NOT_FOUND); stats.getServers().get(srv).RefreshServerFromRequest(serverRequestBody); + stats.getServers().get(srv).setIp(realIPService.getIP()); serversHandler.pushServer(srv, stats.getServers().get(srv)); return new ResponseEntity(HttpStatus.OK); } diff --git a/src/main/java/app/entities/server/Server.java b/src/main/java/app/entities/server/Server.java index 1b70228..35461c0 100644 --- a/src/main/java/app/entities/server/Server.java +++ b/src/main/java/app/entities/server/Server.java @@ -32,6 +32,7 @@ public class Server extends ExternalValveClient { List players = new ArrayList<>(); long last_update = 0; DockerStats dockerStats = new DockerStats(); + String ip; @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String db; diff --git a/src/main/java/app/utils/RealIPService.java b/src/main/java/app/utils/RealIPService.java index 3e7b729..8905cbf 100644 --- a/src/main/java/app/utils/RealIPService.java +++ b/src/main/java/app/utils/RealIPService.java @@ -16,11 +16,13 @@ public class RealIPService { } public String getIP() { - if (Instant.now().getEpochSecond() - lst_update < 60) + if (Instant.now().getEpochSecond() - lst_update < 300) return lst_ip; - HashMap response = restTemplate.getForEntity("https://ifconfig.co/json", HashMap.class).getBody(); - lst_ip = (String) response.getOrDefault("ip", ""); + try { + HashMap response = restTemplate.getForEntity("https://ifconfig.co/json", HashMap.class).getBody(); + lst_ip = (String) response.getOrDefault("ip", ""); + } catch (Exception e) {} return lst_ip; }