From c2613aab129c8d1d5aba3b7ed02609059a826c84 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 16 Jul 2025 19:00:05 +0200 Subject: Federation tuff --- .../Controllers/Spec/FederationKeysController.cs | 42 ++++++++++++++++++++++ .../Spec/FederationVersionController.cs | 19 ++++++++++ .../Controllers/Spec/WellKnownController.cs | 19 ++++++++++ 3 files changed, 80 insertions(+) create mode 100644 Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationKeysController.cs create mode 100644 Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationVersionController.cs create mode 100644 Utilities/LibMatrix.FederationTest/Controllers/Spec/WellKnownController.cs (limited to 'Utilities/LibMatrix.FederationTest/Controllers/Spec') diff --git a/Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationKeysController.cs b/Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationKeysController.cs new file mode 100644 index 0000000..6516415 --- /dev/null +++ b/Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationKeysController.cs @@ -0,0 +1,42 @@ +using LibMatrix.Abstractions; +using LibMatrix.Federation.Extensions; +using LibMatrix.FederationTest.Services; +using LibMatrix.Homeservers; +using LibMatrix.Responses.Federation; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.FederationTest.Controllers.Spec; + +[ApiController] +[Route("_matrix/key/v2/")] +public class FederationKeysController(FederationTestConfiguration config, FederationKeyStore keyStore) { + static FederationKeysController() { + Console.WriteLine("INFO | FederationKeysController initialized."); + } + + private static SignedObject? _cachedServerKeysResponse; + private static SemaphoreSlim _serverKeyCacheLock = new SemaphoreSlim(1, 1); + + [HttpGet("server")] + public async Task> GetServerKeys() { + await _serverKeyCacheLock.WaitAsync(); + if (_cachedServerKeysResponse == null || _cachedServerKeysResponse.TypedContent.ValidUntil < DateTime.Now + TimeSpan.FromSeconds(30)) { + var keys = keyStore.GetCurrentSigningKey(); + _cachedServerKeysResponse = new ServerKeysResponse() { + ValidUntil = DateTime.Now + TimeSpan.FromMinutes(1), + ServerName = config.ServerName, + OldVerifyKeys = [], + VerifyKeysById = new() { + { + new() { Algorithm = "ed25519", KeyId = "0" }, new ServerKeysResponse.CurrentVerifyKey() { + Key = keys.publicKey.ToUnpaddedBase64(), + } + } + } + }.Sign(config.ServerName, new VersionedKeyId() { Algorithm = "ed25519", KeyId = "0" }, keys.privateKey); + } + _serverKeyCacheLock.Release(); + + return _cachedServerKeysResponse; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationVersionController.cs b/Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationVersionController.cs new file mode 100644 index 0000000..d146cfd --- /dev/null +++ b/Utilities/LibMatrix.FederationTest/Controllers/Spec/FederationVersionController.cs @@ -0,0 +1,19 @@ +using LibMatrix.Homeservers; +using LibMatrix.Responses.Federation; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.FederationTest.Controllers.Spec; + +[ApiController] +[Route("_matrix/federation/v1/")] +public class FederationVersionController : ControllerBase { + [HttpGet("version")] + public ServerVersionResponse GetVersion() { + return new ServerVersionResponse { + Server = new() { + Name = "LibMatrix.Federation", + Version = "0.0.0", + } + }; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.FederationTest/Controllers/Spec/WellKnownController.cs b/Utilities/LibMatrix.FederationTest/Controllers/Spec/WellKnownController.cs new file mode 100644 index 0000000..b91868c --- /dev/null +++ b/Utilities/LibMatrix.FederationTest/Controllers/Spec/WellKnownController.cs @@ -0,0 +1,19 @@ +using LibMatrix.Services.WellKnownResolver.WellKnownResolvers; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.FederationTest.Controllers.Spec; + +[ApiController] +[Route(".well-known/")] +public class WellKnownController(ILogger logger) : ControllerBase { + static WellKnownController() { + Console.WriteLine("INFO | WellKnownController initialized."); + } + [HttpGet("matrix/server")] + public ServerWellKnown GetMatrixServerWellKnown() { + // {Request.Headers["X-Forwarded-Proto"].FirstOrDefault(Request.Scheme)}:// + return new() { + Homeserver = $"{Request.Headers["X-Forwarded-Host"].FirstOrDefault(Request.Host.Host)}:{Request.Headers["X-Forwarded-Port"].FirstOrDefault("443")}", + }; + } +} \ No newline at end of file -- cgit 1.5.1