about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverResolverService.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-11-23 05:42:33 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-11-23 05:42:33 +0100
commit3e934eee892f69a8f78b94950993000522702769 (patch)
tree6aa0d3d26c9a07a7a3e097fe28abb785400bfbd6 /LibMatrix/Services/HomeserverResolverService.cs
parentAdd license retroactively, matching where the code originated from (MatrixRoo... (diff)
downloadLibMatrix-3e934eee892f69a8f78b94950993000522702769.tar.xz
Moderation bot work
Diffstat (limited to 'LibMatrix/Services/HomeserverResolverService.cs')
-rw-r--r--LibMatrix/Services/HomeserverResolverService.cs14
1 files changed, 9 insertions, 5 deletions
diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs
index c8b6bb7..556bf86 100644
--- a/LibMatrix/Services/HomeserverResolverService.cs
+++ b/LibMatrix/Services/HomeserverResolverService.cs
@@ -7,7 +7,9 @@ using Microsoft.Extensions.Logging;
 namespace LibMatrix.Services;
 
 public class HomeserverResolverService(ILogger<HomeserverResolverService>? logger = null) {
-    private readonly MatrixHttpClient _httpClient = new();
+    private readonly MatrixHttpClient _httpClient = new() {
+        Timeout = TimeSpan.FromMilliseconds(10000)
+    };
 
     private static readonly ConcurrentDictionary<string, WellKnownUris> _wellKnownCache = new();
     private static readonly ConcurrentDictionary<string, SemaphoreSlim> _wellKnownSemaphores = new();
@@ -20,7 +22,7 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge
             _wellKnownSemaphores[homeserver].Release();
             return known;
         }
-        
+
         logger?.LogInformation("Resolving homeserver: {}", homeserver);
         var res = new WellKnownUris {
             Client = await _tryResolveFromClientWellknown(homeserver),
@@ -33,11 +35,12 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge
 
     private async Task<string?> _tryResolveFromClientWellknown(string homeserver) {
         if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
-        if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client")) {
+        try {
             var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/client");
             var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString();
             return hs;
         }
+        catch { }
 
         logger?.LogInformation("No client well-known...");
         return null;
@@ -45,13 +48,14 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge
 
     private async Task<string?> _tryResolveFromServerWellknown(string homeserver) {
         if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
-        if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server")) {
+        try {
             var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server");
             var hs = resp.GetProperty("m.server").GetString();
             if (!hs.StartsWithAnyOf("http://", "https://"))
                 hs = $"https://{hs}";
             return hs;
         }
+        catch { }
 
         // fallback: most servers host these on the same location
         var clientUrl = await _tryResolveFromClientWellknown(homeserver);
@@ -74,4 +78,4 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge
         public string? Client { get; set; }
         public string? Server { get; set; }
     }
-}
+}
\ No newline at end of file