MediaLocator.razor (3565B)
1 @page "/MediaLocator" 2 @inject HttpClient Http 3 <h3>Media locator</h3> 4 <hr/> 5 6 <b>This is going to expose your IP address to all these homeservers!</b> 7 <details> 8 <summary>Checked homeserver list (@homeservers.Count entries)</summary> 9 <ul> 10 @foreach (var hs in homeservers) 11 { 12 <li>@hs</li> 13 } 14 </ul> 15 </details> 16 <button @onclick="addMoreHomeservers">Add more homeservers</button> 17 <br/> 18 <span>MXC URL: </span> 19 <input type="text" @bind="mxcUrl" /> 20 <button @onclick="executeSearch">Search</button> 21 22 @if (successResults.Count > 0) 23 { 24 <h4>Successes</h4> 25 <ul> 26 @foreach (var result in successResults) 27 { 28 <li>@result</li> 29 } 30 </ul> 31 } 32 33 @if (errorResults.Count > 0) 34 { 35 <h4>Errors</h4> 36 <ul> 37 @foreach (var result in errorResults) 38 { 39 <li>@result</li> 40 } 41 </ul> 42 } 43 44 45 @code { 46 string mxcUrl { get; set; } 47 List<string> successResults = new List<string>(); 48 List<string> errorResults = new List<string>(); 49 List<string> homeservers = new List<string>(); 50 51 protected override async Task OnInitializedAsync() 52 { 53 await base.OnInitializedAsync(); 54 homeservers.AddRange(new [] 55 { 56 "matrix.org", 57 "feline.support", 58 "rory.gay", 59 "the-apothecary.club", 60 "envs.net", 61 "projectsegfau.lt" 62 }); 63 } 64 65 async Task executeSearch() 66 { 67 var sem = new SemaphoreSlim(128, 128); 68 homeservers.ForEach(async hs => 69 { 70 await sem.WaitAsync(); 71 var httpClient = new HttpClient { BaseAddress = new Uri(hs) }; 72 httpClient.Timeout = TimeSpan.FromSeconds(5); 73 var rmu = mxcUrl.Replace("mxc://", $"{hs}/_matrix/media/r0/download/"); 74 try 75 { 76 var res = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, rmu)); 77 if (res.IsSuccessStatusCode) 78 { 79 successResults.Add($"{hs}: found - {res.Content.Headers.ContentLength} bytes"); 80 StateHasChanged(); 81 return; 82 } 83 errorResults.Add($"Error: {hs} - {res.StatusCode}\n" + await res.Content.ReadAsStringAsync()); 84 } 85 catch (Exception e) 86 { 87 errorResults.Add($"Error: {e}"); 88 } 89 finally 90 { 91 sem.Release(); 92 } 93 StateHasChanged(); 94 }); 95 } 96 97 98 async Task addMoreHomeservers() 99 { 100 await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); 101 var res = await Http.GetAsync("/homeservers.txt"); 102 var content = await res.Content.ReadAsStringAsync(); 103 homeservers.Clear(); 104 var lines = content.Split("\n"); 105 106 var rhs = new RemoteHomeServer("rory.gay"); 107 var sem = new SemaphoreSlim(128, 128); 108 lines.ToList().ForEach(async line => 109 { 110 await sem.WaitAsync(); 111 try 112 { 113 homeservers.Add(await rhs.ResolveHomeserverFromWellKnown(line)); 114 StateHasChanged(); 115 if(Random.Shared.Next(0,101) == 50) 116 await LocalStorageWrapper.SaveCacheToLocalStorage(LocalStorage); 117 } 118 catch (Exception e) 119 { 120 Console.WriteLine(e); 121 } 122 finally 123 { 124 sem.Release(); 125 } 126 }); 127 128 129 StateHasChanged(); 130 } 131 }