From 8a8fefb0db600ccfa8b396aab2c554519142c4ca Mon Sep 17 00:00:00 2001 From: gsd Date: Fri, 28 Jun 2024 00:22:33 +0300 Subject: [PATCH] real ip fix --- .../java/app/controllers/StatsController.java | 34 +++++++++++++++++++ src/main/java/app/utils/RealIPService.java | 31 +++++++++++++++++ .../java/app/utils/RealIPServiceTest.java | 12 +++++++ 3 files changed, 77 insertions(+) create mode 100644 src/main/java/app/utils/RealIPService.java create mode 100644 src/test/java/app/utils/RealIPServiceTest.java diff --git a/src/main/java/app/controllers/StatsController.java b/src/main/java/app/controllers/StatsController.java index e9063ed..528a93f 100644 --- a/src/main/java/app/controllers/StatsController.java +++ b/src/main/java/app/controllers/StatsController.java @@ -6,6 +6,7 @@ import app.entities.Stats; import app.services.ExternalServices; import app.services.io.readers.ServersReader; import app.updates.OnlineUpdater; +import app.utils.RealIPService; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -28,6 +29,7 @@ public class StatsController { private OnlineUpdater onlineUpdater; private HashMap, HashMap, Long>> cache = new HashMap<>(); private ServersReader serversReader; + private RealIPService realIPService; @Autowired public StatsController(Stats stats, ExternalServices externalServices, OnlineUpdater onlineUpdater, ServersReader serversReader){ @@ -35,6 +37,7 @@ public class StatsController { this.externalServices = externalServices; this.onlineUpdater = onlineUpdater; this.serversReader = serversReader; + this.realIPService = new RealIPService(); } @GetMapping @@ -86,4 +89,35 @@ public class StatsController { public ResponseEntity reloadConfig(HttpServletRequest request) { return new ResponseEntity<>(serversReader.call(false), HttpStatus.OK); } + + @GetMapping("/gc/ip") + @CollectStatistic(stage = CollectStages.COMBINED) + public ResponseEntity getConnectIP(@RequestParam Integer port) { + return new ResponseEntity<>(realIPService.genIPWithPort(port), HttpStatus.OK); + } + + @GetMapping("/gc/pg") + @ResponseBody + @CollectStatistic(stage = CollectStages.COMBINED) + public ResponseEntity getConnectPage(@RequestParam Integer port) { + String route = realIPService.genIPWithPort(port); + String page = """ + + + +

Try open server use steam

+

If not connected use cmd in console: %s

+ + + +"""; + return new ResponseEntity(page.formatted( + "connect " + route, "steam://connect/" + route + ), HttpStatus.OK); + } } diff --git a/src/main/java/app/utils/RealIPService.java b/src/main/java/app/utils/RealIPService.java new file mode 100644 index 0000000..3e7b729 --- /dev/null +++ b/src/main/java/app/utils/RealIPService.java @@ -0,0 +1,31 @@ +package app.utils; + +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.time.Instant; +import java.util.HashMap; + +public class RealIPService { + private final RestTemplate restTemplate; + private final long lst_update = 0; + private String lst_ip = ""; + + public RealIPService() { + this.restTemplate = new RestTemplate(); + } + + public String getIP() { + if (Instant.now().getEpochSecond() - lst_update < 60) + return lst_ip; + + HashMap response = restTemplate.getForEntity("https://ifconfig.co/json", HashMap.class).getBody(); + lst_ip = (String) response.getOrDefault("ip", ""); + return lst_ip; + } + + public String genIPWithPort(Integer port) { + return new StringBuilder().append(getIP()).append(":").append(port).toString(); + } + +} diff --git a/src/test/java/app/utils/RealIPServiceTest.java b/src/test/java/app/utils/RealIPServiceTest.java new file mode 100644 index 0000000..b8a6401 --- /dev/null +++ b/src/test/java/app/utils/RealIPServiceTest.java @@ -0,0 +1,12 @@ +package app.utils; + +import org.junit.Test; + +public class RealIPServiceTest { + + @Test + public void getIP() { + RealIPService realIPService = new RealIPService(); + System.out.println(realIPService.getIP()); + } +}