fixed ip allocation service
This commit is contained in:
@@ -12,15 +12,12 @@ public class IpAllocationService {
|
||||
|
||||
private final IpAllocationRepository repository;
|
||||
private final RouterService routerService;
|
||||
private final OpenVpnService openVpnService;
|
||||
|
||||
public IpAllocationService(
|
||||
IpAllocationRepository repository,
|
||||
RouterService routerService,
|
||||
OpenVpnService openVpnService) {
|
||||
RouterService routerService) {
|
||||
this.repository = repository;
|
||||
this.routerService = routerService;
|
||||
this.openVpnService = openVpnService;
|
||||
}
|
||||
|
||||
public IpAllocationResponse allocate(UUID routerId, CreateIpAllocationRequest request) {
|
||||
@@ -32,16 +29,11 @@ public class IpAllocationService {
|
||||
|
||||
int number = resolveNumber(request);
|
||||
|
||||
String lanSubnet = "192.168." + number + ".0";
|
||||
String lanSubnet = router.getLanSubnet();
|
||||
String vpnIp = "198.20.1." + number;
|
||||
|
||||
validateNumber(number);
|
||||
validateNoDuplicates(lanSubnet, vpnIp);
|
||||
validateAgainstLiveClients(
|
||||
request.getClientName(),
|
||||
vpnIp,
|
||||
lanSubnet
|
||||
);
|
||||
|
||||
IpAllocation allocation = new IpAllocation();
|
||||
allocation.setId(UUID.randomUUID());
|
||||
@@ -65,10 +57,9 @@ public class IpAllocationService {
|
||||
}
|
||||
|
||||
for (int number = 2; number <= 254; number++) {
|
||||
String lanSubnet = "192.168." + number + ".0";
|
||||
String vpnIp = "198.20.1." + number;
|
||||
|
||||
if (!repository.existsByLanSubnet(lanSubnet) && !repository.existsByVpnIp(vpnIp)) {
|
||||
if (!repository.existsByVpnIp(vpnIp)) {
|
||||
return number;
|
||||
}
|
||||
}
|
||||
@@ -92,24 +83,6 @@ public class IpAllocationService {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateAgainstLiveClients(String clientName, String vpnIp, String lanSubnet) {
|
||||
var clients = openVpnService.listClients();
|
||||
|
||||
for (var client : clients) {
|
||||
if (client.clientName().equalsIgnoreCase(clientName)) {
|
||||
throw new IllegalArgumentException("Client already exists on VPS: " + clientName);
|
||||
}
|
||||
|
||||
if (client.vpnIp().equals(vpnIp)) {
|
||||
throw new IllegalArgumentException("VPN IP already in use on VPS: " + vpnIp);
|
||||
}
|
||||
|
||||
if (client.lanSubnet().equals(lanSubnet)) {
|
||||
throw new IllegalArgumentException("LAN subnet already in use on VPS: " + lanSubnet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(UUID id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user