blob: 60018625408b57624f01fce01bffd56dc4a24ead (
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
|
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Web;
using ArcaneLibs.Extensions;
using LibMatrix.Extensions;
using LibMatrix.Responses;
using LibMatrix.Services;
namespace LibMatrix.Homeservers;
public class FederationClient(string baseUrl) {
public static async Task<FederationClient?> TryCreate(string baseUrl, string? proxy = null) {
try {
return await Create(baseUrl, proxy);
}
catch (Exception e) {
Console.WriteLine($"Failed to create homeserver {baseUrl}: {e.Message}");
return null;
}
}
public static async Task<FederationClient> Create(string baseUrl, string? proxy = null) {
var homeserver = new FederationClient(baseUrl);
homeserver.WellKnownUris = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl);
if(string.IsNullOrWhiteSpace(proxy) && string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Client))
Console.WriteLine($"Failed to resolve homeserver client URI for {baseUrl}");
if(string.IsNullOrWhiteSpace(proxy) && string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server))
Console.WriteLine($"Failed to resolve homeserver server URI for {baseUrl}");
if (!string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server))
homeserver.HttpClient = new() {
BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Server ?? throw new InvalidOperationException($"Failed to resolve homeserver server URI for {baseUrl}")),
Timeout = TimeSpan.FromSeconds(120)
};
if (proxy is not null) {
homeserver.HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl);
}
return homeserver;
}
public string BaseUrl { get; } = baseUrl;
public MatrixHttpClient HttpClient { get; set; } = null!;
public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } = null!;
public async Task<ServerVersionResponse> GetServerVersionAsync() {
return await HttpClient.GetFromJsonAsync<ServerVersionResponse>("/_matrix/federation/v1/version");
}
}
public class ServerVersionResponse {
[JsonPropertyName("server")]
public required ServerInfo Server { get; set; }
// ReSharper disable once ClassNeverInstantiated.Global
public class ServerInfo {
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("version")]
public string Version { get; set; }
}
}
|