about summary refs log tree commit diff
diff options
context:
space:
mode:
m---------ArcaneLibs0
-rw-r--r--LibMatrix/Extensions/MatrixHttpClient.Single.cs12
-rw-r--r--LibMatrix/Services/ServiceInstaller.cs7
-rw-r--r--LibMatrix/Services/WellKnownResolverService.cs173
-rw-r--r--LibMatrix/Services/WellKnownResolvers/ClientWellKnownResolver.cs53
-rw-r--r--LibMatrix/Services/WellKnownResolvers/SupportWellKnownResolver.cs54
-rw-r--r--Tests/LibMatrix.Tests/Config.cs2
-rw-r--r--Tests/LibMatrix.Tests/Tests/HomeserverResolverTests/ClientWellKnownResolverTests.cs41
-rw-r--r--Tests/LibMatrix.Tests/Tests/LegacyHomeserverResolverTests.cs (renamed from Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs)4
9 files changed, 214 insertions, 132 deletions
diff --git a/ArcaneLibs b/ArcaneLibs
-Subproject 9010841d686cfce2e55c3a4d0251d93d3546e8b
+Subproject 209108f67cddce1b7b94eabcd8ab38805bcfe3e
diff --git a/LibMatrix/Extensions/MatrixHttpClient.Single.cs b/LibMatrix/Extensions/MatrixHttpClient.Single.cs

