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
26 changes: 14 additions & 12 deletions Titanium.Web.Proxy/CertificateHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
Expand All @@ -26,11 +25,13 @@ internal bool ValidateServerCertificate(
//if user callback is registered then do it
if (ServerCertificateValidationCallback != null)
{
var args = new CertificateValidationEventArgs();
var args = new CertificateValidationEventArgs
{
Certificate = certificate,
Chain = chain,
SslPolicyErrors = sslPolicyErrors
};

args.Certificate = certificate;
args.Chain = chain;
args.SslPolicyErrors = sslPolicyErrors;


Delegate[] invocationList = ServerCertificateValidationCallback.GetInvocationList();
Expand Down Expand Up @@ -73,7 +74,6 @@ internal X509Certificate SelectClientCertificate(
string[] acceptableIssuers)
{
X509Certificate clientCertificate = null;
var customSslStream = sender as SslStream;

if (acceptableIssuers != null &&
acceptableIssuers.Length > 0 &&
Expand All @@ -100,13 +100,15 @@ internal X509Certificate SelectClientCertificate(
//If user call back is registered
if (ClientCertificateSelectionCallback != null)
{
var args = new CertificateSelectionEventArgs();
var args = new CertificateSelectionEventArgs
{
TargetHost = targetHost,
LocalCertificates = localCertificates,
RemoteCertificate = remoteCertificate,
AcceptableIssuers = acceptableIssuers,
ClientCertificate = clientCertificate
};

args.TargetHost = targetHost;
args.LocalCertificates = localCertificates;
args.RemoteCertificate = remoteCertificate;
args.AcceptableIssuers = acceptableIssuers;
args.ClientCertificate = clientCertificate;

Delegate[] invocationList = ClientCertificateSelectionCallback.GetInvocationList();
Task[] handlerTasks = new Task[invocationList.Length];
Expand Down
1 change: 0 additions & 1 deletion Titanium.Web.Proxy/Decompression/GZipDecompression.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Shared;

namespace Titanium.Web.Proxy.Decompression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
public class CertificateSelectionEventArgs : EventArgs
{
/// <summary>
/// Sender object.
/// </summary>
public object Sender { get; internal set; }
/// <summary>
/// Target host.
/// </summary>
public string TargetHost { get; internal set; }
/// <summary>
/// Local certificates.
/// </summary>
public X509CertificateCollection LocalCertificates { get; internal set; }
/// <summary>
/// Remote certificate.
/// </summary>
public X509Certificate RemoteCertificate { get; internal set; }
/// <summary>
/// Acceptable issuers.
/// </summary>
public string[] AcceptableIssuers { get; internal set; }

/// <summary>
/// Client Certificate.
/// </summary>
public X509Certificate ClientCertificate { get; set; }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// An argument passed on to the user for validating the server certificate during SSL authentication
/// </summary>
public class CertificateValidationEventArgs : EventArgs, IDisposable
public class CertificateValidationEventArgs : EventArgs
{
/// <summary>
/// Certificate
/// </summary>
public X509Certificate Certificate { get; internal set; }
/// <summary>
/// Certificate chain
/// </summary>
public X509Chain Chain { get; internal set; }
/// <summary>
/// SSL policy errors.
/// </summary>
public SslPolicyErrors SslPolicyErrors { get; internal set; }

/// <summary>
/// is a valid certificate?
/// </summary>
public bool IsValid { get; set; }

public void Dispose()
{

}
}
}
27 changes: 21 additions & 6 deletions Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class SessionEventArgs : EventArgs, IDisposable
/// </summary>
public Guid Id => WebSession.RequestId;

//Should we send a rerequest
/// <summary>
/// Should we send a rerequest
/// </summary>
public bool ReRequest
{
get;
Expand All @@ -56,7 +58,9 @@ public bool ReRequest
/// </summary>
public bool IsHttps => WebSession.Request.RequestUri.Scheme == Uri.UriSchemeHttps;


/// <summary>
/// Client End Point.
/// </summary>
public IPEndPoint ClientEndPoint => (IPEndPoint)ProxyClient.TcpClient.Client.RemoteEndPoint;

/// <summary>
Expand All @@ -65,8 +69,14 @@ public bool ReRequest
/// </summary>
public HttpWebClient WebSession { get; set; }

/// <summary>
/// Are we using a custom upstream HTTP proxy?
/// </summary>
public ExternalProxy CustomUpStreamHttpProxyUsed { get; set; }

/// <summary>
/// Are we using a custom upstream HTTPS proxy?
/// </summary>
public ExternalProxy CustomUpStreamHttpsProxyUsed { get; set; }

/// <summary>
Expand Down Expand Up @@ -105,15 +115,15 @@ private async Task ReadRequestBody()
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
if (WebSession.Request.IsChunked)
{
await this.ProxyClient.ClientStreamReader.CopyBytesToStreamChunked(bufferSize, requestBodyStream);
await ProxyClient.ClientStreamReader.CopyBytesToStreamChunked(bufferSize, requestBodyStream);
}
else
{
//If not chunked then its easy just read the whole body with the content length mentioned in the request header
if (WebSession.Request.ContentLength > 0)
{
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream,
await ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream,
WebSession.Request.ContentLength);

}
Expand Down Expand Up @@ -431,7 +441,7 @@ public async Task Ok(byte[] result, Dictionary<string, HttpHeader> headers)
            await GenericResponse(html, null, status);
}

/// <summary>
/// <summary>
/// Before request is made to server 
/// Respond with the specified HTML string to client
/// and the specified status
Expand Down Expand Up @@ -485,12 +495,17 @@ public async Task GenericResponse(byte[] result, Dictionary<string, HttpHeader>
WebSession.Request.CancelRequest = true;
}

/// <summary>
/// Redirect to URL.
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public async Task Redirect(string url)
{
var response = new RedirectResponse();

response.HttpVersion = WebSession.Request.HttpVersion;
response.ResponseHeaders.Add("Location", new Models.HttpHeader("Location", url));
response.ResponseHeaders.Add("Location", new HttpHeader("Location", url));
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty);

await Respond(response);
Expand Down
7 changes: 5 additions & 2 deletions Titanium.Web.Proxy/Exceptions/BodyNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;


namespace Titanium.Web.Proxy.Exceptions
{
/// <summary>
/// An expception thrown when body is unexpectedly empty
/// </summary>
public class BodyNotFoundException : ProxyException
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="message"></param>
public BodyNotFoundException(string message)
: base(message)
{
Expand Down
5 changes: 4 additions & 1 deletion Titanium.Web.Proxy/Extensions/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Titanium.Web.Proxy.Extensions
{
/// <summary>
/// Extension methods for Byte Arrays.
/// </summary>
public static class ByteArrayExtensions
{
/// <summary>
Expand All @@ -14,7 +17,7 @@ public static class ByteArrayExtensions
/// <returns></returns>
public static T[] SubArray<T>(this T[] data, int index, int length)
{
T[] result = new T[length];
var result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
Expand Down
13 changes: 3 additions & 10 deletions Titanium.Web.Proxy/Extensions/TcpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ internal static class TcpExtensions
internal static bool IsConnected(this Socket client)
{
// This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking;
var blockingState = client.Blocking;

try
{
byte[] tmp = new byte[1];
var tmp = new byte[1];

client.Blocking = false;
client.Send(tmp, 0, 0);
Expand All @@ -25,14 +25,7 @@ internal static bool IsConnected(this Socket client)
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
{
return true;
}
else
{
return false;
}
return e.NativeErrorCode.Equals(10035);
}
finally
{
Expand Down
12 changes: 6 additions & 6 deletions Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Shared;

namespace Titanium.Web.Proxy.Helpers
{
Expand All @@ -16,15 +15,15 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
internal class CustomBinaryReader : IDisposable
{
private Stream stream;
private Encoding encoding;
private readonly Stream stream;
private readonly Encoding encoding;

internal CustomBinaryReader(Stream stream)
{
this.stream = stream;

//default to UTF-8
this.encoding = Encoding.UTF8;
encoding = Encoding.UTF8;
}

internal Stream BaseStream => stream;
Expand All @@ -40,7 +39,7 @@ internal async Task<string> ReadLineAsync()
var lastChar = default(char);
var buffer = new byte[1];

while ((await this.stream.ReadAsync(buffer, 0, 1)) > 0)
while ((await stream.ReadAsync(buffer, 0, 1)) > 0)
{
//if new line
if (lastChar == '\r' && buffer[0] == '\n')
Expand Down Expand Up @@ -82,6 +81,7 @@ internal async Task<List<string>> ReadAllLinesAsync()
/// <summary>
/// Read the specified number of raw bytes from the base stream
/// </summary>
/// <param name="bufferSize"></param>
/// <param name="totalBytesToRead"></param>
/// <returns></returns>
internal async Task<byte[]> ReadBytesAsync(int bufferSize, long totalBytesToRead)
Expand All @@ -98,7 +98,7 @@ internal async Task<byte[]> ReadBytesAsync(int bufferSize, long totalBytesToRead

using (var outStream = new MemoryStream())
{
while ((bytesRead += await this.stream.ReadAsync(buffer, 0, bytesToRead)) > 0)
while ((bytesRead += await stream.ReadAsync(buffer, 0, bytesToRead)) > 0)
{
await outStream.WriteAsync(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
Expand Down
Loading