diff options
Diffstat (limited to 'LibMatrix/Services')
-rw-r--r-- | LibMatrix/Services/HomeserverProviderService.cs | 74 | ||||
-rw-r--r-- | LibMatrix/Services/HomeserverResolverService.cs | 10 | ||||
-rw-r--r-- | LibMatrix/Services/ServiceInstaller.cs | 2 |
3 files changed, 43 insertions, 43 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 366f0ca..776c7eb 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -1,7 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json.Serialization; +using System.Threading; +using System.Threading.Tasks; +using ArcaneLibs.Extensions; using LibMatrix.Extensions; +using LibMatrix.Homeservers; using LibMatrix.Responses; using Microsoft.Extensions.Logging; @@ -22,46 +29,53 @@ public class HomeserverProviderService { } private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new(); - private static Dictionary<string, AuthenticatedHomeServer> _authenticatedHomeServerCache = new(); + private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeServerCache = new(); - public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken, - string? overrideFullDomain = null) { - var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1)); + public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken, + string? proxy = null) { + var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver + accessToken, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(); - if (_authenticatedHomeServerCache.ContainsKey(homeserver+accessToken)) { + if (_authenticatedHomeServerCache.ContainsKey(homeserver + accessToken)) { sem.Release(); - return _authenticatedHomeServerCache[homeserver+accessToken]; + return _authenticatedHomeServerCache[homeserver + accessToken]; } - var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken); - hs.FullHomeServerDomain = overrideFullDomain ?? - await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); - hs._httpClient.Dispose(); - hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) }; - hs._httpClient.Timeout = TimeSpan.FromSeconds(120); + var domain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); + var hc = new MatrixHttpClient { BaseAddress = new Uri(domain) }; + + AuthenticatedHomeserverGeneric hs; + if (true) { + hs = new AuthenticatedHomeserverMxApiExtended(_tieredStorageService, homeserver, accessToken); + } + else { + hs = new AuthenticatedHomeserverGeneric(_tieredStorageService, homeserver, accessToken); + } + + hs.FullHomeServerDomain = domain; + hs._httpClient = hc; + hs._httpClient.Timeout = TimeSpan.FromMinutes(15); hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!; - _authenticatedHomeServerCache[homeserver+accessToken] = hs; + lock(_authenticatedHomeServerCache) + _authenticatedHomeServerCache[homeserver + accessToken] = hs; sem.Release(); return hs; } - public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? overrideFullDomain = null) { + public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? proxy = null) { var hs = new RemoteHomeServer(homeserver); - hs.FullHomeServerDomain = overrideFullDomain ?? - await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); + hs.FullHomeServerDomain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); hs._httpClient.Dispose(); hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) }; hs._httpClient.Timeout = TimeSpan.FromSeconds(120); return hs; } - public async Task<LoginResponse> Login(string homeserver, string user, string password, - string? overrideFullDomain = null) { - var hs = await GetRemoteHomeserver(homeserver, overrideFullDomain); + public async Task<LoginResponse> Login(string homeserver, string user, string password, string? proxy = null) { + var hs = await GetRemoteHomeserver(homeserver, proxy); var payload = new LoginRequest { Identifier = new LoginRequest.LoginIdentifier { User = user }, Password = password @@ -70,26 +84,4 @@ public class HomeserverProviderService { var data = await resp.Content.ReadFromJsonAsync<LoginResponse>(); return data!; } - - private class LoginRequest { - [JsonPropertyName("type")] - public string Type { get; set; } = "m.login.password"; - - [JsonPropertyName("identifier")] - public LoginIdentifier Identifier { get; set; } = new(); - - [JsonPropertyName("password")] - public string Password { get; set; } = ""; - - [JsonPropertyName("initial_device_display_name")] - public string InitialDeviceDisplayName { get; set; } = "Rory&::LibMatrix"; - - public class LoginIdentifier { - [JsonPropertyName("type")] - public string Type { get; set; } = "m.id.user"; - - [JsonPropertyName("user")] - public string User { get; set; } = ""; - } - } } diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index 78c5c37..dcd0fe9 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -1,4 +1,10 @@ +using System; +using System.Collections.Generic; +using System.IO; using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using ArcaneLibs.Extensions; using LibMatrix.Extensions; using Microsoft.Extensions.Logging; @@ -26,9 +32,9 @@ public class HomeserverResolverService { if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); var sem = _wellKnownSemaphores.GetOrCreate(homeserver, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(); - if (_wellKnownCache.ContainsKey(homeserver)) { + if (_wellKnownCache.TryGetValue(homeserver, out var known)) { sem.Release(); - return _wellKnownCache[homeserver]; + return known; } string? result = null; diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index b1c98e1..9c4cdb9 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -1,3 +1,5 @@ +using System; +using System.Linq; using Microsoft.Extensions.DependencyInjection; namespace LibMatrix.Services; |