Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add MSC4040 matrix-fed service lookups (#16137)
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Sep 5, 2023
1 parent c9cec2d commit b1d71c6
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 34 deletions.
1 change: 1 addition & 0 deletions changelog.d/16137.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).
12 changes: 12 additions & 0 deletions scripts-dev/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ 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-fed", "tcp", server_name)[0]
print(
f"SRV lookup on _matrix-fed._tcp.{server_name} gave {srv}",
file=sys.stderr,
)
return srv.host, srv.port, server_name
except Exception:
pass
# Fall back to deprecated `matrix` service
try:
srv = srvlookup.lookup("matrix", "tcp", server_name)[0]
print(
Expand All @@ -337,6 +348,7 @@ def _lookup(server_name: str) -> Tuple[str, int, str]:
)
return srv.host, srv.port, server_name
except Exception:
# Fall even further back to just port 8448
return server_name, 8448, server_name

@staticmethod
Expand Down
29 changes: 24 additions & 5 deletions synapse/http/federation/matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,34 @@ async def _resolve_server(self) -> List[Server]:
if port or _is_ip_literal(host):
return [Server(host, port or 8448)]

# Check _matrix-fed._tcp SRV record.
logger.debug("Looking up SRV record for %s", host.decode(errors="replace"))
server_list = await self._srv_resolver.resolve_service(
b"_matrix-fed._tcp." + host
)

if server_list:
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
"Got %s from SRV lookup for %s",
", ".join(map(str, server_list)),
host.decode(errors="replace"),
)
return server_list

# No _matrix-fed._tcp SRV record, fallback to legacy _matrix._tcp SRV record.
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 SRV lookup for %s",
", ".join(map(str, server_list)),
host.decode(errors="replace"),
)
if logger.isEnabledFor(logging.DEBUG):
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
Expand Down
Loading

0 comments on commit b1d71c6

Please sign in to comment.