diff --git a/src/main/java/com/litoralregas/vpnprovisioner/vps/VpsController.java b/src/main/java/com/litoralregas/vpnprovisioner/vps/VpsController.java index 83a28f4..304728b 100644 --- a/src/main/java/com/litoralregas/vpnprovisioner/vps/VpsController.java +++ b/src/main/java/com/litoralregas/vpnprovisioner/vps/VpsController.java @@ -1,5 +1,6 @@ package com.litoralregas.vpnprovisioner.vps; +import com.litoralregas.vpnprovisioner.vps.dto.VpsHealthResponse; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; @@ -14,8 +15,8 @@ public class VpsController { } @GetMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE) - public String health() { - return wireGuardVpsService.getVpsHealthJson(); + public VpsHealthResponse health() { + return wireGuardVpsService.getVpsHealth(); } @PostMapping( diff --git a/src/main/java/com/litoralregas/vpnprovisioner/vps/WireGuardVpsService.java b/src/main/java/com/litoralregas/vpnprovisioner/vps/WireGuardVpsService.java index 73ea5bc..ac10487 100644 --- a/src/main/java/com/litoralregas/vpnprovisioner/vps/WireGuardVpsService.java +++ b/src/main/java/com/litoralregas/vpnprovisioner/vps/WireGuardVpsService.java @@ -1,5 +1,8 @@ package com.litoralregas.vpnprovisioner.vps; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.litoralregas.vpnprovisioner.vps.dto.VpsHealthResponse; import org.springframework.stereotype.Service; import java.util.HashSet; @@ -14,9 +17,11 @@ public class WireGuardVpsService { Pattern.compile("\\b198\\.19\\.\\d{1,3}\\.\\d{1,3}\\b"); private final SshService sshService; + private final ObjectMapper objectMapper; - public WireGuardVpsService(SshService sshService) { + public WireGuardVpsService(SshService sshService, ObjectMapper objectMapper) { this.sshService = sshService; + this.objectMapper = objectMapper; } public Set findUsedVpnIps() { @@ -66,7 +71,7 @@ public class WireGuardVpsService { ); } - public String getVpsHealthJson() { + public VpsHealthResponse getVpsHealth() { SshCommandResult result = sshService.executeOnConfiguredVps( "sudo /usr/local/sbin/lr-vps-health" ); @@ -77,7 +82,14 @@ public class WireGuardVpsService { ); } - return result.stdout(); + try { + return objectMapper.readValue(result.stdout(), VpsHealthResponse.class); + } catch (JsonProcessingException e) { + throw new IllegalStateException( + "Invalid VPS health JSON returned by script", + e + ); + } } public String restoreLastWireGuardBackup() { diff --git a/src/main/java/com/litoralregas/vpnprovisioner/vps/dto/VpsHealthResponse.java b/src/main/java/com/litoralregas/vpnprovisioner/vps/dto/VpsHealthResponse.java new file mode 100644 index 0000000..2ac9808 --- /dev/null +++ b/src/main/java/com/litoralregas/vpnprovisioner/vps/dto/VpsHealthResponse.java @@ -0,0 +1,17 @@ +package com.litoralregas.vpnprovisioner.vps.dto; + +public record VpsHealthResponse( + String wireGuardInterface, + boolean wireGuardRunning, + int wireGuardPeerCount, + boolean wireGuardConfigExists, + String udp2rawService, + boolean udp2rawActive, + String latestWireGuardBackup, + String systemUptime, + int diskUsagePercent, + int memoryUsagePercent, + String loadAverage, + String publicIp +) { +} \ No newline at end of file