From c77f893cd85075bc58aeb58b8c728b28f5db56d1 Mon Sep 17 00:00:00 2001 From: Danijar Hafner Date: Mon, 16 Sep 2024 09:12:02 +0000 Subject: [PATCH] Fix IPv6 binding to any IP --- portal/__init__.py | 2 +- portal/server_socket.py | 4 ++-- portal/utils.py | 8 +++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/portal/__init__.py b/portal/__init__.py index 78d57cf..5e96d96 100644 --- a/portal/__init__.py +++ b/portal/__init__.py @@ -1,4 +1,4 @@ -__version__ = '3.2.4' +__version__ = '3.3.0' import multiprocessing as mp try: diff --git a/portal/server_socket.py b/portal/server_socket.py index c58e209..9627460 100644 --- a/portal/server_socket.py +++ b/portal/server_socket.py @@ -42,10 +42,10 @@ def __init__(self, port, name='Server', **kwargs): self.options = Options(**{**contextlib.context.serverkw, **kwargs}) if self.options.ipv6: self.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - self.addr = (self.options.host, port, 0, 0) + self.addr = (self.options.host or '::', port, 0, 0) else: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.addr = (self.options.host, port) + self.addr = (self.options.host or '0.0.0.0', port) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self._log(f'Binding to {self.addr[0]}:{self.addr[1]}') diff --git a/portal/utils.py b/portal/utils.py index dd5863e..a929fe5 100644 --- a/portal/utils.py +++ b/portal/utils.py @@ -98,10 +98,12 @@ def free_port(): # Return a port that is currently free. This function is not thread or # process safe, because there is no way to guarantee that the port will still # be free at the time it will be used. - if contextlib.context.serverkw.get('ipv6', False): - family, addr = socket.AF_INET6, ('localhost', 0, 0, 0) + ipv6 = contextlib.context.serverkw.get('ipv6', False) + host = contextlib.context.serverkw.get('host', '') + if ipv6: + family, addr = socket.AF_INET6, (host or '::', 0, 0, 0) else: - family, addr = socket.AF_INET, ('localhost', 0) + family, addr = socket.AF_INET, (host or '0.0.0.0', 0) sock = socket.socket(family, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(addr)