From d363513884d2d7afa2f46d7a45e9a232bcac8077 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Aug 2024 18:42:51 +0530 Subject: [PATCH] Fix listen_on with IPv6 address --- kitty/boss.py | 7 +++---- kitty/utils.py | 9 ++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index 209f6ea37..91fbc2186 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -173,10 +173,9 @@ def listen_on(spec: str) -> tuple[int, str]: atexit.register(remove_socket_file, s, socket_path) s.bind(address) s.listen() - if isinstance(address, tuple): - h, resolved_port = s.getsockname() - sfamily, host, port = spec.split(':', 2) - spec = f'{sfamily}:{host}:{resolved_port}' + if isinstance(address, tuple): # tcp socket + h, resolved_port = s.getsockname()[:2] + spec = spec.rpartition(':')[0] + f':{resolved_port}' return s.fileno(), spec diff --git a/kitty/utils.py b/kitty/utils.py index 3c879b46e..0d0fc826c 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -492,7 +492,14 @@ def parse_address_spec(spec: str) -> tuple[AddressFamily, Union[tuple[str, int], socket_path = address elif protocol in ('tcp', 'tcp6'): family = socket.AF_INET if protocol == 'tcp' else socket.AF_INET6 - host, port = rest.rsplit(':', 1) + if rest.startswith('['): # ] + host = rest[1:] + host, sep, leftover = host.rpartition(']') + _, port = leftover.rsplit(':', 1) + if ':' in host and protocol == 'tcp': + family = socket.AF_INET6 + else: + host, port = rest.rsplit(':', 1) address = host, int(port) else: raise ValueError(f'Unknown protocol in listen-on value: {spec}')