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

Add MSC4040 matrix-fed service lookups #16137

Merged
merged 13 commits into from
Sep 5, 2023
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).
17 changes: 14 additions & 3 deletions scripts-dev/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,26 @@ 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
clokep marked this conversation as resolved.
Show resolved Hide resolved
try:
srv = srvlookup.lookup("matrix", "tcp", server_name)[0]
srv = srvlookup.lookup("matrix-fed", "tcp", server_name)[0]
print(
f"SRV lookup on _matrix._tcp.{server_name} gave {srv}",
f"SRV lookup on _matrix-fed._tcp.{server_name} gave {srv}",
file=sys.stderr,
)
return srv.host, srv.port, server_name
except Exception:
return server_name, 8448, server_name
# Fall back to deprecated `matrix` service
try:
srv = srvlookup.lookup("matrix", "tcp", server_name)[0]
print(
f"SRV lookup on _matrix._tcp.{server_name} gave {srv}",
file=sys.stderr,
)
return srv.host, srv.port, server_name
except Exception:
# Fall even further back to just port 8448
return server_name, 8448, server_name
clokep marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def _get_well_known(server_name: str) -> Optional[str]:
Expand Down
17 changes: 16 additions & 1 deletion synapse/http/federation/matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ async def _resolve_server(self) -> List[Server]:
return [Server(host, port or 8448)]

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

if server_list:
clokep marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(
Expand All @@ -410,6 +412,19 @@ async def _resolve_server(self) -> List[Server]:
)
return server_list

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:
clokep marked this conversation as resolved.
Show resolved Hide resolved
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
logger.debug("No SRV records for %s", host.decode(errors="replace"))
return [Server(host, 8448)]
Expand Down
Loading