Commit 30e48c8d authored by titanium007's avatar titanium007

#4 Pull in Nekopanda changes

Pull user:Nekopanda changes; Also moved system proxy, certificate
helpers to Proxy core project
parent 5c13bdc1
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
# Visual Studio 2015 cache/options directory
.vs/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# Windows Azure Build Output
csx/
*.build.csdef
# Windows Store app package directory
AppPackages/
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
\ No newline at end of file
...@@ -6,55 +6,49 @@ using System.Reflection; ...@@ -6,55 +6,49 @@ using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using Titanium.Web.Proxy.Test.Helpers; using Titanium.Web.Proxy.Helpers;
namespace Titanium.Web.Proxy.Test namespace Titanium.Web.Proxy.Test
{ {
public class Program public class Program
{ {
static ProxyTestController controller = new ProxyTestController();
public static void Main(string[] args) public static void Main(string[] args)
{ {
//On Console exit reset system proxy //On Console exit make sure we also exit the proxy
handler = new ConsoleEventDelegate(ConsoleEventCallback); handler = new ConsoleEventDelegate(ConsoleEventCallback);
SetConsoleCtrlHandler(handler, true); SetConsoleCtrlHandler(handler, true);
//Start proxy controller
var controller = new ProxyTestController();
controller.Visited += PageVisited;
controller.StartProxy();
Console.Write("Do you want to monitor HTTPS? (Y/N):"); Console.Write("Do you want to monitor HTTPS? (Y/N):");
if(Console.ReadLine().Trim().ToLower()=="y" ) if(Console.ReadLine().Trim().ToLower()=="y" )
{ {
InstallCertificate(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)); controller.EnableSSL = true;
SystemProxyHelper.EnableProxyHTTPS("localhost", controller.ListeningPort);
} }
Console.Write("Do you want to set this as a System Proxy? (Y/N):");
if (Console.ReadLine().Trim().ToLower() == "y")
{
controller.SetAsSystemProxy = true;
}
controller.Visited += PageVisited;
//Start proxy controller
controller.StartProxy();
Console.WriteLine("Hit any key to exit.."); Console.WriteLine("Hit any key to exit..");
Console.WriteLine(); Console.WriteLine();
Console.Read(); Console.Read();
//Reset System Proxy on exit
SystemProxyHelper.DisableAllProxy();
FireFoxHelper.RemoveFirefox();
controller.Stop(); controller.Stop();
} }
private static void InstallCertificate(string CertificateDirectory)
{
X509Certificate2 certificate = new X509Certificate2(Path.Combine(CertificateDirectory , "Titanium Proxy Test Root Certificate.cer"));
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();
}
private static void PageVisited(VisitedEventArgs e) private static void PageVisited(VisitedEventArgs e)
{ {
Console.WriteLine(string.Concat("Visited: ", e.URL)); Console.WriteLine(string.Concat("Visited: ", e.URL));
...@@ -65,8 +59,7 @@ namespace Titanium.Web.Proxy.Test ...@@ -65,8 +59,7 @@ namespace Titanium.Web.Proxy.Test
{ {
try try
{ {
SystemProxyHelper.DisableAllProxy(); controller.Stop();
FireFoxHelper.RemoveFirefox();
} }
catch { } catch { }
......
...@@ -8,7 +8,7 @@ using System.DirectoryServices.AccountManagement; ...@@ -8,7 +8,7 @@ using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory; using System.DirectoryServices.ActiveDirectory;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy; using Titanium.Web.Proxy;
using Titanium.Web.Proxy.Test.Helpers; using Titanium.Web.Proxy.Helpers;
...@@ -20,6 +20,8 @@ namespace Titanium.Web.Proxy.Test ...@@ -20,6 +20,8 @@ namespace Titanium.Web.Proxy.Test
private string _lastURL = string.Empty; private string _lastURL = string.Empty;
public int ListeningPort { get; set; } public int ListeningPort { get; set; }
public bool EnableSSL { get; set; }
public bool SetAsSystemProxy { get; set; }
public void StartProxy() public void StartProxy()
{ {
...@@ -29,12 +31,14 @@ namespace Titanium.Web.Proxy.Test ...@@ -29,12 +31,14 @@ namespace Titanium.Web.Proxy.Test
ProxyServer.BeforeRequest += OnRequest; ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse; ProxyServer.BeforeResponse += OnResponse;
} }
ProxyServer.Start(); ProxyServer.EnableSSL = EnableSSL;
ProxyServer.SetAsSystemProxy = SetAsSystemProxy;
SystemProxyHelper.EnableProxyHTTP("localhost", ProxyServer.ListeningPort); ProxyServer.Start();
FireFoxHelper.AddFirefox();
ListeningPort = ProxyServer.ListeningPort; ListeningPort = ProxyServer.ListeningPort;
...@@ -167,6 +171,8 @@ namespace Titanium.Web.Proxy.Test ...@@ -167,6 +171,8 @@ namespace Titanium.Web.Proxy.Test
return builder.ToString(); return builder.ToString();
} }
} }
public class VisitedEventArgs : EventArgs public class VisitedEventArgs : EventArgs
{ {
......
...@@ -54,20 +54,12 @@ ...@@ -54,20 +54,12 @@
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ProxyTestController.cs" /> <Compile Include="ProxyTestController.cs" />
<Compile Include="Helpers\Firefox.cs" />
<Compile Include="Helpers\SystemProxy.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Capture.PNG" /> <Content Include="Capture.PNG" />
<Content Include="makecert.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<None Include="Titanium Proxy Test Root Certificate.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj"> <ProjectReference Include="..\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj">
......
...@@ -17,5 +17,7 @@ ...@@ -17,5 +17,7 @@
<files> <files>
<file src="Titanium.Web.Proxy\bin\$configuration$\Titanium.Web.Proxy.dll" target="lib\net40\" /> <file src="Titanium.Web.Proxy\bin\$configuration$\Titanium.Web.Proxy.dll" target="lib\net40\" />
<file src="Titanium.Web.Proxy\bin\$configuration$\Ionic.Zip.dll" target="lib\net40\" /> <file src="Titanium.Web.Proxy\bin\$configuration$\Ionic.Zip.dll" target="lib\net40\" />
<file src="Titanium.Web.Proxy\bin\$configuration$\makecert.exe" />
<file src="Titanium.Web.Proxy\bin\$configuration$\DO_NOT_TRUST_FiddlerRoot.cer" />
</files> </files>
</package> </package>
\ No newline at end of file
...@@ -5,10 +5,10 @@ VisualStudioVersion = 12.0.31101.0 ...@@ -5,10 +5,10 @@ VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B6DBABDC-C985-4872-9C38-B4E5079CBC4B}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B6DBABDC-C985-4872-9C38-B4E5079CBC4B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy", "Titanium.Web.Proxy\Titanium.Web.Proxy.csproj", "{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy.Test", "Titanium.Web.Proxy.Test\Titanium.Web.Proxy.Test.csproj", "{F3B7E553-1904-4E80-BDC7-212342B5C952}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy.Test", "Titanium.Web.Proxy.Test\Titanium.Web.Proxy.Test.csproj", "{F3B7E553-1904-4E80-BDC7-212342B5C952}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy", "Titanium.Web.Proxy\Titanium.Web.Proxy.csproj", "{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
...@@ -19,6 +19,14 @@ Global ...@@ -19,6 +19,14 @@ Global
Release|x86 = Release|x86 Release|x86 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|x64.ActiveCfg = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|x86.ActiveCfg = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|Any CPU.Build.0 = Release|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|x64.ActiveCfg = Release|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|x86.ActiveCfg = Release|Any CPU
{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Debug|Any CPU.Build.0 = Debug|Any CPU {8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Debug|x64.ActiveCfg = Debug|x64 {8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Debug|x64.ActiveCfg = Debug|x64
...@@ -31,14 +39,6 @@ Global ...@@ -31,14 +39,6 @@ Global
{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Release|x64.Build.0 = Release|x64 {8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Release|x64.Build.0 = Release|x64
{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Release|x86.ActiveCfg = Release|x86 {8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Release|x86.ActiveCfg = Release|x86
{8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Release|x86.Build.0 = Release|x86 {8D73A1BE-868C-42D2-9ECE-F32CC1A02906}.Release|x86.Build.0 = Release|x86
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|x64.ActiveCfg = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Debug|x86.ActiveCfg = Debug|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|Any CPU.Build.0 = Release|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|x64.ActiveCfg = Release|Any CPU
{F3B7E553-1904-4E80-BDC7-212342B5C952}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
...@@ -3,12 +3,13 @@ using System.Security.Cryptography.X509Certificates; ...@@ -3,12 +3,13 @@ using System.Security.Cryptography.X509Certificates;
using System.Diagnostics; using System.Diagnostics;
using System.Threading; using System.Threading;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
public class CertificateHelper public class CertificateHelper
{ {
private static X509Store store; private static X509Store store;
public static X509Certificate2 GetCertificate(string Hostname) public static X509Certificate2 GetCertificate(string RootCertificateName, string Hostname)
{ {
if (store == null) if (store == null)
...@@ -16,29 +17,29 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -16,29 +17,29 @@ namespace Titanium.Web.Proxy.Helpers
store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly); store.Open(OpenFlags.ReadOnly);
} }
var CachedCertificate = GetCertifiateFromStore(Hostname); var CachedCertificate = GetCertifiateFromStore(RootCertificateName, Hostname);
if (CachedCertificate == null) if (CachedCertificate == null)
CreateClientCertificate(Hostname); CreateClientCertificate(RootCertificateName, Hostname);
return GetCertifiateFromStore(Hostname); return GetCertifiateFromStore(RootCertificateName, Hostname);
} }
private static X509Certificate2 GetCertifiateFromStore(string Hostname) private static X509Certificate2 GetCertifiateFromStore(string RootCertificateName, string Hostname)
{ {
X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindBySubjectName, Hostname, true); X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindBySubjectName, Hostname, true);
foreach (var certificate in certificateCollection) foreach (var certificate in certificateCollection)
{ {
if (certificate.SubjectName.Name.StartsWith("CN=" + Hostname) && certificate.IssuerName.Name.StartsWith("CN=Titanium_Proxy_Test_Root")) if (certificate.SubjectName.Name.StartsWith("CN=" + Hostname) && certificate.IssuerName.Name.StartsWith("CN=" + RootCertificateName))
return certificate; return certificate;
} }
return null; return null;
} }
private static void CreateClientCertificate(string Hostname) private static void CreateClientCertificate(string RootCertificateName, string Hostname)
{ {
Process process = new Process(); Process process = new Process();
...@@ -50,7 +51,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -50,7 +51,7 @@ namespace Titanium.Web.Proxy.Helpers
// Setup executable and parameters // Setup executable and parameters
process.StartInfo.FileName = @"makecert.exe"; process.StartInfo.FileName = @"makecert.exe";
process.StartInfo.Arguments = @"-pe -ss my -n ""CN=" + Hostname + @""" -sky exchange -in Titanium_Proxy_Test_Root -is my -eku 1.3.6.1.5.5.7.3.1 -cy end -a sha1 -m 132 -b 10/07/2012"; 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 // Go
process.Start(); process.Start();
...@@ -59,5 +60,21 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -59,5 +60,21 @@ namespace Titanium.Web.Proxy.Helpers
} }
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();
}
} }
} }
...@@ -4,7 +4,7 @@ using System.Linq; ...@@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.IO; using System.IO;
namespace Titanium.Web.Proxy.Test.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
public class FireFoxHelper public class FireFoxHelper
{ {
......
...@@ -5,7 +5,7 @@ using System.Text; ...@@ -5,7 +5,7 @@ using System.Text;
using Microsoft.Win32; using Microsoft.Win32;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Titanium.Web.Proxy.Test.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
public static class SystemProxyHelper public static class SystemProxyHelper
{ {
......
...@@ -9,13 +9,13 @@ using System.Threading.Tasks; ...@@ -9,13 +9,13 @@ using System.Threading.Tasks;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
public class TcpHelper public class TcpHelper
{ {
private static readonly int BUFFER_SIZE = 8192; private static readonly int BUFFER_SIZE = 8192;
private static readonly String[] colonSpaceSplit = new string[] { ": " }; private static readonly String[] colonSpaceSplit = new string[] { ": " };
public static void SendRaw(string Hostname, int TunnelPort, System.IO.Stream ClientStream) public static void SendRaw(string Hostname, int TunnelPort, System.IO.Stream ClientStream)
{ {
System.Net.Sockets.TcpClient tunnelClient = new System.Net.Sockets.TcpClient(Hostname, TunnelPort); System.Net.Sockets.TcpClient tunnelClient = new System.Net.Sockets.TcpClient(Hostname, TunnelPort);
var tunnelStream = tunnelClient.GetStream(); var tunnelStream = tunnelClient.GetStream();
...@@ -27,7 +27,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -27,7 +27,7 @@ namespace Titanium.Web.Proxy.Helpers
sendRelay.Start(); sendRelay.Start();
receiveRelay.Start(); receiveRelay.Start();
Task.WaitAll(sendRelay, receiveRelay); Task.WaitAll(sendRelay, receiveRelay);
if (tunnelStream != null) if (tunnelStream != null)
tunnelStream.Close(); tunnelStream.Close();
...@@ -35,14 +35,14 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -35,14 +35,14 @@ namespace Titanium.Web.Proxy.Helpers
if (tunnelClient != null) if (tunnelClient != null)
tunnelClient.Close(); tunnelClient.Close();
} }
public static void SendRaw(string HttpCmd, string SecureHostName, ref List<string> RequestLines, bool IsHttps, Stream ClientStream) public static void SendRaw(string HttpCmd, string SecureHostName, ref List<string> RequestLines, bool IsHttps, Stream ClientStream)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append(HttpCmd); sb.Append(HttpCmd);
sb.Append(Environment.NewLine); sb.Append(Environment.NewLine);
string hostname= SecureHostName; string hostname = SecureHostName;
for(int i = 1; i < RequestLines.Count;i++) for (int i = 1; i < RequestLines.Count; i++)
{ {
var header = RequestLines[i]; var header = RequestLines[i];
...@@ -64,14 +64,14 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -64,14 +64,14 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
sb.Append(header); sb.Append(header);
sb.Append(Environment.NewLine); sb.Append(Environment.NewLine);
} }
sb.Append(Environment.NewLine); sb.Append(Environment.NewLine);
int tunnelPort = 80; int tunnelPort = 80;
if (IsHttps) if (IsHttps)
{ {
tunnelPort = 443; tunnelPort = 443;
} }
...@@ -86,14 +86,14 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -86,14 +86,14 @@ namespace Titanium.Web.Proxy.Helpers
tunnelStream = sslStream; tunnelStream = sslStream;
} }
var sendRelay = new Task(() => StreamHelper.CopyTo(sb.ToString(), ClientStream, tunnelStream, BUFFER_SIZE)); var sendRelay = new Task(() => StreamHelper.CopyTo(sb.ToString(), ClientStream, tunnelStream, BUFFER_SIZE));
var receiveRelay = new Task(() => StreamHelper.CopyTo(tunnelStream, ClientStream, BUFFER_SIZE)); var receiveRelay = new Task(() => StreamHelper.CopyTo(tunnelStream, ClientStream, BUFFER_SIZE));
sendRelay.Start(); sendRelay.Start();
receiveRelay.Start(); receiveRelay.Start();
Task.WaitAll(sendRelay, receiveRelay); Task.WaitAll(sendRelay, receiveRelay);
if (tunnelStream != null) if (tunnelStream != null)
tunnelStream.Close(); tunnelStream.Close();
...@@ -102,4 +102,4 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -102,4 +102,4 @@ namespace Titanium.Web.Proxy.Helpers
tunnelClient.Close(); tunnelClient.Close();
} }
} }
} }
\ No newline at end of file
...@@ -121,4 +121,4 @@ namespace Titanium.Web.Proxy.Models ...@@ -121,4 +121,4 @@ namespace Titanium.Web.Proxy.Models
public IPAddress ipAddress { get; set; } public IPAddress ipAddress { get; set; }
} }
} }
\ No newline at end of file
...@@ -43,8 +43,9 @@ namespace Titanium.Web.Proxy ...@@ -43,8 +43,9 @@ namespace Titanium.Web.Proxy
public IPAddress ListeningIPInterface { get; set; } public static IPAddress ListeningIPInterface { get; set; }
public static string RootCertificateName { get; set; }
public static Int32 ListeningPort public static Int32 ListeningPort
{ {
...@@ -78,6 +79,7 @@ namespace Titanium.Web.Proxy ...@@ -78,6 +79,7 @@ namespace Titanium.Web.Proxy
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));
...@@ -85,16 +87,37 @@ namespace Titanium.Web.Proxy ...@@ -85,16 +87,37 @@ namespace Titanium.Web.Proxy
ShouldListen = true; ShouldListen = true;
listenerThread.Start(listener); listenerThread.Start(listener);
if (SetAsSystemProxy)
{
SystemProxyHelper.EnableProxyHTTP("localhost", ListeningPort);
FireFoxHelper.AddFirefox();
RootCertificateName = RootCertificateName == null ? "DO_NOT_TRUST_FiddlerRoot" : RootCertificateName;
CertificateHelper.InstallCertificate(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), string.Concat(RootCertificateName, ".cer")));
if (EnableSSL)
{
SystemProxyHelper.EnableProxyHTTPS("localhost", ListeningPort);
}
}
return true; return true;
} }
public static void Stop() public static void Stop()
{ {
if (SetAsSystemProxy)
{
SystemProxyHelper.DisableAllProxy();
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)
...@@ -125,19 +148,23 @@ namespace Titanium.Web.Proxy ...@@ -125,19 +148,23 @@ namespace Titanium.Web.Proxy
} }
public static void AcceptTcpClientCallback(IAsyncResult ar) public static void AcceptTcpClientCallback(IAsyncResult ar)
{ {
// Get the listener that handles the client request. try
TcpListener listener = (TcpListener)ar.AsyncState; {
TcpClient client = null; // Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;
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) { }
} }
private static void ProcessClient(Object param) private static void ProcessClient(Object param)
...@@ -157,5 +184,9 @@ namespace Titanium.Web.Proxy ...@@ -157,5 +184,9 @@ namespace Titanium.Web.Proxy
} }
public static bool EnableSSL { get; set; }
public static bool SetAsSystemProxy { get; set; }
} }
} }
\ No newline at end of file
...@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy ...@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy
partial class ProxyServer partial class ProxyServer
{ {
private static void HandleClientRequest(TcpClient Client) private static void HandleClientRequest(TcpClient Client)
{ {
...@@ -82,20 +82,20 @@ namespace Titanium.Web.Proxy ...@@ -82,20 +82,20 @@ namespace Titanium.Web.Proxy
int.TryParse(splitBuffer[1].Split(':')[1], out tunnelPort); int.TryParse(splitBuffer[1].Split(':')[1], out tunnelPort);
if (tunnelPort == 0) tunnelPort = 80; if (tunnelPort == 0) tunnelPort = 80;
var isSecure = true; var isSecure = true;
for(int i=1;i<requestLines.Count;i++) for (int i = 1; i < requestLines.Count; i++)
{ {
var rawHeader = requestLines[i]; var rawHeader = requestLines[i];
String[] header = rawHeader.ToLower().Trim().Split(colonSpaceSplit, 2, StringSplitOptions.None); String[] header = rawHeader.ToLower().Trim().Split(colonSpaceSplit, 2, StringSplitOptions.None);
if ((header[0] == "host") ) if ((header[0] == "host"))
{
var hostDetails = header[1].ToLower().Trim().Split(':');
if (hostDetails.Length > 1)
{ {
var hostDetails = header[1].ToLower().Trim().Split(':'); isSecure = false;
if(hostDetails.Length>1)
{
isSecure = false;
}
} }
}
} }
requestLines.Clear(); requestLines.Clear();
...@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy ...@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy
connectStreamWriter.WriteLine(); connectStreamWriter.WriteLine();
connectStreamWriter.Flush(); connectStreamWriter.Flush();
if (tunnelPort != 443) if (tunnelPort != 443)
{ {
...@@ -120,11 +120,11 @@ namespace Titanium.Web.Proxy ...@@ -120,11 +120,11 @@ namespace Titanium.Web.Proxy
return; return;
} }
Monitor.Enter(certificateAccessLock); Monitor.Enter(certificateAccessLock);
var _certificate = CertificateHelper.GetCertificate(tunnelHostName); var _certificate = CertificateHelper.GetCertificate(RootCertificateName, tunnelHostName);
Monitor.Exit(certificateAccessLock); Monitor.Exit(certificateAccessLock);
SslStream sslStream = null; SslStream sslStream = null;
if (!pinnedCertificateClients.Contains(tunnelHostName)&&isSecure) if (!pinnedCertificateClients.Contains(tunnelHostName) && isSecure)
{ {
sslStream = new SslStream(clientStream, true); sslStream = new SslStream(clientStream, true);
try try
...@@ -134,18 +134,18 @@ namespace Titanium.Web.Proxy ...@@ -134,18 +134,18 @@ namespace Titanium.Web.Proxy
catch (AuthenticationException ex) catch (AuthenticationException ex)
{ {
if (pinnedCertificateClients.Contains(tunnelHostName) == false) if (pinnedCertificateClients.Contains(tunnelHostName) == false)
{ {
pinnedCertificateClients.Add(tunnelHostName); pinnedCertificateClients.Add(tunnelHostName);
} }
throw ex; throw ex;
} }
} }
else else
{ {
TcpHelper.SendRaw(tunnelHostName, tunnelPort, clientStreamReader.BaseStream); TcpHelper.SendRaw(tunnelHostName, tunnelPort, clientStreamReader.BaseStream);
...@@ -182,7 +182,7 @@ namespace Titanium.Web.Proxy ...@@ -182,7 +182,7 @@ namespace Titanium.Web.Proxy
{ {
count++; count++;
MemoryStream mw = null; MemoryStream mw = null;
StreamWriter sw = null; StreamWriter sw = null;
args = new SessionEventArgs(BUFFER_SIZE); args = new SessionEventArgs(BUFFER_SIZE);
...@@ -234,7 +234,7 @@ namespace Titanium.Web.Proxy ...@@ -234,7 +234,7 @@ namespace Titanium.Web.Proxy
if ((header[0] == "upgrade") && (header[1] == "websocket")) if ((header[0] == "upgrade") && (header[1] == "websocket"))
{ {
TcpHelper.SendRaw(httpCmd, tunnelHostName, ref requestLines, args.IsSecure, clientStreamReader.BaseStream); TcpHelper.SendRaw(httpCmd, tunnelHostName, ref requestLines, args.IsSecure, clientStreamReader.BaseStream);
...@@ -265,7 +265,7 @@ namespace Titanium.Web.Proxy ...@@ -265,7 +265,7 @@ namespace Titanium.Web.Proxy
args.ipAddress = ((IPEndPoint)Client.Client.RemoteEndPoint).Address; args.ipAddress = ((IPEndPoint)Client.Client.RemoteEndPoint).Address;
args.IsAlive = args.ProxyRequest.KeepAlive; args.IsAlive = args.ProxyRequest.KeepAlive;
BeforeRequest(null, args); BeforeRequest(null, args);
} }
if (args.Cancel) if (args.Cancel)
{ {
...@@ -286,7 +286,7 @@ namespace Titanium.Web.Proxy ...@@ -286,7 +286,7 @@ namespace Titanium.Web.Proxy
args.ProxyRequest.ConnectionGroupName = connectionGroup; args.ProxyRequest.ConnectionGroupName = connectionGroup;
args.ProxyRequest.AllowWriteStreamBuffering = true; args.ProxyRequest.AllowWriteStreamBuffering = true;
args.FinishedRequestEvent = new ManualResetEvent(false); args.FinishedRequestEvent = new ManualResetEvent(false);
...@@ -632,4 +632,4 @@ namespace Titanium.Web.Proxy ...@@ -632,4 +632,4 @@ namespace Titanium.Web.Proxy
} }
} }
\ No newline at end of file
...@@ -302,9 +302,9 @@ namespace Titanium.Web.Proxy ...@@ -302,9 +302,9 @@ namespace Titanium.Web.Proxy
else else
SendChunked(Data, OutStream); SendChunked(Data, OutStream);
} }
} }
} }
\ No newline at end of file
...@@ -74,6 +74,8 @@ ...@@ -74,6 +74,8 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Helpers\Firefox.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\Certificate.cs" />
...@@ -89,8 +91,16 @@ ...@@ -89,8 +91,16 @@
<Folder Include="Properties\" /> <Folder Include="Properties\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="DO_NOT_TRUST_FiddlerRoot.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="makecert.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
\ No newline at end of file
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