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

Commit c6150bf

Browse files
authored
Merge pull request #650 from justcoding121/master
beta
2 parents 43e2988 + 16cccdc commit c6150bf

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class SessionEventArgsBase : EventArgs, IDisposable
2222
{
2323
private static bool isWindowsAuthenticationSupported => RunTime.IsWindows;
2424

25-
internal readonly CancellationTokenSource CancellationTokenSource;
25+
internal readonly CancellationTokenSource? CancellationTokenSource;
2626

2727
internal TcpServerConnection ServerConnection => HttpClient.Connection;
2828

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System;
1+
using System;
2+
using System.Reflection;
23
using System.Text;
34
using System.Runtime.InteropServices;
5+
using System.Runtime.Versioning;
46

57
namespace Titanium.Web.Proxy.Helpers
68
{
@@ -41,6 +43,62 @@ public static class RunTime
4143
public static bool IsUwpOnWindows => IsWindows && UwpHelper.IsRunningAsUwp();
4244

4345
public static bool IsMac => isRunningOnMac;
46+
47+
/// <summary>
48+
/// Is socket reuse available to use?
49+
/// </summary>
50+
public static bool IsSocketReuseAvailable => isSocketReuseAvailable();
51+
52+
private static bool? _isSocketReuseAvailable;
53+
54+
private static bool isSocketReuseAvailable()
55+
{
56+
// use the cached value if we have one
57+
if (_isSocketReuseAvailable != null)
58+
return _isSocketReuseAvailable.Value;
59+
60+
try
61+
{
62+
if (IsWindows)
63+
{
64+
// since we are on windows just return true
65+
// store the result in our static object so we don't have to be bothered going through all this more than once
66+
_isSocketReuseAvailable = true;
67+
return true;
68+
}
69+
70+
// get the currently running framework name and version (EX: .NETFramework,Version=v4.5.1) (Ex: .NETCoreApp,Version=v2.0)
71+
string ver = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
72+
73+
if (ver == null)
74+
return false; // play it safe if we can not figure out what the framework is
75+
76+
// make sure we are on .NETCoreApp
77+
ver = ver.ToLower(); // make everything lowercase to simplify comparison
78+
if (ver.Contains(".netcoreapp"))
79+
{
80+
var versionString = ver.Replace(".netcoreapp,version=v", "");
81+
var versionArr = versionString.Split('.');
82+
var majorVersion = Convert.ToInt32(versionArr[0]);
83+
84+
var result = majorVersion >= 3; // version 3 and up supports socket reuse
85+
86+
// store the result in our static object so we don't have to be bothered going through all this more than once
87+
_isSocketReuseAvailable = result;
88+
return result;
89+
}
90+
91+
// store the result in our static object so we don't have to be bothered going through all this more than once
92+
_isSocketReuseAvailable = false;
93+
return false;
94+
}
95+
catch
96+
{
97+
// store the result in our static object so we don't have to be bothered going through all this more than once
98+
_isSocketReuseAvailable = false;
99+
return false;
100+
}
101+
}
44102

45103
// https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
46104
private class UwpHelper

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
315315
tcpClient.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
316316
tcpClient.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds);
317317

318-
// linux has a bug with socket reuse in .net core.
319-
if (proxyServer.ReuseSocket && RunTime.IsWindows)
318+
if (proxyServer.ReuseSocket && RunTime.IsSocketReuseAvailable)
320319
{
321320
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
322321
}
@@ -417,7 +416,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
417416
throw;
418417
}
419418

