Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] proxy support #744

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/BaGet.Core/Configuration/BaGetOptions.cs
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;
using BaGet.Core.Configuration;

namespace BaGet.Core
{
Expand Down Expand Up @@ -50,5 +50,7 @@ public class BaGetOptions
public SearchOptions Search { get; set; }

public MirrorOptions Mirror { get; set; }

public ProxyOptions Proxy { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/BaGet.Core/Configuration/ProxyOptions.cs
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace BaGet.Core.Configuration
{
public class ProxyOptions
{
public string Address { get; set; }
}
}
17 changes: 14 additions & 3 deletions src/BaGet.Core/Extensions/DependencyInjectionExtensions.cs
Expand Up @@ -2,11 +2,12 @@
using System.Net;
using System.Net.Http;
using System.Reflection;
using BaGet.Protocol;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using BaGet.Core.Configuration;
using BaGet.Protocol;

namespace BaGet.Core
{
Expand Down Expand Up @@ -65,6 +66,7 @@ private static void AddConfiguration(this IServiceCollection services)
services.AddBaGetOptions<DatabaseOptions>(nameof(BaGetOptions.Database));
services.AddBaGetOptions<FileSystemStorageOptions>(nameof(BaGetOptions.Storage));
services.AddBaGetOptions<MirrorOptions>(nameof(BaGetOptions.Mirror));
services.AddBaGetOptions<ProxyOptions>(nameof(BaGetOptions.Proxy));
services.AddBaGetOptions<SearchOptions>(nameof(BaGetOptions.Search));
services.AddBaGetOptions<StorageOptions>(nameof(BaGetOptions.Storage));
}
Expand Down Expand Up @@ -174,10 +176,19 @@ private static HttpClient HttpClientFactory(IServiceProvider provider)
var assemblyName = assembly.GetName().Name;
var assemblyVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0";

var client = new HttpClient(new HttpClientHandler
var httpClientHandler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
});
};

var proxyOptions = provider.GetRequiredService<IOptions<ProxyOptions>>();

if (!string.IsNullOrEmpty(proxyOptions.Value.Address))
{
httpClientHandler.Proxy = new WebProxy(proxyOptions.Value.Address);
}

var client = new HttpClient(httpClientHandler);

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