Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ public async Task Respond(Response response)
/// </summary>
public void Dispose()
{
WebSession.Dispose();
}
}
}
9 changes: 8 additions & 1 deletion Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand All @@ -18,10 +19,15 @@ internal class CustomBinaryReader : IDisposable
private readonly byte[] staticBuffer;
private readonly Encoding encoding;

private static readonly ConcurrentQueue<byte[]> buffers = new ConcurrentQueue<byte[]>();

internal CustomBinaryReader(CustomBufferedStream stream, int bufferSize)
{
this.stream = stream;
staticBuffer = new byte[bufferSize];
if (!buffers.TryDequeue(out staticBuffer) || staticBuffer.Length != bufferSize)
{
staticBuffer = new byte[bufferSize];
}

this.bufferSize = bufferSize;

Expand Down Expand Up @@ -148,6 +154,7 @@ internal async Task<byte[]> ReadBytesAsync(long totalBytesToRead)

public void Dispose()
{
buffers.Enqueue(staticBuffer);
}

private void ResizeBuffer(ref byte[] buffer, long size)
Expand Down
9 changes: 8 additions & 1 deletion Titanium.Web.Proxy/Http/HttpWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// Used to communicate with the server over HTTP(S)
/// </summary>
public class HttpWebClient
public class HttpWebClient : IDisposable
{
/// <summary>
/// Connection to server
Expand Down Expand Up @@ -208,5 +208,12 @@ internal async Task ReceiveResponse()
//Read the response headers in to unique and non-unique header collections
await HeaderParser.ReadHeaders(ServerConnection.StreamReader, Response.NonUniqueResponseHeaders, Response.ResponseHeaders);
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
}
}
}
26 changes: 12 additions & 14 deletions Titanium.Web.Proxy/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClient tcpCli

if (string.IsNullOrEmpty(httpCmd))
{
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return;
}

Expand Down Expand Up @@ -99,7 +98,6 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClient tcpCli

if (await CheckAuthorization(clientStreamWriter, connectRequestHeaders) == false)
{
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return;
}

Expand All @@ -111,22 +109,22 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClient tcpCli
{
sslStream = new SslStream(clientStream, true);

var certificate = endPoint.GenericCertificate ?? CertificateManager.CreateCertificate(httpRemoteUri.Host, false);
var certificate = endPoint.GenericCertificate ??
CertificateManager.CreateCertificate(httpRemoteUri.Host, false);

//Successfully managed to authenticate the client using the fake certificate
await sslStream.AuthenticateAsServerAsync(certificate, false,
SupportedSslProtocols, false);
//HTTPS server created - we can now decrypt the client's traffic
clientStream = new CustomBufferedStream(sslStream, BufferSize);

clientStreamReader.Dispose();
clientStreamReader = new CustomBinaryReader(clientStream, BufferSize);
clientStreamWriter = new StreamWriter(clientStream) { NewLine = ProxyConstants.NewLine };
clientStreamWriter = new StreamWriter(clientStream) {NewLine = ProxyConstants.NewLine};
}
catch
{
sslStream?.Dispose();

Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return;
}

Expand All @@ -141,24 +139,25 @@ await sslStream.AuthenticateAsServerAsync(certificate, false,
//write back successfull CONNECT response
await WriteConnectResponse(clientStreamWriter, version);

await TcpHelper.SendRaw(this,
await TcpHelper.SendRaw(this,
httpRemoteUri.Host, httpRemoteUri.Port,
null, version, null,
false,
false,
clientStream, tcpConnectionFactory);

Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return;
}
//Now create the request
await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.Host : null, endPoint, connectRequestHeaders);
httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.Host : null, endPoint,
connectRequestHeaders);
}
catch (Exception)
{
Dispose(clientStream,
clientStreamReader,
clientStreamWriter, null);
}
finally
{
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
}
}

Expand Down Expand Up @@ -544,7 +543,6 @@ await TcpHelper.SendRaw(this,
break;
}
}

}

/// <summary>
Expand Down