[vpn] implement creating vpns

This commit is contained in:
Nico Schottelius 2020-12-13 17:59:35 +01:00
commit cd19c47fdb
6 changed files with 130 additions and 55 deletions

View file

@ -25,6 +25,24 @@ class WireGuardVPNPool(models.Model):
vpn_server_hostname = models.CharField(max_length=256)
wireguard_private_key = models.CharField(max_length=48)
@property
def max_pool_index(self):
"""
Return the highest possible network / last network id
"""
bits = self.subnetwork_mask - self.network_mask
return (2**bits)-1
@property
def ip_network(self):
return ipaddress.ip_network(f"{self.network}/{self.network_mask}")
def __str__(self):
return f"{self.ip_network} (subnets: /{self.subnetwork_mask})"
class WireGuardVPN(models.Model):
"""
Created VPNNetworks
@ -34,10 +52,39 @@ class WireGuardVPN(models.Model):
vpnpool = models.ForeignKey(WireGuardVPNPool,
on_delete=models.CASCADE)
address = models.GenericIPAddressField(primary_key=True)
pool_index = models.IntegerField(unique=True)
wireguard_public_key = models.CharField(max_length=48)
@property
def network_mask(self):
return self.vpnpool.subnetwork_mask
@property
def address(self):
"""
Locate the correct subnet in the supernet
First get the network itself
"""
net = self.vpnpool.ip_network
subnet = net[(2**(128-self.vpnpool.subnetwork_mask)) * self.pool_index]
return str(subnet)
def __str__(self):
return f"{self.address} ({self.pool_index})"
class WireGuardVPNFreeLeases(models.Model):
"""
Previously used VPNNetworks
"""
vpnpool = models.ForeignKey(WireGuardVPNPool,
on_delete=models.CASCADE)
pool_index = models.IntegerField(unique=True)
################################################################################