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

Commit b3bbac3

Browse files
authored
Merge pull request #698 from justcoding121/master
beta
2 parents 727e750 + 1fe10a9 commit b3bbac3

20 files changed

+98
-79
lines changed

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "NetCore|Debug|Basic Example",
66
"type": "coreclr",
77
"request": "launch",
8-
"program": "${workspaceRoot}/examples/Titanium.Web.Proxy.Examples.Basic/bin/Debug/netcoreapp2.0/Titanium.Web.Proxy.Examples.Basic.NetCore.dll",
8+
"program": "${workspaceRoot}/examples/Titanium.Web.Proxy.Examples.Basic/bin/Debug/netcoreapp3.1/Titanium.Web.Proxy.Examples.Basic.NetCore.dll",
99
"args": [],
1010
"cwd": "${workspaceRoot}",
1111
"stopAtEntry": false,
@@ -16,7 +16,7 @@
1616
"name": "NetCore|Release|Basic Example",
1717
"type": "coreclr",
1818
"request": "launch",
19-
"program": "${workspaceRoot}/examples/Titanium.Web.Proxy.Examples.Basic/bin/Release/netcoreapp2.0/Titanium.Web.Proxy.Examples.Basic.NetCore.dll",
19+
"program": "${workspaceRoot}/examples/Titanium.Web.Proxy.Examples.Basic/bin/Release/netcoreapp3.1/Titanium.Web.Proxy.Examples.Basic.NetCore.dll",
2020
"args": [],
2121
"cwd": "${workspaceRoot}",
2222
"stopAtEntry": false,

examples/Titanium.Web.Proxy.Examples.Basic/Titanium.Web.Proxy.Examples.Basic.NetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
5+
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
<LangVersion>7.1</LangVersion>
88
</PropertyGroup>

examples/Titanium.Web.Proxy.Examples.Basic/Titanium.Web.Proxy.Examples.Basic.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net461;netcoreapp3.0</TargetFrameworks>
5+
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
<LangVersion>7.1</LangVersion>
88
</PropertyGroup>

examples/Titanium.Web.Proxy.Examples.WindowsService/Titanium.Web.Proxy.Examples.WindowsService.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@
103103
</ItemGroup>
104104
<ItemGroup>
105105
<PackageReference Include="Microsoft.Win32.Registry">
106-
<Version>4.6.0</Version>
106+
<Version>4.7.0</Version>
107107
</PackageReference>
108108
<PackageReference Include="System.Numerics.Vectors">
109109
<Version>4.5.0</Version>
110110
</PackageReference>
111111
<PackageReference Include="System.Runtime.CompilerServices.Unsafe">
112-
<Version>4.6.0</Version>
112+
<Version>4.7.0</Version>
113113
</PackageReference>
114114
<PackageReference Include="System.ServiceProcess.ServiceController">
115-
<Version>4.6.0</Version>
115+
<Version>4.7.0</Version>
116116
</PackageReference>
117117
<PackageReference Include="System.Threading.Tasks.Extensions">
118118
<Version>4.5.3</Version>

examples/Titanium.Web.Proxy.Examples.Wpf/Titanium.Web.Proxy.Examples.Wpf.NetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFrameworks>netcoreapp3.0</TargetFrameworks>
5+
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
66
<UseWPF>true</UseWPF>
77
</PropertyGroup>
88

