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/HomeserverResolverService.cs | |
parent | Dependency injection stuff (diff) | |
download | MatrixUtils-a331eb2f118e0051c6c6744a20d6b0934c4d6d6f.tar.xz |
Update stuff
Diffstat (limited to 'MatrixRoomUtils.Core/Services/HomeserverResolverService.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Services/HomeserverResolverService.cs | 76 |
1 files changed, 76 insertions, 0 deletions
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 |