Unverified Commit 41fd8c1b authored by honfika's avatar honfika Committed by GitHub

Merge pull request #336 from justcoding121/develop

unnecessary conditinal codes removed (since .netstandard2 supports th…
parents e7046616 65850f44
...@@ -108,12 +108,15 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -108,12 +108,15 @@ namespace Titanium.Web.Proxy.Examples.Basic
foreach (var endPoint in proxyServer.ProxyEndPoints) foreach (var endPoint in proxyServer.ProxyEndPoints)
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port); Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
#if NET45 #if NETSTANDARD2_0
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
{
//Only explicit proxies can be set as system proxy! //Only explicit proxies can be set as system proxy!
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint); //proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint); //proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp); proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
#endif }
} }
public void Stop() public void Stop()
......
#if NET45
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
namespace Titanium.Web.Proxy.Extensions
{
internal static class DotNet45Extensions
{
/// <summary>
/// Disposes the specified client.
/// Int .NET framework 4.5 the TcpClient class has no Dispose method,
/// it is available from .NET 4.6, see
/// https://msdn.microsoft.com/en-us/library/dn823304(v=vs.110).aspx
/// </summary>
/// <param name="client">The client.</param>
internal static void Dispose(this TcpClient client)
{
client.Close();
}
/// <summary>
/// Disposes the specified store.
/// Int .NET framework 4.5 the X509Store class has no Dispose method,
/// it is available from .NET 4.6, see
/// https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.dispose(v=vs.110).aspx
/// </summary>
/// <param name="store">The store.</param>
internal static void Dispose(this X509Store store)
{
store.Close();
}
}
}
#endif
#if NET45 using System;
using System;
using System.IO; using System.IO;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
...@@ -51,4 +50,3 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -51,4 +50,3 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
} }
#endif
#if NET45 using System;
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
...@@ -22,4 +21,3 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -22,4 +21,3 @@ namespace Titanium.Web.Proxy.Helpers
internal delegate bool ConsoleEventDelegate(int eventType); internal delegate bool ConsoleEventDelegate(int eventType);
} }
} }
#endif
...@@ -25,7 +25,6 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -25,7 +25,6 @@ namespace Titanium.Web.Proxy.Helpers
return FindProcessIdFromLocalPort(port, IpVersion.Ipv6); return FindProcessIdFromLocalPort(port, IpVersion.Ipv6);
} }
#if NET45
/// <summary> /// <summary>
/// Adapated from below link /// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost /// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
...@@ -75,56 +74,5 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -75,56 +74,5 @@ namespace Titanium.Web.Proxy.Helpers
return isLocalhost; return isLocalhost;
} }
#else
/// <summary>
/// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
internal static bool IsLocalIpAddress(IPAddress address)
{
// get local IP addresses
var localIPs = Dns.GetHostAddressesAsync(Dns.GetHostName()).Result;
// test if any host IP equals to any local IP or to localhost
return IPAddress.IsLoopback(address) || localIPs.Contains(address);
}
internal static bool IsLocalIpAddress(string hostName)
{
bool isLocalhost = false;
var localhost = Dns.GetHostEntryAsync("127.0.0.1").Result;
if (hostName == localhost.HostName)
{
var hostEntry = Dns.GetHostEntryAsync(hostName).Result;
isLocalhost = hostEntry.AddressList.Any(IPAddress.IsLoopback);
}
if (!isLocalhost)
{
localhost = Dns.GetHostEntryAsync(Dns.GetHostName()).Result;
IPAddress ipAddress;
if (IPAddress.TryParse(hostName, out ipAddress))
isLocalhost = localhost.AddressList.Any(x => x.Equals(ipAddress));
if (!isLocalhost)
{
try
{
var hostEntry = Dns.GetHostEntryAsync(hostName).Result;
isLocalhost = localhost.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
}
catch (SocketException)
{
}
}
}
return isLocalhost;
}
#endif
} }
} }
#if NET45 using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
...@@ -199,4 +198,3 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -199,4 +198,3 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
} }
#endif
#if NET45 using System;
using System;
using System.Linq; using System.Linq;
using Microsoft.Win32; using Microsoft.Win32;
...@@ -328,4 +327,3 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -328,4 +327,3 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
} }
#endif
#if NET45 using System;
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// Helper classes for setting system proxy settings // Helper classes for setting system proxy settings
...@@ -131,4 +130,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp ...@@ -131,4 +130,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
} }
} }
} }
#endif
#if NET45
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
...@@ -18,4 +17,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp ...@@ -18,4 +17,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
public override bool IsInvalid => handle == IntPtr.Zero; public override bool IsInvalid => handle == IntPtr.Zero;
} }
} }
#endif
#if NET45
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
...@@ -331,4 +330,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp ...@@ -331,4 +330,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
} }
} }
} }
#endif
...@@ -265,6 +265,7 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -265,6 +265,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
X509Certificate2 signingCert = null) X509Certificate2 signingCert = null)
{ {
X509Certificate2 rCert = null; X509Certificate2 rCert = null;
#if NET45
if (switchToMTAIfNeeded && Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA) if (switchToMTAIfNeeded && Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA)
{ {
var manualResetEvent = new ManualResetEvent(false); var manualResetEvent = new ManualResetEvent(false);
...@@ -277,6 +278,7 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -277,6 +278,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
manualResetEvent.Close(); manualResetEvent.Close();
return rCert; return rCert;
} }
#endif
//Subject //Subject
string fullSubject = $"CN={sSubjectCN}"; string fullSubject = $"CN={sSubjectCN}";
......
...@@ -349,7 +349,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -349,7 +349,7 @@ namespace Titanium.Web.Proxy.Network
} }
finally finally
{ {
x509Store.Dispose(); x509Store.Close();
} }
} }
...@@ -471,8 +471,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -471,8 +471,8 @@ namespace Titanium.Web.Proxy.Network
} }
finally finally
{ {
x509RootStore.Dispose(); x509RootStore.Close();
x509PersonalStore.Dispose(); x509PersonalStore.Close();
} }
} }
...@@ -511,8 +511,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -511,8 +511,8 @@ namespace Titanium.Web.Proxy.Network
} }
finally finally
{ {
x509RootStore.Dispose(); x509RootStore.Close();
x509PersonalStore.Dispose(); x509PersonalStore.Close();
} }
} }
......
...@@ -72,7 +72,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -72,7 +72,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
//It helps to avoid eventual deterioration of performance due to TCP port exhaustion //It helps to avoid eventual deterioration of performance due to TCP port exhaustion
//due to default TCP CLOSE_WAIT timeout for 4 minutes //due to default TCP CLOSE_WAIT timeout for 4 minutes
TcpClient.LingerState = new LingerOption(true, 0); TcpClient.LingerState = new LingerOption(true, 0);
TcpClient.Dispose(); TcpClient.Close();
} }
} }
catch catch
......
...@@ -52,12 +52,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -52,12 +52,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
try try
{ {
#if NET45
client = new TcpClient(upStreamEndPoint); client = new TcpClient(upStreamEndPoint);
#else
client = new TcpClient();
client.Client.Bind(upStreamEndPoint);
#endif
//If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections //If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections
if (useUpstreamProxy) if (useUpstreamProxy)
...@@ -119,7 +114,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -119,7 +114,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
catch (Exception) catch (Exception)
{ {
stream?.Dispose(); stream?.Dispose();
client?.Dispose(); client?.Close();
throw; throw;
} }
......
...@@ -11,9 +11,7 @@ using System.Threading.Tasks; ...@@ -11,9 +11,7 @@ using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Extensions; using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
#if NET45
using Titanium.Web.Proxy.Helpers.WinHttp; using Titanium.Web.Proxy.Helpers.WinHttp;
#endif
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network; using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.Network.Tcp; using Titanium.Web.Proxy.Network.Tcp;
...@@ -26,13 +24,8 @@ namespace Titanium.Web.Proxy ...@@ -26,13 +24,8 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public partial class ProxyServer : IDisposable public partial class ProxyServer : IDisposable
{ {
#if NET45
internal static readonly string UriSchemeHttp = Uri.UriSchemeHttp; internal static readonly string UriSchemeHttp = Uri.UriSchemeHttp;
internal static readonly string UriSchemeHttps = Uri.UriSchemeHttps; internal static readonly string UriSchemeHttps = Uri.UriSchemeHttps;
#else
internal const string UriSchemeHttp = "http";
internal const string UriSchemeHttps = "https";
#endif
/// <summary> /// <summary>
/// An default exception log func /// An default exception log func
...@@ -64,7 +57,6 @@ namespace Titanium.Web.Proxy ...@@ -64,7 +57,6 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
private TcpConnectionFactory tcpConnectionFactory { get; } private TcpConnectionFactory tcpConnectionFactory { get; }
#if NET45
private WinHttpWebProxyFinder systemProxyResolver; private WinHttpWebProxyFinder systemProxyResolver;
/// <summary> /// <summary>
...@@ -76,7 +68,6 @@ namespace Titanium.Web.Proxy ...@@ -76,7 +68,6 @@ namespace Titanium.Web.Proxy
/// Set firefox to use default system proxy /// Set firefox to use default system proxy
/// </summary> /// </summary>
private readonly FireFoxProxySettingsManager firefoxProxySettingsManager = new FireFoxProxySettingsManager(); private readonly FireFoxProxySettingsManager firefoxProxySettingsManager = new FireFoxProxySettingsManager();
#endif
/// <summary> /// <summary>
/// Buffer size used throughout this proxy /// Buffer size used throughout this proxy
...@@ -281,11 +272,11 @@ namespace Titanium.Web.Proxy ...@@ -281,11 +272,11 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// List of supported Ssl versions /// List of supported Ssl versions
/// </summary> /// </summary>
public SslProtocols SupportedSslProtocols { get; set; } =
#if NET45 #if NET45
public SslProtocols SupportedSslProtocols { get; set; } = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3; SslProtocols.Ssl3 |
#else
public SslProtocols SupportedSslProtocols { get; set; } = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
#endif #endif
SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
/// <summary> /// <summary>
/// Total number of active client connections /// Total number of active client connections
...@@ -317,12 +308,10 @@ namespace Titanium.Web.Proxy ...@@ -317,12 +308,10 @@ namespace Titanium.Web.Proxy
ProxyEndPoints = new List<ProxyEndPoint>(); ProxyEndPoints = new List<ProxyEndPoint>();
tcpConnectionFactory = new TcpConnectionFactory(); tcpConnectionFactory = new TcpConnectionFactory();
#if NET45 if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
if (!RunTime.IsRunningOnMono)
{ {
systemProxySettingsManager = new SystemProxyManager(); systemProxySettingsManager = new SystemProxyManager();
} }
#endif
CertificateManager = new CertificateManager(ExceptionFunc); CertificateManager = new CertificateManager(ExceptionFunc);
if (rootCertificateName != null) if (rootCertificateName != null)
...@@ -375,7 +364,6 @@ namespace Titanium.Web.Proxy ...@@ -375,7 +364,6 @@ namespace Titanium.Web.Proxy
} }
} }
#if NET45
/// <summary> /// <summary>
/// Set the given explicit end point as the default proxy server for current machine /// Set the given explicit end point as the default proxy server for current machine
/// </summary> /// </summary>
...@@ -521,7 +509,6 @@ namespace Titanium.Web.Proxy ...@@ -521,7 +509,6 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager.DisableAllProxy(); systemProxySettingsManager.DisableAllProxy();
} }
#endif
/// <summary> /// <summary>
/// Start this proxy server /// Start this proxy server
...@@ -533,10 +520,9 @@ namespace Titanium.Web.Proxy ...@@ -533,10 +520,9 @@ namespace Titanium.Web.Proxy
throw new Exception("Proxy is already running."); throw new Exception("Proxy is already running.");
} }
#if NET45
//clear any system proxy settings which is pointing to our own endpoint (causing a cycle) //clear any system proxy settings which is pointing to our own endpoint (causing a cycle)
//due to non gracious proxy shutdown before or something else //due to non gracious proxy shutdown before or something else
if (systemProxySettingsManager != null) if (systemProxySettingsManager != null && RunTime.IsWindows)
{ {
var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry(); var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry();
if (proxyInfo.Proxies != null) if (proxyInfo.Proxies != null)
...@@ -568,7 +554,6 @@ namespace Titanium.Web.Proxy ...@@ -568,7 +554,6 @@ namespace Titanium.Web.Proxy
GetCustomUpStreamProxyFunc = GetSystemUpStreamProxy; GetCustomUpStreamProxyFunc = GetSystemUpStreamProxy;
} }
#endif
ProxyRunning = true; ProxyRunning = true;
...@@ -597,8 +582,7 @@ namespace Titanium.Web.Proxy ...@@ -597,8 +582,7 @@ namespace Titanium.Web.Proxy
throw new Exception("Proxy is not running."); throw new Exception("Proxy is not running.");
} }
#if NET45 if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
if (!RunTime.IsRunningOnMono)
{ {
bool setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy); bool setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
...@@ -607,7 +591,6 @@ namespace Titanium.Web.Proxy ...@@ -607,7 +591,6 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager.RestoreOriginalSettings(); systemProxySettingsManager.RestoreOriginalSettings();
} }
} }
#endif
foreach (var endPoint in ProxyEndPoints) foreach (var endPoint in ProxyEndPoints)
{ {
...@@ -656,7 +639,6 @@ namespace Titanium.Web.Proxy ...@@ -656,7 +639,6 @@ namespace Titanium.Web.Proxy
CertificateManager?.Dispose(); CertificateManager?.Dispose();
} }
#if NET45
/// <summary> /// <summary>
/// Listen on the given end point on local machine /// Listen on the given end point on local machine
/// </summary> /// </summary>
...@@ -670,29 +652,6 @@ namespace Titanium.Web.Proxy ...@@ -670,29 +652,6 @@ namespace Titanium.Web.Proxy
// accept clients asynchronously // accept clients asynchronously
endPoint.Listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint); endPoint.Listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
} }
#else
private async void Listen(ProxyEndPoint endPoint)
{
endPoint.Listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
endPoint.Listener.Start();
endPoint.Port = ((IPEndPoint)endPoint.Listener.LocalEndpoint).Port;
while (ProxyRunning)
{
try
{
TcpClient tcpClient = await endPoint.Listener.AcceptTcpClientAsync();
if (tcpClient != null)
Task.Run(async () => HandleClient(tcpClient, endPoint));
}
catch (ObjectDisposedException)
{
// proxy was stopped
}
}
}
#endif
/// <summary> /// <summary>
/// Verifiy if its safe to set this end point as System proxy /// Verifiy if its safe to set this end point as System proxy
...@@ -713,7 +672,6 @@ namespace Titanium.Web.Proxy ...@@ -713,7 +672,6 @@ namespace Titanium.Web.Proxy
} }
} }
#if NET45
/// <summary> /// <summary>
/// Gets the system up stream proxy. /// Gets the system up stream proxy.
/// </summary> /// </summary>
...@@ -724,7 +682,6 @@ namespace Titanium.Web.Proxy ...@@ -724,7 +682,6 @@ namespace Titanium.Web.Proxy
var proxy = systemProxyResolver.GetProxy(sessionEventArgs.WebSession.Request.RequestUri); var proxy = systemProxyResolver.GetProxy(sessionEventArgs.WebSession.Request.RequestUri);
return Task.FromResult(proxy); return Task.FromResult(proxy);
} }
#endif
private void EnsureRootCertificate() private void EnsureRootCertificate()
{ {
...@@ -739,7 +696,6 @@ namespace Titanium.Web.Proxy ...@@ -739,7 +696,6 @@ namespace Titanium.Web.Proxy
} }
} }
#if NET45
/// <summary> /// <summary>
/// When a connection is received from client act /// When a connection is received from client act
/// </summary> /// </summary>
...@@ -778,7 +734,6 @@ namespace Titanium.Web.Proxy ...@@ -778,7 +734,6 @@ namespace Titanium.Web.Proxy
// Get the listener that handles the client request. // Get the listener that handles the client request.
endPoint.Listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint); endPoint.Listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
} }
#endif
private async Task HandleClient(TcpClient tcpClient, ProxyEndPoint endPoint) private async Task HandleClient(TcpClient tcpClient, ProxyEndPoint endPoint)
{ {
...@@ -811,7 +766,7 @@ namespace Titanium.Web.Proxy ...@@ -811,7 +766,7 @@ namespace Titanium.Web.Proxy
//It helps to avoid eventual deterioration of performance due to TCP port exhaustion //It helps to avoid eventual deterioration of performance due to TCP port exhaustion
//due to default TCP CLOSE_WAIT timeout for 4 minutes //due to default TCP CLOSE_WAIT timeout for 4 minutes
tcpClient.LingerState = new LingerOption(true, 0); tcpClient.LingerState = new LingerOption(true, 0);
tcpClient.Dispose(); tcpClient.Close();
} }
} }
catch catch
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Win32.Registry">
<Version>4.4.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Principal.Windows"> <PackageReference Include="System.Security.Principal.Windows">
<Version>4.4.0</Version> <Version>4.4.0</Version>
</PackageReference> </PackageReference>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment