From 4307209bf746f0c51db41c7b820636543b9c102e Mon Sep 17 00:00:00 2001 From: "Reichenbach, Michael" Date: Tue, 25 Jun 2019 16:57:53 +0200 Subject: [PATCH] feat(cli): add default proxy support to cli The cli uses the default windows proxy and credentials for requests. Closes #1373 --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 7 ++++- .../Volo/Abp/Cli/Http/CliHttpClient.cs | 29 +++++++++++++++++++ .../Volo/Abp/Cli/Http/CliHttpClientHandler.cs | 14 +++++++++ .../Volo/Abp/Cli/NuGet/NuGetService.cs | 6 ++-- .../Cli/ProjectBuilding/AbpIoTemplateStore.cs | 25 ++-------------- .../ProjectNugetPackageAdder.cs | 4 +-- .../SolutionModuleAdder.cs | 4 +-- 7 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClient.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientHandler.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index eb22185b73..11e2e23a98 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -1,4 +1,5 @@ -using System.Text; +using System; +using System.Text; using Volo.Abp.Cli.Commands; using Volo.Abp.Domain; using Volo.Abp.IdentityModel; @@ -18,6 +19,10 @@ namespace Volo.Abp.Cli { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + // TODO: workaround until subsequent issues of https://github.com/dotnet/corefx/issues/30166 are resolved + // a permanent fix will probably be published with the release of .net core 3.0: https://github.com/dotnet/corefx/issues/36553 + AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false); + Configure(options => { options.Commands["help"] = typeof(HelpCommand); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClient.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClient.cs new file mode 100644 index 0000000000..c065d83200 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClient.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Text; + +namespace Volo.Abp.Cli.Http +{ + public class CliHttpClient : HttpClient + { + public CliHttpClient() : base(new CliHttpClientHandler()) + { + Timeout = TimeSpan.FromSeconds(30); + + AddAuthentication(this); + } + + private static void AddAuthentication(HttpClient client) + { + if (File.Exists(CliPaths.AccessToken)) + { + var accessToken = File.ReadAllText(CliPaths.AccessToken, Encoding.UTF8); + if (!accessToken.IsNullOrEmpty()) + { + client.SetBearerToken(accessToken); + } + } + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientHandler.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientHandler.cs new file mode 100644 index 0000000000..7ecfa42871 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Http/CliHttpClientHandler.cs @@ -0,0 +1,14 @@ +using System.Net; +using System.Net.Http; + +namespace Volo.Abp.Cli.Http +{ + public class CliHttpClientHandler : HttpClientHandler + { + public CliHttpClientHandler() + { + Proxy = WebRequest.GetSystemWebProxy(); + DefaultProxyCredentials = CredentialCache.DefaultCredentials; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs index be0a444d0d..25ab3c4cc9 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/NuGet/NuGetService.cs @@ -3,8 +3,8 @@ using NuGet.Versioning; using System; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; +using Volo.Abp.Cli.Http; using Volo.Abp.DependencyInjection; using Volo.Abp.Json; using Volo.Abp.Threading; @@ -26,10 +26,8 @@ namespace Volo.Abp.Cli.NuGet public async Task GetLatestVersionOrNullAsync(string packageId, bool includePreviews = false, bool includeNightly = false) { - using (var client = new HttpClient()) + using (var client = new CliHttpClient()) { - client.Timeout = TimeSpan.FromSeconds(30); - var url = includeNightly ? $"https://www.myget.org/F/abp-nightly/api/v3/flatcontainer/{packageId.ToLowerInvariant()}/index.json" : $"https://api.nuget.org/v3-flatcontainer/{packageId.ToLowerInvariant()}/index.json"; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs index b3d8ec4aac..918a0b7d35 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoTemplateStore.cs @@ -6,6 +6,7 @@ using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Volo.Abp.Cli.Http; using Volo.Abp.Cli.ProjectBuilding.Building; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; @@ -82,12 +83,8 @@ namespace Volo.Abp.Cli.ProjectBuilding { var postData = JsonSerializer.Serialize(new GetLatestTemplateVersionDto { Name = name }); - using (var client = new HttpClient()) + using (var client = new CliHttpClient()) { - client.Timeout = TimeSpan.FromSeconds(30); - - AddAuthentication(client); - var responseMessage = await client.PostAsync( $"{CliUrls.WwwAbpIo}api/download/template/get-version/", new StringContent(postData, Encoding.UTF8, MimeTypes.Application.Json), @@ -108,12 +105,8 @@ namespace Volo.Abp.Cli.ProjectBuilding { var postData = JsonSerializer.Serialize(input); - using (var client = new HttpClient()) + using (var client = new CliHttpClient()) { - client.Timeout = TimeSpan.FromMinutes(3); - - AddAuthentication(client); - var responseMessage = await client.PostAsync( $"{CliUrls.WwwAbpIo}api/download/template/", new StringContent(postData, Encoding.UTF8, MimeTypes.Application.Json), @@ -129,18 +122,6 @@ namespace Volo.Abp.Cli.ProjectBuilding } } - private static void AddAuthentication(HttpClient client) - { - if (File.Exists(CliPaths.AccessToken)) - { - var accessToken = File.ReadAllText(CliPaths.AccessToken, Encoding.UTF8); - if (!accessToken.IsNullOrEmpty()) - { - client.SetBearerToken(accessToken); - } - } - } - public class TemplateDownloadInputDto { public string Name { get; set; } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs index 3a7dab7b3a..2b2941a30d 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs @@ -3,10 +3,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using System.Net.Http; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Cli.Http; using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; using Volo.Abp.IO; @@ -71,7 +71,7 @@ namespace Volo.Abp.Cli.ProjectModification protected virtual async Task FindNugetPackageInfoAsync(string moduleName) { - using (var client = new HttpClient()) + using (var client = new CliHttpClient()) { var url = $"{CliUrls.WwwAbpIo}api/app/nugetPackage/byName/?name=" + moduleName; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs index e7a65c0ae8..840f62741c 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs @@ -6,8 +6,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; -using System.Net.Http; using System.Threading.Tasks; +using Volo.Abp.Cli.Http; using Volo.Abp.DependencyInjection; using Volo.Abp.Json; @@ -123,7 +123,7 @@ namespace Volo.Abp.Cli.ProjectModification protected virtual async Task FindModuleInfoAsync(string moduleName) { - using (var client = new HttpClient()) + using (var client = new CliHttpClient()) { var url = $"{CliUrls.WwwAbpIo}api/app/module/byName/?name=" + moduleName;