Skip to content

Commit

Permalink
Add MSC4040 matrix-fed service lookups (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Sep 12, 2023
1 parent 5bc2d72 commit 2267ee2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.d/576.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support resolving homeservers using `matrix-fed` DNS SRV records from [MSC4040](https://github.com/matrix-org/matrix-spec-proposals/pull/4040).
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 2267ee2

Please sign in to comment.