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
|
@page "/Login"
@using System.Text.Json
@using LibMatrix.Responses
@inject ILocalStorageService LocalStorage
@inject IJSRuntime JsRuntime
<h3>Login</h3>
<hr/>
<span>
<span>@@</span><!--
--><FancyTextBox @bind-Value="@newRecordInput.username"></FancyTextBox><!--
--><span>:</span><!--
--><FancyTextBox @bind-Value="@newRecordInput.homeserver"></FancyTextBox>
via
<FancyTextBox @bind-Value="@newRecordInput.password" IsPassword="true"></FancyTextBox>
</span>
<span style="display: block;">
<label>Password:</label>
<FancyTextBox @bind-Value="@newRecordInput.password" IsPassword="true"></FancyTextBox>
</span>
<button @onclick="AddRecord">Add account to queue</button>
<br/>
<InputFile OnChange="@FileChanged" accept=".tsv"></InputFile>
<br/>
<br/><br/>
<h4>Parsed records</h4>
<hr/>
<table border="1">
<thead>
<td>Username</td>
<td>Homeserver</td>
<td>Password</td>
<td>Proxy</td>
</thead>
@foreach (var record in records) {
var r = record;
<tr style="background-color: @(LoggedInSessions.Any(x => x.UserId == $"@{r.username}:{r.homeserver}" && x.Proxy == r.proxy) ? "green" : "unset")">
<td style="border-width: 1px;">
<FancyTextBox @bind-Value="@r.homeserver"></FancyTextBox>
</td>
<td style="border-width: 1px;">
<FancyTextBox @bind-Value="@r.username"></FancyTextBox>
</td>
<td style="border-width: 1px;">
<FancyTextBox @bind-Value="@r.password" IsPassword="true"></FancyTextBox>
</td>
<td style="border-width: 1px;">
<FancyTextBox @bind-Value="@r.proxy"></FancyTextBox>
</td>
<td>
<a role="button" @onclick="() => records.Remove(r)">Remove</a>
</td>
</tr>
}
</table>
<br/>
<button @onclick="Login">Login</button>
<br/>
<LogView></LogView>
@code {
readonly List<(string homeserver, string username, string password, string? proxy)> records = new();
(string homeserver, string username, string password, string? proxy) newRecordInput = ("", "", "", null);
List<UserAuth>? LoggedInSessions { get; set; } = new();
async Task Login() {
var loginTasks = records.Select(async record => {
var (homeserver, username, password, proxy) = record;
if (LoggedInSessions.Any(x => x.UserId == $"@{username}:{homeserver}" && x.Proxy == proxy)) return;
try {
var result = new UserAuth(await hsProvider.Login(homeserver, username, password, proxy)) {
Proxy = proxy
};
if (result == null) {
Console.WriteLine($"Failed to login to {homeserver} as {username}!");
return;
}
Console.WriteLine($"Obtained access token for {result.UserId}!");
await MRUStorage.AddToken(result);
LoggedInSessions = await MRUStorage.GetAllTokens();
}
catch (Exception e) {
Console.WriteLine($"Failed to login to {homeserver} as {username}!");
Console.WriteLine(e);
}
StateHasChanged();
});
await Task.WhenAll(loginTasks);
}
private async Task FileChanged(InputFileChangeEventArgs obj) {
LoggedInSessions = await MRUStorage.GetAllTokens();
Console.WriteLine(JsonSerializer.Serialize(obj, new JsonSerializerOptions {
WriteIndented = true
}));
await using var rs = obj.File.OpenReadStream();
using var sr = new StreamReader(rs);
var tsvData = await sr.ReadToEndAsync();
records.Clear();
foreach (var line in tsvData.Split('\n')) {
string?[] parts = line.Split('\t');
if (parts.Length < 3)
continue;
string? via = parts.Length > 3 ? parts[3] : null;
records.Add((parts[0], parts[1], parts[2], via));
}
}
private async Task AddRecord() {
LoggedInSessions = await MRUStorage.GetAllTokens();
records.Add(newRecordInput);
newRecordInput = ("", "", "", null);
}
}
|