Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 1405c3a

Browse files
committed
Merge branch 'develop' into beta
2 parents a988e27 + 5d22fe0 commit 1405c3a

File tree

10 files changed

+308
-250
lines changed

10 files changed

+308
-250
lines changed

Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public ProxyTestController()
3232
proxyServer = new ProxyServer();
3333

3434
//generate root certificate without storing it in file system
35-
//proxyServer.CertificateManager.CreateTrustedRootCertificate(false);
35+
//proxyServer.CertificateManager.CreateRootCertificate(false);
36+
3637
//proxyServer.CertificateManager.TrustRootCertificate();
38+
//proxyServer.CertificateManager.TrustRootCertificateAsAdmin();
3739

3840
proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message);
3941
proxyServer.ForwardToUpstreamGateway = true;

Examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ public MainWindow()
8888
////note : load now (if existed)
8989
//proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword");
9090

91-
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true)
92-
{
93-
ExcludedHttpsHostNameRegex = new[] { "ssllabs.com" },
94-
//IncludedHttpsHostNameRegex = new string[0],
95-
};
91+
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true);
92+
9693

9794
proxyServer.AddEndPoint(explicitEndPoint);
9895
//proxyServer.UpStreamHttpProxy = new ExternalProxy

Tests/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ private static readonly string[] hostNames
1515
private readonly Random random = new Random();
1616

1717
[TestMethod]
18-
public async Task Simple_Create_Certificate_Test()
18+
public async Task Simple_BC_Create_Certificate_Test()
1919
{
2020
var tasks = new List<Task>();
2121

22-
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e => { })).Value);
22+
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e =>
23+
{
24+
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
25+
})).Value);
2326

27+
mgr.CertificateEngine = CertificateEngine.BouncyCastle;
2428
mgr.ClearIdleCertificates();
25-
26-
foreach (string host in hostNames)
27-
{
28-
tasks.Add(Task.Run(async () =>
29+
for (int i = 0; i < 5; i++)
30+
foreach (string host in hostNames)
2931
{
30-
31-
//get the connection
32-
var certificate = await mgr.CreateCertificateAsync(host);
33-
34-
Assert.IsNotNull(certificate);
35-
}));
36-
}
32+
tasks.Add(Task.Run(() =>
33+
{
34+
//get the connection
35+
var certificate = mgr.CreateCertificate(host, false);
36+
Assert.IsNotNull(certificate);
37+
}));
38+
}
3739

3840
await Task.WhenAll(tasks.ToArray());
3941

@@ -42,30 +44,34 @@ public async Task Simple_Create_Certificate_Test()
4244

4345
//uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above)
4446
//cannot run this test in build server since trusting the certificate won't happen successfully
45-
//[TestMethod]
47+
[TestMethod]
4648
public async Task Simple_Create_Win_Certificate_Test()
4749
{
4850
var tasks = new List<Task>();
4951

50-
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e => { })).Value);
52+
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e =>
53+
{
54+
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
55+
})).Value);
56+
57+
mgr.CertificateEngine = CertificateEngine.DefaultWindows;
5158
mgr.CreateRootCertificate(true);
52-
mgr.TrustRootCertificate();
59+
mgr.TrustRootCertificate(true);
5360
mgr.ClearIdleCertificates();
54-
mgr.CertificateEngine = CertificateEngine.DefaultWindows;
5561

56-
foreach (string host in hostNames)
57-
{
58-
tasks.Add(Task.Run(async () =>
62+
for (int i = 0; i < 5; i++)
63+
foreach (string host in hostNames)
5964
{
60-
//get the connection
61-
var certificate = await mgr.CreateCertificateAsync(host);
62-
63-
Assert.IsNotNull(certificate);
64-
}));
65-
}
65+
tasks.Add(Task.Run(() =>
66+
{
67+
//get the connection
68+
var certificate = mgr.CreateCertificate(host, false);
69+
Assert.IsNotNull(certificate);
70+
}));
71+
}
6672

6773
await Task.WhenAll(tasks.ToArray());
68-
74+
mgr.RemoveTrustedRootCertificate(true);
6975
mgr.StopClearIdleCertificates();
7076
}
7177
}

