From 72d23e4c33fd21da0dc33bb11ceacc1c54cefc77 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 12 Jul 2024 11:01:16 +0200 Subject: [PATCH] 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. --- coldbrew/vpn/models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/coldbrew/vpn/models.py b/coldbrew/vpn/models.py index 8ec957a..16d7bc8 100644 --- a/coldbrew/vpn/models.py +++ b/coldbrew/vpn/models.py @@ -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)