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

Support MSC4040 SRV records #427

Merged
merged 1 commit into from
Aug 20, 2023
Merged
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
43 changes: 39 additions & 4 deletions matrix/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ func GetServerApiUrl(hostname string) (string, string, error) {
return url, wkHost, nil
}

// Step 3c: if the delegated host is not an IP and doesn't have a port, start a SRV lookup and use it
// Step 3c: if the delegated host is not an IP and doesn't have a port, start a SRV lookup and use it.
// Note: we ignore errors here because the hostname will fail elsewhere.
logrus.Debug("Doing SRV on WK host ", wkHost)
_, addrs, _ := net.LookupSRV("matrix", "tcp", wkHost)
_, addrs, _ := net.LookupSRV("matrix-fed", "tcp", wkHost)
if len(addrs) > 0 {
// Trim off the trailing period if there is one (golang doesn't like this)
realAddr := addrs[0].Target
Expand All @@ -154,6 +154,24 @@ func GetServerApiUrl(hostname string) (string, string, error) {
return url, wkHost, nil
}

// Step 3d: if the delegated host is not an IP and doesn't have a port, start a DEPRECATED SRV
// lookup and use it.
// Note: we ignore errors here because the hostname will fail elsewhere.
logrus.Debug("Doing SRV on WK host ", wkHost)
_, addrs, _ = net.LookupSRV("matrix", "tcp", wkHost)
if len(addrs) > 0 {
// Trim off the trailing period if there is one (golang doesn't like this)
realAddr := addrs[0].Target
if realAddr[len(realAddr)-1:] == "." {
realAddr = realAddr[0 : len(realAddr)-1]
}
url := fmt.Sprintf("https://%s", net.JoinHostPort(realAddr, strconv.Itoa(int(addrs[0].Port))))
server := cachedServer{url, wkHost}
apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration)
logrus.Debug("Server API URL for " + hostname + " is " + url + " (WK; SRV-Deprecated)")
return url, wkHost, nil
}

// Step 3d: use the delegated host as-is
logrus.Debug("Using .well-known as-is for ", wkHost)
url := fmt.Sprintf("https://%s", net.JoinHostPort(wkHost, wkPort))
Expand All @@ -172,7 +190,7 @@ func GetServerApiUrl(hostname string) (string, string, error) {
// Step 4: try resolving a hostname using SRV records and use it
// Note: we ignore errors here because the hostname will fail elsewhere.
logrus.Debug("Doing SRV for host ", hostname)
_, addrs, _ := net.LookupSRV("matrix", "tcp", hostname)
_, addrs, _ := net.LookupSRV("matrix-fed", "tcp", hostname)
if len(addrs) > 0 {
// Trim off the trailing period if there is one (golang doesn't like this)
realAddr := addrs[0].Target
Expand All @@ -186,7 +204,24 @@ func GetServerApiUrl(hostname string) (string, string, error) {
return url, h, nil
}

// Step 5: use the target host as-is
// Step 5: try resolving a hostname using DEPRECATED SRV records and use it
// Note: we ignore errors here because the hostname will fail elsewhere.
logrus.Debug("Doing SRV for host ", hostname)
_, addrs, _ = net.LookupSRV("matrix", "tcp", hostname)
if len(addrs) > 0 {
// Trim off the trailing period if there is one (golang doesn't like this)
realAddr := addrs[0].Target
if realAddr[len(realAddr)-1:] == "." {
realAddr = realAddr[0 : len(realAddr)-1]
}
url := fmt.Sprintf("https://%s", net.JoinHostPort(realAddr, strconv.Itoa(int(addrs[0].Port))))
server := cachedServer{url, h}
apiUrlCacheInstance.Set(hostname, server, cache.DefaultExpiration)
logrus.Debug("Server API URL for " + hostname + " is " + url + " (SRV-Deprecated)")
return url, h, nil
}

// Step 6: use the target host as-is
logrus.Debug("Using host as-is: ", hostname)
url := fmt.Sprintf("https://%s", net.JoinHostPort(h, p))
server := cachedServer{url, h}
Expand Down
Loading