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

Commit f9add9f

Browse files
Merge pull request #569 from justcoding121/bugfix/fix-fake-tunnel-failure
Fix fake tunnel failure
2 parents 6fa32c7 + ac02c68 commit f9add9f

File tree

7 files changed

+80
-29
lines changed

7 files changed

+80
-29
lines changed

docs/api/Titanium.Web.Proxy.ProxyServer.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ <h5 class="parameters">Parameters</h5>
11781178
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_DisableAllSystemProxies.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.DisableAllSystemProxies%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
11791179
</span>
11801180
<span class="small pull-right mobile-hide">
1181-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L524">View Source</a>
1181+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L526">View Source</a>
11821182
</span>
11831183
<a id="Titanium_Web_Proxy_ProxyServer_DisableAllSystemProxies_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableAllSystemProxies*"></a>
11841184
<h4 id="Titanium_Web_Proxy_ProxyServer_DisableAllSystemProxies" data-uid="Titanium.Web.Proxy.ProxyServer.DisableAllSystemProxies">DisableAllSystemProxies()</h4>
@@ -1194,7 +1194,7 @@ <h5 class="decalaration">Declaration</h5>
11941194
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_DisableSystemHttpProxy.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.DisableSystemHttpProxy%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
11951195
</span>
11961196
<span class="small pull-right mobile-hide">
1197-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L495">View Source</a>
1197+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L496">View Source</a>
11981198
</span>
11991199
<a id="Titanium_Web_Proxy_ProxyServer_DisableSystemHttpProxy_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemHttpProxy*"></a>
12001200
<h4 id="Titanium_Web_Proxy_ProxyServer_DisableSystemHttpProxy" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemHttpProxy">DisableSystemHttpProxy()</h4>
@@ -1210,7 +1210,7 @@ <h5 class="decalaration">Declaration</h5>
12101210
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_DisableSystemHttpsProxy.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.DisableSystemHttpsProxy%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
12111211
</span>
12121212
<span class="small pull-right mobile-hide">
1213-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L503">View Source</a>
1213+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L504">View Source</a>
12141214
</span>
12151215
<a id="Titanium_Web_Proxy_ProxyServer_DisableSystemHttpsProxy_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemHttpsProxy*"></a>
12161216
<h4 id="Titanium_Web_Proxy_ProxyServer_DisableSystemHttpsProxy" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemHttpsProxy">DisableSystemHttpsProxy()</h4>
@@ -1226,7 +1226,7 @@ <h5 class="decalaration">Declaration</h5>
12261226
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_DisableSystemProxy_Titanium_Web_Proxy_Models_ProxyProtocolType_.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.DisableSystemProxy(Titanium.Web.Proxy.Models.ProxyProtocolType)%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
12271227
</span>
12281228
<span class="small pull-right mobile-hide">
1229-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L511">View Source</a>
1229+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L512">View Source</a>
12301230
</span>
12311231
<a id="Titanium_Web_Proxy_ProxyServer_DisableSystemProxy_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemProxy*"></a>
12321232
<h4 id="Titanium_Web_Proxy_ProxyServer_DisableSystemProxy_Titanium_Web_Proxy_Models_ProxyProtocolType_" data-uid="Titanium.Web.Proxy.ProxyServer.DisableSystemProxy(Titanium.Web.Proxy.Models.ProxyProtocolType)">DisableSystemProxy(ProxyProtocolType)</h4>
@@ -1259,7 +1259,7 @@ <h5 class="parameters">Parameters</h5>
12591259
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_Dispose.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.Dispose%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
12601260
</span>
12611261
<span class="small pull-right mobile-hide">
1262-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L851">View Source</a>
1262+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L854">View Source</a>
12631263
</span>
12641264
<a id="Titanium_Web_Proxy_ProxyServer_Dispose_" data-uid="Titanium.Web.Proxy.ProxyServer.Dispose*"></a>
12651265
<h4 id="Titanium_Web_Proxy_ProxyServer_Dispose" data-uid="Titanium.Web.Proxy.ProxyServer.Dispose">Dispose()</h4>
@@ -1418,7 +1418,7 @@ <h5 class="parameters">Parameters</h5>
14181418
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_Start.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.Start%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
14191419
</span>
14201420
<span class="small pull-right mobile-hide">
1421-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L537">View Source</a>
1421+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L540">View Source</a>
14221422
</span>
14231423
<a id="Titanium_Web_Proxy_ProxyServer_Start_" data-uid="Titanium.Web.Proxy.ProxyServer.Start*"></a>
14241424
<h4 id="Titanium_Web_Proxy_ProxyServer_Start" data-uid="Titanium.Web.Proxy.ProxyServer.Start">Start()</h4>
@@ -1434,7 +1434,7 @@ <h5 class="decalaration">Declaration</h5>
14341434
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_ProxyServer_Stop.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.ProxyServer.Stop%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
14351435
</span>
14361436
<span class="small pull-right mobile-hide">
1437-
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L595">View Source</a>
1437+
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/ProxyServer.cs/#L598">View Source</a>
14381438
</span>
14391439
<a id="Titanium_Web_Proxy_ProxyServer_Stop_" data-uid="Titanium.Web.Proxy.ProxyServer.Stop*"></a>
14401440
<h4 id="Titanium_Web_Proxy_ProxyServer_Stop" data-uid="Titanium.Web.Proxy.ProxyServer.Stop">Stop()</h4>

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
4747
{
4848
string connectHostname = null;
4949
TunnelConnectSessionEventArgs connectArgs = null;
50-
50+
5151

5252
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
5353
if (await HttpHelper.IsConnectMethod(clientStream) == 1)
@@ -154,11 +154,17 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
154154

155155
if (EnableTcpServerConnectionPrefetch)
156156
{
157-
//don't pass cancellation token here
158-
//it could cause floating server connections when client exits
159-
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(this, connectArgs,
160-
isConnect: true, applicationProtocols: null, noCache: false,
161-
cancellationToken: CancellationToken.None);
157+
//make sure the host can be resolved before creating the prefetch task
158+
var ipAddresses = await Dns.GetHostAddressesAsync(connectArgs.HttpClient.Request.RequestUri.Host);
159+
160+
if (ipAddresses != null && ipAddresses.Length > 0)
161+
{
162+
//don't pass cancellation token here
163+
//it could cause floating server connections when client exits
164+
prefetchConnectionTask = tcpConnectionFactory.GetServerConnection(this, connectArgs,
165+
isConnect: true, applicationProtocols: null, noCache: false,
166+
cancellationToken: CancellationToken.None);
167+
}
162168
}
163169

164170
X509Certificate2 certificate = null;

src/Titanium.Web.Proxy/Network/CertificateManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Diagnostics;
@@ -149,7 +149,7 @@ public CertificateEngine CertificateEngine
149149
set
150150
{
151151
// For Mono (or Non-Windows) only Bouncy Castle is supported
152-
if (!RunTime.IsWindows || RunTime.IsRunningOnMono)
152+
if (!RunTime.IsWindows)
153153
{
154154
value = CertificateEngine.BouncyCastle;
155155
}
@@ -662,7 +662,7 @@ public void TrustRootCertificate(bool machineTrusted = false)
662662
/// <returns>True if success.</returns>
663663
public bool TrustRootCertificateAsAdmin(bool machineTrusted = false)
664664
{
665-
if (!RunTime.IsWindows || RunTime.IsRunningOnMono)
665+
if (!RunTime.IsWindows)
666666
{
667667
return false;
668668
}
@@ -805,7 +805,7 @@ public void RemoveTrustedRootCertificate(bool machineTrusted = false)
805805
/// <returns>Should also remove from machine store?</returns>
806806
public bool RemoveTrustedRootCertificateAsAdmin(bool machineTrusted = false)
807807
{
808-
if (!RunTime.IsWindows || RunTime.IsRunningOnMono)
808+
if (!RunTime.IsWindows)
809809
{
810810
return false;
811811
}

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
280280
};
281281

282282
//linux has a bug with socket reuse in .net core.
283-
if (proxyServer.ReuseSocket && (RunTime.IsWindows || RunTime.IsRunningOnMono))
283+
if (proxyServer.ReuseSocket && RunTime.IsWindows)
284284
{
285285
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
286286
}

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
102102

103103
ProxyEndPoints = new List<ProxyEndPoint>();
104104
tcpConnectionFactory = new TcpConnectionFactory(this);
105-
if (!RunTime.IsRunningOnMono && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
105+
if (RunTime.IsWindows && !RunTime.IsUwpOnWindows)
106106
{
107107
systemProxySettingsManager = new SystemProxyManager();
108108
}
@@ -417,9 +417,10 @@ public void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
417417
/// <param name="protocolType">The proxy protocol type.</param>
418418
public void SetAsSystemProxy(ExplicitProxyEndPoint endPoint, ProxyProtocolType protocolType)
419419
{
420-
if (RunTime.IsRunningOnMono)
420+
if (!RunTime.IsWindows)
421421
{
422-
throw new Exception("Mono Runtime do not support system proxy settings.");
422+
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
423+
Please manually confugure you operating system to use this proxy's port and address.");
423424
}
424425

425426
validateEndPointAsSystemProxy(endPoint);
@@ -510,9 +511,10 @@ public void DisableSystemHttpsProxy()
510511
/// </summary>
511512
public void DisableSystemProxy(ProxyProtocolType protocolType)
512513
{
513-
if (RunTime.IsRunningOnMono)
514+
if (!RunTime.IsWindows)
514515
{
515-
throw new Exception("Mono Runtime do not support system proxy settings.");
516+
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
517+
Please manually confugure you operating system to use this proxy's port and address.");
516518
}
517519

518520
systemProxySettingsManager.RemoveProxy(protocolType);
@@ -523,9 +525,10 @@ public void DisableSystemProxy(ProxyProtocolType protocolType)
523525
/// </summary>
524526
public void DisableAllSystemProxies()
525527
{
526-
if (RunTime.IsRunningOnMono)
528+
if (!RunTime.IsWindows)
527529
{
528-
throw new Exception("Mono Runtime do not support system proxy settings.");
530+
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
531+
Please manually confugure you operating system to use this proxy's port and address.");
529532
}
530533

531534
systemProxySettingsManager.DisableAllProxy();
@@ -599,7 +602,7 @@ public void Stop()
599602
throw new Exception("Proxy is not running.");
600603
}
601604

602-
if (!RunTime.IsRunningOnMono && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
605+
if (RunTime.IsWindows && !RunTime.IsUwpOnWindows)
603606
{
604607
bool setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>()
605608
.Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
@@ -632,7 +635,7 @@ private void listen(ProxyEndPoint endPoint)
632635
endPoint.Listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
633636

634637
//linux/macOS has a bug with socket reuse in .net core.
635-
if (ReuseSocket && (RunTime.IsWindows || RunTime.IsRunningOnMono))
638+
if (ReuseSocket && RunTime.IsWindows)
636639
{
637640
endPoint.Listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
638641
}

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
5+
using System.Net.Sockets;
56
#if NETCOREAPP2_1
67
using System.Net.Security;
78
#endif
@@ -27,7 +28,7 @@ namespace Titanium.Web.Proxy
2728
public partial class ProxyServer
2829
{
2930
private bool isWindowsAuthenticationEnabledAndSupported =>
30-
EnableWinAuth && RunTime.IsWindows && !RunTime.IsRunningOnMono;
31+
EnableWinAuth && RunTime.IsWindows;
3132

3233
/// <summary>
3334
/// This is the core request handler method for a particular connection from client.
@@ -188,8 +189,20 @@ await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
188189
//If prefetch task is available.
189190
if (connection == null && prefetchTask != null)
190191
{
191-
connection = await prefetchTask;
192+
try
193+
{
194+
connection = await prefetchTask;
195+
}
196+
catch (SocketException e)
197+
{
198+
if(e.SocketErrorCode != SocketError.HostNotFound)
199+
{
200+
throw;
201+
}
202+
}
203+
192204
prefetchTask = null;
205+
193206
}
194207

195208
// create a new connection if cache key changes.

tests/Titanium.Web.Proxy.IntegrationTests/HttpsTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,34 @@ public async Task Can_Handle_Https_Request()
3333
Assert.AreEqual("I am server. I received your greetings.", body);
3434
}
3535

36+
[TestMethod]
37+
public async Task Can_Handle_Https_Fake_Tunnel_Request()
38+
{
39+
var testSuite = new TestSuite();
40+
41+
var server = testSuite.GetServer();
42+
server.HandleRequest((context) =>
43+
{
44+
return context.Response.WriteAsync("I am server. I received your greetings.");
45+
});
46+
47+
var proxy = testSuite.GetProxy();
48+
proxy.BeforeRequest += async (sender, e) =>
49+
{
50+
e.HttpClient.Request.RequestUri = new Uri(server.ListeningHttpUrl);
51+
await Task.FromResult(0);
52+
};
53+
54+
var client = testSuite.GetClient(proxy);
55+
56+
var response = await client.PostAsync(new Uri($"https://{Guid.NewGuid().ToString()}.com"),
57+
new StringContent("hello server. I am a client."));
58+
59+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
60+
var body = await response.Content.ReadAsStringAsync();
61+
62+
Assert.AreEqual("I am server. I received your greetings.", body);
63+
}
64+
3665
}
3766
}

0 commit comments

Comments
 (0)