summary refs log tree commit diff
path: root/ModAS.Server/Controllers/HomeController.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2023-12-31 16:38:20 +0100
committerRory& <root@rory.gay>2023-12-31 16:38:20 +0100
commit05289f61d7bd0650ef511cc92a8a657c493dce30 (patch)
treeed38ef598f9e37eaaf294b8864f361553869ee3c /ModAS.Server/Controllers/HomeController.cs
parentAdd auth, start of commit script (diff)
downloadModAS-master.tar.xz
Clean up swagger, clean up auth code HEAD github/master master
Diffstat (limited to 'ModAS.Server/Controllers/HomeController.cs')
-rw-r--r--ModAS.Server/Controllers/HomeController.cs38
1 files changed, 37 insertions, 1 deletions
diff --git a/ModAS.Server/Controllers/HomeController.cs b/ModAS.Server/Controllers/HomeController.cs
index eb17966..48432d5 100644
--- a/ModAS.Server/Controllers/HomeController.cs
+++ b/ModAS.Server/Controllers/HomeController.cs
@@ -1,5 +1,13 @@
 using System.Diagnostics;
+using System.Net.Http.Headers;
+using System.Security.Cryptography;
+using System.Text;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using System.Web;
 using Microsoft.AspNetCore.Mvc;
+using ModAS.Server.Controllers.AppService;
+using MxApiExtensions.Services;
 
 namespace ModAS.Server.Controllers;
 
@@ -7,10 +15,38 @@ namespace ModAS.Server.Controllers;
 ///    Manages the visual homepage.
 /// </summary>
 [ApiController]
-public class HomeController : Controller {
+public class HomeController(AppServiceRegistration asr, ModASConfiguration config) : Controller {
+    private const string ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
     /// <inheritdoc cref="HomeController"/>
     [HttpGet("/_matrix/_modas")]
+    [ApiExplorerSettings(IgnoreApi = true)] //hide from swagger
     public IActionResult Index() {
         return LocalRedirect("/index.html");
     }
+
+    [HttpGet("/_matrix/_modas/version")]
+    public IActionResult Version() {
+        return Ok(new {
+            Version = Modas.Server.Version.VersionString
+        });
+    }
+
+    [HttpGet("/_matrix/_modas/ping")]
+    public async Task<IActionResult> Ping() {
+        var txn = new PingController.TransactionIdContainer() {
+            TransactionId = RandomNumberGenerator.GetString(ValidChars, 32)
+        };
+        var url = $"{config.HomeserverUrl}/_matrix/client/v1/appservice/{HttpUtility.UrlEncode(asr.Id)}/ping";
+        var hrm = new HttpRequestMessage(HttpMethod.Post, url) {
+            Content = new StringContent(JsonSerializer.Serialize(txn), Encoding.UTF8, "application/json"),
+            Headers = {
+                Authorization = new AuthenticationHeaderValue("Bearer", asr.AppServiceToken)
+            }
+        };
+        var req = await new HttpClient().SendAsync(hrm);
+        var resp = await req.Content.ReadFromJsonAsync<JsonObject>();
+        resp!["tnxId"] = txn.TransactionId;
+        return Ok(resp);
+    }
 }
\ No newline at end of file