about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverResolverService.cs
blob: a4a18e5168cdfb289c49dd3c2a489a8ef0783fb0 (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
using System.Collections.Concurrent;
using System.Text.Json;
using ArcaneLibs.Extensions;
using LibMatrix.Extensions;
using Microsoft.Extensions.Logging;

namespace LibMatrix.Services;

public class HomeserverResolverService(ILogger<HomeserverResolverService>? logger = null) {
    private readonly MatrixHttpClient _httpClient = new() {
        Timeout = TimeSpan.FromMilliseconds(10000)
    };

    private static readonly ConcurrentDictionary<string, WellKnownUris> WellKnownCache = new();
    private static readonly ConcurrentDictionary<string, SemaphoreSlim> WellKnownSemaphores = new();

    public async Task<WellKnownUris> ResolveHomeserverFromWellKnown(string homeserver) {
        if (homeserver is null) throw new ArgumentNullException(nameof(homeserver));
        WellKnownSemaphores.TryAdd(homeserver, new SemaphoreSlim(1, 1));
        await WellKnownSemaphores[homeserver].WaitAsync();
        if (WellKnownCache.TryGetValue(homeserver, out var known)) {
            WellKnownSemaphores[homeserver].Release();
            return known;
        }

        logger?.LogInformation("Resolving homeserver: {}", homeserver);
        var res = new WellKnownUris {
            Client = await _tryResolveFromClientWellknown(homeserver),
            Server = await _tryResolveFromServerWellknown(homeserver)
        };
        WellKnownCache.TryAdd(homeserver, res);
        WellKnownSemaphores[homeserver].Release();
        return res;
    }

    private async Task<string?> _tryResolveFromClientWellknown(string homeserver) {
        if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
        try {
            var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/client");
            var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString();
            return hs;
        }
        catch {
            // ignored
        }

        logger?.LogInformation("No client well-known...");
        return null;
    }

    private async Task<string?> _tryResolveFromServerWellknown(string homeserver) {
        if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver;
        try {
            var resp = await _httpClient.GetFromJsonAsync<JsonElement>($"{homeserver}/.well-known/matrix/server");
            var hs = resp.GetProperty("m.server").GetString();
            if (hs is null) throw new InvalidDataException("m.server is null");
            if (!hs.StartsWithAnyOf("http://", "https://"))
                hs = $"https://{hs}";
            return hs;
        }
        catch {
            // ignored
        }

        // fallback: most servers host these on the same location
        var clientUrl = await _tryResolveFromClientWellknown(homeserver);
        if (clientUrl is not null && await _httpClient.CheckSuccessStatus($"{clientUrl}/_matrix/federation/v1/version"))
            return clientUrl;

        logger?.LogInformation("No server well-known...");
        return null;
    }

    public async Task<string?> ResolveMediaUri(string homeserver, string mxc) {
        if (homeserver is null) throw new ArgumentNullException(nameof(homeserver));
        if (mxc is null) throw new ArgumentNullException(nameof(mxc));
        if (!mxc.StartsWith("mxc://")) throw new InvalidDataException("mxc must start with mxc://");
        homeserver = (await ResolveHomeserverFromWellKnown(homeserver)).Client;
        return mxc.Replace("mxc://", $"{homeserver}/_matrix/media/v3/download/");
    }

    public class WellKnownUris {
        public string? Client { get; set; }
        public string? Server { get; set; }
    }
}