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

Commit 448e450

Browse files
committed
#521 Fix modroid start issue
1 parent 4220af2 commit 448e450

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/Titanium.Web.Proxy/Network/DefaultCertificateDiskCache.cs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Reflection;
44
using System.Security.Cryptography.X509Certificates;
5+
using Titanium.Web.Proxy.Helpers;
56

67
namespace Titanium.Web.Proxy.Network
78
{
@@ -13,24 +14,24 @@ internal sealed class DefaultCertificateDiskCache : ICertificateCache
1314
private string rootCertificatePath;
1415
private string certificatePath;
1516

16-
public X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags)
17+
public X509Certificate2 LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags)
1718
{
18-
string filePath = getRootCertificatePath(name);
19-
return loadCertificate(filePath, password, storageFlags);
19+
string path = getRootCertificatePath(pathOrName);
20+
return loadCertificate(path, password, storageFlags);
2021
}
2122

22-
public void SaveRootCertificate(string name, string password, X509Certificate2 certificate)
23+
public void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate)
2324
{
24-
string filePath = getRootCertificatePath(name);
25+
string path = getRootCertificatePath(pathOrName);
2526
byte[] exported = certificate.Export(X509ContentType.Pkcs12, password);
26-
File.WriteAllBytes(filePath, exported);
27+
File.WriteAllBytes(path, exported);
2728
}
2829

2930
/// <inheritdoc />
3031
public X509Certificate2 LoadCertificate(string subjectName, X509KeyStorageFlags storageFlags)
3132
{
32-
string filePath = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
33-
return loadCertificate(filePath, string.Empty, storageFlags);
33+
string path = Path.Combine(getCertificatePath(), subjectName + defaultCertificateFileExtension);
34+
return loadCertificate(path, string.Empty, storageFlags);
3435
}
3536

3637
/// <inheritdoc />
@@ -55,12 +56,12 @@ public void Clear()
5556
certificatePath = null;
5657
}
5758

58-
private X509Certificate2 loadCertificate(string filePath, string password, X509KeyStorageFlags storageFlags)
59+
private X509Certificate2 loadCertificate(string path, string password, X509KeyStorageFlags storageFlags)
5960
{
6061
byte[] exported;
6162
try
6263
{
63-
exported = File.ReadAllBytes(filePath);
64+
exported = File.ReadAllBytes(path);
6465
}
6566
catch (IOException)
6667
{
@@ -71,15 +72,15 @@ private X509Certificate2 loadCertificate(string filePath, string password, X509K
7172
return new X509Certificate2(exported, password, storageFlags);
7273
}
7374

74-
private string getRootCertificatePath(string filePath)
75+
private string getRootCertificatePath(string pathOrName)
7576
{
76-
if (Path.IsPathRooted(filePath))
77+
if (Path.IsPathRooted(pathOrName))
7778
{
78-
return filePath;
79+
return pathOrName;
7980
}
8081

8182
return Path.Combine(getRootCertificateDirectory(),
82-
string.IsNullOrEmpty(filePath) ? defaultRootCertificateFileName : filePath);
83+
string.IsNullOrEmpty(pathOrName) ? defaultRootCertificateFileName : pathOrName);
8384
}
8485

8586
private string getCertificatePath()
@@ -104,17 +105,32 @@ private string getRootCertificateDirectory()
104105
{
105106
if (rootCertificatePath == null)
106107
{
107-
string assemblyLocation = GetType().Assembly.Location;
108-
109-
// dynamically loaded assemblies returns string.Empty location
110-
if (assemblyLocation == string.Empty)
108+
if (RunTime.IsUwpOnWindows)
109+
{
110+
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
111+
}
112+
else if (RunTime.IsLinux)
111113
{
112-
assemblyLocation = Assembly.GetEntryAssembly().Location;
114+
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
113115
}
116+
else if (RunTime.IsMac)
117+
{
118+
rootCertificatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
119+
}
120+
else
121+
{
122+
string assemblyLocation = GetType().Assembly.Location;
114123

115-
string path = Path.GetDirectoryName(assemblyLocation);
124+
// dynamically loaded assemblies returns string.Empty location
125+
if (assemblyLocation == string.Empty)
126+
{
127+
assemblyLocation = Assembly.GetEntryAssembly().Location;
128+
}
116129

117-
rootCertificatePath = path ?? throw new NullReferenceException();
130+
string path = Path.GetDirectoryName(assemblyLocation);
131+
132+
rootCertificatePath = path ?? throw new NullReferenceException();
133+
}
118134
}
119135

120136
return rootCertificatePath;

src/Titanium.Web.Proxy/Network/ICertificateCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public interface ICertificateCache
77
/// <summary>
88
/// Loads the root certificate from the storage.
99
/// </summary>
10-
X509Certificate2 LoadRootCertificate(string name, string password, X509KeyStorageFlags storageFlags);
10+
X509Certificate2 LoadRootCertificate(string pathOrName, string password, X509KeyStorageFlags storageFlags);
1111

1212
/// <summary>
1313
/// Saves the root certificate to the storage.
1414
/// </summary>
15-
void SaveRootCertificate(string name, string password, X509Certificate2 certificate);
15+
void SaveRootCertificate(string pathOrName, string password, X509Certificate2 certificate);
1616

1717
/// <summary>
1818
/// Loads certificate from the storage. Returns true if certificate does not exist.

0 commit comments

Comments
 (0)