Browse Source

real ip fix 2

master
gsd 9 months ago
parent
commit
a6af36dbb9
  1. 8
      src/main/java/app/controllers/StatsController.java
  2. 4
      src/main/java/app/controllers/server/ServerUpdaterController.java
  3. 1
      src/main/java/app/entities/server/Server.java
  4. 8
      src/main/java/app/utils/RealIPService.java

8
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<String> getConnectIP(@RequestParam Integer port) {
public ResponseEntity<String> 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 = """
<!DOCTYPE html>

4
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);
}

1
src/main/java/app/entities/server/Server.java

@ -32,6 +32,7 @@ public class Server extends ExternalValveClient {
List<RCONPlayer> players = new ArrayList<>();
long last_update = 0;
DockerStats dockerStats = new DockerStats();
String ip;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String db;

8
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;
}

Loading…
Cancel
Save