Commit 745f8869 authored by titanium007's avatar titanium007

Merge cleanup

Remove outdated files. Don't install certificate if not using HTTPS
parent f65cd0fd
using System;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using System.Threading;
namespace Titanium.Web.Proxy.Helpers
{
public class CertificateHelper
{
private static X509Store store;
public static X509Certificate2 GetCertificate(string RootCertificateName, string Hostname)
{
if (store == null)
{
store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
}
var CachedCertificate = GetCertifiateFromStore(RootCertificateName, Hostname);
if (CachedCertificate == null)
CreateClientCertificate(RootCertificateName, Hostname);
return GetCertifiateFromStore(RootCertificateName, Hostname);
}
private static X509Certificate2 GetCertifiateFromStore(string RootCertificateName, string Hostname)
{
X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindBySubjectName, Hostname, true);
foreach (var certificate in certificateCollection)
{
if (certificate.SubjectName.Name.StartsWith("CN=" + Hostname) && certificate.IssuerName.Name.StartsWith("CN=" + RootCertificateName))
return certificate;
}
return null;
}
private static void CreateClientCertificate(string RootCertificateName, string Hostname)
{
Process process = new Process();
// Stop the process from opening a new window
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Setup executable and parameters
process.StartInfo.FileName = @"makecert.exe";
process.StartInfo.Arguments = @" -pe -ss my -n ""CN=" + Hostname + @", O=DO_NOT_TRUST, OU=Created by http://www.fiddler2.com"" -sky exchange -in " + RootCertificateName + " -is my -eku 1.3.6.1.5.5.7.3.1 -cy end -a sha1 -m 132 -b 06/26/2014";
// Go
process.Start();
process.WaitForExit();
}
public static void InstallCertificate(string certificatePath)
{
X509Certificate2 certificate = new X509Certificate2(certificatePath);
X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();
X509Store store1 = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store1.Open(OpenFlags.ReadWrite);
store1.Add(certificate);
store1.Close();
}
}
}
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Net.Security; using System.Net.Security;
using System.Security.Authentication; using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Diagnostics; using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
namespace Titanium.Web.Proxy namespace Titanium.Web.Proxy
{ {
/// <summary> /// <summary>
/// Proxy Server Main class /// Proxy Server Main class
/// </summary> /// </summary>
public partial class ProxyServer public partial class ProxyServer
{ {
private static readonly int BUFFER_SIZE = 8192; private static readonly int BUFFER_SIZE = 8192;
private static readonly char[] semiSplit = new char[] { ';' }; private static readonly char[] semiSplit = new char[] { ';' };
private static readonly String[] colonSpaceSplit = new string[] { ": " }; private static readonly String[] colonSpaceSplit = new string[] { ": " };
private static readonly char[] spaceSplit = new char[] { ' ' }; private static readonly char[] spaceSplit = new char[] { ' ' };
private static readonly Regex cookieSplitRegEx = new Regex(@",(?! )"); private static readonly Regex cookieSplitRegEx = new Regex(@",(?! )");
private static object certificateAccessLock = new object(); private static object certificateAccessLock = new object();
private static List<string> pinnedCertificateClients = new List<string>(); private static List<string> pinnedCertificateClients = new List<string>();
private static ManualResetEvent tcpClientConnected = new ManualResetEvent(false); private static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
private static TcpListener listener; private static TcpListener listener;
private static Thread listenerThread; private static Thread listenerThread;
public static event EventHandler<SessionEventArgs> BeforeRequest; public static event EventHandler<SessionEventArgs> BeforeRequest;
public static event EventHandler<SessionEventArgs> BeforeResponse; public static event EventHandler<SessionEventArgs> BeforeResponse;
public static IPAddress ListeningIPInterface { get; set; } public static IPAddress ListeningIPInterface { get; set; }
public static string RootCertificateName { get; set; } public static string RootCertificateName { get; set; }
public static Int32 ListeningPort public static Int32 ListeningPort
{ {
get get
{ {
return ((IPEndPoint)listener.LocalEndpoint).Port; return ((IPEndPoint)listener.LocalEndpoint).Port;
} }
} }
public static CertificateManager CertManager { get; set; } public static CertificateManager CertManager { get; set; }
...@@ -60,146 +60,146 @@ namespace Titanium.Web.Proxy ...@@ -60,146 +60,146 @@ namespace Titanium.Web.Proxy
static ProxyServer() static ProxyServer()
{ {
CertManager = new CertificateManager("Titanium", CertManager = new CertificateManager("Titanium",
"Titanium Root Certificate Authority"); "Titanium Root Certificate Authority");
} }
public ProxyServer() public ProxyServer()
{ {
System.Net.ServicePointManager.Expect100Continue = false; System.Net.ServicePointManager.Expect100Continue = false;
System.Net.WebRequest.DefaultWebProxy = null; System.Net.WebRequest.DefaultWebProxy = null;
System.Net.ServicePointManager.DefaultConnectionLimit = 10; System.Net.ServicePointManager.DefaultConnectionLimit = 10;
ServicePointManager.DnsRefreshTimeout = 3 * 60 * 1000;//3 minutes ServicePointManager.DnsRefreshTimeout = 3 * 60 * 1000;//3 minutes
ServicePointManager.MaxServicePointIdleTime = 3 * 60 * 1000; ServicePointManager.MaxServicePointIdleTime = 3 * 60 * 1000;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ {
if (sslPolicyErrors == SslPolicyErrors.None) return true; if (sslPolicyErrors == SslPolicyErrors.None) return true;
else else
return false; return false;
}; };
NetFrameworkHelper.URLPeriodFix(); NetFrameworkHelper.URLPeriodFix();
} }
private static bool ShouldListen { get; set; } private static bool ShouldListen { get; set; }
public static bool Start() public static bool Start()
{ {
listener = new TcpListener(IPAddress.Any, 0); listener = new TcpListener(IPAddress.Any, 0);
listener.Start(); listener.Start();
listenerThread = new Thread(new ParameterizedThreadStart(Listen)); listenerThread = new Thread(new ParameterizedThreadStart(Listen));
listenerThread.IsBackground = true; listenerThread.IsBackground = true;
ShouldListen = true; ShouldListen = true;
listenerThread.Start(listener); listenerThread.Start(listener);
if (SetAsSystemProxy) if (SetAsSystemProxy)
{ {
SystemProxyHelper.EnableProxyHTTP("localhost", ListeningPort); SystemProxyHelper.EnableProxyHTTP("localhost", ListeningPort);
FireFoxHelper.AddFirefox(); FireFoxHelper.AddFirefox();
RootCertificateName = RootCertificateName == null ? "DO_NOT_TRUST_FiddlerRoot" : RootCertificateName;
if (EnableSSL)
bool certTrusted = CertManager.CreateTrustedRootCertificate(); {
if (!certTrusted) RootCertificateName = RootCertificateName == null ? "DO_NOT_TRUST_FiddlerRoot" : RootCertificateName;
{
// The user didn't want to install the self-signed certificate to the root store. bool certTrusted = CertManager.CreateTrustedRootCertificate();
} if (!certTrusted)
//CertificateHelper.InstallCertificate(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), string.Concat(RootCertificateName, ".cer"))); {
// The user didn't want to install the self-signed certificate to the root store.
if (EnableSSL) }
{
SystemProxyHelper.EnableProxyHTTPS("localhost", ListeningPort); SystemProxyHelper.EnableProxyHTTPS("localhost", ListeningPort);
} }
} }
return true; return true;
} }
public static void Stop() public static void Stop()
{ {
if (SetAsSystemProxy) if (SetAsSystemProxy)
{ {
SystemProxyHelper.DisableAllProxy(); SystemProxyHelper.DisableAllProxy();
FireFoxHelper.RemoveFirefox(); FireFoxHelper.RemoveFirefox();
} }
ShouldListen = false; ShouldListen = false;
listener.Stop(); listener.Stop();
listenerThread.Interrupt(); listenerThread.Interrupt();
} }
private static void Listen(Object obj) private static void Listen(Object obj)
{ {
TcpListener listener = (TcpListener)obj; TcpListener listener = (TcpListener)obj;
try try
{ {
while (ShouldListen) while (ShouldListen)
{ {
// Set the event to nonsignaled state. // Set the event to nonsignaled state.
tcpClientConnected.Reset(); tcpClientConnected.Reset();
listener.BeginAcceptTcpClient(new AsyncCallback(AcceptTcpClientCallback), listener); listener.BeginAcceptTcpClient(new AsyncCallback(AcceptTcpClientCallback), listener);
// Wait until a connection is made and processed before // Wait until a connection is made and processed before
// continuing. // continuing.
tcpClientConnected.WaitOne(); tcpClientConnected.WaitOne();
} }
} }
catch (ThreadInterruptedException) { } catch (ThreadInterruptedException) { }
catch (SocketException ex) catch (SocketException ex)
{ {
Debug.WriteLine(ex.Message); Debug.WriteLine(ex.Message);
} }
} }
public static void AcceptTcpClientCallback(IAsyncResult ar) public static void AcceptTcpClientCallback(IAsyncResult ar)
{ {
try try
{ {
// Get the listener that handles the client request. // Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState; TcpListener listener = (TcpListener)ar.AsyncState;
TcpClient client = null; TcpClient client = null;
// End the operation and display the received data on // End the operation and display the received data on
// the console. // the console.
client = listener.EndAcceptTcpClient(ar); client = listener.EndAcceptTcpClient(ar);
Task.Factory.StartNew(() => ProcessClient(client)); Task.Factory.StartNew(() => ProcessClient(client));
// Signal the calling thread to continue. // Signal the calling thread to continue.
tcpClientConnected.Set(); tcpClientConnected.Set();
} }
catch(ObjectDisposedException) { } catch(ObjectDisposedException) { }
} }
private static void ProcessClient(Object param) private static void ProcessClient(Object param)
{ {
try try
{ {
TcpClient client = param as TcpClient; TcpClient client = param as TcpClient;
HandleClientRequest(client); HandleClientRequest(client);
client.Close(); client.Close();
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex.Message); Debug.WriteLine(ex.Message);
} }
} }
public static bool EnableSSL { get; set; } public static bool EnableSSL { get; set; }
public static bool SetAsSystemProxy { get; set; } public static bool SetAsSystemProxy { get; set; }
} }
} }
\ No newline at end of file
...@@ -81,7 +81,6 @@ ...@@ -81,7 +81,6 @@
<Compile Include="Helpers\SystemProxy.cs" /> <Compile Include="Helpers\SystemProxy.cs" />
<Compile Include="RequestHandler.cs" /> <Compile Include="RequestHandler.cs" />
<Compile Include="ResponseHandler.cs" /> <Compile Include="ResponseHandler.cs" />
<Compile Include="Helpers\Certificate.cs" />
<Compile Include="Helpers\CustomBinaryReader.cs" /> <Compile Include="Helpers\CustomBinaryReader.cs" />
<Compile Include="Helpers\NetFramework.cs" /> <Compile Include="Helpers\NetFramework.cs" />
<Compile Include="Helpers\Compression.cs" /> <Compile Include="Helpers\Compression.cs" />
......
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