diff --git a/BTCPayServer.Tests/GreenfieldAPITests.cs b/BTCPayServer.Tests/GreenfieldAPITests.cs index 4d139de48..5d08d3571 100644 --- a/BTCPayServer.Tests/GreenfieldAPITests.cs +++ b/BTCPayServer.Tests/GreenfieldAPITests.cs @@ -1474,6 +1474,7 @@ namespace BTCPayServer.Tests var newDeliveryId = await clientProfile.RedeliverWebhook(user.StoreId, hook.Id, delivery.Id); req = await fakeServer.GetNextRequest(); req.Response.StatusCode = 404; + Assert.StartsWith("BTCPayServer", Assert.Single(req.Request.Headers.UserAgent)); await TestUtils.EventuallyAsync(async () => { // Releasing semaphore several times may help making this test less flaky diff --git a/BTCPayServer/HostedServices/BitpayIPNSender.cs b/BTCPayServer/HostedServices/BitpayIPNSender.cs index f2c04fb55..513edd423 100644 --- a/BTCPayServer/HostedServices/BitpayIPNSender.cs +++ b/BTCPayServer/HostedServices/BitpayIPNSender.cs @@ -43,7 +43,7 @@ namespace BTCPayServer.HostedServices readonly InvoiceRepository _InvoiceRepository; private readonly EmailSenderFactory _EmailSenderFactory; private readonly StoreRepository _StoreRepository; - + public const string NamedClient = "bitpay-ipn"; public BitpayIPNSender( IHttpClientFactory httpClientFactory, IBackgroundJobClient jobClient, @@ -52,7 +52,7 @@ namespace BTCPayServer.HostedServices StoreRepository storeRepository, EmailSenderFactory emailSenderFactory) { - _Client = httpClientFactory.CreateClient(); + _Client = httpClientFactory.CreateClient(NamedClient); _JobClient = jobClient; _EventAggregator = eventAggregator; _InvoiceRepository = invoiceRepository; @@ -232,7 +232,6 @@ namespace BTCPayServer.HostedServices request.RequestUri = new Uri(notification.NotificationURL, UriKind.Absolute); request.Content = new StringContent(notificationString, UTF8, "application/json"); - using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); cts.CancelAfter(TimeSpan.FromMinutes(1.0)); var response = await _Client.SendAsync(request, cts.Token); diff --git a/BTCPayServer/HostedServices/WebhookSender.cs b/BTCPayServer/HostedServices/WebhookSender.cs index 337ef1755..cf02605f1 100644 --- a/BTCPayServer/HostedServices/WebhookSender.cs +++ b/BTCPayServer/HostedServices/WebhookSender.cs @@ -13,6 +13,7 @@ using BTCPayServer.Controllers.Greenfield; using BTCPayServer.Data; using BTCPayServer.Events; using BTCPayServer.Logging; +using BTCPayServer.Services; using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Stores; using Microsoft.EntityFrameworkCore; @@ -31,6 +32,7 @@ namespace BTCPayServer.HostedServices { readonly Encoding UTF8 = new UTF8Encoding(false); public readonly static JsonSerializerSettings DefaultSerializerSettings; + static WebhookSender() { DefaultSerializerSettings = WebhookEvent.DefaultSerializerSettings; @@ -38,6 +40,7 @@ namespace BTCPayServer.HostedServices public const string OnionNamedClient = "greenfield-webhook.onion"; public const string ClearnetNamedClient = "greenfield-webhook.clearnet"; public const string LoopbackNamedClient = "greenfield-webhook.loopback"; + public static string[] AllClients = new[] { OnionNamedClient, ClearnetNamedClient, LoopbackNamedClient }; private HttpClient GetClient(Uri uri) { return HttpClientFactory.CreateClient(uri.IsOnion() ? OnionNamedClient : uri.IsLoopback ? LoopbackNamedClient : ClearnetNamedClient); diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 08e582bc0..65a11c966 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -477,6 +477,17 @@ namespace BTCPayServer.Hosting services.AddSingleton(); services.AddSingleton(o => o.GetRequiredService()); } + + var userAgent = new System.Net.Http.Headers.ProductInfoHeaderValue("BTCPayServer", BTCPayServerEnvironment.GetInformationalVersion()); + foreach (var clientName in WebhookSender.AllClients.Concat(new[] { BitpayIPNSender.NamedClient })) + { + services.AddHttpClient(clientName) + .ConfigureHttpClient(client => + { + client.DefaultRequestHeaders.UserAgent.Add(userAgent); + }); + } + return services; } diff --git a/BTCPayServer/Services/BTCPayServerEnvironment.cs b/BTCPayServer/Services/BTCPayServerEnvironment.cs index 28c60e263..791683670 100644 --- a/BTCPayServer/Services/BTCPayServerEnvironment.cs +++ b/BTCPayServer/Services/BTCPayServerEnvironment.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.Linq; +using System.Net.Http; using System.Reflection; using System.Text; using BTCPayServer.Configuration; @@ -17,7 +18,7 @@ namespace BTCPayServer.Services readonly TorServices torServices; public BTCPayServerEnvironment(IWebHostEnvironment env, BTCPayNetworkProvider provider, TorServices torServices, BTCPayServerOptions opts) { - Version = typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute().InformationalVersion; + Version = GetInformationalVersion(); Commit = typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute()?.ShortSHA; #if DEBUG Build = "Debug"; @@ -35,6 +36,12 @@ namespace BTCPayServer.Services this.torServices = torServices; CheatMode = opts.CheatMode; } + + internal static string GetInformationalVersion() + { + return typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute().InformationalVersion; + } + public IWebHostEnvironment Environment { get; set;