about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Tools/MediaLocator.razor
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-01-24 02:31:56 +0100
committerRory& <root@rory.gay>2024-01-24 17:05:25 +0100
commit03313562d21d5db9bf6a14ebbeab80e06c883d3a (patch)
treee000546a2ee8e6a886a7ed9fd01ad674178fb7cb /MatrixUtils.Web/Pages/Tools/MediaLocator.razor
parentMake RMU installable (diff)
downloadMatrixUtils-03313562d21d5db9bf6a14ebbeab80e06c883d3a.tar.xz
MRU->RMU, fixes, cleanup
Diffstat (limited to 'MatrixUtils.Web/Pages/Tools/MediaLocator.razor')
-rw-r--r--MatrixUtils.Web/Pages/Tools/MediaLocator.razor111
1 files changed, 111 insertions, 0 deletions
diff --git a/MatrixUtils.Web/Pages/Tools/MediaLocator.razor b/MatrixUtils.Web/Pages/Tools/MediaLocator.razor
new file mode 100644
index 0000000..38c9b71
--- /dev/null
+++ b/MatrixUtils.Web/Pages/Tools/MediaLocator.razor
@@ -0,0 +1,111 @@
+@page "/Tools/MediaLocator"
+@using LibMatrix.Homeservers
+@inject HttpClient Http
+<h3>Media locator</h3>
+<hr/>
+
+<b>This is going to expose your IP address to all these homeservers!</b>
+<details>
+    <summary>Checked homeserver list (@homeservers.Count entries)</summary>
+    <ul>
+        @foreach (var hs in homeservers) {
+            <li>@hs</li>
+        }
+    </ul>
+</details>
+<button @onclick="addMoreHomeservers">Add more homeservers</button>
+<br/>
+<span>MXC URL: </span>
+<input type="text" @bind="mxcUrl"/>
+<button @onclick="executeSearch">Search</button>
+
+@if (successResults.Count > 0) {
+    <h4>Successes</h4>
+    <ul>
+        @foreach (var result in successResults) {
+            <li>@result</li>
+        }
+    </ul>
+}
+
+@if (errorResults.Count > 0) {
+    <h4>Errors</h4>
+    <ul>
+        @foreach (var result in errorResults) {
+            <li>@result</li>
+        }
+    </ul>
+}
+
+
+@code {
+    string mxcUrl { get; set; }
+    readonly List<string> successResults = new();
+    readonly List<string> errorResults = new();
+    readonly List<string> homeservers = new();
+
+    protected override async Task OnInitializedAsync() {
+        await base.OnInitializedAsync();
+        homeservers.AddRange(new[] {
+            "matrix.org",
+            "feline.support",
+            "rory.gay",
+            "the-apothecary.club",
+            "envs.net",
+            "projectsegfau.lt"
+        });
+    }
+
+    Task executeSearch() {
+        var sem = new SemaphoreSlim(128, 128);
+        homeservers.ForEach(async hs => {
+            await sem.WaitAsync();
+            var httpClient = new HttpClient { BaseAddress = new Uri(hs) };
+            httpClient.Timeout = TimeSpan.FromSeconds(5);
+            var rmu = mxcUrl.Replace("mxc://", $"{hs}/_matrix/media/v3/download/");
+            try {
+                var res = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, rmu));
+                if (res.IsSuccessStatusCode) {
+                    successResults.Add($"{hs}: found - {res.Content.Headers.ContentLength} bytes");
+                    StateHasChanged();
+                    return;
+                }
+                errorResults.Add($"Error: {hs} - {res.StatusCode}\n" + await res.Content.ReadAsStringAsync());
+            }
+            catch (Exception e) {
+                errorResults.Add($"Error: {e}");
+            }
+            finally {
+                sem.Release();
+            }
+            StateHasChanged();
+        });
+        return Task.CompletedTask;
+    }
+
+    async Task addMoreHomeservers() {
+        var res = await Http.GetAsync("/homeservers.txt");
+        var content = await res.Content.ReadAsStringAsync();
+        homeservers.Clear();
+        var lines = content.Split("\n");
+
+        var rhs = new RemoteHomeserver("rory.gay");
+        var sem = new SemaphoreSlim(128, 128);
+        lines.ToList().ForEach(async line => {
+            await sem.WaitAsync();
+            try {
+                homeservers.Add((await hsResolver.ResolveHomeserverFromWellKnown(line)).Client);
+                StateHasChanged();
+            }
+            catch (Exception e) {
+                Console.WriteLine(e);
+            }
+            finally {
+                sem.Release();
+            }
+        });
+
+        StateHasChanged();
+    }
+
+}