mirror of
https://github.com/g3rv4/FakeRelay.git
synced 2024-11-25 00:33:09 +01:00
Avoid recreating the HttpClient every time
This commit is contained in:
parent
8f3a66a0ca
commit
0fff72236f
@ -1,4 +1,5 @@
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace FakeRelay.Core.Helpers;
|
namespace FakeRelay.Core.Helpers;
|
||||||
|
|
||||||
@ -23,28 +24,34 @@ public static class MastodonHelper
|
|||||||
}}");
|
}}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<string> SendMessageToInboxAsync(string targetHost, string content)
|
private static HttpClient? _client;
|
||||||
|
private static HttpClient Client => _client ??= GetClient();
|
||||||
|
|
||||||
|
private static HttpClient GetClient()
|
||||||
{
|
{
|
||||||
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
|
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
|
||||||
client.DefaultRequestHeaders.Add("User-Agent", $"FakeRelay (hosted at {Config.Instance.Host})");
|
client.DefaultRequestHeaders.Add("User-Agent", $"FakeRelay (hosted at {Config.Instance.Host})");
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string> SendMessageToInboxAsync(string targetHost, string content)
|
||||||
|
{
|
||||||
var date = DateTime.UtcNow;
|
var date = DateTime.UtcNow;
|
||||||
|
|
||||||
var digest = CryptographyHelper.GetSHA256Digest(content);
|
var digest = CryptographyHelper.GetSHA256Digest(content);
|
||||||
var requestContent = new StringContent(content);
|
|
||||||
|
|
||||||
requestContent.Headers.Add("Digest", "SHA-256=" + digest);
|
var request = new HttpRequestMessage(HttpMethod.Post, $"https://{targetHost}/inbox");
|
||||||
|
request.Headers.Date = date;
|
||||||
|
request.Headers.Add("Digest", "SHA-256=" + digest);
|
||||||
|
|
||||||
|
request.Content = new StringContent(content, Encoding.UTF8, "application/activity+json");
|
||||||
var stringToSign = $"(request-target): post /inbox\ndate: {date.ToString("R")}\nhost: {targetHost}\ndigest: SHA-256={digest}\ncontent-length: {content.Length}";
|
var stringToSign = $"(request-target): post /inbox\ndate: {date.ToString("R")}\nhost: {targetHost}\ndigest: SHA-256={digest}\ncontent-length: {content.Length}";
|
||||||
var signature = CryptographyHelper.Sign(stringToSign);
|
var signature = CryptographyHelper.Sign(stringToSign);
|
||||||
requestContent.Headers.Add("Signature", $@"keyId=""https://{Config.Instance.Host}/actor#main-key"",algorithm=""rsa-sha256"",headers=""(request-target) date host digest content-length"",signature=""{signature}""");
|
request.Headers.Add("Signature", $@"keyId=""https://{Config.Instance.Host}/actor#main-key"",algorithm=""rsa-sha256"",headers=""(request-target) date host digest content-length"",signature=""{signature}""");
|
||||||
|
|
||||||
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/activity+json");
|
|
||||||
client.DefaultRequestHeaders.Date = date;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await client.PostAsync($"https://{targetHost}/inbox", requestContent);
|
var response = await Client.SendAsync(request);
|
||||||
return await response.Content.ReadAsStringAsync();
|
return await response.Content.ReadAsStringAsync();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
Loading…
Reference in New Issue
Block a user