diff --git a/src/Tgstation.Server.Common/HttpClientFactory.cs b/src/Tgstation.Server.Common/HttpClientFactory.cs new file mode 100644 index 0000000000..d0ed887fcb --- /dev/null +++ b/src/Tgstation.Server.Common/HttpClientFactory.cs @@ -0,0 +1,41 @@ +using System; +using System.Net.Http.Headers; + +namespace Tgstation.Server.Common +{ + /// + /// that creates s. + /// + public sealed class HttpClientFactory : IAbstractHttpClientFactory + { + /// + public IHttpClient CreateClient() + { + var client = new HttpClient(); + try + { + client.DefaultRequestHeaders.UserAgent.Add(userAgent); + return client; + } + catch + { + client.Dispose(); + throw; + } + } + + /// + /// The used as created client's User-Agent header on request. + /// + readonly ProductInfoHeaderValue userAgent; + + /// + /// Initializes a new instance of the class. + /// + /// The value of . + public HttpClientFactory(ProductInfoHeaderValue userAgent) + { + this.userAgent = userAgent ?? throw new ArgumentNullException(nameof(userAgent)); + } + } +} diff --git a/src/Tgstation.Server.Host/Utils/IAbstractHttpClientFactory.cs b/src/Tgstation.Server.Common/IAbstractHttpClientFactory.cs similarity index 79% rename from src/Tgstation.Server.Host/Utils/IAbstractHttpClientFactory.cs rename to src/Tgstation.Server.Common/IAbstractHttpClientFactory.cs index c71df883dd..d43c24e987 100644 --- a/src/Tgstation.Server.Host/Utils/IAbstractHttpClientFactory.cs +++ b/src/Tgstation.Server.Common/IAbstractHttpClientFactory.cs @@ -1,6 +1,4 @@ -using Tgstation.Server.Common; - -namespace Tgstation.Server.Host.Utils +namespace Tgstation.Server.Common { /// /// Creates s. diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index 1c4e3a62a9..9ce061cd03 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -5,6 +5,7 @@ using System.IdentityModel.Tokens.Jwt; using System.Linq; using Cyberboss.AspNetCore.AsyncInitializer; + using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors.Infrastructure; @@ -17,12 +18,15 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; + using Newtonsoft.Json; + using Serilog; using Serilog.Events; using Serilog.Formatting.Display; using Tgstation.Server.Api; +using Tgstation.Server.Common; using Tgstation.Server.Host.Components; using Tgstation.Server.Host.Components.Byond; using Tgstation.Server.Host.Components.Chat; diff --git a/src/Tgstation.Server.Host/IO/FileDownloader.cs b/src/Tgstation.Server.Host/IO/FileDownloader.cs index 8da81cc128..5cd08b7635 100644 --- a/src/Tgstation.Server.Host/IO/FileDownloader.cs +++ b/src/Tgstation.Server.Host/IO/FileDownloader.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; -using Tgstation.Server.Host.Utils; +using Tgstation.Server.Common; namespace Tgstation.Server.Host.IO { diff --git a/src/Tgstation.Server.Host/Security/OAuth/DiscordOAuthValidator.cs b/src/Tgstation.Server.Host/Security/OAuth/DiscordOAuthValidator.cs index 5de94462bc..1f6d9c751e 100644 --- a/src/Tgstation.Server.Host/Security/OAuth/DiscordOAuthValidator.cs +++ b/src/Tgstation.Server.Host/Security/OAuth/DiscordOAuthValidator.cs @@ -3,8 +3,8 @@ using Microsoft.Extensions.Logging; using Tgstation.Server.Api.Models; +using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; -using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Security.OAuth { diff --git a/src/Tgstation.Server.Host/Security/OAuth/GenericOAuthValidator.cs b/src/Tgstation.Server.Host/Security/OAuth/GenericOAuthValidator.cs index d79ddfb26e..50709b5522 100644 --- a/src/Tgstation.Server.Host/Security/OAuth/GenericOAuthValidator.cs +++ b/src/Tgstation.Server.Host/Security/OAuth/GenericOAuthValidator.cs @@ -15,7 +15,6 @@ using Tgstation.Server.Api; using Tgstation.Server.Api.Models; using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; -using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Security.OAuth { diff --git a/src/Tgstation.Server.Host/Security/OAuth/InvisionCommunityOAuthValidator.cs b/src/Tgstation.Server.Host/Security/OAuth/InvisionCommunityOAuthValidator.cs index b2fe904e60..a7568e47a7 100644 --- a/src/Tgstation.Server.Host/Security/OAuth/InvisionCommunityOAuthValidator.cs +++ b/src/Tgstation.Server.Host/Security/OAuth/InvisionCommunityOAuthValidator.cs @@ -3,8 +3,8 @@ using Microsoft.Extensions.Logging; using Tgstation.Server.Api.Models; +using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; -using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Security.OAuth { diff --git a/src/Tgstation.Server.Host/Security/OAuth/KeycloakOAuthValidator.cs b/src/Tgstation.Server.Host/Security/OAuth/KeycloakOAuthValidator.cs index c6ae182ff6..74e87cb819 100644 --- a/src/Tgstation.Server.Host/Security/OAuth/KeycloakOAuthValidator.cs +++ b/src/Tgstation.Server.Host/Security/OAuth/KeycloakOAuthValidator.cs @@ -3,8 +3,8 @@ using Microsoft.Extensions.Logging; using Tgstation.Server.Api.Models; +using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; -using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Security.OAuth { diff --git a/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs b/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs index 0dc30acbc0..a217de3935 100644 --- a/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs +++ b/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Tgstation.Server.Api.Models; +using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.Utils; diff --git a/src/Tgstation.Server.Host/Security/OAuth/TGForumsOAuthValidator.cs b/src/Tgstation.Server.Host/Security/OAuth/TGForumsOAuthValidator.cs index a531525e40..767e37f6e6 100644 --- a/src/Tgstation.Server.Host/Security/OAuth/TGForumsOAuthValidator.cs +++ b/src/Tgstation.Server.Host/Security/OAuth/TGForumsOAuthValidator.cs @@ -3,8 +3,8 @@ using Microsoft.Extensions.Logging; using Tgstation.Server.Api.Models; +using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; -using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Security.OAuth { diff --git a/src/Tgstation.Server.Host/Swarm/SwarmService.cs b/src/Tgstation.Server.Host/Swarm/SwarmService.cs index 60cfed0198..af907c1985 100644 --- a/src/Tgstation.Server.Host/Swarm/SwarmService.cs +++ b/src/Tgstation.Server.Host/Swarm/SwarmService.cs @@ -15,6 +15,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Tgstation.Server.Api.Models.Response; +using Tgstation.Server.Common; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Database; diff --git a/tests/Tgstation.Server.Tests/Live/Instance/ByondTest.cs b/tests/Tgstation.Server.Tests/Live/Instance/ByondTest.cs index 6531ac585b..26c8b67686 100644 --- a/tests/Tgstation.Server.Tests/Live/Instance/ByondTest.cs +++ b/tests/Tgstation.Server.Tests/Live/Instance/ByondTest.cs @@ -12,6 +12,7 @@ using Tgstation.Server.Api.Models; using Tgstation.Server.Api.Models.Request; using Tgstation.Server.Client; using Tgstation.Server.Client.Components; +using Tgstation.Server.Common; using Tgstation.Server.Host.Components.Byond; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.IO; @@ -102,21 +103,22 @@ namespace Tgstation.Server.Tests.Live.Instance var generalConfigOptionsMock = new Mock>(); generalConfigOptionsMock.SetupGet(x => x.Value).Returns(new GeneralConfiguration()); - var byondInstaller = new PlatformIdentifier().IsWindows - ? (IByondInstaller)new WindowsByondInstaller( + var assemblyInformationProvider = new AssemblyInformationProvider(); + var fileDownloader = new FileDownloader( + new HttpClientFactory(assemblyInformationProvider.ProductInfoHeaderValue), + Mock.Of>()); + + IByondInstaller byondInstaller = new PlatformIdentifier().IsWindows + ? new WindowsByondInstaller( Mock.Of(), Mock.Of(), - new FileDownloader( - new ConcreteHttpClientFactory(), - Mock.Of>()), + fileDownloader, generalConfigOptionsMock.Object, Mock.Of>()) : new PosixByondInstaller( Mock.Of(), Mock.Of(), - new FileDownloader( - new ConcreteHttpClientFactory(), - Mock.Of>()), + fileDownloader, Mock.Of>()); using var windowsByondInstaller = byondInstaller as WindowsByondInstaller; diff --git a/tests/Tgstation.Server.Tests/Live/Instance/ConcreteHttpClientFactory.cs b/tests/Tgstation.Server.Tests/Live/Instance/ConcreteHttpClientFactory.cs deleted file mode 100644 index 7ee373986c..0000000000 --- a/tests/Tgstation.Server.Tests/Live/Instance/ConcreteHttpClientFactory.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Tgstation.Server.Common; -using Tgstation.Server.Host.Utils; - -namespace Tgstation.Server.Tests.Live.Instance -{ - sealed class ConcreteHttpClientFactory : IAbstractHttpClientFactory - { - public IHttpClient CreateClient() => new HttpClient(); - } -} diff --git a/tools/Tgstation.Server.Migrator/ConcreteHttpClientFactory.cs b/tools/Tgstation.Server.Migrator/ConcreteHttpClientFactory.cs deleted file mode 100644 index 65d9ac2893..0000000000 --- a/tools/Tgstation.Server.Migrator/ConcreteHttpClientFactory.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Tgstation.Server.Common; -using Tgstation.Server.Host.Utils; - -namespace Tgstation.Server.Migrator -{ - sealed class ConcreteHttpClientFactory : IAbstractHttpClientFactory - { - public IHttpClient CreateClient() => new HttpClient(); - } -} diff --git a/tools/Tgstation.Server.Migrator/Program.cs b/tools/Tgstation.Server.Migrator/Program.cs index 298877c75a..6d6406500c 100644 --- a/tools/Tgstation.Server.Migrator/Program.cs +++ b/tools/Tgstation.Server.Migrator/Program.cs @@ -21,9 +21,9 @@ using Octokit; using Tgstation.Server.Api; using Tgstation.Server.Client; +using Tgstation.Server.Common; using Tgstation.Server.Host.IO; using Tgstation.Server.Host.Setup; -using Tgstation.Server.Migrator; using FileMode = System.IO.FileMode; @@ -264,6 +264,7 @@ try new ProductInfoHeaderValue( assemblyName.Name!, assemblyName.Version!.Semver().ToString()); + var httpClientFactory = new HttpClientFactory(productInfoHeaderValue); if (!runtimeInstalled) { // RUNTIME DONWLOAD @@ -279,9 +280,9 @@ try Console.WriteLine($"Downloading {downloadUri} to {Path.GetFullPath(dotnetDownloadFilePath)}..."); - using var httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.UserAgent.Add(productInfoHeaderValue); - var webRequestTask = httpClient.GetAsync(downloadUri); + using var httpClient = httpClientFactory.CreateClient(); + using var request = new HttpRequestMessage(HttpMethod.Get, downloadUri); + var webRequestTask = httpClient.SendAsync(request, default); using var response = await webRequestTask; response.EnsureSuccessStatusCode(); using (var responseStream = await response.Content.ReadAsStreamAsync()) @@ -382,7 +383,6 @@ try // TGS5 DOWNLOAD AND UNZIP Console.WriteLine("Downloading TGS5..."); - var httpClientFactory = new ConcreteHttpClientFactory(); using (var loggerFactory = LoggerFactory.Create(builder => { })) { var fileDownloader = new FileDownloader(httpClientFactory, loggerFactory.CreateLogger());