Skip to content

Commit eee803d

Browse files
committed
proxy support. Merge PR: loic-sharma#744
1 parent 7959ecd commit eee803d

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/BaGet.Core/Configuration/BaGetOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ public class BaGetOptions
5454
public SearchOptions Search { get; set; }
5555

5656
public MirrorOptions Mirror { get; set; }
57+
58+
public ProxyOptions Proxy { get; set; }
5759
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace BaGet.Core.Configuration;
4+
5+
public class ProxyOptions
6+
{
7+
public string Address { get; set; }
8+
}

src/BaGet.Core/Extensions/DependencyInjectionExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BaGet.Core.Configuration;
2+
13
namespace BaGet.Core;
24

35
public static partial class DependencyInjectionExtensions
@@ -50,6 +52,7 @@ private static void AddConfiguration(this IServiceCollection services)
5052
services.AddBaGetOptions<DatabaseOptions>(nameof(BaGetOptions.Database));
5153
services.AddBaGetOptions<FileSystemStorageOptions>(nameof(BaGetOptions.Storage));
5254
services.AddBaGetOptions<MirrorOptions>(nameof(BaGetOptions.Mirror));
55+
services.AddBaGetOptions<ProxyOptions>(nameof(BaGetOptions.Proxy));
5356
services.AddBaGetOptions<SearchOptions>(nameof(BaGetOptions.Search));
5457
services.AddBaGetOptions<StorageOptions>(nameof(BaGetOptions.Storage));
5558
}
@@ -159,10 +162,19 @@ private static HttpClient HttpClientFactory(IServiceProvider provider)
159162
var assemblyName = assembly.GetName().Name;
160163
var assemblyVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0";
161164

162-
var client = new HttpClient(new HttpClientHandler
165+
var httpClientHandler = new HttpClientHandler
163166
{
164167
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
165-
});
168+
};
169+
170+
var proxyOptions = provider.GetRequiredService<IOptions<ProxyOptions>>();
171+
172+
if (!string.IsNullOrEmpty(proxyOptions.Value.Address))
173+
{
174+
httpClientHandler.Proxy = new System.Net.WebProxy(proxyOptions.Value.Address);
175+
}
176+
177+
var client = new HttpClient(httpClientHandler);
166178

167179
client.DefaultRequestHeaders.Add("User-Agent", $"{assemblyName}/{assemblyVersion}");
168180
client.Timeout = TimeSpan.FromSeconds(options.PackageDownloadTimeoutSeconds);

0 commit comments

Comments
 (0)