about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/Tools/MediaLocator.razor
blob: 59ec79e2618b4f24f070971426677f3d7c28e3ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
@page "/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();
    }

}