Skip to content
This repository was archived by the owner on Dec 18, 2017. It is now read-only.

Commit 243c045

Browse files
committed
Add proxy support in kpm
- Add proxy option to "kpm restore" subcommand - "kpm restore" uses global proxy (http_proxy env var) by default - Add support for authenticated proxies
1 parent e538a54 commit 243c045

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Microsoft.Framework.PackageManager/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public int Main(string[] args)
5656
CommandOptionType.MultipleValue);
5757
var optFallbackSource = c.Option("-f|--fallbacksource <FEED>",
5858
"A list of packages sources to use as a fallback", CommandOptionType.MultipleValue);
59+
var optProxy = c.Option("-p|--proxy <ADDRESS>", "The HTTP proxy to use when retrieving packages",
60+
CommandOptionType.SingleValue);
5961
c.HelpOption("-?|-h|--help");
6062

6163
c.OnExecute(() =>
@@ -73,6 +75,16 @@ public int Main(string[] args)
7375
{
7476
command.FallbackSources = optFallbackSource.Values;
7577
}
78+
if (optProxy.HasValue())
79+
{
80+
#if NET45
81+
Environment.SetEnvironmentVariable("http_proxy", optProxy.Value(),
82+
EnvironmentVariableTarget.Process);
83+
#else
84+
throw new NotImplementedException(
85+
"TODO: \"kpm --proxy\" is not supported on current target framework");
86+
#endif
87+
}
7688
var success = command.ExecuteCommand();
7789

7890
return success ? 0 : 1;

src/Microsoft.Framework.PackageManager/Restore/NuGet/PackageFeed.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.IO.Compression;
1010
using System.IO.Packaging;
1111
using System.Linq;
12+
using System.Net;
1213
using System.Net.Http;
1314
using System.Net.Http.Headers;
1415
using System.Text;
@@ -44,7 +45,37 @@ public PackageFeed(
4445
_userName = userName;
4546
_password = password;
4647
_report = report;
47-
_httpClient = new HttpClient();
48+
49+
var proxy = Environment.GetEnvironmentVariable("http_proxy");
50+
if (string.IsNullOrEmpty(proxy))
51+
{
52+
_httpClient = new HttpClient();
53+
}
54+
else
55+
{
56+
// To use an authenticated proxy, the proxy address should be in the form of
57+
// "http://user:[email protected]:8888"
58+
var proxyUriBuilder = new UriBuilder(proxy);
59+
var webProxy = new WebProxy(proxy);
60+
if (string.IsNullOrEmpty(proxyUriBuilder.UserName))
61+
{
62+
// If no credentials were specified we use default credentials
63+
webProxy.Credentials = CredentialCache.DefaultCredentials;
64+
}
65+
else
66+
{
67+
ICredentials credentials = new NetworkCredential(proxyUriBuilder.UserName,
68+
proxyUriBuilder.Password);
69+
webProxy.Credentials = credentials;
70+
}
71+
72+
var handler = new HttpClientHandler
73+
{
74+
Proxy = webProxy,
75+
UseProxy = true
76+
};
77+
_httpClient = new HttpClient(handler);
78+
}
4879
}
4980

5081
Dictionary<string, Task<IEnumerable<PackageInfo>>> _cache = new Dictionary<string, Task<IEnumerable<PackageInfo>>>();

0 commit comments

Comments
 (0)