Parse OpenVPN clients into structured response

This commit is contained in:
litoral05
2026-05-05 11:54:05 +01:00
parent 072d82e185
commit ea0b002af1
3 changed files with 62 additions and 5 deletions
@@ -0,0 +1,13 @@
package com.litoralregas.openvpn.openvpn;
public record OpenVpnClientResponse(
String clientName,
String vpnIp,
String vpnNetmask,
String lanSubnet,
String lanNetmask,
boolean serverRoute,
boolean bundle,
boolean folder
) {
}
@@ -1,8 +1,9 @@
package com.litoralregas.openvpn.openvpn; package com.litoralregas.openvpn.openvpn;
import com.litoralregas.openvpn.ssh.SshCommandResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RestController
@RequestMapping("/api/openvpn") @RequestMapping("/api/openvpn")
public class OpenVpnController { public class OpenVpnController {
@@ -14,7 +15,7 @@ public class OpenVpnController {
} }
@GetMapping("/clients") @GetMapping("/clients")
public SshCommandResult getClients() { public List<OpenVpnClientResponse> getClients() {
return service.listClients(); return service.listClients();
} }
} }
@@ -1,9 +1,11 @@
package com.litoralregas.openvpn.openvpn; package com.litoralregas.openvpn.openvpn;
import com.litoralregas.openvpn.ssh.SshCommandResult;
import com.litoralregas.openvpn.ssh.SshService; import com.litoralregas.openvpn.ssh.SshService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service @Service
public class OpenVpnService { public class OpenVpnService {
@@ -15,9 +17,50 @@ public class OpenVpnService {
this.sshService = sshService; this.sshService = sshService;
} }
public SshCommandResult listClients() { public List<OpenVpnClientResponse> listClients() {
return sshService.executeOnConfiguredVps( var result = sshService.executeOnConfiguredVps(
TOOLS_PATH + "/list-clients.sh" TOOLS_PATH + "/list-clients.sh"
); );
if (result.exitCode() != 0) {
throw new IllegalStateException(result.stderr());
}
return parseClients(result.stdout());
}
private List<OpenVpnClientResponse> parseClients(String stdout) {
List<OpenVpnClientResponse> clients = new ArrayList<>();
String[] blocks = stdout.split("\\n\\n");
for (String block : blocks) {
if (!block.contains("Client:")) {
continue;
}
clients.add(new OpenVpnClientResponse(
extractValue(block, "Client:"),
extractValue(block, "VPN IP:"),
extractValue(block, "VPN netmask:"),
extractValue(block, "LAN subnet:"),
extractValue(block, "LAN netmask:"),
"yes".equalsIgnoreCase(extractValue(block, "Server route:")),
"yes".equalsIgnoreCase(extractValue(block, "Bundle:")),
"yes".equalsIgnoreCase(extractValue(block, "Folder:"))
));
}
return clients;
}
private String extractValue(String block, String label) {
for (String line : block.split("\\n")) {
if (line.trim().startsWith(label)) {
return line.substring(line.indexOf(":") + 1).trim();
}
}
return null;
} }
} }