Commit aa3a0147 authored by Honfika's avatar Honfika

unnecessary conditinal codes removed (since .netstandard2 supports them). they...

unnecessary conditinal codes removed (since .netstandard2 supports them). they are platform specifix, not target framework specific.
parent fe1d64ff
......@@ -108,12 +108,15 @@ namespace Titanium.Web.Proxy.Examples.Basic
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);
#if NET45
#if NETSTANDARD2_0
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
#endif
{
//Only explicit proxies can be set as system proxy!
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
#endif
}
}
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;
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;
namespace Titanium.Web.Proxy.Helpers
......@@ -22,4 +21,3 @@ namespace Titanium.Web.Proxy.Helpers
internal delegate bool ConsoleEventDelegate(int eventType);
}
}
#endif
......@@ -25,7 +25,6 @@ namespace Titanium.Web.Proxy.Helpers
return FindProcessIdFromLocalPort(port, IpVersion.Ipv6);
}
#if NET45
/// <summary>
/// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
......@@ -75,56 +74,5 @@ namespace Titanium.Web.Proxy.Helpers
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.Linq;
using System.Text;
......@@ -199,4 +198,3 @@ namespace Titanium.Web.Proxy.Helpers
}
}
}
#endif
#if NET45
using System;
using System;
using System.Linq;
using Microsoft.Win32;
......@@ -328,4 +327,3 @@ namespace Titanium.Web.Proxy.Helpers
}
}
}
#endif
#if NET45
using System;
using System;
using System.Runtime.InteropServices;
// Helper classes for setting system proxy settings
......@@ -131,4 +130,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
}
}
}
#endif
#if NET45
using System;
using System.Runtime.InteropServices;
......@@ -18,4 +17,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
public override bool IsInvalid => handle == IntPtr.Zero;
}
}
#endif
#if NET45
using System;
using System.Collections.Generic;
using System.Net;
......@@ -331,4 +330,3 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
}
}
}
#endif
......@@ -265,6 +265,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
X509Certificate2 signingCert = null)
{
X509Certificate2 rCert = null;
#if NET45
if (switchToMTAIfNeeded && Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA)
{
var manualResetEvent = new ManualResetEvent(false);
......@@ -277,6 +278,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
manualResetEvent.Close();
return rCert;
}
#endif
//Subject
string fullSubject = $"CN={sSubjectCN}";
......
......@@ -349,7 +349,7 @@ namespace Titanium.Web.Proxy.Network
}
finally
{
x509Store.Dispose();
x509Store.Close();
}
}
......@@ -471,8 +471,8 @@ namespace Titanium.Web.Proxy.Network
}
finally
{
x509RootStore.Dispose();
x509PersonalStore.Dispose();
x509RootStore.Close();
x509PersonalStore.Close();
}
}
......@@ -511,8 +511,8 @@ namespace Titanium.Web.Proxy.Network
}
finally
{
x509RootStore.Dispose();
x509PersonalStore.Dispose();
x509RootStore.Close();
x509PersonalStore.Close();
}
}
......
......@@ -72,7 +72,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
//It helps to avoid eventual deterioration of performance due to TCP port exhaustion
//due to default TCP CLOSE_WAIT timeout for 4 minutes
TcpClient.LingerState = new LingerOption(true, 0);
TcpClient.Dispose();
TcpClient.Close();
}
}
catch
......
......@@ -52,12 +52,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
try
{
#if NET45
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 (useUpstreamProxy)
......@@ -119,7 +114,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
catch (Exception)
{
stream?.Dispose();
client?.Dispose();
client?.Close();
throw;
}
......
......@@ -11,9 +11,7 @@ using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers;
#if NET45
using Titanium.Web.Proxy.Helpers.WinHttp;
#endif
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;
using Titanium.Web.Proxy.Network.Tcp;
......@@ -26,13 +24,8 @@ namespace Titanium.Web.Proxy
/// </summary>
public partial class ProxyServer : IDisposable
{
#if NET45
internal static readonly string UriSchemeHttp = Uri.UriSchemeHttp;
internal static readonly string UriSchemeHttps = Uri.UriSchemeHttps;
#else
internal const string UriSchemeHttp = "http";
internal const string UriSchemeHttps = "https";
#endif
/// <summary>
/// An default exception log func
......@@ -64,7 +57,6 @@ namespace Titanium.Web.Proxy
/// </summary>
private TcpConnectionFactory tcpConnectionFactory { get; }
#if NET45
private WinHttpWebProxyFinder systemProxyResolver;
/// <summary>
......@@ -76,7 +68,6 @@ namespace Titanium.Web.Proxy
/// Set firefox to use default system proxy
/// </summary>
private readonly FireFoxProxySettingsManager firefoxProxySettingsManager = new FireFoxProxySettingsManager();
#endif
/// <summary>
/// Buffer size used throughout this proxy
......@@ -281,11 +272,11 @@ namespace Titanium.Web.Proxy
/// <summary>
/// List of supported Ssl versions
/// </summary>
public SslProtocols SupportedSslProtocols { get; set; } =
#if NET45
public SslProtocols SupportedSslProtocols { get; set; } = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3;
#else
public SslProtocols SupportedSslProtocols { get; set; } = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
SslProtocols.Ssl3 |
#endif
SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
/// <summary>
/// Total number of active client connections
......@@ -317,12 +308,10 @@ namespace Titanium.Web.Proxy
ProxyEndPoints = new List<ProxyEndPoint>();
tcpConnectionFactory = new TcpConnectionFactory();
#if NET45
if (!RunTime.IsRunningOnMono)
if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
{
systemProxySettingsManager = new SystemProxyManager();
}
#endif
CertificateManager = new CertificateManager(ExceptionFunc);
if (rootCertificateName != null)
......@@ -375,7 +364,6 @@ namespace Titanium.Web.Proxy
}
}
#if NET45
/// <summary>
/// Set the given explicit end point as the default proxy server for current machine
/// </summary>
......@@ -521,7 +509,6 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager.DisableAllProxy();
}
#endif
/// <summary>
/// Start this proxy server
......@@ -533,10 +520,9 @@ namespace Titanium.Web.Proxy
throw new Exception("Proxy is already running.");
}
#if NET45
//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
if (systemProxySettingsManager != null)
if (systemProxySettingsManager != null && RunTime.IsWindows)
{
var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry();
if (proxyInfo.Proxies != null)
......@@ -568,7 +554,6 @@ namespace Titanium.Web.Proxy
GetCustomUpStreamProxyFunc = GetSystemUpStreamProxy;
}
#endif
ProxyRunning = true;
......@@ -597,8 +582,7 @@ namespace Titanium.Web.Proxy
throw new Exception("Proxy is not running.");
}
#if NET45
if (!RunTime.IsRunningOnMono)
if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
{
bool setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
......@@ -607,7 +591,6 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager.RestoreOriginalSettings();
}
}
#endif
foreach (var endPoint in ProxyEndPoints)
{
......@@ -656,7 +639,6 @@ namespace Titanium.Web.Proxy
CertificateManager?.Dispose();
}
#if NET45
/// <summary>
/// Listen on the given end point on local machine
/// </summary>
......@@ -670,29 +652,6 @@ namespace Titanium.Web.Proxy
// accept clients asynchronously
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>
/// Verifiy if its safe to set this end point as System proxy
......@@ -713,7 +672,6 @@ namespace Titanium.Web.Proxy
}
}
#if NET45
/// <summary>
/// Gets the system up stream proxy.
/// </summary>
......@@ -724,7 +682,6 @@ namespace Titanium.Web.Proxy
var proxy = systemProxyResolver.GetProxy(sessionEventArgs.WebSession.Request.RequestUri);
return Task.FromResult(proxy);
}
#endif
private void EnsureRootCertificate()
{
......@@ -739,7 +696,6 @@ namespace Titanium.Web.Proxy
}
}
#if NET45
/// <summary>
/// When a connection is received from client act
/// </summary>
......@@ -778,7 +734,6 @@ namespace Titanium.Web.Proxy
// Get the listener that handles the client request.
endPoint.Listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
}
#endif
private async Task HandleClient(TcpClient tcpClient, ProxyEndPoint endPoint)
{
......@@ -811,7 +766,7 @@ namespace Titanium.Web.Proxy
//It helps to avoid eventual deterioration of performance due to TCP port exhaustion
//due to default TCP CLOSE_WAIT timeout for 4 minutes
tcpClient.LingerState = new LingerOption(true, 0);
tcpClient.Dispose();
tcpClient.Close();
}
}
catch
......
......@@ -17,6 +17,9 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Win32.Registry">
<Version>4.4.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Principal.Windows">
<Version>4.4.0</Version>
</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