Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MSC4040 matrix-fed service lookups #576

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading