Commit b296e183 authored by Honfika's avatar Honfika

nuget update + CancellationToken parameters added

parent 86a1db20
......@@ -51,8 +51,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="StreamExtended, Version=1.0.141.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.141-beta\lib\net45\StreamExtended.dll</HintPath>
<Reference Include="StreamExtended, Version=1.0.147.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.147-beta\lib\net45\StreamExtended.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="StreamExtended" version="1.0.141-beta" targetFramework="net45" />
<package id="StreamExtended" version="1.0.147-beta" targetFramework="net45" />
</packages>
\ No newline at end of file
......@@ -5,7 +5,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
public class BeforeSslAuthenticateEventArgs : EventArgs
{
internal CancellationTokenSource TaskCancellationSource;
internal readonly CancellationTokenSource TaskCancellationSource;
internal BeforeSslAuthenticateEventArgs(CancellationTokenSource taskCancellationSource)
{
......
......@@ -23,7 +23,7 @@ namespace Titanium.Web.Proxy.EventArguments
protected readonly ExceptionHandler ExceptionFunc;
internal CancellationTokenSource cancellationTokenSource;
internal readonly CancellationTokenSource CancellationTokenSource;
/// <summary>
/// Constructor to initialize the proxy
......@@ -40,7 +40,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
BufferSize = bufferSize;
ExceptionFunc = exceptionFunc;
this.cancellationTokenSource = cancellationTokenSource;
CancellationTokenSource = cancellationTokenSource;
ProxyClient = new ProxyClient();
WebSession = new HttpWebClient(bufferSize, request);
......@@ -151,7 +151,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
public void TerminateSession()
{
cancellationTokenSource.Cancel();
CancellationTokenSource.Cancel();
}
}
}
......@@ -44,7 +44,7 @@ namespace Titanium.Web.Proxy
if (await HttpHelper.IsConnectMethod(clientStream) == 1)
{
//read the first line HTTP command
string httpCmd = await clientStreamReader.ReadLineAsync();
string httpCmd = await clientStreamReader.ReadLineAsync(cancellationTokenSource.Token);
if (string.IsNullOrEmpty(httpCmd))
{
return;
......@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy
await clientStreamWriter.WriteResponseAsync(response);
var clientHelloInfo = await SslTools.PeekClientHello(clientStream);
var clientHelloInfo = await SslTools.PeekClientHello(clientStream, cancellationTokenSource.Token);
bool isClientHello = clientHelloInfo != null;
if (isClientHello)
......@@ -150,7 +150,7 @@ namespace Titanium.Web.Proxy
options.ClientCertificateRequired = false;
options.EnabledSslProtocols = SupportedSslProtocols;
options.CertificateRevocationCheckMode = X509RevocationMode.NoCheck;
await sslStream.AuthenticateAsServerAsync(options, new CancellationToken(false));
await sslStream.AuthenticateAsServerAsync(options, cancellationTokenSource.Token);
//HTTPS server created - we can now decrypt the client's traffic
clientStream = new CustomBufferedStream(sslStream, BufferSize);
......@@ -173,7 +173,7 @@ namespace Titanium.Web.Proxy
}
}
if (connectArgs.cancellationTokenSource.IsCancellationRequested)
if (cancellationTokenSource.IsCancellationRequested)
{
throw new Exception("Session was terminated by user.");
}
......@@ -182,7 +182,7 @@ namespace Titanium.Web.Proxy
if (!decryptSsl || !isClientHello)
{
//create new connection
using (var connection = await GetServerConnection(connectArgs, true))
using (var connection = await GetServerConnection(connectArgs, true, cancellationTokenSource.Token))
{
if (isClientHello)
{
......@@ -195,8 +195,8 @@ namespace Titanium.Web.Proxy
try
{
// clientStream.Available sbould be at most BufferSize because it is using the same buffer size
await clientStream.ReadAsync(data, 0, available);
await connection.StreamWriter.WriteAsync(data, 0, available, true);
await clientStream.ReadAsync(data, 0, available, cancellationTokenSource.Token);
await connection.StreamWriter.WriteAsync(data, 0, available, true, cancellationTokenSource.Token);
}
finally
{
......@@ -204,14 +204,14 @@ namespace Titanium.Web.Proxy
}
}
var serverHelloInfo = await SslTools.PeekServerHello(connection.Stream);
var serverHelloInfo = await SslTools.PeekServerHello(connection.Stream, cancellationTokenSource.Token);
((ConnectResponse)connectArgs.WebSession.Response).ServerHelloInfo = serverHelloInfo;
}
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferSize,
(buffer, offset, count) => { connectArgs.OnDataSent(buffer, offset, count); },
(buffer, offset, count) => { connectArgs.OnDataReceived(buffer, offset, count); },
ExceptionFunc, connectArgs.cancellationTokenSource);
ExceptionFunc, connectArgs.CancellationTokenSource);
}
return;
......
......@@ -2,6 +2,7 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StreamExtended.Helpers;
using StreamExtended.Network;
......@@ -114,12 +115,12 @@ namespace Titanium.Web.Proxy.Helpers
}
}
public async Task WriteAsync(byte[] data, int offset, int count, bool flush)
public async Task WriteAsync(byte[] data, int offset, int count, bool flush, CancellationToken cancellationToken)
{
await WriteAsync(data, offset, count);
await WriteAsync(data, offset, count, cancellationToken);
if (flush)
{
await FlushAsync();
await FlushAsync(cancellationToken);
}
}
......
......@@ -31,10 +31,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <param name="proxyServer"></param>
/// <param name="upStreamEndPoint"></param>
/// <param name="externalProxy"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
internal async Task<TcpConnection> CreateClient(string remoteHostName, int remotePort,
List<SslApplicationProtocol> applicationProtocols, Version httpVersion, bool isHttps, bool isConnect,
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy)
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy, CancellationToken cancellationToken)
{
bool useUpstreamProxy = false;
......@@ -89,7 +90,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
using (var reader = new CustomBinaryReader(stream, proxyServer.BufferSize))
{
string httpStatus = await reader.ReadLineAsync();
string httpStatus = await reader.ReadLineAsync(cancellationToken);
Response.ParseResponseLine(httpStatus, out _, out int statusCode, out string statusDescription);
......@@ -99,7 +100,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
throw new Exception("Upstream proxy failed to create a secure tunnel");
}
await reader.ReadAndIgnoreAllLinesAsync();
await reader.ReadAndIgnoreAllLinesAsync(cancellationToken);
}
}
......@@ -123,7 +124,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
options.ClientCertificates = null;
options.EnabledSslProtocols = proxyServer.SupportedSslProtocols;
options.CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation;
await sslStream.AuthenticateAsClientAsync(options, new CancellationToken(false));
await sslStream.AuthenticateAsClientAsync(options, cancellationToken);
}
client.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
......
......@@ -196,7 +196,7 @@ namespace Titanium.Web.Proxy
if (connection == null)
{
connection = await GetServerConnection(args, false);
connection = await GetServerConnection(args, false, cancellationTokenSource.Token);
}
//if upgrading to websocket then relay the requet without reading the contents
......@@ -230,7 +230,7 @@ namespace Titanium.Web.Proxy
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferSize,
(buffer, offset, count) => { args.OnDataSent(buffer, offset, count); },
(buffer, offset, count) => { args.OnDataReceived(buffer, offset, count); },
ExceptionFunc, args.cancellationTokenSource);
ExceptionFunc, cancellationTokenSource);
return;
}
......@@ -249,7 +249,7 @@ namespace Titanium.Web.Proxy
return;
}
if (args.cancellationTokenSource.IsCancellationRequested)
if (cancellationTokenSource.IsCancellationRequested)
{
throw new Exception("Session was terminated by user.");
}
......@@ -364,8 +364,9 @@ namespace Titanium.Web.Proxy
/// </summary>
/// <param name="args"></param>
/// <param name="isConnect"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<TcpConnection> GetServerConnection(SessionEventArgsBase args, bool isConnect)
private async Task<TcpConnection> GetServerConnection(SessionEventArgsBase args, bool isConnect, CancellationToken cancellationToken)
{
ExternalProxy customUpStreamProxy = null;
......@@ -383,7 +384,8 @@ namespace Titanium.Web.Proxy
args.WebSession.ConnectRequest?.ClientHelloInfo?.GetAlpn(),
args.WebSession.Request.HttpVersion, isHttps, isConnect,
this, args.WebSession.UpStreamEndPoint ?? UpStreamEndPoint,
customUpStreamProxy ?? (isHttps ? UpStreamHttpsProxy : UpStreamHttpProxy));
customUpStreamProxy ?? (isHttps ? UpStreamHttpsProxy : UpStreamHttpProxy),
cancellationToken);
}
/// <summary>
......
......@@ -12,8 +12,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.4" />
<PackageReference Include="StreamExtended" Version="1.0.141-beta" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.2" />
<PackageReference Include="StreamExtended" Version="1.0.147-beta" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
......
......@@ -14,8 +14,8 @@
<copyright>Copyright &#x00A9; Titanium. All rights reserved.</copyright>
<tags></tags>
<dependencies>
<dependency id="StreamExtended" version="1.0.141-beta" />
<dependency id="Portable.BouncyCastle" version="1.8.1.4" />
<dependency id="StreamExtended" version="1.0.147-beta" />
<dependency id="Portable.BouncyCastle" version="1.8.2" />
</dependencies>
</metadata>
<files>
......
......@@ -34,7 +34,7 @@ namespace Titanium.Web.Proxy
try
{
var clientHelloInfo = await SslTools.PeekClientHello(clientStream);
var clientHelloInfo = await SslTools.PeekClientHello(clientStream, cancellationTokenSource.Token);
bool isHttps = clientHelloInfo != null;
string httpsHostName = null;
......@@ -47,7 +47,7 @@ namespace Titanium.Web.Proxy
args.SniHostName = httpsHostName;
await endPoint.InvokeBeforeSslAuthenticate(this, args, ExceptionFunc);
if (args.TaskCancellationSource.IsCancellationRequested)
if (cancellationTokenSource.IsCancellationRequested)
{
throw new Exception("Session was terminated by user.");
}
......@@ -102,9 +102,9 @@ namespace Titanium.Web.Proxy
try
{
// clientStream.Available sbould be at most BufferSize because it is using the same buffer size
await clientStream.ReadAsync(data, 0, available);
await serverStream.WriteAsync(data, 0, available);
await serverStream.FlushAsync();
await clientStream.ReadAsync(data, 0, available, cancellationTokenSource.Token);
await serverStream.WriteAsync(data, 0, available, cancellationTokenSource.Token);
await serverStream.FlushAsync(cancellationTokenSource.Token);
}
finally
{
......@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy
//var serverHelloInfo = await SslTools.PeekServerHello(serverStream);
await TcpHelper.SendRaw(clientStream, serverStream, BufferSize,
null, null, ExceptionFunc, args.TaskCancellationSource);
null, null, ExceptionFunc, cancellationTokenSource);
}
}
}
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Portable.BouncyCastle" version="1.8.1.4" targetFramework="net45" />
<package id="StreamExtended" version="1.0.141-beta" targetFramework="net45" />
<package id="Portable.BouncyCastle" version="1.8.2" targetFramework="net45" />
<package id="StreamExtended" version="1.0.147-beta" targetFramework="net45" />
</packages>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment