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

Commit

Permalink
Merge pull request #655 from justcoding121/master
Browse files Browse the repository at this point in the history
beta
  • Loading branch information
honfika authored Nov 16, 2019
2 parents c6150bf + 7325e32 commit 183f960
Show file tree
Hide file tree
Showing 59 changed files with 786 additions and 889 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ private async Task writeToConsole(string message, bool useRedColor = false)
}

@lock.Release();

}

///// <summary>
Expand Down
11 changes: 3 additions & 8 deletions src/Titanium.Web.Proxy/CertificateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ internal bool ValidateServerCertificate(object sender, X509Certificate certifica
// if user callback is registered then do it
if (ServerCertificateValidationCallback != null)
{
var args = new CertificateValidationEventArgs
{
Certificate = certificate,
Chain = chain,
SslPolicyErrors = sslPolicyErrors
};
var args = new CertificateValidationEventArgs(certificate, chain, sslPolicyErrors);

// why is the sender null?
ServerCertificateValidationCallback.InvokeAsync(this, args, exceptionFunc).Wait();
ServerCertificateValidationCallback.InvokeAsync(this, args, ExceptionFunc).Wait();
return args.IsValid;
}

Expand Down Expand Up @@ -90,7 +85,7 @@ internal bool ValidateServerCertificate(object sender, X509Certificate certifica
};

// why is the sender null?
ClientCertificateSelectionCallback.InvokeAsync(this, args, exceptionFunc).Wait();
ClientCertificateSelectionCallback.InvokeAsync(this, args, ExceptionFunc).Wait();
return args.ClientCertificate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
public class CertificateValidationEventArgs : EventArgs
{
public CertificateValidationEventArgs(X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Certificate = certificate;
Chain = chain;
SslPolicyErrors = sslPolicyErrors;
}

/// <summary>
/// Server certificate.
/// </summary>
public X509Certificate Certificate { get; internal set; }
public X509Certificate Certificate { get; }

/// <summary>
/// Certificate chain.
/// </summary>
public X509Chain Chain { get; internal set; }
public X509Chain Chain { get; }

/// <summary>
/// SSL policy errors.
/// </summary>
public SslPolicyErrors SslPolicyErrors { get; internal set; }
public SslPolicyErrors SslPolicyErrors { get; }

/// <summary>
/// Is the given server certificate valid?
Expand Down
12 changes: 7 additions & 5 deletions src/Titanium.Web.Proxy/EventArguments/LimitedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ namespace Titanium.Web.Proxy.EventArguments
internal class LimitedStream : Stream
{
private readonly IBufferPool bufferPool;
private readonly ICustomStreamReader baseStream;
private readonly CustomBufferedStream baseStream;
private readonly bool isChunked;
private long bytesRemaining;

private bool readChunkTrail;

internal LimitedStream(ICustomStreamReader baseStream, IBufferPool bufferPool, bool isChunked,
internal LimitedStream(CustomBufferedStream baseStream, IBufferPool bufferPool, bool isChunked,
long contentLength)
{
this.baseStream = baseStream;
Expand Down Expand Up @@ -49,12 +49,12 @@ private void getNextChunk()
if (readChunkTrail)
{
// read the chunk trail of the previous chunk
string s = baseStream.ReadLineAsync().Result;
string? s = baseStream.ReadLineAsync().Result;
}

readChunkTrail = true;

string chunkHead = baseStream.ReadLineAsync().Result;
string? chunkHead = baseStream.ReadLineAsync().Result;
int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
if (idx >= 0)
{
Expand All @@ -73,7 +73,9 @@ private void getNextChunk()
bytesRemaining = -1;

// chunk trail
baseStream.ReadLineAsync().Wait();
var task = baseStream.ReadLineAsync();
if (!task.IsCompleted)
task.AsTask().Wait();
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Http.Responses;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.StreamExtended.Network;

namespace Titanium.Web.Proxy.EventArguments
Expand Down Expand Up @@ -36,15 +37,8 @@ public class SessionEventArgs : SessionEventArgsBase
/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal SessionEventArgs(ProxyServer server, ProxyEndPoint endPoint,
CancellationTokenSource cancellationTokenSource)
: this(server, endPoint, null, cancellationTokenSource)
{
}

protected SessionEventArgs(ProxyServer server, ProxyEndPoint endPoint,
Request? request, CancellationTokenSource cancellationTokenSource)
: base(server, endPoint, cancellationTokenSource, request)
internal SessionEventArgs(ProxyServer server, ProxyEndPoint endPoint, ProxyClient proxyClient, ConnectRequest? connectRequest, CancellationTokenSource cancellationTokenSource)
: base(server, endPoint, proxyClient, connectRequest, null, cancellationTokenSource)
{
}

Expand Down Expand Up @@ -72,7 +66,7 @@ public bool ReRequest
/// </summary>
public event EventHandler<MultipartRequestPartSentEventArgs>? MultipartRequestPartSent;

private ICustomStreamReader getStreamReader(bool isRequest)
private CustomBufferedStream getStreamReader(bool isRequest)
{
return isRequest ? ProxyClient.ClientStream : HttpClient.Connection.Stream;
}
Expand Down Expand Up @@ -333,7 +327,7 @@ private async Task copyBodyAsync(bool isRequest, bool useOriginalHeaderValues, H
/// Read a line from the byte stream
/// </summary>
/// <returns></returns>
private async Task<long> readUntilBoundaryAsync(ICustomStreamReader reader, long totalBytesToRead, ReadOnlyMemory<char> boundary, CancellationToken cancellationToken)
private async Task<long> readUntilBoundaryAsync(ILineStream reader, long totalBytesToRead, ReadOnlyMemory<char> boundary, CancellationToken cancellationToken)
{
int bufferDataLength = 0;

Expand Down
18 changes: 6 additions & 12 deletions src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class SessionEventArgsBase : EventArgs, IDisposable
{
private static bool isWindowsAuthenticationSupported => RunTime.IsWindows;

internal readonly CancellationTokenSource? CancellationTokenSource;
internal readonly CancellationTokenSource CancellationTokenSource;

internal TcpServerConnection ServerConnection => HttpClient.Connection;

Expand All @@ -40,25 +40,19 @@ public abstract class SessionEventArgsBase : EventArgs, IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="SessionEventArgsBase" /> class.
/// </summary>
private SessionEventArgsBase(ProxyServer server)
private protected SessionEventArgsBase(ProxyServer server, ProxyEndPoint endPoint,
ProxyClient proxyClient, ConnectRequest? connectRequest, Request? request, CancellationTokenSource cancellationTokenSource)
{
BufferPool = server.BufferPool;
ExceptionFunc = server.ExceptionFunc;
TimeLine["Session Created"] = DateTime.Now;
}

protected SessionEventArgsBase(ProxyServer server, ProxyEndPoint endPoint,
CancellationTokenSource cancellationTokenSource,
Request? request) : this(server)
{
CancellationTokenSource = cancellationTokenSource;

ProxyClient = new ProxyClient();
HttpClient = new HttpWebClient(request);
ProxyClient = proxyClient;
HttpClient = new HttpWebClient(connectRequest, request, new Lazy<int>(() => ProxyClient.Connection.GetProcessId(endPoint)));
LocalEndPoint = endPoint;
EnableWinAuth = server.EnableWinAuth && isWindowsAuthenticationSupported;

HttpClient.ProcessId = new Lazy<int>(() => ProxyClient.Connection.GetProcessId(endPoint));
}

/// <summary>
Expand All @@ -84,7 +78,7 @@ public bool EnableWinAuth
get => enableWinAuth;
set
{
if (!isWindowsAuthenticationSupported)
if (value && !isWindowsAuthenticationSupported)
throw new Exception("Windows Authentication is not supported");

enableWinAuth = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.StreamExtended.Network;

namespace Titanium.Web.Proxy.EventArguments
Expand All @@ -14,10 +15,9 @@ public class TunnelConnectSessionEventArgs : SessionEventArgsBase
private bool? isHttpsConnect;

internal TunnelConnectSessionEventArgs(ProxyServer server, ProxyEndPoint endPoint, ConnectRequest connectRequest,
CancellationTokenSource cancellationTokenSource)
: base(server, endPoint, cancellationTokenSource, connectRequest)
ProxyClient proxyClient, CancellationTokenSource cancellationTokenSource)
: base(server, endPoint, proxyClient, connectRequest, connectRequest, cancellationTokenSource)
{
HttpClient.ConnectRequest = connectRequest;
}

/// <summary>
Expand Down
17 changes: 7 additions & 10 deletions src/Titanium.Web.Proxy/ExplicitClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Http2;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.Network.Tcp;
using Titanium.Web.Proxy.StreamExtended;
using Titanium.Web.Proxy.StreamExtended.Network;
Expand Down Expand Up @@ -53,32 +54,30 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
if (await HttpHelper.IsConnectMethod(clientStream, BufferPool, cancellationToken) == 1)
{
// read the first line HTTP command
string httpCmd = await clientStream.ReadLineAsync(cancellationToken);
string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (string.IsNullOrEmpty(httpCmd))
{
return;
}

Request.ParseRequestLine(httpCmd, out string _, out string httpUrl, out var version);
Request.ParseRequestLine(httpCmd!, out string _, out string httpUrl, out var version);

var httpRemoteUri = new Uri("http://" + httpUrl);
connectHostname = httpRemoteUri.Host;

var connectRequest = new ConnectRequest
{
RequestUri = httpRemoteUri,
OriginalUrl = httpUrl,
OriginalUrlData = HttpHeader.Encoding.GetBytes(httpUrl),
HttpVersion = version
};

await HeaderParser.ReadHeaders(clientStream, connectRequest.Headers, cancellationToken);

connectArgs = new TunnelConnectSessionEventArgs(this, endPoint, connectRequest,
cancellationTokenSource);
new ProxyClient(clientConnection, clientStream, clientStreamWriter), cancellationTokenSource);
clientStream.DataRead += (o, args) => connectArgs.OnDataSent(args.Buffer, args.Offset, args.Count);
clientStream.DataWrite += (o, args) => connectArgs.OnDataReceived(args.Buffer, args.Offset, args.Count);
connectArgs.ProxyClient.Connection = clientConnection;
connectArgs.ProxyClient.ClientStream = clientStream;

await endPoint.InvokeBeforeTunnelConnectRequest(this, connectArgs, ExceptionFunc);

Expand Down Expand Up @@ -303,7 +302,7 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
if (connectArgs != null && await HttpHelper.IsPriMethod(clientStream, BufferPool, cancellationToken) == 1)
{
// todo
string httpCmd = await clientStream.ReadLineAsync(cancellationToken);
string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (httpCmd == "PRI * HTTP/2.0")
{
connectArgs.HttpClient.ConnectRequest!.TunnelType = TunnelType.Http2;
Expand Down Expand Up @@ -336,10 +335,8 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
var connectionPreface = new ReadOnlyMemory<byte>(Http2Helper.ConnectionPreface);
await connection.StreamWriter.WriteAsync(connectionPreface, cancellationToken);
await Http2Helper.SendHttp2(clientStream, connection.Stream,
() => new SessionEventArgs(this, endPoint, cancellationTokenSource)
() => new SessionEventArgs(this, endPoint, new ProxyClient(clientConnection, clientStream, clientStreamWriter), connectArgs?.HttpClient.ConnectRequest, cancellationTokenSource)
{
ProxyClient = { Connection = clientConnection },
HttpClient = { ConnectRequest = connectArgs?.HttpClient.ConnectRequest },
UserData = connectArgs?.UserData
},
async args => { await onBeforeRequest(args); },
Expand Down
27 changes: 27 additions & 0 deletions src/Titanium.Web.Proxy/Extensions/HttpHeaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Titanium.Web.Proxy.Models;

namespace Titanium.Web.Proxy.Extensions
{
internal static class HttpHeaderExtensions
{
internal static string GetString(this ByteString str)
{
return GetString(str.Span);
}

internal static string GetString(this ReadOnlySpan<byte> bytes)
{
#if NETSTANDARD2_1
return HttpHeader.Encoding.GetString(bytes);
#else
return HttpHeader.Encoding.GetString(bytes.ToArray());
#endif
}

internal static ByteString GetByteString(this string str)
{
return HttpHeader.Encoding.GetBytes(str);
}
}
}
10 changes: 5 additions & 5 deletions src/Titanium.Web.Proxy/Extensions/SslExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ internal class SslClientAuthenticationOptions
{
internal bool AllowRenegotiation { get; set; }

internal string TargetHost { get; set; }
internal string? TargetHost { get; set; }

internal X509CertificateCollection ClientCertificates { get; set; }
internal X509CertificateCollection? ClientCertificates { get; set; }

internal LocalCertificateSelectionCallback LocalCertificateSelectionCallback { get; set; }
internal LocalCertificateSelectionCallback? LocalCertificateSelectionCallback { get; set; }

internal SslProtocols EnabledSslProtocols { get; set; }

internal X509RevocationMode CertificateRevocationCheckMode { get; set; }

internal List<SslApplicationProtocol> ApplicationProtocols { get; set; }
internal List<SslApplicationProtocol>? ApplicationProtocols { get; set; }

internal RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get; set; }
internal RemoteCertificateValidationCallback? RemoteCertificateValidationCallback { get; set; }

internal EncryptionPolicy EncryptionPolicy { get; set; }
}
Expand Down
1 change: 1 addition & 0 deletions src/Titanium.Web.Proxy/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using Titanium.Web.Proxy.Models;

namespace Titanium.Web.Proxy.Extensions
{
Expand Down
3 changes: 2 additions & 1 deletion src/Titanium.Web.Proxy/Extensions/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace Titanium.Web.Proxy.Extensions
{
Expand All @@ -12,5 +13,5 @@ internal static string GetOriginalPathAndQuery(this Uri uri)

return uri.IsWellFormedOriginalString() ? uri.PathAndQuery : uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
}
}
}
}
Loading

0 comments on commit 183f960

Please sign in to comment.