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

Commit 27ccf18

Browse files
committed
#543 Fix reverse proxy. add tests.
1 parent 8db8280 commit 27ccf18

File tree

12 files changed

+561
-238
lines changed

12 files changed

+561
-238
lines changed

src/Titanium.Web.Proxy/TransparentClientHandler.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
3434
var clientStream = new CustomBufferedStream(clientConnection.GetStream(), BufferPool, BufferSize);
3535
var clientStreamWriter = new HttpResponseWriter(clientStream, BufferPool, BufferSize);
3636

37-
Task<TcpServerConnection> prefetchConnectionTask = null;
38-
bool closeServerConnection = false;
39-
bool calledRequestHandler = false;
40-
4137
try
4238
{
4339
var clientHelloInfo = await SslTools.PeekClientHello(clientStream, BufferPool, cancellationToken);
@@ -63,16 +59,7 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
6359

6460
if (endPoint.DecryptSsl && args.DecryptSsl)
6561
{
66-
if(EnableTcpServerConnectionPrefetch)
67-
{
68-
//don't pass cancellation token here
69-
//it could cause floating server connections when client exits
70-
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(httpsHostName, endPoint.Port,
71-
httpVersion: null, isHttps: true, applicationProtocols: null, isConnect: false,
72-
proxyServer: this, session: null, upStreamEndPoint: UpStreamEndPoint, externalProxy: UpStreamHttpsProxy,
73-
noCache: false, cancellationToken: CancellationToken.None);
74-
}
75-
62+
7663
SslStream sslStream = null;
7764

7865
//do client authentication using fake certificate
@@ -140,39 +127,29 @@ await TcpHelper.SendRaw(clientStream, serverStream, BufferPool, BufferSize,
140127
return;
141128
}
142129
}
143-
calledRequestHandler = true;
144130
// HTTPS server created - we can now decrypt the client's traffic
145131
// Now create the request
146132
await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
147-
cancellationTokenSource, isHttps ? httpsHostName : null, null, prefetchConnectionTask);
133+
cancellationTokenSource, isHttps ? httpsHostName : null, null, null);
148134
}
149135
catch (ProxyException e)
150136
{
151-
closeServerConnection = true;
152137
onException(clientStream, e);
153138
}
154139
catch (IOException e)
155140
{
156-
closeServerConnection = true;
157141
onException(clientStream, new Exception("Connection was aborted", e));
158142
}
159143
catch (SocketException e)
160144
{
161-
closeServerConnection = true;
162145
onException(clientStream, new Exception("Could not connect", e));
163146
}
164147
catch (Exception e)
165148
{
166-
closeServerConnection = true;
167149
onException(clientStream, new Exception("Error occured in whilst handling the client", e));
168150
}
169151
finally
170152
{
171-
if (!calledRequestHandler)
172-
{
173-
await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection);
174-
}
175-
176153
clientStream.Dispose();
177154

178155
if (!cancellationTokenSource.IsCancellationRequested)

tests/Titanium.Web.Proxy.IntegrationTests/Utilities/TestHelper.cs renamed to tests/Titanium.Web.Proxy.IntegrationTests/Helpers/TestHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
using System.Net;
33
using System.Net.Http;
44

5-
namespace Titanium.Web.Proxy.IntegrationTests
5+
namespace Titanium.Web.Proxy.IntegrationTests.Helpers
66
{
77
public static class TestHelper
88
{
9-
public static HttpClient CreateHttpClient(int localProxyPort)
9+
public static HttpClient GetHttpClient(int localProxyPort)
1010
{
1111
var proxy = new TestProxy($"http://localhost:{localProxyPort}");
1212

@@ -19,6 +19,11 @@ public static HttpClient CreateHttpClient(int localProxyPort)
1919
return new HttpClient(handler);
2020
}
2121

22+
public static HttpClient GetHttpClient()
23+
{
24+
return new HttpClient(new HttpClientHandler());
25+
}
26+
2227
public class TestProxy : IWebProxy
2328
{
2429
public Uri ProxyUri { get; set; }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace Titanium.Web.Proxy.IntegrationTests
9+
{
10+
[TestClass]
11+
public class HttpsTests
12+
{
13+
[TestMethod]
14+
public async Task Can_Handle_Https_Request()
15+
{
16+
var testSuite = new TestSuite();
17+
18+
var server = testSuite.GetServer();
19+
server.HandleRequest((context) =>
20+
{
21+
return context.Response.WriteAsync("I am server. I received your greetings.");
22+
});
23+
24+
var proxy = testSuite.GetProxy();
25+
var client = testSuite.GetClient(proxy);
26+
27+
var response = await client.PostAsync(new Uri(server.ListeningHttpsUrl),
28+
new StringContent("hello server. I am a client."));
29+
30+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
31+
var body = await response.Content.ReadAsStringAsync();
32+
33+
Assert.AreEqual("I am server. I received your greetings.", body);
34+
}
35+
36+
}
37+
}

0 commit comments

Comments
 (0)