From d22af149b5aa6225b97575eab9d05dc384b345f7 Mon Sep 17 00:00:00 2001
From: Sam Therapy <sam@samtherapy.net>
Date: Thu, 2 Feb 2023 21:07:10 +0100
Subject: [PATCH] fix(AP): add request header

Signed-off-by: Sam Therapy <sam@samtherapy.net>
---
 src/BirdsiteLive.Domain/ActivityPubService.cs | 9 +++++----
 src/BirdsiteLive.Domain/MigrationService.cs   | 2 +-
 src/BirdsiteLive.Domain/TheFedInfoService.cs  | 2 +-
 src/BirdsiteLive/Startup.cs                   | 5 ++++-
 4 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/BirdsiteLive.Domain/ActivityPubService.cs b/src/BirdsiteLive.Domain/ActivityPubService.cs
index 2e0129d..d1d66c1 100644
--- a/src/BirdsiteLive.Domain/ActivityPubService.cs
+++ b/src/BirdsiteLive.Domain/ActivityPubService.cs
@@ -2,6 +2,7 @@
 using System.Linq;
 using System.Net;
 using System.Net.Http;
+using System.Runtime.InteropServices;
 using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
@@ -54,7 +55,7 @@ namespace BirdsiteLive.Domain
 
             var url = $"https://{splittedAcct[1]}/.well-known/webfinger?resource=acct:{splittedAcct[0]}@{splittedAcct[1]}";
 
-            var httpClient = _httpClientFactory.CreateClient();
+            var httpClient = _httpClientFactory.CreateClient("BirdsiteLIVE");
             httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
             var result = await httpClient.GetAsync(url);
 
@@ -68,7 +69,7 @@ namespace BirdsiteLive.Domain
 
         public async Task<Actor> GetUser(string objectId)
         {
-            var httpClient = _httpClientFactory.CreateClient();
+            var httpClient = _httpClientFactory.CreateClient("BirdsiteLIVE");
             httpClient.DefaultRequestHeaders.Add("Accept", "application/activity+json");
             var result = await httpClient.GetAsync(objectId);
 
@@ -156,7 +157,7 @@ namespace BirdsiteLive.Domain
 
             var signature = _cryptoService.SignAndGetSignatureHeader(date, actorUrl, targetHost, digest, usedInbox);
 
-            var client = _httpClientFactory.CreateClient();
+            var client = _httpClientFactory.CreateClient("BirdsiteLIVE");
             var httpRequestMessage = new HttpRequestMessage
             {
                 Method = HttpMethod.Post,
@@ -178,7 +179,7 @@ namespace BirdsiteLive.Domain
 
         public async Task<WebFingerData> WebFinger(string account)
         {
-            var httpClient = _httpClientFactory.CreateClient();
+            var httpClient = _httpClientFactory.CreateClient("BirdsiteLIVE");
             var result = await httpClient.GetAsync("https://" + account.Split('@')[1] + "/.well-known/webfinger?resource=acct:" + account);
             var content = await result.Content.ReadAsStringAsync();
 
diff --git a/src/BirdsiteLive.Domain/MigrationService.cs b/src/BirdsiteLive.Domain/MigrationService.cs
index e0dfdb5..e6ed32e 100644
--- a/src/BirdsiteLive.Domain/MigrationService.cs
+++ b/src/BirdsiteLive.Domain/MigrationService.cs
@@ -298,7 +298,7 @@ namespace BirdsiteLive.Domain
 
                         var url = string.Format(urlPattern, host, id, tweetId);
 
-                        var client = _httpClientFactory.CreateClient();
+                        var client = _httpClientFactory.CreateClient("BirdsiteLIVE");
                         var result = await client.PostAsync(url, null);
                         result.EnsureSuccessStatusCode();
                     }
diff --git a/src/BirdsiteLive.Domain/TheFedInfoService.cs b/src/BirdsiteLive.Domain/TheFedInfoService.cs
index e85d7fa..76dbbda 100644
--- a/src/BirdsiteLive.Domain/TheFedInfoService.cs
+++ b/src/BirdsiteLive.Domain/TheFedInfoService.cs
@@ -85,7 +85,7 @@ namespace BirdsiteLive.Domain
             };
             //add authorization headers if necessary here
             httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
-            var httpClient = _httpClientFactory.CreateClient();
+            var httpClient = _httpClientFactory.CreateClient("BirdsiteLIVE");
             using (var response = await httpClient.SendAsync(httpRequestMessage, cancellationToken))
             {
                 //if (response.IsSuccessStatusCode)
diff --git a/src/BirdsiteLive/Startup.cs b/src/BirdsiteLive/Startup.cs
index 9758c94..58898ba 100644
--- a/src/BirdsiteLive/Startup.cs
+++ b/src/BirdsiteLive/Startup.cs
@@ -2,6 +2,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net.Http;
+using System.Net.Http.Headers;
 using System.Threading.Tasks;
 using BirdsiteLive.Common.Settings;
 using BirdsiteLive.Common.Structs;
@@ -50,7 +51,9 @@ namespace BirdsiteLive
 
             services.AddControllersWithViews();
 
-            services.AddHttpClient();
+            services.AddHttpClient("BirdsiteLIVE", httpClient => {
+                httpClient.DefaultRequestHeaders.Add("User-Agent", $"BirdsiteLIVE/${Program.VERSION}; +https://{Configuration["Instance:Domain"]}");
+            });
         }
 
         public void ConfigureContainer(ServiceRegistry services)
