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

Commit 17abd9b

Browse files
authored
Merge pull request #711 from justcoding121/master
stable
2 parents 30b1823 + d21ea35 commit 17abd9b

33 files changed

+213
-139
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/ProxyTestController.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ private async Task onRequest(object sender, SessionEventArgs e)
207207
{
208208
e.GetState().PipelineInfo.AppendLine(nameof(onRequest) + ":" + e.HttpClient.Request.RequestUri);
209209

210+
var clientLocalIp = e.ClientLocalEndPoint.Address;
211+
if (!clientLocalIp.Equals(IPAddress.Loopback) && !clientLocalIp.Equals(IPAddress.IPv6Loopback))
212+
{
213+
e.HttpClient.UpStreamEndPoint = new IPEndPoint(clientLocalIp, 0);
214+
}
215+
216+
if (e.HttpClient.Request.Url.Contains("yahoo.com"))
217+
{
218+
e.CustomUpStreamProxy = new ExternalProxy("localhost", 8888);
219+
}
220+
210221
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
211222
await writeToConsole(e.HttpClient.Request.Url);
212223

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/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<GridViewColumn Header="Process" DisplayMemberBinding="{Binding Process}" />
3232
<GridViewColumn Header="SentBytes" DisplayMemberBinding="{Binding SentDataCount}" />
3333
<GridViewColumn Header="ReceivedBytes" DisplayMemberBinding="{Binding ReceivedDataCount}" />
34-
<GridViewColumn Header="ClientEndPoint" DisplayMemberBinding="{Binding ClientEndPoint}" />
34+
<GridViewColumn Header="ClientRemoteEndPoint" DisplayMemberBinding="{Binding ClientRemoteEndPoint}" />
3535
<GridViewColumn Header="ClientConnectionId" DisplayMemberBinding="{Binding ClientConnectionId}" />
3636
<GridViewColumn Header="ServerConnectionId" DisplayMemberBinding="{Binding ServerConnectionId}" />
3737
</GridView>

examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private SessionListItem createSessionListItem(SessionEventArgsBase e)
229229
ClientConnectionId = e.ClientConnectionId,
230230
ServerConnectionId = e.ServerConnectionId,
231231
HttpClient = e.HttpClient,
232-
ClientEndPoint = e.ClientEndPoint,
232+
ClientRemoteEndPoint = e.ClientRemoteEndPoint,
233+
ClientLocalEndPoint = e.ClientLocalEndPoint,
233234
IsTunnelConnect = isTunnelConnect
234235
};
235236

examples/Titanium.Web.Proxy.Examples.Wpf/SessionListItem.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public Guid ServerConnectionId
3838

3939
public HttpWebClient HttpClient { get; set; }
4040

41-
public IPEndPoint ClientEndPoint { get; set; }
41+
public IPEndPoint ClientLocalEndPoint { get; set; }
42+
43+
public IPEndPoint ClientRemoteEndPoint { get; set; }
4244

4345
public bool IsTunnelConnect { get; set; }
4446

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class SessionEventArgs : SessionEventArgsBase
2525
/// </summary>
2626
private bool reRequest;
2727

28-
private WebSocketDecoder webSocketDecoder;
28+
private WebSocketDecoder? webSocketDecoder;
2929

