Commit 1405c3ab authored by justcoding121's avatar justcoding121

Merge branch 'develop' into beta

parents a988e27d 5d22fe00
...@@ -32,8 +32,10 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -32,8 +32,10 @@ namespace Titanium.Web.Proxy.Examples.Basic
proxyServer = new ProxyServer(); proxyServer = new ProxyServer();
//generate root certificate without storing it in file system //generate root certificate without storing it in file system
//proxyServer.CertificateManager.CreateTrustedRootCertificate(false); //proxyServer.CertificateManager.CreateRootCertificate(false);
//proxyServer.CertificateManager.TrustRootCertificate(); //proxyServer.CertificateManager.TrustRootCertificate();
//proxyServer.CertificateManager.TrustRootCertificateAsAdmin();
proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message); proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message);
proxyServer.ForwardToUpstreamGateway = true; proxyServer.ForwardToUpstreamGateway = true;
......
...@@ -88,11 +88,8 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -88,11 +88,8 @@ namespace Titanium.Web.Proxy.Examples.Wpf
////note : load now (if existed) ////note : load now (if existed)
//proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword"); //proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword");
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true) var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true);
{
ExcludedHttpsHostNameRegex = new[] { "ssllabs.com" },
//IncludedHttpsHostNameRegex = new string[0],
};
proxyServer.AddEndPoint(explicitEndPoint); proxyServer.AddEndPoint(explicitEndPoint);
//proxyServer.UpStreamHttpProxy = new ExternalProxy //proxyServer.UpStreamHttpProxy = new ExternalProxy
......
...@@ -15,25 +15,27 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -15,25 +15,27 @@ namespace Titanium.Web.Proxy.UnitTests
private readonly Random random = new Random(); private readonly Random random = new Random();
[TestMethod] [TestMethod]
public async Task Simple_Create_Certificate_Test() public async Task Simple_BC_Create_Certificate_Test()
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e => { })).Value); var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e =>
{
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
})).Value);
mgr.CertificateEngine = CertificateEngine.BouncyCastle;
mgr.ClearIdleCertificates(); mgr.ClearIdleCertificates();
for (int i = 0; i < 5; i++)
foreach (string host in hostNames) foreach (string host in hostNames)
{
tasks.Add(Task.Run(async () =>
{ {
tasks.Add(Task.Run(() =>
//get the connection {
var certificate = await mgr.CreateCertificateAsync(host); //get the connection
var certificate = mgr.CreateCertificate(host, false);
Assert.IsNotNull(certificate); Assert.IsNotNull(certificate);
})); }));
} }
await Task.WhenAll(tasks.ToArray()); await Task.WhenAll(tasks.ToArray());
...@@ -42,30 +44,34 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -42,30 +44,34 @@ namespace Titanium.Web.Proxy.UnitTests
//uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above) //uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above)
//cannot run this test in build server since trusting the certificate won't happen successfully //cannot run this test in build server since trusting the certificate won't happen successfully
//[TestMethod] [TestMethod]
public async Task Simple_Create_Win_Certificate_Test() public async Task Simple_Create_Win_Certificate_Test()
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e => { })).Value); var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e =>
{
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
})).Value);
mgr.CertificateEngine = CertificateEngine.DefaultWindows;
mgr.CreateRootCertificate(true); mgr.CreateRootCertificate(true);
mgr.TrustRootCertificate(); mgr.TrustRootCertificate(true);
mgr.ClearIdleCertificates(); mgr.ClearIdleCertificates();
mgr.CertificateEngine = CertificateEngine.DefaultWindows;
foreach (string host in hostNames) for (int i = 0; i < 5; i++)
{ foreach (string host in hostNames)
tasks.Add(Task.Run(async () =>
{ {
//get the connection tasks.Add(Task.Run(() =>
var certificate = await mgr.CreateCertificateAsync(host); {
//get the connection
Assert.IsNotNull(certificate); var certificate = mgr.CreateCertificate(host, false);
})); Assert.IsNotNull(certificate);
} }));
}
await Task.WhenAll(tasks.ToArray()); await Task.WhenAll(tasks.ToArray());
mgr.RemoveTrustedRootCertificate(true);
mgr.StopClearIdleCertificates(); mgr.StopClearIdleCertificates();
} }
} }
......
...@@ -75,42 +75,6 @@ namespace Titanium.Web.Proxy.Models ...@@ -75,42 +75,6 @@ namespace Titanium.Web.Proxy.Models
/// </summary> /// </summary>
public X509Certificate2 GenericCertificate { get; set; } public X509Certificate2 GenericCertificate { get; set; }
/// <summary>
/// List of host names to exclude using Regular Expressions.
/// </summary>
[Obsolete("ExcludedHttpsHostNameRegex is deprecated, please use BeforeTunnelConnect event instead.")]
public IEnumerable<string> ExcludedHttpsHostNameRegex
{
get { return ExcludedHttpsHostNameRegexList?.Select(x => x.ToString()).ToList(); }
set
{
if (IncludedHttpsHostNameRegex != null)
{
throw new ArgumentException("Cannot set excluded when included is set");
}
ExcludedHttpsHostNameRegexList = value?.Select(x => new Regex(x, RegexOptions.Compiled)).ToList();
}
}
/// <summary>
/// List of host names to exclude using Regular Expressions.
/// </summary>
[Obsolete("IncludedHttpsHostNameRegex is deprecated, please use BeforeTunnelConnect event instead.")]
public IEnumerable<string> IncludedHttpsHostNameRegex
{
get { return IncludedHttpsHostNameRegexList?.Select(x => x.ToString()).ToList(); }
set
{
if (ExcludedHttpsHostNameRegex != null)
{
throw new ArgumentException("Cannot set included when excluded is set");
}
IncludedHttpsHostNameRegexList = value?.Select(x => new Regex(x, RegexOptions.Compiled)).ToList();
}
}
/// <summary> /// <summary>
/// Return true if this HTTP connect request should'nt be decrypted and instead be relayed /// Return true if this HTTP connect request should'nt be decrypted and instead be relayed
/// Valid only for explicit endpoints /// Valid only for explicit endpoints
......
...@@ -17,6 +17,7 @@ using Org.BouncyCastle.Pkcs; ...@@ -17,6 +17,7 @@ using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security; using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.X509; using Org.BouncyCastle.X509;
using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Network.Certificate namespace Titanium.Web.Proxy.Network.Certificate
{ {
...@@ -25,8 +26,6 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -25,8 +26,6 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// </summary> /// </summary>
internal class BCCertificateMaker : ICertificateMaker internal class BCCertificateMaker : ICertificateMaker
{ {
public static readonly Regex CNRemoverRegex = new Regex(@"^CN\s*=\s*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private const int certificateValidDays = 1825; private const int certificateValidDays = 1825;
private const int certificateGraceDays = 366; private const int certificateGraceDays = 366;
...@@ -149,7 +148,7 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -149,7 +148,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
{ {
try try
{ {
x509Certificate.FriendlyName = CNRemoverRegex.Replace(subjectName, string.Empty); x509Certificate.FriendlyName = ProxyConstants.CNRemoverRegex.Replace(subjectName, string.Empty);
} }
catch (PlatformNotSupportedException) catch (PlatformNotSupportedException)
{ {
......
...@@ -250,17 +250,8 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -250,17 +250,8 @@ namespace Titanium.Web.Proxy.Network.Certificate
typeX509Enrollment.InvokeMember("InstallResponse", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue); typeX509Enrollment.InvokeMember("InstallResponse", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue);
typeValue = new object[] { null, 0, 1 }; typeValue = new object[] { null, 0, 1 };
try string empty = (string)typeX509Enrollment.InvokeMember("CreatePFX", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue);
{ return new X509Certificate2(Convert.FromBase64String(empty), string.Empty, X509KeyStorageFlags.Exportable);
string empty = (string)typeX509Enrollment.InvokeMember("CreatePFX", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue);
return new X509Certificate2(Convert.FromBase64String(empty), string.Empty, X509KeyStorageFlags.Exportable);
}
catch (Exception)
{
// ignored
}
return null;
} }
private X509Certificate2 MakeCertificateInternal(string sSubjectCN, bool isRoot, private X509Certificate2 MakeCertificateInternal(string sSubjectCN, bool isRoot,
......
...@@ -245,8 +245,8 @@ namespace Titanium.Web.Proxy ...@@ -245,8 +245,8 @@ namespace Titanium.Web.Proxy
} }
CertificateManager = new CertificateManager(ExceptionFunc); CertificateManager = new CertificateManager(ExceptionFunc);
CertificateManager.TrustRoot = trustRootCertificate; CertificateManager.UserTrustRoot = trustRootCertificate;
CertificateManager.TrustRootAsAdministrator = trustRootCertificateAsAdmin; CertificateManager.MachineTrustRootAsAdministrator = trustRootCertificateAsAdmin;
if (rootCertificateName != null) if (rootCertificateName != null)
{ {
......
...@@ -67,16 +67,6 @@ namespace Titanium.Web.Proxy ...@@ -67,16 +67,6 @@ namespace Titanium.Web.Proxy
//filter out excluded host names //filter out excluded host names
bool excluded = false; bool excluded = false;
if (endPoint.ExcludedHttpsHostNameRegex != null)
{
excluded = endPoint.ExcludedHttpsHostNameRegexList.Any(x => x.IsMatch(connectHostname));
}
if (endPoint.IncludedHttpsHostNameRegex != null)
{
excluded = !endPoint.IncludedHttpsHostNameRegexList.Any(x => x.IsMatch(connectHostname));
}
if(endPoint.BeforeTunnelConnect!=null) if(endPoint.BeforeTunnelConnect!=null)
{ {
excluded = await endPoint.BeforeTunnelConnect(connectHostname); excluded = await endPoint.BeforeTunnelConnect(connectHostname);
......
namespace Titanium.Web.Proxy.Shared using System.Text.RegularExpressions;
namespace Titanium.Web.Proxy.Shared
{ {
/// <summary> /// <summary>
/// Literals shared by Proxy Server /// Literals shared by Proxy Server
...@@ -13,5 +15,7 @@ ...@@ -13,5 +15,7 @@
internal static readonly char[] EqualSplit = { '=' }; internal static readonly char[] EqualSplit = { '=' };
internal static readonly byte[] NewLine = {(byte)'\r', (byte)'\n' }; internal static readonly byte[] NewLine = {(byte)'\r', (byte)'\n' };
public static readonly Regex CNRemoverRegex = new Regex(@"^CN\s*=\s*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment