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:
parent
08b4b28645
commit
72d23e4c33
1 changed files with 6 additions and 4 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue