From c5ffe9d3c50ebe266522132a268b5ba84efa00b7 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sat, 19 Aug 2023 17:25:01 -0600 Subject: [PATCH] Add MSC4040 `matrix-fed` service lookups See https://github.com/matrix-org/matrix-spec/pull/1624 --- scripts-dev/federation_client.py | 17 ++++++++++++++--- .../http/federation/matrix_federation_agent.py | 13 ++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/scripts-dev/federation_client.py b/scripts-dev/federation_client.py index 5ad334b4d800..8d5d19d6d6c0 100755 --- a/scripts-dev/federation_client.py +++ b/scripts-dev/federation_client.py @@ -329,15 +329,26 @@ def _lookup(server_name: str) -> Tuple[str, int, str]: raise ValueError("Invalid host:port '%s'" % (server_name,)) return out[0], port, out[0] + # Look up SRV for Matrix 1.8 `matrix-fed` service first try: - srv = srvlookup.lookup("matrix", "tcp", server_name)[0] + srv = srvlookup.lookup("matrix-fed", "tcp", server_name)[0] print( - f"SRV lookup on _matrix._tcp.{server_name} gave {srv}", + f"SRV lookup on _matrix-fed._tcp.{server_name} gave {srv}", file=sys.stderr, ) return srv.host, srv.port, server_name except Exception: - return server_name, 8448, server_name + # Fall back to deprecated `matrix` service + try: + srv = srvlookup.lookup("matrix", "tcp", server_name)[0] + print( + f"SRV lookup on _matrix._tcp.{server_name} gave {srv}", + file=sys.stderr, + ) + return srv.host, srv.port, server_name + except Exception: + # Fall even further back to just port 8448 + return server_name, 8448, server_name @staticmethod def _get_well_known(server_name: str) -> Optional[str]: diff --git a/synapse/http/federation/matrix_federation_agent.py b/synapse/http/federation/matrix_federation_agent.py index 91a24efcd019..a3a5c0d8a1ff 100644 --- a/synapse/http/federation/matrix_federation_agent.py +++ b/synapse/http/federation/matrix_federation_agent.py @@ -400,7 +400,7 @@ async def _resolve_server(self) -> List[Server]: return [Server(host, port or 8448)] logger.debug("Looking up SRV record for %s", host.decode(errors="replace")) - server_list = await self._srv_resolver.resolve_service(b"_matrix._tcp." + host) + server_list = await self._srv_resolver.resolve_service(b"_matrix-fed._tcp." + host) if server_list: logger.debug( @@ -410,6 +410,17 @@ async def _resolve_server(self) -> List[Server]: ) return server_list + logger.debug("Looking up deprecated SRV record for %s", host.decode(errors="replace")) + server_list = await self._srv_resolver.resolve_service(b"_matrix._tcp." + host) + + if server_list: + logger.debug( + "Got %s from deprecated SRV lookup for %s", + ", ".join(map(str, server_list)), + host.decode(errors="replace"), + ) + return server_list + # No SRV records, so we fallback to host and 8448 logger.debug("No SRV records for %s", host.decode(errors="replace")) return [Server(host, 8448)]