Agent: Fix min_range in get_free_tcp_port()

get_free_tcp_port() effectively ignored the min_range parameter by using
min(1, min_range). This meant that min_range was always 1 (unless a
negative value was passed). As ports lower than 1024 are privileged on
Linux, this lead to the agent trying to bind to ports that it did not
have permission to. By using max(1, min_range), We insure that min_range
is always at least 1, but will still use the provided parameter (1024 by
default).
This commit is contained in:
Mike Salvatore 2022-01-12 12:20:32 -05:00
parent 16219b714b
commit 8f53a5ccd0
1 changed files with 3 additions and 3 deletions

View File

@ -108,14 +108,14 @@ else:
return routes return routes
def get_free_tcp_port(min_range=1000, max_range=65535): def get_free_tcp_port(min_range=1024, max_range=65535):
start_range = min(1, min_range) min_range = max(1, min_range)
max_range = min(65535, max_range) max_range = min(65535, max_range)
in_use = [conn.laddr[1] for conn in psutil.net_connections()] in_use = [conn.laddr[1] for conn in psutil.net_connections()]
for i in range(min_range, max_range): for i in range(min_range, max_range):
port = randint(start_range, max_range) port = randint(min_range, max_range)
if port not in in_use: if port not in in_use:
return port return port