about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/MediaLocator.razor
blob: 19a0d4ed9bd1e910db864bde798d858f0cbadea4 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@page "/MediaLocator"
@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; }
    List<string> successResults = new List<string>();
    List<string> errorResults = new List<string>();
    List<string> homeservers = new List<string>();

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        homeservers.AddRange(new []
        {
            "matrix.org",
            "feline.support",
            "rory.gay",
            "the-apothecary.club",
            "envs.net",
            "projectsegfau.lt"
        });
    }

    async 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/r0/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();
        });
    }


    async Task addMoreHomeservers()
    {
        await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
        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 rhs.ResolveHomeserverFromWellKnown(line));
                StateHasChanged();
                if(Random.Shared.Next(0,101) == 50) 
                    await LocalStorageWrapper.SaveCacheToLocalStorage(LocalStorage);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                sem.Release();
            }
        });


        StateHasChanged();
    }
}