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

Commit 26d815c

Browse files
committed
BlockConnect property added to TunenelConnectSessionEventArgs
1 parent 1b1bda0 commit 26d815c

File tree

8 files changed

+192
-125
lines changed

8 files changed

+192
-125
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ await Dispatcher.InvokeAsync(() =>
128128
});
129129
}
130130

131-
private async Task ProxyServer_BeforeTunnelConnectResponse(object sender, SessionEventArgs e)
131+
private async Task ProxyServer_BeforeTunnelConnectResponse(object sender, TunnelConnectSessionEventArgs e)
132132
{
133133
await Dispatcher.InvokeAsync(() =>
134134
{
@@ -191,15 +191,15 @@ await Dispatcher.InvokeAsync(() =>
191191
});
192192
}
193193

194-
private SessionListItem AddSession(SessionEventArgs e)
194+
private SessionListItem AddSession(SessionEventArgsBase e)
195195
{
196196
var item = CreateSessionListItem(e);
197197
Sessions.Add(item);
198198
sessionDictionary.Add(e.WebSession, item);
199199
return item;
200200
}
201201

202-
private SessionListItem CreateSessionListItem(SessionEventArgs e)
202+
private SessionListItem CreateSessionListItem(SessionEventArgsBase e)
203203
{
204204
lastSessionNumber++;
205205
bool isTunnelConnect = e is TunnelConnectSessionEventArgs;

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 12 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,17 @@ namespace Titanium.Web.Proxy.EventArguments
2222
/// A proxy session ends when client terminates connection to proxy
2323
/// or when server terminates connection from proxy
2424
/// </summary>
25-
public class SessionEventArgs : EventArgs, IDisposable
25+
public class SessionEventArgs : SessionEventArgsBase
2626
{
2727
private static readonly byte[] emptyData = new byte[0];
2828

29-
/// <summary>
30-
/// Size of Buffers used by this object
31-
/// </summary>
32-
private readonly int bufferSize;
33-
34-
private readonly ExceptionHandler exceptionFunc;
35-
3629
/// <summary>
3730
/// Backing field for corresponding public property
3831
/// </summary>
3932
private bool reRequest;
4033

41-
/// <summary>
42-
/// Holds a reference to client
43-
/// </summary>
44-
internal ProxyClient ProxyClient { get; }
45-
4634
private bool hasMulipartEventSubscribers => MultipartRequestPartSent != null;
4735

48-
/// <summary>
49-
/// Returns a unique Id for this request/response session
50-
/// same as RequestId of WebSession
51-
/// </summary>
52-
public Guid Id => WebSession.RequestId;
53-
5436
/// <summary>
5537
/// Should we send the request again
5638
/// </summary>
@@ -68,42 +50,11 @@ public bool ReRequest
6850
}
6951
}
7052

71-
/// <summary>
72-
/// Does this session uses SSL
73-
/// </summary>
74-
public bool IsHttps => WebSession.Request.IsHttps;
75-
76-
/// <summary>
77-
/// Client End Point.
78-
/// </summary>
79-
public IPEndPoint ClientEndPoint => (IPEndPoint)ProxyClient.TcpClient.Client.RemoteEndPoint;
80-
81-
/// <summary>
82-
/// A web session corresponding to a single request/response sequence
83-
/// within a proxy connection
84-
/// </summary>
85-
public HttpWebClient WebSession { get; }
86-
87-
/// <summary>
88-
/// Are we using a custom upstream HTTP(S) proxy?
89-
/// </summary>
90-
public ExternalProxy CustomUpStreamProxyUsed { get; internal set; }
91-
92-
public event EventHandler<DataEventArgs> DataSent;
93-
94-
public event EventHandler<DataEventArgs> DataReceived;
95-
9653
/// <summary>
9754
/// Occurs when multipart request part sent.
9855
/// </summary>
9956
public event EventHandler<MultipartRequestPartSentEventArgs> MultipartRequestPartSent;
10057

101-
public ProxyEndPoint LocalEndPoint { get; }
102-
103-
public bool IsTransparent => LocalEndPoint is TransparentProxyEndPoint;
104-
105-
public Exception Exception { get; internal set; }
106-
10758
/// <summary>
10859
/// Constructor to initialize the proxy
10960
/// </summary>
@@ -113,32 +64,8 @@ internal SessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ExceptionHandl
11364
}
11465

11566
protected SessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ExceptionHandler exceptionFunc, Request request)
67+
: base(bufferSize, endPoint, exceptionFunc, request)
11668
{
117-
this.bufferSize = bufferSize;
118-
this.exceptionFunc = exceptionFunc;
119-
120-
ProxyClient = new ProxyClient();
121-
WebSession = new HttpWebClient(bufferSize, request);
122-
LocalEndPoint = endPoint;
123-
124-
WebSession.ProcessId = new Lazy<int>(() =>
125-
{
126-
if (RunTime.IsWindows)
127-
{
128-
var remoteEndPoint = (IPEndPoint)ProxyClient.TcpClient.Client.RemoteEndPoint;
129-
130-
//If client is localhost get the process id
131-
if (NetworkHelper.IsLocalIpAddress(remoteEndPoint.Address))
132-
{
133-
return NetworkHelper.GetProcessIdFromPort(remoteEndPoint.Port, endPoint.IpV6Enabled);
134-
}
135-
136-
//can't access process Id of remote request from remote machine
137-
return -1;
138-
}
139-
140-
throw new PlatformNotSupportedException();
141-
});
14269
}
14370

