fix: improve port assignment for VPN instances

Modified the port assignment logic to avoid potential collisions by pre-filtering used ports before selecting from the available pool. This ensures each VPN instance gets a unique port more efficiently, reducing the risk of assignment conflicts and improving the reliability of VPN setup.
This commit is contained in:
Kumi 2024-07-12 11:01:16 +02:00
parent 08b4b28645
commit 72d23e4c33
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -33,11 +33,13 @@ class VPN(models.Model):
self.public_key = get_public_key(self.private_key)
while not self.port:
port = random.randint(10000, 40000)
if not self.port:
pool = list(range(10000, 40000))
if not VPN.objects.filter(port=port).exists():
self.port = port
for vpn in VPN.objects.all():
pool.remove(vpn.port)
self.port = random.choice(pool)
super().save(*args, **kwargs)