From 740f742d6993900355a2978fe08ed599e4c543c5 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Tue, 22 Aug 2023 08:32:30 -0600 Subject: [PATCH] Support MSC4040 SRV lookups (#411) For https://github.com/matrix-org/matrix-federation-tester/pull/142 See https://github.com/matrix-org/matrix-spec/pull/1624 Co-authored-by: devonh --- fclient/resolve.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/fclient/resolve.go b/fclient/resolve.go index d0f1a9d2..66c0c8cf 100644 --- a/fclient/resolve.go +++ b/fclient/resolve.go @@ -108,7 +108,7 @@ func resolveServer(ctx context.Context, serverName spec.ServerName, checkWellKno // well as 3.3 and 3.4) func handleNoWellKnown(ctx context.Context, serverName spec.ServerName) (results []ResolutionResult) { // 4. If the /.well-known request resulted in an error response - _, records, err := net.DefaultResolver.LookupSRV(ctx, "matrix", "tcp", string(serverName)) + records, err := lookupSRV(ctx, serverName) if err == nil && len(records) > 0 { for _, rec := range records { // If the domain is a FQDN, remove the trailing dot at the end. This @@ -142,3 +142,24 @@ func handleNoWellKnown(ctx context.Context, serverName spec.ServerName) (results return } + +func lookupSRV(ctx context.Context, serverName spec.ServerName) ([]*net.SRV, error) { + // Check matrix-fed service first, as of Matrix 1.8 + _, records, err := net.DefaultResolver.LookupSRV(ctx, "matrix-fed", "tcp", string(serverName)) + if err != nil { + if dnserr, ok := err.(*net.DNSError); ok { + if !dnserr.IsNotFound { + // not found errors are expected, but everything else is very much not + return records, err + } + } else { + return records, err + } + } else { + return records, nil // we got a hit on the matrix-fed service, so use that + } + + // we didn't get a hit on matrix-fed, so try deprecated matrix service + _, records, err = net.DefaultResolver.LookupSRV(ctx, "matrix", "tcp", string(serverName)) + return records, err // we don't need to process this here +}