fix: ensure unique VPN port assignment

Updated the port assignment logic to guarantee that a newly assigned port is unique by avoiding conflicts with existing port assignments. This addresses potential issues with port collisions that could disrupt VPN operations.
This commit is contained in:
Kumi 2024-07-12 10:59:11 +02:00
parent ed86554e54
commit 08b4b28645
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -33,8 +33,11 @@ class VPN(models.Model):
self.public_key = get_public_key(self.private_key)
if not self.port:
self.port = random.randint(10000, 40000)
while not self.port:
port = random.randint(10000, 40000)
if not VPN.objects.filter(port=port).exists():
self.port = port
super().save(*args, **kwargs)