14471
private CustomBufferedStream GetStream(bool isRequest)
@@ -188,30 +115,6 @@ internal async Task ClearResponse()
188115
WebSession.Response = new Response();
189116
}
190117

191-
internal void OnDataSent(byte[] buffer, int offset, int count)
192-
{
193-
try
194-
{
195-
DataSent?.Invoke(this, new DataEventArgs(buffer, offset, count));
196-
}
197-
catch (Exception ex)
198-
{
199-
exceptionFunc(new Exception("Exception thrown in user event", ex));
200-
}
201-
}
202-
203-
internal void OnDataReceived(byte[] buffer, int offset, int count)
204-
{
205-
try
206-
{
207-
DataReceived?.Invoke(this, new DataEventArgs(buffer, offset, count));
208-
}
209-
catch (Exception ex)
210-
{
211-
exceptionFunc(new Exception("Exception thrown in user event", ex));
212-
}
213-
}
214-
215118
internal void OnMultipartRequestPartSent(string boundary, HeaderCollection headers)
216119
{
217120
try
@@ -220,7 +123,7 @@ internal void OnMultipartRequestPartSent(string boundary, HeaderCollection heade
220123
}
221124
catch (Exception ex)
222125
{
223-
exceptionFunc(new Exception("Exception thrown in user event", ex));
126+
ExceptionFunc(new Exception("Exception thrown in user event", ex));
224127
}
225128
}
226129

@@ -257,7 +160,7 @@ private async Task<byte[]> ReadBodyAsync(bool isRequest)
257160
{
258161
using (var bodyStream = new MemoryStream())
259162
{
260-
var writer = new HttpWriter(bodyStream, bufferSize);
163+
var writer = new HttpWriter(bodyStream, BufferSize);
261164

262165
if (isRequest)
263166
{
@@ -282,7 +185,7 @@ internal async Task SyphonOutBodyAsync(bool isRequest)
282185

283186
using (var bodyStream = new MemoryStream())
284187
{
285-
var writer = new HttpWriter(bodyStream, bufferSize);
188+
var writer = new HttpWriter(bodyStream, BufferSize);
286189
await CopyBodyAsync(isRequest, writer, TransformationMode.None, null);
287190
}
288191
}
@@ -303,8 +206,8 @@ internal async Task CopyRequestBodyAsync(HttpWriter writer, TransformationMode t
303206
var reader = GetStreamReader(true);
304207
string boundary = HttpHelper.GetBoundaryFromContentType(request.ContentType);
305208

306-
using (var copyStream = new CopyStream(reader, writer, bufferSize))
307-
using (var copyStreamReader = new CustomBinaryReader(copyStream, bufferSize))
209+
using (var copyStream = new CopyStream(reader, writer, BufferSize))
210+
using (var copyStreamReader = new CustomBinaryReader(copyStream, BufferSize))
308211
{
309212
while (contentLength > copyStream.ReadBytes)
310213
{
@@ -365,8 +268,8 @@ private async Task CopyBodyAsync(bool isRequest, HttpWriter writer, Transformati
365268

366269
try
367270
{
368-
var bufStream = new CustomBufferedStream(s, bufferSize, true);
369-
reader = new CustomBinaryReader(bufStream, bufferSize);
271+
var bufStream = new CustomBufferedStream(s, BufferSize, true);
272+
reader = new CustomBinaryReader(bufStream, BufferSize);
370273

371274
await writer.CopyBodyAsync(reader, false, -1, onCopy);
372275
}
@@ -388,7 +291,7 @@ private async Task<long> ReadUntilBoundaryAsync(CustomBinaryReader reader, long
388291
{
389292
int bufferDataLength = 0;
390293

391-
var buffer = BufferPool.GetBuffer(bufferSize);
294+
var buffer = BufferPool.GetBuffer(BufferSize);
392295
try
393296
{
394297
int boundaryLength = boundary.Length + 4;
@@ -684,16 +587,10 @@ public void TerminateServerConnection()
684587
/// <summary>
685588
/// implement any cleanup here
686589
/// </summary>
687-
public void Dispose()
590+
public override void Dispose()
688591
{
689-
CustomUpStreamProxyUsed = null;
690-
691-
DataSent = null;
692-
DataReceived = null;
693592
MultipartRequestPartSent = null;
694-
Exception = null;
695-
696-
WebSession.FinishSession();
593+
base.Dispose();
697594
}
698595
}
699596
}

0 commit comments

Comments
 (0)