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

Commit 63ce4b3

Browse files
authored
Merge pull request #376 from justcoding121/develop
Beta
2 parents e817f10 + f11b6b6 commit 63ce4b3

File tree

12 files changed

+476
-361
lines changed

12 files changed

+476
-361
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,18 @@ private async Task OnRequest(object sender, SessionEventArgs e)
170170
//requestBodyHistory[e.Id] = bodyString;
171171
}
172172

173-
////To cancel a request with a custom HTML content
174-
////Filter URL
175-
//if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
176-
//{
177-
// await e.Ok("<!DOCTYPE html>" +
178-
// "<html><body><h1>" +
179-
// "Website Blocked" +
180-
// "</h1>" +
181-
// "<p>Blocked by titanium web proxy.</p>" +
182-
// "</body>" +
183-
// "</html>");
184-
//}
173+
//To cancel a request with a custom HTML content
174+
//Filter URL
175+
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
176+
{
177+
await e.Ok("<!DOCTYPE html>" +
178+
"<html><body><h1>" +
179+
"Website Blocked" +
180+
"</h1>" +
181+
"<p>Blocked by titanium web proxy.</p>" +
182+
"</body>" +
183+
"</html>");
184+
}
185185

186186
////Redirect example
187187
//if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Linq;
@@ -23,7 +23,7 @@ public partial class MainWindow : Window
2323

2424
private int lastSessionNumber;
2525

26-
public ObservableCollection<SessionListItem> Sessions { get; } = new ObservableCollection<SessionListItem>();
26+
public ObservableCollection<SessionListItem> Sessions { get; } = new ObservableCollection<SessionListItem>();
2727

2828
public SessionListItem SelectedSession
2929
{
@@ -63,10 +63,35 @@ public MainWindow()
6363
{
6464
proxyServer = new ProxyServer();
6565
//proxyServer.CertificateEngine = CertificateEngine.DefaultWindows;
66+
67+
////Set a password for the .pfx file
68+
//proxyServer.PfxPassword = "PfxPassword";
69+
70+
////Set Name(path) of the Root certificate file
71+
//proxyServer.PfxFilePath = @"C:\NameFolder\rootCert.pfx";
72+
73+
////do you want Replace an existing Root certificate file(.pfx) if password is incorrect(RootCertificate=null)? yes====>true
74+
//proxyServer.OverwritePfxFile = true;
75+
76+
////save all fake certificates in folder "crts"(will be created in proxy dll directory)
77+
////if create new Root certificate file(.pfx) ====> delete folder "crts"
78+
//proxyServer.SaveFakeCertificates = true;
79+
80+
//Trust Root Certificate
6681
proxyServer.TrustRootCertificate = true;
67-
proxyServer.CertificateManager.TrustRootCertificateAsAdministrator();
82+
proxyServer.TrustRootCertificateAsAdministrator = true;
83+
6884
proxyServer.ForwardToUpstreamGateway = true;
6985

86+
////if you need Load or Create Certificate now. ////// "true" if you need Enable===> Trust the RootCertificate used by this proxy server
87+
//proxyServer.EnsureRootCertificate(true);
88+
89+
////or load directly certificate(As Administrator if need this)
90+
////and At the same time chose path and password
91+
////if password is incorrect and (overwriteRootCert=true)(RootCertificate=null) ====> replace an existing .pfx file
92+
////note : load now (if existed)
93+
//proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword");
94+
7095
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true)
7196
{
7297
ExcludedHttpsHostNameRegex = new[] { "ssllabs.com" },

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ public class SessionEventArgs : EventArgs, IDisposable
2929
/// </summary>
3030
private readonly int bufferSize;
3131

32-
/// <summary>
33-
/// Holds a reference to proxy response handler method
34-
/// </summary>
35-
private Func<SessionEventArgs, Task> httpResponseHandler;
36-
3732
private readonly Action<Exception> exceptionFunc;
3833

3934
/// <summary>
@@ -108,11 +103,9 @@ public bool ReRequest
108103
/// </summary>
109104
internal SessionEventArgs(int bufferSize,
110105
ProxyEndPoint endPoint,
111-
Func<SessionEventArgs, Task> httpResponseHandler,
112106
Action<Exception> exceptionFunc)
113107
{
114108
this.bufferSize = bufferSize;
115-
this.httpResponseHandler = httpResponseHandler;
116109
this.exceptionFunc = exceptionFunc;
117110

118111
ProxyClient = new ProxyClient();
@@ -536,16 +529,18 @@ private async Task<byte[]> GetDecompressedBodyAsync(string encodingType, byte[]
536529
/// </summary>
537530
/// <param name="html"></param>
538531
/// <param name="headers"></param>
539-
public async Task Ok(string html, Dictionary<string, HttpHeader> headers)
532+
public async Task Ok(string html, Dictionary<string, HttpHeader> headers = null)
540533
{
541534
var response = new OkResponse();
542-
response.Headers.AddHeaders(headers);
535+
if (headers != null)
536+
{
537+
response.Headers.AddHeaders(headers);
538+
}
539+
543540
response.HttpVersion = WebSession.Request.HttpVersion;
544541
response.Body = response.Encoding.GetBytes(html ?? string.Empty);
545542

546543
await Respond(response);
547-
548-
WebSession.Request.CancelRequest = true;
549544
}
550545

551546
/// <summary>
@@ -635,8 +630,6 @@ public async Task Respond(Response response)
635630

636631
WebSession.Response = response;
637632

638-
await httpResponseHandler(this);
639-
640633
WebSession.Request.CancelRequest = true;
641634
}
642635

@@ -645,7 +638,6 @@ public async Task Respond(Response response)
645638
/// </summary>
646639
public void Dispose()
647640
{
648-
httpResponseHandler = null;
649641
CustomUpStreamProxyUsed = null;
650642

651643
DataSent = null;

Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class TunnelConnectSessionEventArgs : SessionEventArgs
99
public bool IsHttpsConnect { get; set; }
1010

1111
internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest, Action<Exception> exceptionFunc)
12-
: base(bufferSize, endPoint, null, exceptionFunc)
12+
: base(bufferSize, endPoint, exceptionFunc)
1313
{
1414
WebSession.Request = connectRequest;
1515
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using System.Security.Cryptography.X509Certificates;
4+
using System.Text.RegularExpressions;
45
using System.Threading;
56
using Org.BouncyCastle.Asn1;
67
using Org.BouncyCastle.Asn1.Pkcs;
@@ -24,6 +25,8 @@ namespace Titanium.Web.Proxy.Network.Certificate
2425
/// </summary>
2526
internal class BCCertificateMaker : ICertificateMaker
2627
{
28+
public static readonly Regex CNRemoverRegex = new Regex(@"^CN\s*=\s*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
29+
2730
private const int certificateValidDays = 1825;
2831
private const int certificateGraceDays = 366;
2932

@@ -146,7 +149,7 @@ private static X509Certificate2 GenerateCertificate(string hostName,
146149
{
147150
try
148151
{
149-
x509Certificate.FriendlyName = subjectName;
152+
x509Certificate.FriendlyName = CNRemoverRegex.Replace(subjectName, string.Empty);
150153
}
151154
catch (PlatformNotSupportedException)
152155
{

0 commit comments

Comments
 (0)