Add VPS-aware VPN IP allocation
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.litoralregas.vpnprovisioner.vps;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Service
|
||||
public class WireGuardVpsService {
|
||||
|
||||
private static final Pattern VPN_IP_PATTERN =
|
||||
Pattern.compile("\\b198\\.19\\.\\d{1,3}\\.\\d{1,3}\\b");
|
||||
|
||||
private final SshService sshService;
|
||||
|
||||
public WireGuardVpsService(SshService sshService) {
|
||||
this.sshService = sshService;
|
||||
}
|
||||
|
||||
public Set<String> findUsedVpnIps() {
|
||||
SshCommandResult result = sshService.executeOnConfiguredVps(
|
||||
"sudo wg show wg0 allowed-ips"
|
||||
);
|
||||
|
||||
if (result.exitCode() != 0) {
|
||||
throw new SshCommandException(
|
||||
"Failed to query WireGuard allowed IPs: " + result.stderr()
|
||||
);
|
||||
}
|
||||
|
||||
return parseVpnIps(result.stdout());
|
||||
}
|
||||
|
||||
Set<String> parseVpnIps(String output) {
|
||||
Set<String> ips = new HashSet<>();
|
||||
|
||||
Matcher matcher = VPN_IP_PATTERN.matcher(output);
|
||||
|
||||
while (matcher.find()) {
|
||||
ips.add(matcher.group());
|
||||
}
|
||||
|
||||
return ips;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user