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

Commit 42e3535

Browse files
committed
allow to gt the dectypted data from a tunnel
1 parent 6535724 commit 42e3535

File tree

3 files changed

+90
-23
lines changed

3 files changed

+90
-23
lines changed

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -231,42 +231,71 @@ private SessionListItem createSessionListItem(SessionEventArgsBase e)
231231
};
232232

233233
//if (isTunnelConnect || e.HttpClient.Request.UpgradeToWebSocket)
234+
e.DataReceived += (sender, args) =>
234235
{
235-
e.DataReceived += (sender, args) =>
236+
var session = (SessionEventArgsBase)sender;
237+
if (sessionDictionary.TryGetValue(session.HttpClient, out var li))
236238
{
237-
var session = (SessionEventArgsBase)sender;
238-
if (sessionDictionary.TryGetValue(session.HttpClient, out var li))
239+
var tunnelType = session.HttpClient.ConnectRequest?.TunnelType ?? TunnelType.Unknown;
240+
if (tunnelType != TunnelType.Unknown)
239241
{
240-
var tunnelType = session.HttpClient.ConnectRequest?.TunnelType ?? TunnelType.Unknown;
241-
if (tunnelType != TunnelType.Unknown)
242-
{
243-
li.Protocol = TunnelTypeToString(tunnelType);
244-
}
242+
li.Protocol = TunnelTypeToString(tunnelType);
243+
}
244+
245+
li.ReceivedDataCount += args.Count;
246+
247+
AppendTransferLog(session.GetHashCode() + (isTunnelConnect ? "_tunnel" : "") + "_received",
248+
args.Buffer, args.Offset, args.Count);
249+
}
250+
};
245251

246-
li.ReceivedDataCount += args.Count;
252+
e.DataSent += (sender, args) =>
253+
{
254+
var session = (SessionEventArgsBase)sender;
255+
if (sessionDictionary.TryGetValue(session.HttpClient, out var li))
256+
{
257+
var tunnelType = session.HttpClient.ConnectRequest?.TunnelType ?? TunnelType.Unknown;
258+
if (tunnelType != TunnelType.Unknown)
259+
{
260+
li.Protocol = TunnelTypeToString(tunnelType);
247261
}
248-
};
249262

250-
e.DataSent += (sender, args) =>
263+
li.SentDataCount += args.Count;
264+
265+
AppendTransferLog(session.GetHashCode() + (isTunnelConnect ? "_tunnel" : "") + "_sent",
266+
args.Buffer, args.Offset, args.Count);
267+
}
268+
};
269+
270+
if (e is TunnelConnectSessionEventArgs te)
271+
{
272+
te.DecryptedDataReceived += (sender, args) =>
251273
{
252274
var session = (SessionEventArgsBase)sender;
253-
if (sessionDictionary.TryGetValue(session.HttpClient, out var li))
254-
{
255-
var tunnelType = session.HttpClient.ConnectRequest?.TunnelType ?? TunnelType.Unknown;
256-
if (tunnelType != TunnelType.Unknown)
257-
{
258-
li.Protocol = TunnelTypeToString(tunnelType);
259-
}
275+
AppendTransferLog(session.GetHashCode() + "_decrypted_received", args.Buffer, args.Offset,
276+
args.Count);
277+
};
260278

261-
li.SentDataCount += args.Count;
262-
}
279+
te.DecryptedDataSent += (sender, args) =>
280+
{
281+
var session = (SessionEventArgsBase)sender;
282+
AppendTransferLog(session.GetHashCode() + "_decrypted_sent", args.Buffer, args.Offset, args.Count);
263283
};
264284
}
265285

266286
item.Update();
267287
return item;
268288
}
269289

290+
private void AppendTransferLog(string fileName, byte[] buffer, int offset, int count)
291+
{
292+
//string basePath = @"c:\!titanium\";
293+
//using (var fs = new FileStream(basePath + fileName, FileMode.Append, FileAccess.Write, FileShare.Read))
294+
//{
295+
// fs.Write(buffer, offset, count);
296+
//}
297+
}
298+
270299
private string TunnelTypeToString(TunnelType tunnelType)
271300
{
272301
switch (tunnelType)

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading;
33
using Titanium.Web.Proxy.Http;
44
using Titanium.Web.Proxy.Models;
5+
using Titanium.Web.Proxy.StreamExtended.Network;
56

67
namespace Titanium.Web.Proxy.EventArguments
78
{
@@ -40,5 +41,39 @@ public bool IsHttpsConnect
4041

4142
internal set => isHttpsConnect = value;
4243
}
44+
45+
/// <summary>
46+
/// Fired when decrypted data is sent within this session to server/client.
47+
/// </summary>
48+
public event EventHandler<DataEventArgs> DecryptedDataSent;
49+
50+
/// <summary>
51+
/// Fired when decrypted data is received within this session from client/server.
52+
/// </summary>
53+
public event EventHandler<DataEventArgs> DecryptedDataReceived;
54+
55+
internal void OnDecryptedDataSent(byte[] buffer, int offset, int count)
56+
{
57+
try
58+
{
59+
DecryptedDataSent?.Invoke(this, new DataEventArgs(buffer, offset, count));
60+
}
61+
catch (Exception ex)
62+
{
63+
ExceptionFunc(new Exception("Exception thrown in user event", ex));
64+
}
65+
}
66+
67+
internal void OnDecryptedDataReceived(byte[] buffer, int offset, int count)
68+
{
69+
try
70+
{
71+
DecryptedDataReceived?.Invoke(this, new DataEventArgs(buffer, offset, count));
72+
}
73+
catch (Exception ex)
74+
{
75+
ExceptionFunc(new Exception("Exception thrown in user event", ex));
76+
}
77+
}
4378
}
4479
}

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
216216

217217
// HTTPS server created - we can now decrypt the client's traffic
218218
clientStream = new CustomBufferedStream(sslStream, BufferPool);
219+
clientStream.DataRead += (o, args) => connectArgs.OnDecryptedDataSent(args.Buffer, args.Offset, args.Count);
220+
clientStream.DataWrite += (o, args) => connectArgs.OnDecryptedDataReceived(args.Buffer, args.Offset, args.Count);
219221
clientStreamWriter = new HttpResponseWriter(clientStream, BufferPool);
220222
}
221223
catch (Exception e)
@@ -286,7 +288,8 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
286288
if (!clientStream.IsClosed && !connection.Stream.IsClosed)
287289
{
288290
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
289-
null, null, connectArgs.CancellationTokenSource, ExceptionFunc);
291+
null, null,
292+
connectArgs.CancellationTokenSource, ExceptionFunc);
290293
}
291294
}
292295
finally
@@ -336,8 +339,8 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
336339
await connection.StreamWriter.WriteLineAsync(cancellationToken);
337340
#if NETCOREAPP2_1
338341
await Http2Helper.SendHttp2(clientStream, connection.Stream, BufferPool.BufferSize,
339-
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); },
340-
(buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); },
342+
(buffer, offset, count) => { connectArgs.OnDecryptedDataSent(buffer, offset, count); },
343+
(buffer, offset, count) => { connectArgs.OnDecryptedDataReceived(buffer, offset, count); },
341344
() => new SessionEventArgs(this, endPoint, cancellationTokenSource)
342345
{
343346
ProxyClient = { Connection = clientConnection },

0 commit comments

Comments
 (0)