about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverProviderService.cs
blob: a674549a7eb2c6bd434985dd02f533c2bad7fca2 (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
using System.Net.Http.Json;
using ArcaneLibs.Collections;
using ArcaneLibs.Extensions;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using Microsoft.Extensions.Logging;

namespace LibMatrix.Services;

public class HomeserverProviderService(ILogger<HomeserverProviderService> logger, HomeserverResolverService hsResolver) {
    private static SemaphoreCache<AuthenticatedHomeserverGeneric> AuthenticatedHomeserverCache = new();
    private static SemaphoreCache<RemoteHomeserver> RemoteHomeserverCache = new();
    private static SemaphoreCache<FederationClient> FederationClientCache = new();

    public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken, string? proxy = null, string? impersonatedMxid = null,
        bool useGeneric = false, bool enableClient = true, bool enableServer = true) {
        if (!enableClient && !enableServer)
            throw new ArgumentException("At least one of enableClient or enableServer must be true");

        return await AuthenticatedHomeserverCache.GetOrAdd($"{homeserver}{accessToken}{proxy}{impersonatedMxid}", async () => {
            var wellKnownUris = await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableClient, enableServer);
            var rhs = new RemoteHomeserver(homeserver, wellKnownUris, proxy);

            AuthenticatedHomeserverGeneric? hs = null;
            if (!useGeneric) {
                var clientVersionsTask = rhs.GetClientVersionsAsync();
                var serverVersionTask = rhs.FederationClient?.GetServerVersionAsync() ?? Task.FromResult<ServerVersionResponse?>(null)!;
                ClientVersionsResponse? clientVersions = new();
                try {
                    clientVersions = await clientVersionsTask;
                }
                catch (Exception e) {
                    logger.LogError(e, "Failed to get client versions for {homeserver}", homeserver);
                }

                ServerVersionResponse? serverVersion;
                try {
                    serverVersion = await serverVersionTask;
                }
                catch (Exception e) {
                    logger.LogWarning(e, "Failed to get server version for {homeserver}", homeserver);
                    throw;
                }

                try {
                    if (clientVersions.UnstableFeatures.TryGetValue("gay.rory.mxapiextensions.v0", out var a) && a)
                        hs = new AuthenticatedHomeserverMxApiExtended(homeserver, wellKnownUris, proxy, accessToken);
                    else {
                        if (serverVersion is { Server.Name: "Synapse" })
                            hs = new AuthenticatedHomeserverSynapse(homeserver, wellKnownUris, proxy, accessToken);
                    }
                }
                catch (Exception e) {
                    logger.LogError(e, "Failed to create authenticated homeserver for {homeserver}", homeserver);
                    throw;
                }
            }

            hs ??= new AuthenticatedHomeserverGeneric(homeserver, wellKnownUris, proxy, accessToken);

            await hs.Initialise();

            if (impersonatedMxid is not null)
                await hs.SetImpersonate(impersonatedMxid);

            return hs;
        });
    }

    public async Task<RemoteHomeserver> GetRemoteHomeserver(string homeserver, string? proxy = null, bool useCache = true, bool enableServer = true) =>
        useCache
            ? await RemoteHomeserverCache.GetOrAdd($"{homeserver}{proxy}",
                async () => { return new RemoteHomeserver(homeserver, await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableServer: enableServer), proxy); })
            : new RemoteHomeserver(homeserver, await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableServer: enableServer), proxy);

    public async Task<FederationClient> GetFederationClient(string homeserver, string keyId, string? proxy = null, bool useCache = true) =>
        useCache
            ? await FederationClientCache.GetOrAdd($"{homeserver}{keyId}{proxy}",
                async () => { return new FederationClient((await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableClient: false)).Server!, proxy); })
            : new FederationClient((await hsResolver.ResolveHomeserverFromWellKnown(homeserver, enableClient: false)).Server!, proxy);

    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.ClientHttpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload);
        var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
        return data!;
    }
}