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
|
using System.Net.Http.Json;
using ArcaneLibs.Extensions;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using Microsoft.Extensions.Logging;
namespace LibMatrix.Services;
public class HomeserverProviderService {
private readonly ILogger<HomeserverProviderService> _logger;
private readonly HomeserverResolverService _homeserverResolverService;
public HomeserverProviderService(ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) {
_logger = logger;
_homeserverResolverService = homeserverResolverService;
}
private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new();
private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeServerCache = new();
private static Dictionary<string, SemaphoreSlim> _remoteHomeserverSemaphore = new();
private static Dictionary<string, RemoteHomeServer> _remoteHomeServerCache = new();
public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken,
string? proxy = null) {
var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver + accessToken, _ => new SemaphoreSlim(1, 1));
await sem.WaitAsync();
lock (_authenticatedHomeServerCache) {
if (_authenticatedHomeServerCache.ContainsKey(homeserver + accessToken)) {
sem.Release();
return _authenticatedHomeServerCache[homeserver + accessToken];
}
}
var domain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
AuthenticatedHomeserverGeneric hs;
if (true) {
hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverMxApiExtended>(homeserver, accessToken);
}
else {
hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverSynapse>(homeserver, accessToken);
}
// (() => hs.WhoAmI) = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!;
lock(_authenticatedHomeServerCache)
_authenticatedHomeServerCache[homeserver + accessToken] = hs;
sem.Release();
return hs;
}
public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? proxy = null) {
var hs = await RemoteHomeServer.Create(proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver));
// hs._httpClient.Dispose();
// hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.ServerName) };
// hs._httpClient.Timeout = TimeSpan.FromSeconds(120);
return hs;
}
public async Task<LoginResponse> Login(string homeserver, string user, string password, string? proxy = null) {
var hs = await GetRemoteHomeserver(homeserver, proxy);
var payload = new LoginRequest {
Identifier = new LoginRequest.LoginIdentifier { User = user },
Password = password
};
var resp = await hs._httpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload);
var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
return data!;
}
}
|