-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom internet proxy credentials with env vars
- Loading branch information
Showing
12 changed files
with
482 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace Paket.Bootstrapper | ||
{ | ||
internal class EnvProxy | ||
{ | ||
private static readonly EnvProxy instance = new EnvProxy(); | ||
private readonly Dictionary<string, IWebProxy> proxies = new Dictionary<string, IWebProxy>(StringComparer.OrdinalIgnoreCase); | ||
|
||
private string[] GetBypassList() | ||
{ | ||
var noproxy = Environment.GetEnvironmentVariable("NO_PROXY"); | ||
if (string.IsNullOrEmpty(noproxy)) | ||
return new string[0]; | ||
return noproxy.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); | ||
} | ||
|
||
private bool TryGetCredentials(Uri uri, out NetworkCredential credentials) | ||
{ | ||
var userPass = uri.UserInfo.Split(new[] { ':' }, 2); | ||
if (userPass.Length == 2 && userPass[0].Length > 0) | ||
{ | ||
credentials = new NetworkCredential(Uri.UnescapeDataString(userPass[0]), Uri.UnescapeDataString(userPass[1])); | ||
return true; | ||
} | ||
credentials = null; | ||
return false; | ||
} | ||
|
||
private void AddProxy(string scheme, string[] bypassList) | ||
{ | ||
var envVarName = string.Format("{0}_PROXY", scheme.ToUpperInvariant()); | ||
var envVarValue = Environment.GetEnvironmentVariable(envVarName); | ||
if (envVarValue == null) | ||
return; | ||
Uri envUri; | ||
if (Uri.TryCreate(envVarValue, UriKind.Absolute, out envUri)) | ||
{ | ||
var proxy = new WebProxy(new Uri(string.Format("{0}://{1}:{2}", scheme, envUri.Host, envUri.Port))); | ||
NetworkCredential credentials; | ||
if (TryGetCredentials(envUri, out credentials)) | ||
proxy.Credentials = credentials; | ||
proxy.BypassProxyOnLocal = true; | ||
proxy.BypassList = bypassList; | ||
proxies.Add(scheme, proxy); | ||
} | ||
} | ||
|
||
protected EnvProxy() | ||
{ | ||
var bypassList = GetBypassList(); | ||
AddProxy("http", bypassList); | ||
AddProxy("https", bypassList); | ||
} | ||
|
||
public static bool TryGetProxyFor(Uri uri, out IWebProxy proxy) | ||
{ | ||
return instance.proxies.TryGetValue(uri.Scheme, out proxy); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Net; | ||
using System.Reflection; | ||
|
||
namespace Paket.Bootstrapper.Tests | ||
{ | ||
[TestFixture] | ||
class EnvWebProxyShould | ||
{ | ||
private sealed class FakeEnvProxy : EnvProxy | ||
{ | ||
public FakeEnvProxy() | ||
{ | ||
var instanceField = typeof(EnvProxy).GetField("instance", BindingFlags.Static | BindingFlags.NonPublic); | ||
instanceField.SetValue(null, this); | ||
} | ||
new public bool TryGetProxyFor(Uri uri, out IWebProxy proxy) | ||
{ | ||
return EnvProxy.TryGetProxyFor(uri, out proxy); | ||
} | ||
} | ||
private sealed class DisposableEnvVar : IDisposable | ||
{ | ||
private readonly string name; | ||
private readonly string oldValue; | ||
public DisposableEnvVar(string name, string value = null) | ||
{ | ||
this.name = name; | ||
oldValue = Environment.GetEnvironmentVariable(name); | ||
Environment.SetEnvironmentVariable(name, value); | ||
} | ||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable(name, oldValue); | ||
} | ||
} | ||
|
||
[Test] public void GetNoProxyIfNoneDefined() | ||
{ | ||
using (new DisposableEnvVar("http_proxy")) | ||
using (new DisposableEnvVar("https_proxy")) | ||
{ | ||
var envProxy = new FakeEnvProxy(); | ||
IWebProxy proxy; | ||
Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); | ||
Assert.IsFalse(envProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); | ||
} | ||
} | ||
|
||
[Test] public void GetHttpProxyWithNoPortNoCredentials() | ||
{ | ||
using (new DisposableEnvVar("http_proxy", "http://proxy.local")) | ||
using (new DisposableEnvVar("no_proxy")) | ||
{ | ||
var envProxy = new FakeEnvProxy(); | ||
IWebProxy proxy; | ||
Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); | ||
var webProxy = proxy as WebProxy; | ||
Assert.IsNotNull(webProxy); | ||
Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local"))); | ||
Assert.IsTrue(webProxy.BypassProxyOnLocal); | ||
Assert.That(webProxy.BypassList.Length, Is.EqualTo(0)); | ||
Assert.IsNull(webProxy.Credentials); | ||
} | ||
} | ||
|
||
[Test] public void GetHttpProxyWithPortNoCredentials() | ||
{ | ||
using (new DisposableEnvVar("http_proxy", "http://proxy.local:8080")) | ||
using (new DisposableEnvVar("no_proxy")) | ||
{ | ||
var envProxy = new FakeEnvProxy(); | ||
IWebProxy proxy; | ||
Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); | ||
var webProxy = proxy as WebProxy; | ||
Assert.IsNotNull(webProxy); | ||
Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); | ||
Assert.IsTrue(webProxy.BypassProxyOnLocal); | ||
Assert.That(webProxy.BypassList.Length, Is.EqualTo(0)); | ||
Assert.IsNull(webProxy.Credentials); | ||
} | ||
} | ||
|
||
[Test] public void GetHttpProxyWithPortAndCredentials() | ||
{ | ||
const string password = "p@ssw0rd:"; | ||
using (new DisposableEnvVar("http_proxy", string.Format("http://user:{0}@proxy.local:8080", Uri.EscapeDataString(password)))) | ||
using (new DisposableEnvVar("no_proxy")) | ||
{ | ||
var envProxy = new FakeEnvProxy(); | ||
IWebProxy proxy; | ||
Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); | ||
var webProxy = proxy as WebProxy; | ||
Assert.IsNotNull(webProxy); | ||
Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); | ||
Assert.IsTrue(webProxy.BypassProxyOnLocal); | ||
Assert.That(webProxy.BypassList.Length, Is.EqualTo(0)); | ||
var credentials = webProxy.Credentials as NetworkCredential; | ||
Assert.IsNotNull(credentials); | ||
Assert.That(credentials.UserName, Is.EqualTo("user")); | ||
Assert.That(credentials.Password, Is.EqualTo(password)); | ||
} | ||
} | ||
|
||
[Test] public void GetHttpsProxyWithPortAndCredentials() | ||
{ | ||
const string password = "p@ssw0rd:"; | ||
using (new DisposableEnvVar("https_proxy", string.Format("https://user:{0}@proxy.local:8080", Uri.EscapeDataString(password)))) | ||
using (new DisposableEnvVar("no_proxy")) | ||
{ | ||
var envProxy = new FakeEnvProxy(); | ||
IWebProxy proxy; | ||
Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("https://github.com"), out proxy)); | ||
var webProxy = proxy as WebProxy; | ||
Assert.IsNotNull(webProxy); | ||
Assert.That(webProxy.Address, Is.EqualTo(new Uri("https://proxy.local:8080"))); | ||
Assert.IsTrue(webProxy.BypassProxyOnLocal); | ||
Assert.That(webProxy.BypassList.Length, Is.EqualTo(0)); | ||
var credentials = webProxy.Credentials as NetworkCredential; | ||
Assert.IsNotNull(credentials); | ||
Assert.That(credentials.UserName, Is.EqualTo("user")); | ||
Assert.That(credentials.Password, Is.EqualTo(password)); | ||
} | ||
} | ||
|
||
[Test] public void GetHttpProxyWithBypassList() | ||
{ | ||
using (new DisposableEnvVar("http_proxy", string.Format("http://proxy.local:8080"))) | ||
using (new DisposableEnvVar("no_proxy", ".local,127.0.0.1")) | ||
{ | ||
var envProxy = new FakeEnvProxy(); | ||
IWebProxy proxy; | ||
Assert.IsTrue(envProxy.TryGetProxyFor(new Uri("http://github.com"), out proxy)); | ||
var webProxy = proxy as WebProxy; | ||
Assert.IsNotNull(webProxy); | ||
Assert.That(webProxy.Address, Is.EqualTo(new Uri("http://proxy.local:8080"))); | ||
Assert.IsTrue(webProxy.BypassProxyOnLocal); | ||
Assert.That(webProxy.BypassList.Length, Is.EqualTo(2)); | ||
Assert.That(webProxy.BypassList[0], Is.EqualTo(".local")); | ||
Assert.That(webProxy.BypassList[1], Is.EqualTo("127.0.0.1")); | ||
Assert.IsNull(webProxy.Credentials); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.