about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverResolverService.cs
blob: 556bf86a3e062d5d0be97134e15c2b5361bcd30f (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
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(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 { }

        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.StartsWithAnyOf("http://", "https://"))
                hs = $"https://{hs}";
            return hs;
        }
        catch { }

        // 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; }
    }
}