Titanium.Web.Proxy/Models/EndPoint.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,42 +75,6 @@ public class ExplicitProxyEndPoint : ProxyEndPoint
7575
/// </summary>
7676
public X509Certificate2 GenericCertificate { get; set; }
7777

78-
/// <summary>
79-
/// List of host names to exclude using Regular Expressions.
80-
/// </summary>
81-
[Obsolete("ExcludedHttpsHostNameRegex is deprecated, please use BeforeTunnelConnect event instead.")]
82-
public IEnumerable<string> ExcludedHttpsHostNameRegex
83-
{
84-
get { return ExcludedHttpsHostNameRegexList?.Select(x => x.ToString()).ToList(); }
85-
set
86-
{
87-
if (IncludedHttpsHostNameRegex != null)
88-
{
89-
throw new ArgumentException("Cannot set excluded when included is set");
90-
}
91-
92-
ExcludedHttpsHostNameRegexList = value?.Select(x => new Regex(x, RegexOptions.Compiled)).ToList();
93-
}
94-
}
95-
96-
/// <summary>
97-
/// List of host names to exclude using Regular Expressions.
98-
/// </summary>
99-
[Obsolete("IncludedHttpsHostNameRegex is deprecated, please use BeforeTunnelConnect event instead.")]
100-
public IEnumerable<string> IncludedHttpsHostNameRegex
101-
{
102-
get { return IncludedHttpsHostNameRegexList?.Select(x => x.ToString()).ToList(); }
103-
set
104-
{
105-
if (ExcludedHttpsHostNameRegex != null)
106-
{
107-
throw new ArgumentException("Cannot set included when excluded is set");
108-
}
109-
110-
IncludedHttpsHostNameRegexList = value?.Select(x => new Regex(x, RegexOptions.Compiled)).ToList();
111-
}
112-
}
113-
11478
/// <summary>
11579
/// Return true if this HTTP connect request should'nt be decrypted and instead be relayed
11680
/// Valid only for explicit endpoints

Titanium.Web.Proxy/Network/Certificate/BCCertificateMaker.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Org.BouncyCastle.Security;
1818
using Org.BouncyCastle.Utilities;
1919
using Org.BouncyCastle.X509;
20+
using Titanium.Web.Proxy.Shared;
2021

2122
namespace Titanium.Web.Proxy.Network.Certificate
2223
{
@@ -25,8 +26,6 @@ namespace Titanium.Web.Proxy.Network.Certificate
2526
/// </summary>
2627
internal class BCCertificateMaker : ICertificateMaker
2728
{
28-
public static readonly Regex CNRemoverRegex = new Regex(@"^CN\s*=\s*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
29-
3029
private const int certificateValidDays = 1825;
3130
private const int certificateGraceDays = 366;
3231

@@ -149,7 +148,7 @@ private static X509Certificate2 GenerateCertificate(string hostName,
149148
{
150149
try
151150
{
152-
x509Certificate.FriendlyName = CNRemoverRegex.Replace(subjectName, string.Empty);
151+
x509Certificate.FriendlyName = ProxyConstants.CNRemoverRegex.Replace(subjectName, string.Empty);
153152
}
154153
catch (PlatformNotSupportedException)
155154
{

Titanium.Web.Proxy/Network/Certificate/WinCertificateMaker.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,8 @@ private X509Certificate2 MakeCertificate(bool isRoot, string subject, string ful
250250
typeX509Enrollment.InvokeMember("InstallResponse", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue);
251251
typeValue = new object[] { null, 0, 1 };
252252

253-
try
254-
{
255-
string empty = (string)typeX509Enrollment.InvokeMember("CreatePFX", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue);
256-
return new X509Certificate2(Convert.FromBase64String(empty), string.Empty, X509KeyStorageFlags.Exportable);
257-
}
258-
catch (Exception)
259-
{
260-
// ignored
261-
}
262-
263-
return null;
253+
string empty = (string)typeX509Enrollment.InvokeMember("CreatePFX", BindingFlags.InvokeMethod, null, x509Enrollment, typeValue);
254+
return new X509Certificate2(Convert.FromBase64String(empty), string.Empty, X509KeyStorageFlags.Exportable);
264255
}
265256

266257
private X509Certificate2 MakeCertificateInternal(string sSubjectCN, bool isRoot,

0 commit comments

Comments
 (0)