diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-28 10:38:45 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-28 10:38:45 +0200 |
commit | a331eb2f118e0051c6c6744a20d6b0934c4d6d6f (patch) | |
tree | a7798d94a3553106aad40507e2dc04ff9d2f9efd /MatrixRoomUtils.Core/Services | |
parent | Dependency injection stuff (diff) | |
download | MatrixUtils-a331eb2f118e0051c6c6744a20d6b0934c4d6d6f.tar.xz |
Update stuff
Diffstat (limited to 'MatrixRoomUtils.Core/Services')
3 files changed, 97 insertions, 3 deletions
diff --git a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs index 0f09a45..8a22d33 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs @@ -1,16 +1,32 @@ +using System.Net.Http.Headers; +using System.Net.Http.Json; +using MatrixRoomUtils.Core.Extensions; +using Microsoft.Extensions.Logging; namespace MatrixRoomUtils.Core.Services; public class HomeserverProviderService { private readonly TieredStorageService _tieredStorageService; + private readonly ILogger<HomeserverProviderService> _logger; + private readonly HomeserverResolverService _homeserverResolverService; - public HomeserverProviderService(TieredStorageService tieredStorageService) { + public HomeserverProviderService(TieredStorageService tieredStorageService, ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) { Console.WriteLine("Homeserver provider service instantiated!"); _tieredStorageService = tieredStorageService; - Console.WriteLine( + _logger = logger; + _homeserverResolverService = homeserverResolverService; + logger.LogDebug( $"New HomeserverProviderService created with TieredStorageService<{string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name))}>!"); } public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken) { - return await new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken).Configure(); + var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken); + hs.FullHomeServerDomain = await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); + hs._httpClient.Dispose(); + hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) }; + hs._httpClient.Timeout = TimeSpan.FromSeconds(5); + hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + + hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!; + return hs; } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs new file mode 100644 index 0000000..f6363ab --- /dev/null +++ b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs @@ -0,0 +1,76 @@ +using System.Net.Http.Json; +using System.Text.Json; +using MatrixRoomUtils.Core.Extensions; +using Microsoft.Extensions.Logging; + +namespace MatrixRoomUtils.Core.Services; + +public class HomeserverResolverService { + private readonly HttpClient _httpClient; + private readonly ILogger<HomeserverResolverService> _logger; + + public HomeserverResolverService(HttpClient httpClient, ILogger<HomeserverResolverService> logger) { + _httpClient = httpClient; + _logger = logger; + } + + public async Task<string> ResolveHomeserverFromWellKnown(string homeserver) { + var res = await _resolveHomeserverFromWellKnown(homeserver); + if (!res.StartsWith("http")) res = "https://" + res; + if (res.EndsWith(":443")) res = res.Substring(0, res.Length - 4); + return res; + } + + private async Task<string> _resolveHomeserverFromWellKnown(string homeserver) { + 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) { + _logger.LogInformation($"Resolved homeserver: {homeserver} -> {result}"); + return result; + } + + throw new InvalidDataException($"Failed to resolve homeserver for {homeserver}! Is it online and configured correctly?"); + } + + private async Task<string?> _tryResolveFromClientWellknown(string homeserver) { + if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; + if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/client")) { + var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/client"); + var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); + return hs; + } + _logger.LogInformation("No client well-known..."); + return null; + } + private async Task<string?> _tryResolveFromServerWellknown(string homeserver) { + if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; + if (await _httpClient.CheckSuccessStatus($"{homeserver}/.well-known/matrix/server")) { + var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server"); + var hs = resp.GetProperty("m.server").GetString(); + return hs; + } + _logger.LogInformation("No server well-known..."); + return null; + } + + private async Task<string?> _tryCheckIfDomainHasHomeserver(string homeserver) { + _logger.LogInformation($"Checking if {homeserver} hosts a homeserver..."); + if (await _httpClient.CheckSuccessStatus($"{homeserver}/_matrix/client/versions")) + return homeserver; + _logger.LogInformation("No homeserver on shortname..."); + return null; + } + + private async Task<string?> _tryCheckIfSubDomainHasHomeserver(string homeserver, string subdomain) { + 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 1b275c5..4a831c1 100644 --- a/MatrixRoomUtils.Core/Services/ServiceInstaller.cs +++ b/MatrixRoomUtils.Core/Services/ServiceInstaller.cs @@ -16,6 +16,8 @@ public static class ServiceInstaller { } //Add services services.AddScoped<HomeserverProviderService>(); + services.AddScoped<HomeserverResolverService>(); + services.AddScoped<HttpClient>(); return services; } |