Skip to content

Commit

Permalink
Add MSC4040 matrix-fed service lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Sep 5, 2023
1 parent 77cb99e commit 7af2a44
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions sydent/http/matrixfederationagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7af2a44

Please sign in to comment.