diff --git a/duniterpy/api/endpoint.py b/duniterpy/api/endpoint.py index 3ef147080a5b3aa780fb5c5bff22c9a89cfea42c..7d57eb0dc93f1b7bf6c82d74817b06a7a2aba8f4 100644 --- a/duniterpy/api/endpoint.py +++ b/duniterpy/api/endpoint.py @@ -14,7 +14,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import re -from typing import Any, Dict, Optional, Type, TypeVar +from ipaddress import ip_address +from typing import Any, Dict, Optional, Tuple, Type, TypeVar from duniterpy import constants as const @@ -171,10 +172,10 @@ class BMAEndpoint(Endpoint): m = BMAEndpoint.re_inline.match(inline) if m is None: raise MalformedDocumentError(BMAEndpoint.API) - server = m["host"] - ipv4 = m["ipv4"] + server, ipv4 = fix_host_ipv4_mix_up(m["host"], m["ipv4"]) ipv6 = m["ipv6"] port = int(m["port"]) + return cls(server, ipv4, ipv6, port) def inline(self) -> str: @@ -265,11 +266,11 @@ class SecuredBMAEndpoint(BMAEndpoint): m = SecuredBMAEndpoint.re_inline.match(inline) if m is None: raise MalformedDocumentError(SecuredBMAEndpoint.API) - server = m["host"] - ipv4 = m["ipv4"] + server, ipv4 = fix_host_ipv4_mix_up(m["host"], m["ipv4"]) ipv6 = m["ipv6"] port = int(m["port"]) path = m["path"] + if not path: path = "" return cls(server, ipv4, ipv6, port, path) @@ -619,11 +620,11 @@ class GVAEndpoint(Endpoint): if m is None: raise MalformedDocumentError(cls.API) flags = m["flags"] - server = m["host"] - ipv4 = m["ipv4"] + server, ipv4 = fix_host_ipv4_mix_up(m["host"], m["ipv4"]) ipv6 = m["ipv6"] port = int(m["port"]) path = m["path"] + if not flags: flags = "" if not path: @@ -744,3 +745,12 @@ def endpoint(value: Any) -> Any: raise TypeError("Cannot convert {0} to endpoint".format(value)) return result + + +def fix_host_ipv4_mix_up(host: str, ipv4: str) -> Tuple[str, str]: + mixed_up = False + try: + mixed_up = ip_address(host).version == 4 and not ipv4 + except ValueError: + pass + return ("", host) if mixed_up else (host, ipv4) diff --git a/tests/api/test_endpoints.py b/tests/api/test_endpoints.py index 75dc7a61931f3fd2c58b82bbb62b13ebbc10bea5..7198759eca0e7b4152b86b7cdb3f2e160b2e7910 100644 --- a/tests/api/test_endpoints.py +++ b/tests/api/test_endpoints.py @@ -87,3 +87,15 @@ class TestEndpoint(unittest.TestCase): self.assertEqual(gvasub_endpoint.path, "gva") assert gvasub_endpoint.inline(), endpoint_str + + def test_gva_host_ipv4_mix_up(self): + endpoint_str = "GVA S 127.0.0.1 443 gva" + gva_endpoint = endpoint.GVAEndpoint.from_inline(endpoint_str) + self.assertEqual(gva_endpoint.server, "") + self.assertEqual(gva_endpoint.ipv4, "127.0.0.1") + + def test_bmas_host_ipv4_mix_up(self): + endpoint_str = "BMAS 127.0.0.1 443 bma" + bmas_endpoint = endpoint.SecuredBMAEndpoint.from_inline(endpoint_str) + self.assertEqual(bmas_endpoint.server, "") + self.assertEqual(bmas_endpoint.ipv4, "127.0.0.1")