From 25b623dd7386d94558cab90ff9cdccf96dabed8f Mon Sep 17 00:00:00 2001 From: ReeceJones Date: Mon, 12 Feb 2024 20:36:59 -0500 Subject: [PATCH] Add IPV6 support to unoserver --- src/unoserver/server.py | 19 ++++++++++++++++++- tests/test_server.py | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/unoserver/server.py b/src/unoserver/server.py index 01cb912..2ae69ac 100644 --- a/src/unoserver/server.py +++ b/src/unoserver/server.py @@ -2,6 +2,7 @@ import logging import os import signal +import socket import subprocess import sys import tempfile @@ -15,6 +16,22 @@ logger = logging.getLogger("unoserver") +class XMLRPCServer(xmlrpc.server.SimpleXMLRPCServer): + def __init__( + self, + addr: tuple[str, int], + allow_none: bool = False, + ) -> None: + addr_info = socket.getaddrinfo(addr[0], addr[1], proto=socket.IPPROTO_TCP) + + if len(addr_info) == 0: + raise RuntimeError(f"Could not get interface information for {addr[0]}:{addr[1]}") + + self.address_family = addr_info[0][0] + self.socket_type = addr_info[0][1] + super().__init__(addr=addr_info[0][4], allow_none=allow_none) + + class UnoServer: def __init__( self, @@ -84,7 +101,7 @@ def signal_handler(signum, frame): def serve(self): # Create server - with xmlrpc.server.SimpleXMLRPCServer( + with XMLRPCServer( (self.interface, int(self.port)), allow_none=True ) as server: self.xmlrcp_server = server diff --git a/tests/test_server.py b/tests/test_server.py index ec3e854..37f87b9 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -26,3 +26,23 @@ def test_server_params(popen_mock, thread_mock): "--accept=socket,host=127.0.0.1,port=2202,tcpNoDelay=1;urp;StarOffice.ComponentContext", ] ) + +@mock.patch("threading.Thread") +@mock.patch("subprocess.Popen") +def test_server_ipv6_params(popen_mock, thread_mock): + srv = server.UnoServer(interface="::", port="2203", uno_port="2202") + srv.start() + popen_mock.assert_called_with( + [ + "libreoffice", + "--headless", + "--invisible", + "--nocrashreport", + "--nodefault", + "--nologo", + "--nofirststartwizard", + "--norestore", + f"-env:UserInstallation={srv.user_installation}", + "--accept=socket,host=127.0.0.1,port=2202,tcpNoDelay=1;urp;StarOffice.ComponentContext", + ] + )