Commit b296e183 authored by Honfika's avatar Honfika

nuget update + CancellationToken parameters added

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