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

Commit 0c70019

Browse files
authored
Merge pull request #693 from justcoding121/master
beta
2 parents f9a74d7 + 9be5226 commit 0c70019

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+252
-211
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Titanium.Web.Proxy.EventArguments;
2+
3+
namespace Titanium.Web.Proxy.Examples.Basic
4+
{
5+
public static class ProxyEventArgsBaseExtensions
6+
{
7+
public static SampleClientState GetState(this ProxyEventArgsBase args)
8+
{
9+
if (args.ClientUserData == null)
10+
{
11+
args.ClientUserData = new SampleClientState();
12+
}
13+
14+
return (SampleClientState)args.ClientUserData;
15+
}
16+
}
17+
}

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
2-
using System.Collections.Concurrent;
3-
using System.Collections.Generic;
42
using System.Linq;
53
using System.Net;
64
using System.Net.Security;
7-
using System.Text;
85
using System.Threading;
96
using System.Threading.Tasks;
107
using Titanium.Web.Proxy.EventArguments;
@@ -64,6 +61,7 @@ public void StartProxy()
6461
{
6562
proxyServer.BeforeRequest += onRequest;
6663
proxyServer.BeforeResponse += onResponse;
64+
proxyServer.AfterResponse += onAfterResponse;
6765

6866
proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
6967
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
@@ -128,19 +126,32 @@ public void Stop()
128126

129127
private async Task<IExternalProxy> onGetCustomUpStreamProxyFunc(SessionEventArgsBase arg)
130128
{
129+
arg.GetState().PipelineInfo.AppendLine(nameof(onGetCustomUpStreamProxyFunc));
130+
131131
// this is just to show the functionality, provided values are junk
132-
return new ExternalProxy() { BypassLocalhost = false, HostName = "127.0.0.9", Port = 9090, Password = "fake", UserName = "fake", UseDefaultCredentials = false };
132+
return new ExternalProxy
133+
{
134+
BypassLocalhost = false, HostName = "127.0.0.9", Port = 9090, Password = "fake", UserName = "fake",
135+
UseDefaultCredentials = false
136+
};
133137
}
134138

135139
private async Task<IExternalProxy> onCustomUpStreamProxyFailureFunc(SessionEventArgsBase arg)
136140
{
141+
arg.GetState().PipelineInfo.AppendLine(nameof(onCustomUpStreamProxyFailureFunc));
142+
137143
// this is just to show the functionality, provided values are junk
138-
return new ExternalProxy() { BypassLocalhost = false, HostName = "127.0.0.10", Port = 9191, Password = "fake2", UserName = "fake2", UseDefaultCredentials = false };
144+
return new ExternalProxy
145+
{
146+
BypassLocalhost = false, HostName = "127.0.0.10", Port = 9191, Password = "fake2", UserName = "fake2",
147+
UseDefaultCredentials = false
148+
};
139149
}
140150

141151
private async Task onBeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e)
142152
{
143153
string hostname = e.HttpClient.Request.RequestUri.Host;
154+
e.GetState().PipelineInfo.AppendLine(nameof(onBeforeTunnelConnectRequest) + ":" + hostname);
144155
await writeToConsole("Tunnel to: " + hostname);
145156

146157
if (hostname.Contains("dropbox.com"))
@@ -186,12 +197,16 @@ private void WebSocketDataSentReceived(SessionEventArgs args, DataEventArgs e, b
186197

187198
private Task onBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEventArgs e)
188199
{
200+
e.GetState().PipelineInfo.AppendLine(nameof(onBeforeTunnelConnectResponse) + ":" + e.HttpClient.Request.RequestUri);
201+
189202
return Task.FromResult(false);
190203
}
191204

192205
// intercept & cancel redirect or update requests
193206
private async Task onRequest(object sender, SessionEventArgs e)
194207
{
208+
e.GetState().PipelineInfo.AppendLine(nameof(onRequest) + ":" + e.HttpClient.Request.RequestUri);
209+
195210
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
196211
await writeToConsole(e.HttpClient.Request.Url);
197212

@@ -233,6 +248,8 @@ private async Task onRequest(object sender, SessionEventArgs e)
233248
// Modify response
234249
private async Task multipartRequestPartSent(object sender, MultipartRequestPartSentEventArgs e)
235250
{
251+
e.GetState().PipelineInfo.AppendLine(nameof(multipartRequestPartSent));
252+
236253
var session = (SessionEventArgs)sender;
237254
await writeToConsole("Multipart form data headers:");
238255
foreach (var header in e.Headers)
@@ -243,6 +260,8 @@ private async Task multipartRequestPartSent(object sender, MultipartRequestPartS
243260

244261
private async Task onResponse(object sender, SessionEventArgs e)
245262
{
263+
e.GetState().PipelineInfo.AppendLine(nameof(onResponse));
264+
246265
if (e.HttpClient.ConnectRequest?.TunnelType == TunnelType.Websocket)
247266
{
248267
e.DataSent += WebSocket_DataSent;
@@ -293,13 +312,20 @@ private async Task onResponse(object sender, SessionEventArgs e)
293312
//}
294313
}
295314

315+
private async Task onAfterResponse(object sender, SessionEventArgs e)
316+
{
317+
await writeToConsole($"Pipelineinfo: {e.GetState().PipelineInfo}", ConsoleColor.Yellow);
318+
}
319+
296320
/// <summary>
297321
/// Allows overriding default certificate validation logic
298322
/// </summary>
299323
/// <param name="sender"></param>
300324
/// <param name="e"></param>
301325
public Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
302326
{
327+
e.GetState().PipelineInfo.AppendLine(nameof(OnCertificateValidation));
328+
303329
// set IsValid to true/false based on Certificate Errors
304330
if (e.SslPolicyErrors == SslPolicyErrors.None)
305331
{
@@ -316,6 +342,8 @@ public Task OnCertificateValidation(object sender, CertificateValidationEventArg
316342
/// <param name="e"></param>
317343
public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e)
318344
{
345+
e.GetState().PipelineInfo.AppendLine(nameof(OnCertificateSelection));
346+
319347
// set e.clientCertificate to override
320348

321349
return Task.FromResult(0);
@@ -352,3 +380,4 @@ private async Task writeToConsole(string message, ConsoleColor? consoleColor = n
352380
//}
353381
}
354382
}
383+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace Titanium.Web.Proxy.Examples.Basic
5+
{
6+
public class SampleClientState
7+
{
8+
public StringBuilder PipelineInfo { get; } = new StringBuilder();
9+
}
10+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
43
using System.IO;
54
using System.Linq;
65
using System.Net;

src/Titanium.Web.Proxy.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
1111
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
1212
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">120</s:Int64>
13+
<s:String x:Key="/Default/CodeStyle/CSharpUsing/MandatoryImports/=System/@EntryIndexedValue">System</s:String>
1314
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForBuiltInTypes/@EntryValue">UseExplicitType</s:String>
1415
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForSimpleTypes/@EntryValue">UseVar</s:String>
1516
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BC/@EntryIndexedValue">BC</s:String>

src/Titanium.Web.Proxy/CertificateHandler.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ public partial class ProxyServer
1212
/// Call back to override server certificate validation
1313
/// </summary>
1414
/// <param name="sender">The sender object.</param>
15+
/// <param name="sessionArgs">The http session.</param>
1516
/// <param name="certificate">The remote certificate.</param>
1617
/// <param name="chain">The certificate chain.</param>
1718
/// <param name="sslPolicyErrors">Ssl policy errors</param>
1819
/// <returns>Return true if valid certificate.</returns>
19-
internal bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain,
20+
internal bool ValidateServerCertificate(object sender, SessionEventArgsBase sessionArgs, X509Certificate certificate, X509Chain chain,
2021
SslPolicyErrors sslPolicyErrors)
2122
{
2223
// if user callback is registered then do it
2324
if (ServerCertificateValidationCallback != null)
2425
{
25-
var args = new CertificateValidationEventArgs(certificate, chain, sslPolicyErrors);
26+
var args = new CertificateValidationEventArgs(sessionArgs, certificate, chain, sslPolicyErrors);
2627

2728
// why is the sender null?
2829
ServerCertificateValidationCallback.InvokeAsync(this, args, ExceptionFunc).Wait();
@@ -43,12 +44,13 @@ internal bool ValidateServerCertificate(object sender, X509Certificate certifica
4344
/// Call back to select client certificate used for mutual authentication
4445
/// </summary>
4546
/// <param name="sender">The sender.</param>
47+
/// <param name="sessionArgs">The http session.</param>
4648
/// <param name="targetHost">The remote hostname.</param>
4749
/// <param name="localCertificates">Selected local certificates by SslStream.</param>
4850
/// <param name="remoteCertificate">The remote certificate of server.</param>
4951
/// <param name="acceptableIssuers">The acceptable issues for client certificate as listed by server.</param>
5052
/// <returns></returns>
51-
internal X509Certificate? SelectClientCertificate(object sender, string targetHost,
53+
internal X509Certificate? SelectClientCertificate(object sender, SessionEventArgsBase sessionArgs, string targetHost,
5254
X509CertificateCollection localCertificates,
5355
X509Certificate remoteCertificate, string[] acceptableIssuers)
5456
{
@@ -75,12 +77,8 @@ internal bool ValidateServerCertificate(object sender, X509Certificate certifica
7577
// If user call back is registered
7678
if (ClientCertificateSelectionCallback != null)
7779
{
78-
var args = new CertificateSelectionEventArgs
80+
var args = new CertificateSelectionEventArgs(sessionArgs, targetHost, localCertificates, remoteCertificate, acceptableIssuers)
7981
{
80-
TargetHost = targetHost,
81-
LocalCertificates = localCertificates,
82-
RemoteCertificate = remoteCertificate,
83-
AcceptableIssuers = acceptableIssuers,
8482
ClientCertificate = clientCertificate
8583
};
8684

src/Titanium.Web.Proxy/Compression/CompressionFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.IO;
33
using System.IO.Compression;
4-
using Titanium.Web.Proxy.Http;
54

65
namespace Titanium.Web.Proxy.Compression
76
{

src/Titanium.Web.Proxy/Compression/DecompressionFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.IO;
33
using System.IO.Compression;
4-
using Titanium.Web.Proxy.Http;
54

65
namespace Titanium.Web.Proxy.Compression
76
{

src/Titanium.Web.Proxy/Compression/HttpCompression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.IO.Compression;
1+
using System;
22

33
namespace Titanium.Web.Proxy.Compression
44
{

src/Titanium.Web.Proxy/EventArguments/BeforeSslAuthenticateEventArgs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using System;
22
using System.Threading;
3+
using Titanium.Web.Proxy.Network.Tcp;
34

45
namespace Titanium.Web.Proxy.EventArguments
56
{
67
/// <summary>
78
/// This is used in transparent endpoint before authenticating client.
89
/// </summary>
9-
public class BeforeSslAuthenticateEventArgs : EventArgs
10+
public class BeforeSslAuthenticateEventArgs : ProxyEventArgsBase
1011
{
1112
internal readonly CancellationTokenSource TaskCancellationSource;
1213

13-
internal BeforeSslAuthenticateEventArgs(CancellationTokenSource taskCancellationSource, string sniHostName)
14+
internal BeforeSslAuthenticateEventArgs(TcpClientConnection clientConnection, CancellationTokenSource taskCancellationSource, string sniHostName) : base(clientConnection)
1415
{
1516
TaskCancellationSource = taskCancellationSource;
1617
SniHostName = sniHostName;

src/Titanium.Web.Proxy/EventArguments/CertificateSelectionEventArgs.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,42 @@ namespace Titanium.Web.Proxy.EventArguments
66
/// <summary>
77
/// An argument passed on to user for client certificate selection during mutual SSL authentication.
88
/// </summary>
9-
public class CertificateSelectionEventArgs : EventArgs
9+
public class CertificateSelectionEventArgs : ProxyEventArgsBase
1010
{
11-
/// <summary>
12-
/// The proxy server instance.
13-
/// </summary>
14-
public object? Sender { get; internal set; }
11+
public CertificateSelectionEventArgs(SessionEventArgsBase session, string targetHost,
12+
X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers) : base(session.ClientConnection)
13+
{
14+
Session = session;
15+
TargetHost = targetHost;
16+
LocalCertificates = localCertificates;
17+
RemoteCertificate = remoteCertificate;
18+
AcceptableIssuers = acceptableIssuers;
19+
}
20+
21+
/// <value>
22+
/// The session.
23+
/// </value>
24+
public SessionEventArgsBase Session { get; }
1525

1626
/// <summary>
1727
/// The remote hostname to which we are authenticating against.
1828
/// </summary>
19-
public string? TargetHost { get; internal set; }
29+
public string TargetHost { get; }
2030

2131
/// <summary>
2232
/// Local certificates in store with matching issuers requested by TargetHost website.
2333
/// </summary>
24-
public X509CertificateCollection? LocalCertificates { get; internal set; }
34+
public X509CertificateCollection LocalCertificates { get; }
2535

2636
/// <summary>
2737
/// Certificate of the remote server.
2838
/// </summary>
29-
public X509Certificate? RemoteCertificate { get; internal set; }
39+
public X509Certificate RemoteCertificate { get; }
3040

3141
/// <summary>
3242
/// Acceptable issuers as listed by remote server.
3343
/// </summary>
34-
public string[]? AcceptableIssuers { get; internal set; }
44+
public string[] AcceptableIssuers { get; }
3545

3646
/// <summary>
3747
/// Client Certificate we selected. Set this value to override.

src/Titanium.Web.Proxy/EventArguments/CertificateValidationEventArgs.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ namespace Titanium.Web.Proxy.EventArguments
88
/// An argument passed on to the user for validating the server certificate
99
/// during SSL authentication.
1010
/// </summary>
11-
public class CertificateValidationEventArgs : EventArgs
11+
public class CertificateValidationEventArgs : ProxyEventArgsBase
1212
{
13-
public CertificateValidationEventArgs(X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
13+
public CertificateValidationEventArgs(SessionEventArgsBase session, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) : base(session.ClientConnection)
1414
{
15+
Session = session;
1516
Certificate = certificate;
1617
Chain = chain;
1718
SslPolicyErrors = sslPolicyErrors;
1819
}
1920

21+
/// <value>
22+
/// The session.
23+
/// </value>
24+
public SessionEventArgsBase Session { get; }
25+
2026
/// <summary>
2127
/// Server certificate.
2228
/// </summary>

src/Titanium.Web.Proxy/EventArguments/MultipartRequestPartSentEventArgs.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
using System;
2-
using Titanium.Web.Proxy.Http;
1+
using Titanium.Web.Proxy.Http;
32

43
namespace Titanium.Web.Proxy.EventArguments
54
{
65
/// <summary>
76
/// Class that wraps the multipart sent request arguments.
87
/// </summary>
9-
public class MultipartRequestPartSentEventArgs : EventArgs
8+
public class MultipartRequestPartSentEventArgs : ProxyEventArgsBase
109
{
11-
internal MultipartRequestPartSentEventArgs(string boundary, HeaderCollection headers)
10+
internal MultipartRequestPartSentEventArgs(SessionEventArgs session, string boundary, HeaderCollection headers) : base(session.ClientConnection)
1211
{
12+
Session = session;
1313
Boundary = boundary;
1414
Headers = headers;
1515
}
1616

17+
/// <value>
18+
/// The session arguments.
19+
/// </value>
20+
public SessionEventArgs Session { get; }
21+
1722
/// <summary>
1823
/// Boundary.
1924
/// </summary>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Titanium.Web.Proxy.Network.Tcp;
3+
4+
namespace Titanium.Web.Proxy.EventArguments
5+
{
6+
/// <summary>
7+
/// The base event arguments
8+
/// </summary>
9+
/// <seealso cref="System.EventArgs" />
10+
public abstract class ProxyEventArgsBase : EventArgs
11+
{
12+
private readonly TcpClientConnection clientConnection;
13+
14+
public object ClientUserData
15+
{
16+
get => clientConnection.ClientUserData;
17+
set => clientConnection.ClientUserData = value;
18+
}
19+
20+
internal ProxyEventArgsBase(TcpClientConnection clientConnection)
21+
{
22+
this.clientConnection = clientConnection;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)