From b933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Thu, 4 May 2023 00:13:25 +0200 Subject: Refactoring --- MatrixRoomUtils.Core/Interfaces/IHomeServer.cs | 57 +++++++++++++++++++++----- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'MatrixRoomUtils.Core/Interfaces/IHomeServer.cs') diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs index 84714f7..438709f 100644 --- a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs +++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs @@ -1,8 +1,8 @@ using System.Net.Http.Json; using System.Text.Json; -using MatrixRoomUtils.Extensions; +using MatrixRoomUtils.Core.Extensions; -namespace MatrixRoomUtils; +namespace MatrixRoomUtils.Core.Interfaces; public class IHomeServer { @@ -10,8 +10,21 @@ public class IHomeServer public string FullHomeServerDomain { get; set; } private protected HttpClient _httpClient { get; set; } = new(); + public async Task ResolveHomeserverFromWellKnown(string homeserver) { + if (RuntimeCache.HomeserverResolutionCache.ContainsKey(homeserver)) + { + if (RuntimeCache.HomeserverResolutionCache[homeserver].ResolutionTime < DateTime.Now.AddHours(1)) + { + Console.WriteLine($"Found cached homeserver: {RuntimeCache.HomeserverResolutionCache[homeserver].Result}"); + return RuntimeCache.HomeserverResolutionCache[homeserver].Result; + } + RuntimeCache.HomeserverResolutionCache.Remove(homeserver); + } + //throw new NotImplementedException(); + + string result = null; Console.WriteLine($"Resolving homeserver: {homeserver}"); if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client")) @@ -20,18 +33,40 @@ public class IHomeServer var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/client"); Console.WriteLine($"Response: {resp.ToString()}"); var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); - return hs; + result = hs; } - Console.WriteLine($"No client well-known..."); - if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server")) + else + { + Console.WriteLine($"No client well-known..."); + if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server")) + { + var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); + var hs = resp.GetProperty("m.server").GetString(); + result = hs; + } + else + { + Console.WriteLine($"No server well-known..."); + if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) result = homeserver; + else + { + Console.WriteLine("No homeserver on shortname..."); + if (await _httpClient.CheckSuccessStatus($"{homeserver.Replace("//", "//matrix.")}/_matrix/client/versions")) result = homeserver.Replace("//", "//matrix."); + else Console.WriteLine($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!"); + } + } + } + + if (result != null) { - var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); - var hs = resp.GetProperty("m.server").GetString(); - return hs; + Console.WriteLine($"Resolved homeserver: {homeserver} -> {result}"); + RuntimeCache.HomeserverResolutionCache.TryAdd(homeserver, new() + { + Result = result, + ResolutionTime = DateTime.Now + }); + return result; } - Console.WriteLine($"No server well-known..."); - if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) return homeserver; - Console.WriteLine($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!"); throw new InvalidDataException($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!"); } } \ No newline at end of file -- cgit 1.4.1