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

Commit e255369

Browse files
authored
Merge pull request #647 from ByronAP/beta
Enable socket reuse based on framework
2 parents ed0800f + dfb69fd commit e255369

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

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,7 +43,63 @@ 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;
4472

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+
}
102+
45103
// https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
46104
private class UwpHelper
47105
{

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

Lines changed: 1 addition & 2 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
}

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)