Parse OpenVPN clients into structured response
This commit is contained in:
@@ -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;
|
||||
|
||||
import com.litoralregas.openvpn.ssh.SshCommandResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/openvpn")
|
||||
public class OpenVpnController {
|
||||
@@ -14,7 +15,7 @@ public class OpenVpnController {
|
||||
}
|
||||
|
||||
@GetMapping("/clients")
|
||||
public SshCommandResult getClients() {
|
||||
public List<OpenVpnClientResponse> getClients() {
|
||||
return service.listClients();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.litoralregas.openvpn.openvpn;
|
||||
|
||||
import com.litoralregas.openvpn.ssh.SshCommandResult;
|
||||
import com.litoralregas.openvpn.ssh.SshService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OpenVpnService {
|
||||
|
||||
@@ -15,9 +17,50 @@ public class OpenVpnService {
|
||||
this.sshService = sshService;
|
||||
}
|
||||
|
||||
public SshCommandResult listClients() {
|
||||
return sshService.executeOnConfiguredVps(
|
||||
public List<OpenVpnClientResponse> listClients() {
|
||||
var result = sshService.executeOnConfiguredVps(
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user