From 2e89a0717a60598904c92499adb7e01b2075fbb9 Mon Sep 17 00:00:00 2001 From: "Emma@Rory&" Date: Wed, 26 Jul 2023 21:02:50 +0200 Subject: MRU desktop start --- .../Services/HomeserverProviderService.cs | 16 +++++++++- .../Services/HomeserverResolverService.cs | 35 ++++++++++++---------- MatrixRoomUtils.Core/Services/ServiceInstaller.cs | 15 +++++----- 3 files changed, 42 insertions(+), 24 deletions(-) (limited to 'MatrixRoomUtils.Core/Services') diff --git a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs index b2ea987..4bc785a 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs @@ -23,8 +23,18 @@ public class HomeserverProviderService { $"New HomeserverProviderService created with TieredStorageService<{string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name))}>!"); } + private static Dictionary _authenticatedHomeserverSemaphore = new(); + private static Dictionary _authenticatedHomeServerCache = new(); + public async Task GetAuthenticatedWithToken(string homeserver, string accessToken, string? overrideFullDomain = null) { + SemaphoreSlim sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1)); + await sem.WaitAsync(); + if (_authenticatedHomeServerCache.ContainsKey(homeserver+accessToken)) { + sem.Release(); + return _authenticatedHomeServerCache[homeserver+accessToken]; + } + var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken); hs.FullHomeServerDomain = overrideFullDomain ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); @@ -34,6 +44,10 @@ public class HomeserverProviderService { hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync("/_matrix/client/v3/account/whoami"))!; + + _authenticatedHomeServerCache[homeserver+accessToken] = hs; + sem.Release(); + return hs; } @@ -80,4 +94,4 @@ public class HomeserverProviderService { public string User { get; set; } = ""; } } -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs index e4d8063..5856000 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs @@ -3,14 +3,15 @@ using System.Text.Json; using MatrixRoomUtils.Core.Extensions; using Microsoft.Extensions.Logging; -namespace MatrixRoomUtils.Core.Services; +namespace MatrixRoomUtils.Core.Services; public class HomeserverResolverService { private readonly MatrixHttpClient _httpClient = new(); private readonly ILogger _logger; - private static Dictionary _wellKnownCache = new(); - + private static readonly Dictionary _wellKnownCache = new(); + private static readonly Dictionary _wellKnownSemaphores = new(); + public HomeserverResolverService(ILogger logger) { _logger = logger; } @@ -23,31 +24,30 @@ public class HomeserverResolverService { } private async Task _resolveHomeserverFromWellKnown(string homeserver) { - if(homeserver is null) throw new ArgumentNullException(nameof(homeserver)); + if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); + SemaphoreSlim sem = _wellKnownSemaphores.GetOrCreate(homeserver, _ => new SemaphoreSlim(1, 1)); + await sem.WaitAsync(); if (_wellKnownCache.ContainsKey(homeserver)) { - if (_wellKnownCache[homeserver] is SemaphoreSlim s) await s.WaitAsync(); - if (_wellKnownCache[homeserver] is string p) return p; + sem.Release(); + return _wellKnownCache[homeserver]; } - _wellKnownCache[homeserver] = new SemaphoreSlim(1); + string? result = null; _logger.LogInformation($"Attempting to resolve homeserver: {homeserver}"); - if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; result ??= await _tryResolveFromClientWellknown(homeserver); result ??= await _tryResolveFromServerWellknown(homeserver); result ??= await _tryCheckIfDomainHasHomeserver(homeserver); - // if(!homeserver.Contains("http")) homeserver = "https://" + homeserver; - // result ??= await _tryCheckIfSubDomainHasHomeserver(homeserver, "matrix"); - // result ??= await _tryCheckIfSubDomainHasHomeserver(homeserver, "chat"); - if(result is not null) { + if (result is not null) { _logger.LogInformation($"Resolved homeserver: {homeserver} -> {result}"); - _wellKnownCache.TryAdd(homeserver, result); + _wellKnownCache[homeserver] = result; + sem.Release(); return result; } - + throw new InvalidDataException($"Failed to resolve homeserver for {homeserver}! Is it online and configured correctly?"); } - + private async Task _tryResolveFromClientWellknown(string homeserver) { if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client")) { @@ -55,9 +55,11 @@ public class HomeserverResolverService { var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); return hs; } + _logger.LogInformation("No client well-known..."); return null; } + private async Task _tryResolveFromServerWellknown(string homeserver) { if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server")) { @@ -65,6 +67,7 @@ public class HomeserverResolverService { var hs = resp.GetProperty("m.server").GetString(); return hs; } + _logger.LogInformation("No server well-known..."); return null; } @@ -81,4 +84,4 @@ public class HomeserverResolverService { homeserver = homeserver.Replace("https://", $"https://{subdomain}."); return await _tryCheckIfDomainHasHomeserver(homeserver); } -} \ No newline at end of file +} diff --git a/MatrixRoomUtils.Core/Services/ServiceInstaller.cs b/MatrixRoomUtils.Core/Services/ServiceInstaller.cs index ef9238d..bd5b1bd 100644 --- a/MatrixRoomUtils.Core/Services/ServiceInstaller.cs +++ b/MatrixRoomUtils.Core/Services/ServiceInstaller.cs @@ -1,9 +1,10 @@ +using MatrixRoomUtils.Core.Extensions; using Microsoft.Extensions.DependencyInjection; -namespace MatrixRoomUtils.Core.Services; +namespace MatrixRoomUtils.Core.Services; public static class ServiceInstaller { - + public static IServiceCollection AddRoryLibMatrixServices(this IServiceCollection services, RoryLibMatrixConfiguration? config = null) { //Check required services if (!services.Any(x => x.ServiceType == typeof(TieredStorageService))) @@ -15,15 +16,15 @@ public static class ServiceInstaller { services.AddSingleton(new RoryLibMatrixConfiguration()); } //Add services - services.AddScoped(); + services.AddSingleton(); services.AddSingleton(); - services.AddScoped(); + // services.AddScoped(); return services; } - - + + } public class RoryLibMatrixConfiguration { public string AppName { get; set; } = "Rory&::LibMatrix"; -} \ No newline at end of file +} -- cgit 1.5.1