2020-04-07 17:45:16 +00:00
|
|
|
import uuid
|
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
from django.db import models
|
2020-04-03 17:27:49 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
|
|
|
|
|
|
|
|
|
|
|
from uncloud_pay.models import Product, RecurringPeriod
|
|
|
|
from uncloud.models import UncloudModel, UncloudStatus
|
|
|
|
|
2020-04-06 20:30:01 +00:00
|
|
|
|
|
|
|
class MACAdress(models.Model):
|
|
|
|
default_prefix = 0x420000000000
|
|
|
|
|
2020-04-03 17:27:49 +00:00
|
|
|
class VPNPool(UncloudModel):
|
|
|
|
"""
|
|
|
|
Network address pools from which VPNs can be created
|
|
|
|
"""
|
|
|
|
|
2020-04-07 17:45:16 +00:00
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2020-04-03 17:27:49 +00:00
|
|
|
|
2020-04-07 17:45:16 +00:00
|
|
|
network = models.GenericIPAddressField(unique=True)
|
2020-04-03 17:27:49 +00:00
|
|
|
network_size = models.IntegerField(validators=[MinValueValidator(0),
|
|
|
|
MaxValueValidator(128)])
|
|
|
|
|
2020-04-08 14:24:39 +00:00
|
|
|
subnetwork_size = models.IntegerField(validators=[
|
|
|
|
MinValueValidator(0),
|
|
|
|
MaxValueValidator(128)
|
|
|
|
])
|
2020-04-07 17:45:16 +00:00
|
|
|
|
2020-04-06 20:30:01 +00:00
|
|
|
vpn_hostname = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
wireguard_private_key = models.CharField(max_length=48)
|
|
|
|
|
2020-04-08 14:24:39 +00:00
|
|
|
@property
|
|
|
|
def num_maximum_networks(self):
|
|
|
|
"""
|
|
|
|
sample:
|
|
|
|
network_size = 40
|
|
|
|
subnetwork_size = 48
|
|
|
|
maximum_networks = 2^(48-40)
|
|
|
|
|
|
|
|
2nd sample:
|
|
|
|
network_size = 8
|
|
|
|
subnetwork_size = 24
|
|
|
|
maximum_networks = 2^(24-8)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return 2**(subnetwork_size - network_size)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def used_networks(self):
|
|
|
|
return self.vpnnetworkreservation_set.objects.filter(vpnpool=self, status='used')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def num_used_networks(self):
|
|
|
|
return len(self.used_networks)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def num_free_networks(self):
|
|
|
|
return self.num_maximum_networks - self.num_used_networks
|
|
|
|
|
|
|
|
@property
|
|
|
|
def next_free_network(self):
|
|
|
|
free_net = self.vpnnetworkreservation_set.objects.filter(vpnpool=self,
|
|
|
|
status='free')
|
|
|
|
|
|
|
|
last_net = self.vpnnetworkreservation_set.objects.filter(vpnpool=self,
|
|
|
|
status='used')
|
|
|
|
|
|
|
|
if num_free_networks == 0:
|
|
|
|
raise Exception("No free networks")
|
|
|
|
|
|
|
|
if len(free_net) > 0:
|
|
|
|
return free_net[0].address
|
|
|
|
|
|
|
|
if len(used_net) > 0:
|
|
|
|
"""
|
|
|
|
sample:
|
|
|
|
|
|
|
|
pool = 2a0a:e5c1:200::/40
|
|
|
|
last_used = 2a0a:e5c1:204::/48
|
|
|
|
|
|
|
|
next:
|
|
|
|
"""
|
|
|
|
|
|
|
|
last_ip = last_net.address
|
|
|
|
# next_ip =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-06 20:30:01 +00:00
|
|
|
|
2020-04-07 17:45:16 +00:00
|
|
|
class VPNNetworkReservation(UncloudModel):
|
2020-04-03 17:27:49 +00:00
|
|
|
"""
|
2020-04-08 14:24:39 +00:00
|
|
|
This class tracks the used VPN networks. It will be deleted, when the product is cancelled.
|
|
|
|
"""
|
2020-04-03 17:27:49 +00:00
|
|
|
vpnpool = models.ForeignKey(VPNPool,
|
2020-04-08 14:24:39 +00:00
|
|
|
on_delete=models.CASCADE)
|
2020-04-07 17:45:16 +00:00
|
|
|
address = models.GenericIPAddressField(primary_key=True)
|
|
|
|
|
2020-04-08 14:24:39 +00:00
|
|
|
status = models.CharField(max_length=256,
|
|
|
|
choices = (
|
|
|
|
('used', 'used'),
|
|
|
|
('free', 'free')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-04-07 17:45:16 +00:00
|
|
|
|
|
|
|
class VPNNetwork(Product):
|
|
|
|
"""
|
|
|
|
A selected network. Used for tracking reservations / used networks
|
|
|
|
"""
|
|
|
|
network = models.ForeignKey(VPNNetworkReservation,
|
2020-04-08 14:24:39 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
editable=False)
|
2020-03-02 06:17:04 +00:00
|
|
|
|
2020-04-06 20:30:01 +00:00
|
|
|
wireguard_public_key = models.CharField(max_length=48)
|