420-
return new TcpServerConnection(proxyServer, tcpClient)
419+
return new TcpServerConnection(proxyServer, tcpClient, stream)
421420
{
422421
UpStreamProxy = externalProxy,
423422
UpStreamEndPoint = upStreamEndPoint,
@@ -426,8 +425,6 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
426425
IsHttps = isHttps,
427426
NegotiatedApplicationProtocol = negotiatedApplicationProtocol,
428427
UseUpstreamProxy = useUpstreamProxy,
429-
StreamWriter = new HttpRequestWriter(stream, proxyServer.BufferPool),
430-
Stream = stream,
431428
Version = httpVersion
432429
};
433430
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
1515
/// </summary>
1616
internal class TcpServerConnection : IDisposable
1717
{
18-
internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient)
18+
internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, CustomBufferedStream stream)
1919
{
2020
this.tcpClient = tcpClient;
2121
LastAccess = DateTime.Now;
2222
this.proxyServer = proxyServer;
2323
this.proxyServer.UpdateServerConnectionCount(true);
24+
StreamWriter = new HttpRequestWriter(stream, proxyServer.BufferPool);
25+
Stream = stream;
2426
}
2527

2628
private ProxyServer proxyServer { get; }
@@ -59,12 +61,12 @@ internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient)
5961
/// <summary>
6062
/// Used to write lines to server
6163
/// </summary>
62-
internal HttpRequestWriter? StreamWriter { get; set; }
64+
internal HttpRequestWriter StreamWriter { get; }
6365

6466
/// <summary>
6567
/// Server stream
6668
/// </summary>
67-
internal CustomBufferedStream? Stream { get; set; }
69+
internal CustomBufferedStream Stream { get; }
6870

6971
/// <summary>
7072
/// Last time this connection was used

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public partial class ProxyServer : IDisposable
5555
/// <summary>
5656
/// Upstream proxy manager.
5757
/// </summary>
58-
private WinHttpWebProxyFinder systemProxyResolver;
58+
private WinHttpWebProxyFinder? systemProxyResolver;
5959

6060

6161
/// <inheritdoc />
@@ -421,7 +421,7 @@ public void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
421421
/// <param name="protocolType">The proxy protocol type.</param>
422422
public void SetAsSystemProxy(ExplicitProxyEndPoint endPoint, ProxyProtocolType protocolType)
423423
{
424-
if (!RunTime.IsWindows)
424+
if (systemProxySettingsManager == null)
425425
{
426426
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
427427
Please manually confugure you operating system to use this proxy's port and address.");
@@ -515,7 +515,7 @@ public void DisableSystemHttpsProxy()
515515
/// </summary>
516516
public void RestoreOriginalProxySettings()
517517
{
518-
if (!RunTime.IsWindows)
518+
if (systemProxySettingsManager == null)
519519
{
520520
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
521521
Please manually configure your operating system to use this proxy's port and address.");
@@ -529,7 +529,7 @@ public void RestoreOriginalProxySettings()
529529
/// </summary>
530530
public void DisableSystemProxy(ProxyProtocolType protocolType)
531531
{
532-
if (!RunTime.IsWindows)
532+
if (systemProxySettingsManager == null)
533533
{
534534
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
535535
Please manually configure your operating system to use this proxy's port and address.");
@@ -543,7 +543,7 @@ public void DisableSystemProxy(ProxyProtocolType protocolType)
543543
/// </summary>
544544
public void DisableAllSystemProxies()
545545
{
546-
if (!RunTime.IsWindows)
546+
if (systemProxySettingsManager == null)
547547
{
548548
throw new NotSupportedException(@"Setting system proxy settings are only supported in Windows.
549549
Please manually confugure you operating system to use this proxy's port and address.");
@@ -622,7 +622,7 @@ public void Stop()
622622
throw new Exception("Proxy is not running.");
623623
}
624624

625-
if (RunTime.IsWindows && !RunTime.IsUwpOnWindows)
625+
if (systemProxySettingsManager != null)
626626
{
627627
bool setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>()
628628
.Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
@@ -654,8 +654,7 @@ private void listen(ProxyEndPoint endPoint)
654654
{
655655
endPoint.Listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
656656

657-
// linux/macOS has a bug with socket reuse in .net core.
658-
if (ReuseSocket && RunTime.IsWindows)
657+
if (ReuseSocket && RunTime.IsSocketReuseAvailable)
659658
{
660659
endPoint.Listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
661660
}

0 commit comments

Comments
 (0)