Commit 40e4301e authored by justcoding121's avatar justcoding121 Committed by justcoding121

Merge pull request #206 from honfika/develop

ArgumentException fix when proxy dll is loaded dynamically
parents 5bef0390 d1e9a963
...@@ -34,6 +34,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -34,6 +34,7 @@ namespace Titanium.Web.Proxy.Network
private readonly ICertificateMaker certEngine; private readonly ICertificateMaker certEngine;
private bool clearCertificates { get; set; } private bool clearCertificates { get; set; }
/// <summary> /// <summary>
/// Cache dictionary /// Cache dictionary
/// </summary> /// </summary>
...@@ -70,17 +71,29 @@ namespace Titanium.Web.Proxy.Network ...@@ -70,17 +71,29 @@ namespace Titanium.Web.Proxy.Network
certificateCache = new ConcurrentDictionary<string, CachedCertificate>(); certificateCache = new ConcurrentDictionary<string, CachedCertificate>();
} }
internal X509Certificate2 GetRootCertificate() private string GetRootCertificatePath()
{
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
// dynamically loaded assemblies returns string.Empty location
if (assemblyLocation == string.Empty)
{ {
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); assemblyLocation = System.Reflection.Assembly.GetEntryAssembly().Location;
}
var path = Path.GetDirectoryName(assemblyLocation);
if (null == path) throw new NullReferenceException(); if (null == path) throw new NullReferenceException();
var fileName = Path.Combine(path, "rootCert.pfx"); var fileName = Path.Combine(path, "rootCert.pfx");
return fileName;
}
internal X509Certificate2 GetRootCertificate()
{
var fileName = GetRootCertificatePath();
if (!File.Exists(fileName)) return null; if (!File.Exists(fileName)) return null;
try try
{ {
return new X509Certificate2(fileName, string.Empty, X509KeyStorageFlags.Exportable); return new X509Certificate2(fileName, string.Empty, X509KeyStorageFlags.Exportable);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -94,7 +107,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -94,7 +107,6 @@ namespace Titanium.Web.Proxy.Network
/// <returns>true if succeeded, else false</returns> /// <returns>true if succeeded, else false</returns>
internal bool CreateTrustedRootCertificate() internal bool CreateTrustedRootCertificate()
{ {
rootCertificate = GetRootCertificate(); rootCertificate = GetRootCertificate();
if (rootCertificate != null) if (rootCertificate != null)
{ {
...@@ -112,9 +124,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -112,9 +124,7 @@ namespace Titanium.Web.Proxy.Network
{ {
try try
{ {
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); var fileName = GetRootCertificatePath();
if (null == path) throw new NullReferenceException();
var fileName = Path.Combine(path, "rootCert.pfx");
File.WriteAllBytes(fileName, rootCertificate.Export(X509ContentType.Pkcs12)); File.WriteAllBytes(fileName, rootCertificate.Export(X509ContentType.Pkcs12));
} }
catch (Exception e) catch (Exception e)
......
...@@ -54,9 +54,9 @@ namespace Titanium.Web.Proxy ...@@ -54,9 +54,9 @@ namespace Titanium.Web.Proxy
var httpCmdSplit = httpCmd.Split(ProxyConstants.SpaceSplit, 3); var httpCmdSplit = httpCmd.Split(ProxyConstants.SpaceSplit, 3);
//Find the request Verb //Find the request Verb
var httpVerb = httpCmdSplit[0]; var httpVerb = httpCmdSplit[0].ToUpper();
httpRemoteUri = httpVerb.ToUpper() == "CONNECT" ? httpRemoteUri = httpVerb == "CONNECT" ?
new Uri("http://" + httpCmdSplit[1]) : new Uri(httpCmdSplit[1]); new Uri("http://" + httpCmdSplit[1]) : new Uri(httpCmdSplit[1]);
//parse the HTTP version //parse the HTTP version
...@@ -78,7 +78,7 @@ namespace Titanium.Web.Proxy ...@@ -78,7 +78,7 @@ namespace Titanium.Web.Proxy
List<HttpHeader> connectRequestHeaders = null; List<HttpHeader> connectRequestHeaders = null;
//Client wants to create a secure tcp tunnel (its a HTTPS request) //Client wants to create a secure tcp tunnel (its a HTTPS request)
if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port != 80) if (httpVerb == "CONNECT" && !excluded && httpRemoteUri.Port != 80)
{ {
httpRemoteUri = new Uri("https://" + httpCmdSplit[1]); httpRemoteUri = new Uri("https://" + httpCmdSplit[1]);
string tmpLine; string tmpLine;
...@@ -131,7 +131,7 @@ namespace Titanium.Web.Proxy ...@@ -131,7 +131,7 @@ namespace Titanium.Web.Proxy
} }
//Sorry cannot do a HTTPS request decrypt to port 80 at this time //Sorry cannot do a HTTPS request decrypt to port 80 at this time
else if (httpVerb.ToUpper() == "CONNECT") else if (httpVerb == "CONNECT")
{ {
//Cyphen out CONNECT request headers //Cyphen out CONNECT request headers
await clientStreamReader.ReadAllLinesAsync(); await clientStreamReader.ReadAllLinesAsync();
......
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