3030
/// <summary>
3131
/// Is this session a HTTP/2 promise?
@@ -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
{
@@ -221,7 +221,7 @@ internal async Task SyphonOutBodyAsync(bool isRequest, CancellationToken cancell
221221

222222
var reader = isRequest ? (HttpStream)ClientStream : HttpClient.Connection.Stream;
223223

224-
await reader.CopyBodyAsync(requestResponse, true, new NullWriter(), TransformationMode.None, null, cancellationToken);
224+
await reader.CopyBodyAsync(requestResponse, true, NullWriter.Instance, TransformationMode.None, null, cancellationToken);
225225
}
226226

227227
/// <summary>

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private protected SessionEventArgsBase(ProxyServer server, ProxyEndPoint endPoin
5959

6060
ClientStream = clientStream;
6161
HttpClient = new HttpWebClient(connectRequest, request, new Lazy<int>(() => clientStream.Connection.GetProcessId(endPoint)));
62-
LocalEndPoint = endPoint;
62+
ProxyEndPoint = endPoint;
6363
EnableWinAuth = server.EnableWinAuth && isWindowsAuthenticationSupported;
6464
}
6565

@@ -94,9 +94,17 @@ public bool EnableWinAuth
9494
public bool IsHttps => HttpClient.Request.IsHttps;
9595

9696
/// <summary>
97-
/// Client End Point.
97+
/// Client Local End Point.
9898
/// </summary>
99-
public IPEndPoint ClientEndPoint => (IPEndPoint)ClientConnection.RemoteEndPoint;
99+
public IPEndPoint ClientLocalEndPoint => (IPEndPoint)ClientConnection.LocalEndPoint;
100+
101+
/// <summary>
102+
/// Client Remote End Point.
103+
/// </summary>
104+
public IPEndPoint ClientRemoteEndPoint => (IPEndPoint)ClientConnection.RemoteEndPoint;
105+
106+
[Obsolete("Use ClientRemoteEndPoint instead.")]
107+
public IPEndPoint ClientEndPoint => ClientRemoteEndPoint;
100108

101109
/// <summary>
102110
/// The web client used to communicate with server for this session.
@@ -106,6 +114,14 @@ public bool EnableWinAuth
106114
[Obsolete("Use HttpClient instead.")]
107115
public HttpWebClient WebSession => HttpClient;
108116

117+
/// <summary>
118+
/// Gets or sets the custom up stream proxy.
119+
/// </summary>
120+
/// <value>
121+
/// The custom up stream proxy.
122+
/// </value>
123+
public IExternalProxy? CustomUpStreamProxy { get; set; }
124+
109125
/// <summary>
110126
/// Are we using a custom upstream HTTP(S) proxy?
111127
/// </summary>
@@ -114,12 +130,15 @@ public bool EnableWinAuth
114130
/// <summary>
115131
/// Local endpoint via which we make the request.
116132
/// </summary>
117-
public ProxyEndPoint LocalEndPoint { get; }
133+
public ProxyEndPoint ProxyEndPoint { get; }
134+
135+
[Obsolete("Use ProxyEndPoint instead.")]
136+
public ProxyEndPoint LocalEndPoint => ProxyEndPoint;
118137

119138
/// <summary>
120139
/// Is this a transparent endpoint?
121140
/// </summary>
122-
public bool IsTransparent => LocalEndPoint is TransparentProxyEndPoint;
141+
public bool IsTransparent => ProxyEndPoint is TransparentProxyEndPoint;
123142

124143
/// <summary>
125144
/// The last exception that happened.

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ 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;
40-
bool calledRequestHandler = false;
41-
42-
SslStream? sslStream = null;
4340

4441
try
4542
{
@@ -191,6 +188,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
191188
}
192189

193190
X509Certificate2? certificate = null;
191+
SslStream? sslStream = null;
194192
try
195193
{
196194
sslStream = new SslStream(clientStream, false);
@@ -221,14 +219,16 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
221219
#endif
222220

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

227225
clientStream.DataRead += (o, args) => connectArgs.OnDecryptedDataSent(args.Buffer, args.Offset, args.Count);
228226
clientStream.DataWrite += (o, args) => connectArgs.OnDecryptedDataReceived(args.Buffer, args.Offset, args.Count);
229227
}
230228
catch (Exception e)
231229
{
230+
sslStream?.Dispose();
231+
232232
var certName = certificate?.GetNameInfo(X509NameType.SimpleName, false);
233233
throw new ProxyConnectException(
234234
$"Couldn't authenticate host '{connectHostname}' with certificate '{certName}'.", e, connectArgs);
@@ -374,10 +374,11 @@ await Http2Helper.SendHttp2(clientStream, connection.Stream,
374374
}
375375
}
376376

377-
calledRequestHandler = true;
377+
var prefetchTask = prefetchConnectionTask;
378+
prefetchConnectionTask = null;
378379

379380
// Now create the request
380-
await handleHttpSessionRequest(endPoint, clientStream, cancellationTokenSource, connectArgs, prefetchConnectionTask);
381+
await handleHttpSessionRequest(endPoint, clientStream, cancellationTokenSource, connectArgs, prefetchTask);
381382
}
382383
catch (ProxyException e)
383384
{
@@ -401,12 +402,13 @@ await Http2Helper.SendHttp2(clientStream, connection.Stream,
401402
}
402403
finally
403404
{
404-
if (!calledRequestHandler)
405+
if (!cancellationTokenSource.IsCancellationRequested)
405406
{
406-
await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection);
407+
cancellationTokenSource.Cancel();
407408
}
408409

409-
sslStream?.Dispose();
410+
await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection);
411+
410412
clientStream.Dispose();
411413
}
412414
}

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)