examples/Titanium.Web.Proxy.Examples.Wpf/Titanium.Web.Proxy.Examples.Wpf.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFrameworks>net461;netcoreapp3.0</TargetFrameworks>
5+
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
66
<UseWPF>true</UseWPF>
77
</PropertyGroup>
88

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using Titanium.Web.Proxy.Network.Tcp;
3+
4+
namespace Titanium.Web.Proxy.EventArguments
5+
{
6+
public class EmptyProxyEventArgs : ProxyEventArgsBase
7+
{
8+
internal EmptyProxyEventArgs(TcpClientConnection clientConnection) : base(clientConnection)
9+
{
10+
}
11+
}
12+
}

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private async Task readResponseBodyAsync(CancellationToken cancellationToken)
190190
private async Task<byte[]> readBodyAsync(bool isRequest, CancellationToken cancellationToken)
191191
{
192192
using var bodyStream = new MemoryStream();
193-
using var writer = new HttpStream(bodyStream, BufferPool);
193+
using var writer = new HttpStream(bodyStream, BufferPool, cancellationToken);
194194

195195
if (isRequest)
196196
{

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
3333
var cancellationTokenSource = new CancellationTokenSource();
3434
var cancellationToken = cancellationTokenSource.Token;
3535

36-
var clientStream = new HttpClientStream(clientConnection, clientConnection.GetStream(), BufferPool);
36+
var clientStream = new HttpClientStream(clientConnection, clientConnection.GetStream(), BufferPool, cancellationToken);
3737

3838
Task<TcpServerConnection>? prefetchConnectionTask = null;
3939
bool closeServerConnection = false;
4040
bool calledRequestHandler = false;
4141

42-
SslStream? sslStream = null;
43-
4442
try
4543
{
4644
TunnelConnectSessionEventArgs? connectArgs = null;
@@ -191,6 +189,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
191189
}
192190

193191
X509Certificate2? certificate = null;
192+
SslStream? sslStream = null;
194193
try
195194
{
196195
sslStream = new SslStream(clientStream, false);
@@ -221,14 +220,16 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
221220
#endif
222221

223222
// HTTPS server created - we can now decrypt the client's traffic
224-
clientStream = new HttpClientStream(clientStream.Connection, sslStream, BufferPool);
223+
clientStream = new HttpClientStream(clientStream.Connection, sslStream, BufferPool, cancellationToken);
225224
sslStream = null; // clientStream was created, no need to keep SSL stream reference
226225

227226
clientStream.DataRead += (o, args) => connectArgs.OnDecryptedDataSent(args.Buffer, args.Offset, args.Count);
228227
clientStream.DataWrite += (o, args) => connectArgs.OnDecryptedDataReceived(args.Buffer, args.Offset, args.Count);
229228
}
230229
catch (Exception e)
231230
{
231+
sslStream?.Dispose();
232+
232233
var certName = certificate?.GetNameInfo(X509NameType.SimpleName, false);
233234
throw new ProxyConnectException(
234235
$"Couldn't authenticate host '{connectHostname}' with certificate '{certName}'.", e, connectArgs);
@@ -401,12 +402,16 @@ await Http2Helper.SendHttp2(clientStream, connection.Stream,
401402
}
402403
finally
403404
{
405+
if (!cancellationTokenSource.IsCancellationRequested)
406+
{
407+
cancellationTokenSource.Cancel();
408+
}
409+
404410
if (!calledRequestHandler)
405411
{
406412
await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection);
407413
}
408414

409-
sslStream?.Dispose();
410415
clientStream.Dispose();
411416
}
412417
}

src/Titanium.Web.Proxy/Extensions/StreamExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ internal static async Task CopyToAsync(this Stream input, Stream output, Action<
4242
{
4343
// cancellation is not working on Socket ReadAsync
4444
// https://github.com/dotnet/corefx/issues/15033
45-
int num = await input.ReadAsync(buffer, 0, buffer.Length, CancellationToken.None)
46-
.withCancellation(cancellationToken);
45+
int num = await input.ReadAsync(buffer, 0, buffer.Length, cancellationToken)
46+
.WithCancellation(cancellationToken);
4747
int bytesRead;
4848
if ((bytesRead = num) != 0 && !cancellationToken.IsCancellationRequested)
4949
{
@@ -62,7 +62,7 @@ internal static async Task CopyToAsync(this Stream input, Stream output, Action<
6262
}
6363
}
6464

65-
private static async Task<T> withCancellation<T>(this Task<T> task, CancellationToken cancellationToken) where T : struct
65+
internal static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken) where T : struct
6666
{
6767
var tcs = new TaskCompletionSource<bool>();
6868
using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))

src/Titanium.Web.Proxy/Helpers/HttpClientStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ internal sealed class HttpClientStream : HttpStream
1212
{
1313
public TcpClientConnection Connection { get; }
1414

15-
internal HttpClientStream(TcpClientConnection connection, Stream stream, IBufferPool bufferPool)
16-
: base(stream, bufferPool)
15+
internal HttpClientStream(TcpClientConnection connection, Stream stream, IBufferPool bufferPool, CancellationToken cancellationToken)
16+
: base(stream, bufferPool, cancellationToken)
1717
{
1818
Connection = connection;
1919
}

src/Titanium.Web.Proxy/Helpers/HttpServerStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Titanium.Web.Proxy.Helpers
1010
{
1111
internal sealed class HttpServerStream : HttpStream
1212
{
13-
internal HttpServerStream(Stream stream, IBufferPool bufferPool)
14-
: base(stream, bufferPool)
13+
internal HttpServerStream(Stream stream, IBufferPool bufferPool, CancellationToken cancellationToken)
14+
: base(stream, bufferPool, cancellationToken)
1515
{
1616
}
1717

0 commit comments

Comments
 (0)