Skip to content

Commit

Permalink
Update port allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
pchristos committed Jul 29, 2016
1 parent 15ca892 commit 1fa8e06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 4 additions & 6 deletions vpn-proxy/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from .tunnels import get_conf, get_client_conf, get_client_script
from .tunnels import add_iptables, del_iptables, add_fwmark, del_fwmark


IFACE_PREFIX = settings.IFACE_PREFIX
SERVER_PORT_START = settings.SERVER_PORT_START
PORT_ALLOC_START, PORT_ALLOC_STOP = settings.PORT_ALLOC_RANGE
Expand Down Expand Up @@ -79,15 +78,14 @@ def check_ip(addr):
def pick_port(PORT_ALLOC_START, PORT_ALLOC_STOP):
"""Find next available port based on Forwarding.
This function is used directly by views.py"""
_port = random.randrange(PORT_ALLOC_START, PORT_ALLOC_STOP)
for _ in xrange(PORT_ALLOC_START, PORT_ALLOC_STOP):
for _ in xrange(100):
_port = random.randrange(PORT_ALLOC_START, PORT_ALLOC_STOP)
try:
Forwarding.objects.get(loc_port=_port)
_port += 1
if _port > PORT_ALLOC_STOP:
_port = PORT_ALLOC_START
except Forwarding.DoesNotExist:
return _port
else:
raise Exception('Cloud not find available port for allocation')


class BaseModel(models.Model):
Expand Down
5 changes: 4 additions & 1 deletion vpn-proxy/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def connection(request, tunnel_id, target, port):
forwarding.enable()
return HttpResponse(forwarding.port)
except Forwarding.DoesNotExist:
loc_port = pick_port()
try:
loc_port = pick_port()
except Exception as exc:
return HttpResponse(str(exc), status=409)
forwarding = Forwarding(loc_port=loc_port, active=False, **entry)
forwarding.save()
forwarding.enable()
Expand Down

0 comments on commit 1fa8e06

Please sign in to comment.