index 56925fb..b42d94e 100644 --- a/LibMatrix/Extensions/MatrixHttpClient.Single.cs +++ b/LibMatrix/Extensions/MatrixHttpClient.Single.cs
@@ -76,7 +76,8 @@ public class MatrixHttpClient { Console.WriteLine($"Sending {request.Method} {BaseAddress}{request.RequestUri} ({Util.BytesToString(request.GetContentLength())})"); if (request.RequestUri is null) throw new NullReferenceException("RequestUri is null"); - if (!request.RequestUri.IsAbsoluteUri) request.RequestUri = new Uri(BaseAddress ?? throw new InvalidOperationException("Relative URI passed, but no BaseAddress is specified!"), request.RequestUri); + if (!request.RequestUri.IsAbsoluteUri) + request.RequestUri = new Uri(BaseAddress ?? throw new InvalidOperationException("Relative URI passed, but no BaseAddress is specified!"), request.RequestUri); foreach (var (key, value) in AdditionalQueryParameters) request.RequestUri = request.RequestUri.AddQuery(key, value); foreach (var (key, value) in DefaultRequestHeaders) { if (request.Headers.Contains(key)) continue; @@ -90,8 +91,9 @@ public class MatrixHttpClient { responseMessage = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); } catch (Exception e) { - Console.WriteLine( - $"Failed to send request {request.Method} {BaseAddress}{request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)}):\n{e}"); + if (!e.Message.StartsWith("TypeError: NetworkError")) + Console.WriteLine( + $"Failed to send request {request.Method} {BaseAddress}{request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)}):\n{e}"); throw; } #if SYNC_HTTPCLIENT @@ -109,13 +111,13 @@ public class MatrixHttpClient { public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) { var responseMessage = await SendUnhandledAsync(request, cancellationToken); if (responseMessage.IsSuccessStatusCode) return responseMessage; - + //retry on gateway timeout if (responseMessage.StatusCode == HttpStatusCode.GatewayTimeout) { request.ResetSendStatus(); return await SendAsync(request, cancellationToken); } - + //error handling var content = await responseMessage.Content.ReadAsStringAsync(cancellationToken); if (content.Length == 0) diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs
index 8b7e54b..ecc3f09 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs
@@ -1,5 +1,5 @@ +using LibMatrix.Services.WellKnownResolvers; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; namespace LibMatrix.Services; @@ -9,7 +9,10 @@ public static class ServiceInstaller { services.AddSingleton(config ?? new RoryLibMatrixConfiguration()); //Add services - services.AddSingleton<HomeserverResolverService>(sp => new HomeserverResolverService(sp.GetRequiredService<ILogger<HomeserverResolverService>>())); + services.AddSingleton<ClientWellKnownResolver>(); + services.AddSingleton<WellKnownResolverService>(); + // Legacy + services.AddSingleton<HomeserverResolverService>(); services.AddSingleton<HomeserverProviderService>(); return services; diff --git a/LibMatrix/Services/WellKnownResolverService.cs b/LibMatrix/Services/WellKnownResolverService.cs
index 0ba79b6..ab2660f 100644 --- a/LibMatrix/Services/WellKnownResolverService.cs +++ b/LibMatrix/Services/WellKnownResolverService.cs
@@ -3,6 +3,7 @@ using System.Text.Json.Serialization; using ArcaneLibs.Collections; using ArcaneLibs.Extensions; using LibMatrix.Extensions; +using LibMatrix.Services.WellKnownResolvers; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -11,11 +12,6 @@ namespace LibMatrix.Services; public class WellKnownResolverService { private readonly MatrixHttpClient _httpClient = new(); - private static readonly SemaphoreCache<ClientWellKnown> ClientWellKnownCache = new(); - private static readonly SemaphoreCache<ServerWellKnown> ServerWellKnownCache = new(); - // private static readonly SemaphoreCache<support> SupportWellKnownCache = new(); - // private static readonly SemaphoreCache<WellKnownRecords> WellKnownCache = new(); - private readonly ILogger<WellKnownResolverService> _logger; public WellKnownResolverService(ILogger<WellKnownResolverService> logger) { @@ -29,131 +25,64 @@ public class WellKnownResolverService { public async Task<WellKnownRecords> TryResolveWellKnownRecords(string homeserver) { WellKnownRecords records = new(); - records.ClientWellKnown = await ClientWellKnownCache.TryGetOrAdd(homeserver, async () => { - _logger.LogTrace($"Resolving client well-known: {homeserver}"); - ClientWellKnown? clientWellKnown = null; - // check if homeserver has a client well-known - try { - - var wk = await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); - } - catch { } - - // return clientWellKnown; - - _logger.LogInformation("No client well-known for {server}...", homeserver); - return null; - }); + _logger.LogDebug($"Resolving well-knowns for {homeserver}"); + return records; - } - - // public async Task<WellKnownUris> ResolveHomeserverFromWellKnown(string homeserver, bool enableClient = true, bool enableServer = true) { - // ArgumentNullException.ThrowIfNull(homeserver); - // - // return await WellKnownCache.GetOrAdd(homeserver, async () => { - // _logger.LogTrace($"Resolving homeserver well-knowns: {homeserver}"); - // var client = enableClient ? _tryResolveClientEndpoint(homeserver) : null; - // var server = enableServer ? _tryResolveServerEndpoint(homeserver) : null; - // - // var res = new WellKnownUris(); - // - // if (client != null) - // res.Client = (await client)?.TrimEnd('/') ?? throw new Exception($"Could not resolve client URL for {homeserver}."); - // - // if (server != null) - // res.Server = (await server)?.TrimEnd('/') ?? throw new Exception($"Could not resolve server URL for {homeserver}."); - // - // _logger.LogInformation("Resolved well-knowns for {hs}: {json}", homeserver, res.ToJson(indent: false)); - // return res; - // }); - // } - // - // private async Task<string?> _tryResolveClientEndpoint(string homeserver) { - // ArgumentNullException.ThrowIfNull(homeserver); - // _logger.LogTrace("Resolving client well-known: {homeserver}", homeserver); - // ClientWellKnown? clientWellKnown = null; - // // check if homeserver has a client well-known - // if (homeserver.StartsWith("https://")) { - // clientWellKnown = await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); - // } - // else if (homeserver.StartsWith("http://")) { - // clientWellKnown = await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"{homeserver}/.well-known/matrix/client"); - // } - // else { - // clientWellKnown ??= await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"https://{homeserver}/.well-known/matrix/client"); - // clientWellKnown ??= await _httpClient.TryGetFromJsonAsync<ClientWellKnown>($"http://{homeserver}/.well-known/matrix/client"); - // - // if (clientWellKnown is null) { - // if (await _httpClient.CheckSuccessStatus($"https://{homeserver}/_matrix/client/versions")) - // return $"https://{homeserver}"; - // if (await _httpClient.CheckSuccessStatus($"http://{homeserver}/_matrix/client/versions")) - // return $"http://{homeserver}"; - // } - // } - // - // if (!string.IsNullOrWhiteSpace(clientWellKnown?.Homeserver.BaseUrl)) - // return clientWellKnown.Homeserver.BaseUrl; - // - // _logger.LogInformation("No client well-known for {server}...", homeserver); - // return null; - // } - // - // private async Task<string?> _tryResolveServerEndpoint(string homeserver) { - // // TODO: implement SRV delegation via DoH: https://developers.google.com/speed/public-dns/docs/doh/json - // ArgumentNullException.ThrowIfNull(homeserver); - // _logger.LogTrace($"Resolving server well-known: {homeserver}"); - // ServerWellKnown? serverWellKnown = null; - // // check if homeserver has a server well-known - // if (homeserver.StartsWith("https://")) { - // serverWellKnown = await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"{homeserver}/.well-known/matrix/server"); - // } - // else if (homeserver.StartsWith("http://")) { - // serverWellKnown = await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"{homeserver}/.well-known/matrix/server"); - // } - // else { - // serverWellKnown ??= await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"https://{homeserver}/.well-known/matrix/server"); - // serverWellKnown ??= await _httpClient.TryGetFromJsonAsync<ServerWellKnown>($"http://{homeserver}/.well-known/matrix/server"); - // } - // - // _logger.LogInformation("Server well-known for {hs}: {json}", homeserver, serverWellKnown?.ToJson() ?? "null"); - // - // if (!string.IsNullOrWhiteSpace(serverWellKnown?.Homeserver)) { - // var resolved = serverWellKnown.Homeserver; - // if (resolved.StartsWith("https://") || resolved.StartsWith("http://")) - // return resolved; - // if (await _httpClient.CheckSuccessStatus($"https://{resolved}/_matrix/federation/v1/version")) - // return $"https://{resolved}"; - // if (await _httpClient.CheckSuccessStatus($"http://{resolved}/_matrix/federation/v1/version")) - // return $"http://{resolved}"; - // _logger.LogWarning("Server well-known points to invalid server: {resolved}", resolved); - // } - // - // // fallback: most servers host C2S and S2S on the same domain - // var clientUrl = await _tryResolveClientEndpoint(homeserver); - // if (clientUrl is not null && await _httpClient.CheckSuccessStatus($"{clientUrl}/_matrix/federation/v1/version")) - // return clientUrl; - // - // _logger.LogInformation("No server well-known for {server}...", homeserver); - // return null; - // } + } - public class ClientWellKnown { - [JsonPropertyName("m.homeserver")] - public WellKnownHomeserver Homeserver { get; set; } - public class WellKnownHomeserver { - [JsonPropertyName("base_url")] - public string BaseUrl { get; set; } - } - } public class ServerWellKnown { [JsonPropertyName("m.server")] - public string Homeserver { get; set; } + public required string Homeserver { get; set; } } - + public class WellKnownRecords { - public ClientWellKnown? ClientWellKnown { get; set; } + public ClientWellKnownResolver.ClientWellKnown? ClientWellKnown { get; set; } public ServerWellKnown? ServerWellKnown { get; set; } + public SupportWellKnownResolver.SupportWellKnown? SupportWellKnown { get; set; } + + /// <summary> + /// Reports the source of the client well-known data. + /// </summary> + public WellKnownSource? ClientWellKnownSource { get; set; } + + /// <summary> + /// Reports the source of the server well-known data. + /// </summary> + public WellKnownSource? ServerWellKnownSource { get; set; } + + /// <summary> + /// Reports the source of the support well-known data. + /// </summary> + public WellKnownSource? SupportWellKnownSource { get; set; } + } + + public struct WellKnownResolutionResult<T> { + public WellKnownResolverService.WellKnownSource Source { get; set; } + public T WellKnown { get; set; } + public List<WellKnownResolverService.WellKnownResolutionWarning> Warnings { get; set; } + } + + public enum WellKnownSource { + None, + Https, + Dns, + Http, + ManualCheck, + Search + } + + public struct WellKnownResolutionWarning { + public WellKnownResolutionWarningType Type { get; set; } + public string Message { get; set; } + public Exception? Exception { get; set; } + + public enum WellKnownResolutionWarningType { + None, + Exception, + InvalidResponse, + Timeout + } } } \ No newline at end of file diff --git a/LibMatrix/Services/WellKnownResolvers/ClientWellKnownResolver.cs b/LibMatrix/Services/WellKnownResolvers/ClientWellKnownResolver.cs new file mode 100644
index 0000000..d4d0166 --- /dev/null +++ b/LibMatrix/Services/WellKnownResolvers/ClientWellKnownResolver.cs
@@ -0,0 +1,53 @@ +using System.Text.Json.Serialization; +using ArcaneLibs.Collections; +using LibMatrix.Extensions; +using Microsoft.Extensions.Logging; + +namespace LibMatrix.Services.WellKnownResolvers; + +public class ClientWellKnownResolver(ILogger<ClientWellKnownResolver> logger) { + private static readonly SemaphoreCache<WellKnownResolutionResult> ClientWellKnownCache = new() { + StoreNulls = false + }; + private static readonly MatrixHttpClient HttpClient = new(); + + public Task<WellKnownResolutionResult> TryResolveClientWellKnown(string homeserver) { + return ClientWellKnownCache.TryGetOrAdd(homeserver, async () => { + logger.LogTrace($"Resolving client well-known: {homeserver}"); + if ((await TryGetClientWellKnownFromHttps(homeserver)) is { } clientWellKnown) + return new() { + Source = WellKnownResolverService.WellKnownSource.Https, + WellKnown = clientWellKnown + }; + + return default!; + }); + } + + private async Task<ClientWellKnown?> TryGetClientWellKnownFromHttps(string homeserver) { + try { + return await HttpClient.TryGetFromJsonAsync<ClientWellKnown>($"https://{homeserver}/.well-known/matrix/client"); + } + catch { + return null; + } + } + + + + public class ClientWellKnown { + [JsonPropertyName("m.homeserver")] + public required WellKnownHomeserver Homeserver { get; set; } + + public class WellKnownHomeserver { + [JsonPropertyName("base_url")] + public required string BaseUrl { get; set; } + } + } + + public struct WellKnownResolutionResult { + public WellKnownResolverService.WellKnownSource Source { get; set; } + public ClientWellKnown WellKnown { get; set; } + public List<WellKnownResolverService.WellKnownResolutionWarning> Warnings { get; set; } + } +} \ No newline at end of file diff --git a/LibMatrix/Services/WellKnownResolvers/SupportWellKnownResolver.cs b/LibMatrix/Services/WellKnownResolvers/SupportWellKnownResolver.cs new file mode 100644
index 0000000..1d7567a --- /dev/null +++ b/LibMatrix/Services/WellKnownResolvers/SupportWellKnownResolver.cs
@@ -0,0 +1,54 @@ +using System.Text.Json.Serialization; +using ArcaneLibs.Collections; +using LibMatrix.Extensions; +using Microsoft.Extensions.Logging; + +namespace LibMatrix.Services.WellKnownResolvers; + +public class SupportWellKnownResolver(ILogger<SupportWellKnownResolver> logger) { + private static readonly SemaphoreCache<WellKnownResolverService.WellKnownResolutionResult<SupportWellKnown>> ClientWellKnownCache = new() { + StoreNulls = false + }; + + private static readonly MatrixHttpClient HttpClient = new(); + + public Task<WellKnownResolverService.WellKnownResolutionResult<SupportWellKnown>> TryResolveClientWellKnown(string homeserver) { + return ClientWellKnownCache.TryGetOrAdd(homeserver, async () => { + logger.LogTrace($"Resolving client well-known: {homeserver}"); + if ((await TryGetClientWellKnownFromHttps(homeserver)) is { } clientWellKnown) + return new() { + Source = WellKnownResolverService.WellKnownSource.Https, + WellKnown = clientWellKnown + }; + return default!; + }); + } + + private async Task<SupportWellKnown?> TryGetClientWellKnownFromHttps(string homeserver) { + try { + return await HttpClient.TryGetFromJsonAsync<SupportWellKnown>($"https://{homeserver}/.well-known/matrix/support"); + } + catch { + return null; + } + } + + public struct SupportWellKnown { + [JsonPropertyName("contacts")] + public List<WellKnownContact>? Contacts { get; set; } + + [JsonPropertyName("support_page")] + public Uri? SupportPage { get; set; } + + public class WellKnownContact { + [JsonPropertyName("email_address")] + public string? EmailAddress { get; set; } + + [JsonPropertyName("matrix_id")] + public string? MatrixId { get; set; } + + [JsonPropertyName("role")] + public required string Role { get; set; } + } + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.Tests/Config.cs b/Tests/LibMatrix.Tests/Config.cs
index 045ea40..b59b238 100644 --- a/Tests/LibMatrix.Tests/Config.cs +++ b/Tests/LibMatrix.Tests/Config.cs
@@ -18,7 +18,7 @@ public class Config { { "matrix.org", "https://matrix-client.matrix.org" }, { "rory.gay", "https://matrix.rory.gay" }, { "feline.support", "https://matrix.feline.support" }, - { "transfem.dev", "https://matrix.transfem.dev" }, + { "transfem.dev", "https://matrix.transfem.dev/" }, { "the-apothecary.club", "https://the-apothecary.club" }, { "nixos.org", "https://matrix.nixos.org" }, { "fedora.im", "https://fedora.ems.host" } diff --git a/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests/ClientWellKnownResolverTests.cs b/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests/ClientWellKnownResolverTests.cs new file mode 100644
index 0000000..f6dfd97 --- /dev/null +++ b/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests/ClientWellKnownResolverTests.cs
@@ -0,0 +1,41 @@ +using LibMatrix.Services; +using LibMatrix.Services.WellKnownResolvers; +using LibMatrix.Tests.Fixtures; +using Xunit.Abstractions; +using Xunit.Microsoft.DependencyInjection.Abstracts; + +namespace LibMatrix.Tests.Tests.HomeserverResolverTests; + +public class ClientWellKnownResolverTests : TestBed<TestFixture> { + private readonly Config _config; + private readonly ClientWellKnownResolver _resolver; + + public ClientWellKnownResolverTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) { + _config = _fixture.GetService<Config>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); + _resolver = _fixture.GetService<ClientWellKnownResolver>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverResolverService)}"); + } + + [Fact] + public async Task ResolveServerClient() { + var tasks = _config.ExpectedHomeserverClientMappings.Select(async mapping => { + var server = await _resolver.TryResolveClientWellKnown(mapping.Key); + Assert.Equal(mapping.Value, server.WellKnown.Homeserver.BaseUrl); + return server; + }).ToList(); + await Task.WhenAll(tasks); + } + + private async Task AssertClientWellKnown(string homeserver, string expected) { + var server = await _resolver.TryResolveClientWellKnown(homeserver); + Assert.Equal(expected, server.WellKnown.Homeserver.BaseUrl); + } + + [Fact] + public Task ResolveMatrixOrg() => AssertClientWellKnown("matrix.org", "https://matrix-client.matrix.org"); + + [Fact] + public Task ResolveRoryGay() => AssertClientWellKnown("rory.gay", "https://matrix.rory.gay"); + + [Fact] + public Task ResolveTransfemDev() => AssertClientWellKnown("transfem.dev", "https://matrix.transfem.dev/"); +} \ No newline at end of file diff --git a/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs b/Tests/LibMatrix.Tests/Tests/LegacyHomeserverResolverTests.cs
index 7226fe9..20dc4fb 100644 --- a/Tests/LibMatrix.Tests/Tests/HomeserverResolverTests.cs +++ b/Tests/LibMatrix.Tests/Tests/LegacyHomeserverResolverTests.cs
@@ -5,11 +5,11 @@ using Xunit.Microsoft.DependencyInjection.Abstracts; namespace LibMatrix.Tests.Tests; -public class HomeserverResolverTests : TestBed<TestFixture> { +public class LegacyHomeserverResolverTests : TestBed<TestFixture> { private readonly Config _config; private readonly HomeserverResolverService _resolver; - public HomeserverResolverTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) { + public LegacyHomeserverResolverTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : base(testOutputHelper, fixture) { _config = _fixture.GetService<Config>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); _resolver = _fixture.GetService<HomeserverResolverService>(_testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(HomeserverResolverService)}"); }