From 3dfb7b81b0fe19d37a7bf1183e248ca10c56277c Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 23 Feb 2024 11:23:27 +0000 Subject: HS emulator --- LibMatrix/Homeservers/RemoteHomeServer.cs | 2 +- LibMatrix/MatrixException.cs | 2 +- LibMatrix/Services/HomeserverResolverService.cs | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) (limited to 'LibMatrix') diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index 5c7d254..422a8a9 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -35,7 +35,7 @@ public class RemoteHomeserver(string baseUrl) { homeserver.ClientHttpClient = new MatrixHttpClient { BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Client ?? throw new InvalidOperationException($"Failed to resolve homeserver client URI for {baseUrl}")), - Timeout = TimeSpan.FromSeconds(120) + Timeout = TimeSpan.FromSeconds(300) }; homeserver.FederationClient = await FederationClient.TryCreate(baseUrl, proxy); diff --git a/LibMatrix/MatrixException.cs b/LibMatrix/MatrixException.cs index 86dbce4..8ec8fd5 100644 --- a/LibMatrix/MatrixException.cs +++ b/LibMatrix/MatrixException.cs @@ -20,7 +20,7 @@ public class MatrixException : Exception { public string RawContent { get; set; } - public object GetAsObject() => new { ErrorCode, Error, SoftLogout, RetryAfterMs }; + public object GetAsObject() => new { errcode = ErrorCode, error = Error, soft_logout = SoftLogout, retry_after_ms = RetryAfterMs }; public string GetAsJson() => GetAsObject().ToJson(ignoreNull: true); public override string Message => diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index a4a18e5..bcef541 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -34,7 +34,14 @@ public class HomeserverResolverService(ILogger? logge } private async Task _tryResolveFromClientWellknown(string homeserver) { - if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; + if (!homeserver.StartsWith("http")) { + if (await _httpClient.CheckSuccessStatus($"https://{homeserver}/.well-known/matrix/client")) + homeserver = "https://" + homeserver; + else if (await _httpClient.CheckSuccessStatus($"http://{homeserver}/.well-known/matrix/client")) { + homeserver = "http://" + homeserver; + } + } + try { var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/client"); var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); @@ -49,7 +56,14 @@ public class HomeserverResolverService(ILogger? logge } private async Task _tryResolveFromServerWellknown(string homeserver) { - if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; + if (!homeserver.StartsWith("http")) { + if (await _httpClient.CheckSuccessStatus($"https://{homeserver}/.well-known/matrix/server")) + homeserver = "https://" + homeserver; + else if (await _httpClient.CheckSuccessStatus($"http://{homeserver}/.well-known/matrix/server")) { + homeserver = "http://" + homeserver; + } + } + try { var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); var hs = resp.GetProperty("m.server").GetString(); -- cgit 1.4.1