Unverified Commit 994b130a authored by jmh76's avatar jmh76 Committed by GitHub

Merge pull request #1 from justcoding121/master

update from origin
parents 4eb392fa 5cfb1f27
...@@ -128,7 +128,7 @@ ...@@ -128,7 +128,7 @@
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsLinux.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsLinux%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> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsLinux.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsLinux%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>
</span> </span>
<span class="small pull-right mobile-hide"> <span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L50">View Source</a> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L47">View Source</a>
</span> </span>
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsLinux_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsLinux*"></a> <a id="Titanium_Web_Proxy_Helpers_RunTime_IsLinux_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsLinux*"></a>
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsLinux" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsLinux">IsLinux</h4> <h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsLinux" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsLinux">IsLinux</h4>
...@@ -158,7 +158,7 @@ ...@@ -158,7 +158,7 @@
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsMac.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsMac%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> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsMac.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsMac%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>
</span> </span>
<span class="small pull-right mobile-hide"> <span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L63">View Source</a> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L60">View Source</a>
</span> </span>
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsMac_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsMac*"></a> <a id="Titanium_Web_Proxy_Helpers_RunTime_IsMac_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsMac*"></a>
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsMac" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsMac">IsMac</h4> <h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsMac" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsMac">IsMac</h4>
...@@ -188,7 +188,7 @@ ...@@ -188,7 +188,7 @@
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows%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> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows%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>
</span> </span>
<span class="small pull-right mobile-hide"> <span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L58">View Source</a> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L55">View Source</a>
</span> </span>
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows*"></a> <a id="Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows*"></a>
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows">IsUwpOnWindows</h4> <h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsUwpOnWindows" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsUwpOnWindows">IsUwpOnWindows</h4>
...@@ -218,7 +218,7 @@ ...@@ -218,7 +218,7 @@
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsWindows.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsWindows%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> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime_IsWindows.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime.IsWindows%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>
</span> </span>
<span class="small pull-right mobile-hide"> <span class="small pull-right mobile-hide">
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L56">View Source</a> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L53">View Source</a>
</span> </span>
<a id="Titanium_Web_Proxy_Helpers_RunTime_IsWindows_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsWindows*"></a> <a id="Titanium_Web_Proxy_Helpers_RunTime_IsWindows_" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsWindows*"></a>
<h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsWindows" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsWindows">IsWindows</h4> <h4 id="Titanium_Web_Proxy_Helpers_RunTime_IsWindows" data-uid="Titanium.Web.Proxy.Helpers.RunTime.IsWindows">IsWindows</h4>
...@@ -254,7 +254,7 @@ ...@@ -254,7 +254,7 @@
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime%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" class="contribution-link">Improve this Doc</a> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/new/master/apiSpec/new?filename=Titanium_Web_Proxy_Helpers_RunTime.md&amp;value=---%0Auid%3A%20Titanium.Web.Proxy.Helpers.RunTime%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" class="contribution-link">Improve this Doc</a>
</li> </li>
<li> <li>
<a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L13" class="contribution-link">View Source</a> <a href="https://github.com/justcoding121/Titanium-Web-Proxy/blob/master/src/Titanium.Web.Proxy/Helpers/RunTime.cs/#L10" class="contribution-link">View Source</a>
</li> </li>
</ul> </ul>
</div> </div>
......
...@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy.Helpers
return writeAsyncInternal(value, false, cancellationToken); return writeAsyncInternal(value, false, cancellationToken);
} }
private Task writeAsyncInternal(string value, bool addNewLine, CancellationToken cancellationToken) private async Task writeAsyncInternal(string value, bool addNewLine, CancellationToken cancellationToken)
{ {
int newLineChars = addNewLine ? newLine.Length : 0; int newLineChars = addNewLine ? newLine.Length : 0;
int charCount = value.Length; int charCount = value.Length;
...@@ -67,7 +67,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -67,7 +67,7 @@ namespace Titanium.Web.Proxy.Helpers
idx += newLineChars; idx += newLineChars;
} }
return stream.WriteAsync(buffer, 0, idx, cancellationToken); await stream.WriteAsync(buffer, 0, idx, cancellationToken);
} }
finally finally
{ {
...@@ -87,7 +87,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -87,7 +87,7 @@ namespace Titanium.Web.Proxy.Helpers
idx += newLineChars; idx += newLineChars;
} }
return stream.WriteAsync(buffer, 0, idx, cancellationToken); await stream.WriteAsync(buffer, 0, idx, cancellationToken);
} }
} }
......
using System.Linq; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
internal class NetworkHelper internal class NetworkHelper
{ {
private static readonly string localhostName = Dns.GetHostName();
private static readonly IPHostEntry localhostEntry = Dns.GetHostEntry(string.Empty);
/// <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
...@@ -19,55 +24,52 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -19,55 +24,52 @@ namespace Titanium.Web.Proxy.Helpers
return true; return true;
} }
// get local IP addresses // test if host IP equals any local IP
var localIPs = Dns.GetHostAddresses(Dns.GetHostName()); return localhostEntry.AddressList.Contains(address);
// test if any host IP equals to any local IP or to localhost
return localIPs.Contains(address);
} }
internal static bool IsLocalIpAddress(string hostName) internal static bool IsLocalIpAddress(string hostName)
{ {
hostName = hostName.ToLower(); if (IPAddress.TryParse(hostName, out var ipAddress)
&& IsLocalIpAddress(ipAddress))
if (hostName == "127.0.0.1"
|| hostName == "localhost")
{ {
return true; return true;
} }
var localhostDnsName = Dns.GetHostName().ToLower(); if (hostName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
//if hostname matches current machine DNS name
if (hostName == localhostDnsName)
{ {
return true; return true;
} }
var isLocalhost = false; //if hostname matches local host name
IPHostEntry hostEntry = null; if (hostName.Equals(localhostName, StringComparison.OrdinalIgnoreCase))
//check if parsable to an IP Address
if (IPAddress.TryParse(hostName, out var ipAddress))
{ {
hostEntry = Dns.GetHostEntry(localhostDnsName); return true;
isLocalhost = hostEntry.AddressList.Any(x => x.Equals(ipAddress));
} }
if (!isLocalhost) // if hostname matches fully qualified local DNS name
if (hostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase))
{ {
try return true;
{ }
hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = hostEntry.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals)); try
} {
catch (SocketException) // do reverse DNS lookup even if hostName is an IP address
var hostEntry = Dns.GetHostEntry(hostName);
// if DNS resolved hostname matches local DNS name,
// or if host IP address list contains any local IP address
if (hostEntry.HostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase)
|| hostEntry.AddressList.Any(hostIP => localhostEntry.AddressList.Contains(hostIP)))
{ {
return true;
} }
} }
catch (SocketException)
{
}
return false;
return isLocalhost;
} }
} }
} }
using System; using System;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
#if NETSTANDARD2_0
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
#endif
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using Titanium.Web.Proxy.Helpers;
namespace Titanium.Web.Proxy.Network namespace Titanium.Web.Proxy.Network
{ {
...@@ -13,24 +14,24 @@ namespace Titanium.Web.Proxy.Network ...@@ -13,24 +14,24 @@ namespace Titanium.Web.Proxy.Network
private string rootCertificatePath; private string rootCertificatePath;
private string certificatePath; private string certificatePath;
public X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags) public X509Certificate2 LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags)
{ {
string filePath = getRootCertificatePath(name); string path = getRootCertificatePath(pathOrName);
return loadCertificate(filePath, password, storageFlags); return loadCertificate(path, password, storageFlags);
} }
public void SaveRootCertificate(string name, string password, X509Certificate2 certificate) public void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate)
{ {
string filePath = getRootCertificatePath(name); string path = getRootCertificatePath(pathOrName);
byte[] exported = certificate.Export(X509ContentType.Pkcs12, password); byte[] exported = certificate.Export(X509ContentType.Pkcs12, password);
File.WriteAllBytes(filePath, exported); File.WriteAllBytes(path, exported);
} }
/// <inheritdoc /> /// <inheritdoc />
public X509Certificate2 LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags) public X509Certificate2 LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
{ {
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension); string path = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
return loadCertificate(filePath, string.Empty, storageFlags); return loadCertificate(path, string.Empty, storageFlags);
} }
/// <inheritdoc /> /// <inheritdoc />
...@@ -55,12 +56,12 @@ namespace Titanium.Web.Proxy.Network ...@@ -55,12 +56,12 @@ namespace Titanium.Web.Proxy.Network
certificatePath = null; certificatePath = null;
} }
private X509Certificate2 loadCertificate(string filePath, string password, X509KeyStorageFlags storageFlags) private X509Certificate2 loadCertificate(string path, string password, X509KeyStorageFlags storageFlags)
{ {
byte[] exported; byte[] exported;
try try
{ {
exported = File.ReadAllBytes(filePath); exported = File.ReadAllBytes(path);
} }
catch (IOException) catch (IOException)
{ {
...@@ -71,15 +72,15 @@ namespace Titanium.Web.Proxy.Network ...@@ -71,15 +72,15 @@ namespace Titanium.Web.Proxy.Network
return new X509Certificate2(exported, password, storageFlags); return new X509Certificate2(exported, password, storageFlags);
} }
private string getRootCertificatePath(string filePath) private string getRootCertificatePath(string pathOrName)
{ {
if (Path.IsPathRooted(filePath)) if (Path.IsPathRooted(pathOrName))
{ {
return filePath; return pathOrName;
} }
return Path.Combine(getRootCertificateDirectory(), return Path.Combine(getRootCertificateDirectory(),
string.IsNullOrEmpty(filePath) ? defaultRootCertificateFileName : filePath); string.IsNullOrEmpty(pathOrName) ? defaultRootCertificateFileName : pathOrName);
} }
private string getCertificatePath() private string getCertificatePath()
...@@ -104,17 +105,32 @@ namespace Titanium.Web.Proxy.Network ...@@ -104,17 +105,32 @@ namespace Titanium.Web.Proxy.Network
{ {
if (rootCertificatePath == null) if (rootCertificatePath == null)
{ {
string assemblyLocation = GetType().Assembly.Location; if (RunTime.IsUwpOnWindows)
{
// dynamically loaded assemblies returns string.Empty location rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (assemblyLocation == string.Empty) }
else if (RunTime.IsLinux)
{ {
assemblyLocation = Assembly.GetEntryAssembly().Location; rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
} }
else if (RunTime.IsMac)
{
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}
else
{
string assemblyLocation = GetType().Assembly.Location;
string path = Path.GetDirectoryName(assemblyLocation); // dynamically loaded assemblies returns string.Empty location
if (assemblyLocation == string.Empty)
{
assemblyLocation = Assembly.GetEntryAssembly().Location;
}
rootCertificatePath = path ?? throw new NullReferenceException(); string path = Path.GetDirectoryName(assemblyLocation);
rootCertificatePath = path ?? throw new NullReferenceException();
}
} }
return rootCertificatePath; return rootCertificatePath;
......
...@@ -7,12 +7,12 @@ namespace Titanium.Web.Proxy.Network ...@@ -7,12 +7,12 @@ namespace Titanium.Web.Proxy.Network
/// <summary> /// <summary>
/// Loads the root certificate from the storage. /// Loads the root certificate from the storage.
/// </summary> /// </summary>
X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags); X509Certificate2 LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags);
/// <summary> /// <summary>
/// Saves the root certificate to the storage. /// Saves the root certificate to the storage.
/// </summary> /// </summary>
void SaveRootCertificate(string name, string password, X509Certificate2 certificate); void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate);
/// <summary> /// <summary>
/// Loads certificate from the storage. Returns true if certificate does not exist. /// Loads certificate from the storage. Returns true if certificate does not exist.
......
...@@ -14,13 +14,22 @@ ...@@ -14,13 +14,22 @@
<copyright>Copyright &#x00A9; Titanium. All rights reserved.</copyright> <copyright>Copyright &#x00A9; Titanium. All rights reserved.</copyright>
<tags></tags> <tags></tags>
<dependencies> <dependencies>
<dependency id="StreamExtended" version="1.0.179" /> <group targetFramework="net45">
<dependency id="Portable.BouncyCastle" version="1.8.2" /> <dependency id="StreamExtended" version="1.0.190" />
<dependency id="BrotliSharpLib" version="0.3.1" /> <dependency id="Portable.BouncyCastle" version="1.8.3.37" />
<dependency id="BrotliSharpLib" version="0.3.1" />
</group>
<group targetFramework="netstandard2.0">
<dependency id="StreamExtended" version="1.0.190" />
<dependency id="Portable.BouncyCastle" version="1.8.3.37" />
<dependency id="BrotliSharpLib" version="0.3.1" />
<dependency id="Microsoft.Win32.Registry" version="4.4.0" />
<dependency id="System.Security.Principal.Windows" version="4.4.1" />
</group>
</dependencies> </dependencies>
</metadata> </metadata>
<files> <files>
<file src="bin\$configuration$\net45\Titanium.Web.Proxy.dll" target="lib\net45" /> <file src="bin\$configuration$\net45\Titanium.Web.Proxy.dll" target="lib\net45" />
<file src="bin\$configuration$\netstandard2.0\Titanium.Web.Proxy.*" target="lib\netstandard2.0" /> <file src="bin\$configuration$\netstandard2.0\Titanium.Web.Proxy.dll" target="lib\netstandard2.0" />
</files> </files>
</package> </package>
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