Commit 448e450a authored by justcoding121's avatar justcoding121

#521 Fix modroid start issue

parent 4220af29
...@@ -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.
......
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