diff --git a/changelog.d/576.feature b/changelog.d/576.feature new file mode 100644 index 00000000..bba6f161 --- /dev/null +++ b/changelog.d/576.feature @@ -0,0 +1 @@ +Support resolving homeservers using `matrix-fed` DNS SRV records from [MSC4040](https://github.com/matrix-org/matrix-spec-proposals/pull/4040). diff --git a/sydent/http/matrixfederationagent.py b/sydent/http/matrixfederationagent.py index f226b67c..81da6418 100644 --- a/sydent/http/matrixfederationagent.py +++ b/sydent/http/matrixfederationagent.py @@ -273,28 +273,42 @@ async def _route_matrix_uri( res = await self._route_matrix_uri(new_uri, lookup_well_known=False) return res - # try a SRV lookup - service_name = b"_matrix._tcp.%s" % (parsed_uri.host,) + # Look up SRV for Matrix 1.8 `matrix-fed` service first + service_name = b"_matrix-fed._tcp.%s" % (parsed_uri.host,) server_list = await self._srv_resolver.resolve_service(service_name) - - if not server_list: - target_host = parsed_uri.host - port = 8448 - logger.debug( - "No SRV record for %s, using %s:%i", - parsed_uri.host.decode("ascii"), - target_host.decode("ascii"), - port, - ) - else: + if server_list: target_host, port = pick_server_from_list(server_list) logger.debug( - "Picked %s:%i from SRV records for %s", + "Picked %s:%i from _matrix-fed SRV records for %s", target_host.decode("ascii"), port, parsed_uri.host.decode("ascii"), ) + else: + # Fall back to deprecated `matrix` service + service_name = b"_matrix._tcp.%s" % (parsed_uri.host,) + server_list = await self._srv_resolver.resolve_service(service_name) + + # Fall even further back to just port 8448 + if not server_list: + target_host = parsed_uri.host + port = 8448 + logger.debug( + "No SRV record for %s, using %s:%i", + parsed_uri.host.decode("ascii"), + target_host.decode("ascii"), + port, + ) + else: + target_host, port = pick_server_from_list(server_list) + logger.debug( + "Picked %s:%i from _matrix SRV records for %s", + target_host.decode("ascii"), + port, + parsed_uri.host.decode("ascii"), + ) + return _RoutingResult( host_header=parsed_uri.netloc, tls_server_name=parsed_uri.host,