Browse Source

real ip fix

master
gsd 9 months ago
parent
commit
8a8fefb0db
  1. 34
      src/main/java/app/controllers/StatsController.java
  2. 31
      src/main/java/app/utils/RealIPService.java
  3. 12
      src/test/java/app/utils/RealIPServiceTest.java

34
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<Map<Integer, String>, HashMap<List<OnlineUpdater.StatsOfPeakOfDay>, 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<Boolean> reloadConfig(HttpServletRequest request) {
return new ResponseEntity<>(serversReader.call(false), HttpStatus.OK);
}
@GetMapping("/gc/ip")
@CollectStatistic(stage = CollectStages.COMBINED)
public ResponseEntity<String> 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 = """
<!DOCTYPE html>
<html>
<body>
<h2>Try open server use steam</h2>
<h2>If not connected use cmd in console: %s</h2>
<script>
function myFunc() {
location.replace("%s");
}
window.onload = myFunc;
</script>
</body>
</html>""";
return new ResponseEntity(page.formatted(
"connect " + route, "steam://connect/" + route
), HttpStatus.OK);
}
}

31
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();
}
}

12
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());
}
}
Loading…
Cancel
Save