Commit bb6691d8 authored by titanium007's avatar titanium007

Merge pull request #40 from titanium007/bug-fixes

Bug fixes
parents b8c85c74 6800b598
...@@ -21,7 +21,7 @@ if(!$Configuration) { $Configuration = $env:Configuration } ...@@ -21,7 +21,7 @@ if(!$Configuration) { $Configuration = $env:Configuration }
if(!$Configuration) { $Configuration = "Release" } if(!$Configuration) { $Configuration = "Release" }
if(!$Version) { $Version = $env:APPVEYOR_BUILD_VERSION } if(!$Version) { $Version = $env:APPVEYOR_BUILD_VERSION }
if(!$Version) { $Version = "1.0.$BuildNumber" } if(!$Version) { $Version = "2.0.$BuildNumber" }
if(!$Branch) { $Branch = $env:APPVEYOR_REPO_BRANCH } if(!$Branch) { $Branch = $env:APPVEYOR_REPO_BRANCH }
if(!$Branch) { $Branch = "local" } if(!$Branch) { $Branch = "local" }
......
...@@ -37,10 +37,21 @@ Setup HTTP proxy: ...@@ -37,10 +37,21 @@ Setup HTTP proxy:
ProxyServer.BeforeRequest += OnRequest; ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse; ProxyServer.BeforeResponse += OnResponse;
ProxyServer.EnableSSL = true; //Exclude Https addresses you don't want to proxy
ProxyServer.SetAsSystemProxy = true; //Usefull for clients that use certificate pinning
//for example dropbox.com
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Loopback, 8000, true){
ExcludedHostNameRegex = new List<string>() { "dropbox.com" }
};
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);
ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.AddEndPoint(transparentEndPoint);
ProxyServer.Start(); ProxyServer.Start();
ProxyServer.SetAsSystemProxy(explicitEndPoint);
//wait here (You can use something else as a wait function, I am using this as a demo) //wait here (You can use something else as a wait function, I am using this as a demo)
Console.Read(); Console.Read();
......
...@@ -14,22 +14,6 @@ namespace Titanium.Web.Proxy.Test ...@@ -14,22 +14,6 @@ namespace Titanium.Web.Proxy.Test
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true); NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
Console.Write("Do you want to monitor HTTPS? (Y/N):");
var readLine = Console.ReadLine();
if (readLine != null && readLine.Trim().ToLower() == "y")
{
Controller.EnableSsl = true;
}
Console.Write("Do you want to set this as a System Proxy? (Y/N):");
var line = Console.ReadLine();
if (line != null && line.Trim().ToLower() == "y")
{
Controller.SetAsSystemProxy = true;
}
//Start proxy controller //Start proxy controller
Controller.StartProxy(); Controller.StartProxy();
......
using System; using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Test namespace Titanium.Web.Proxy.Test
{ {
public class ProxyTestController public class ProxyTestController
{ {
public int ListeningPort { get; set; }
public bool EnableSsl { get; set; }
public bool SetAsSystemProxy { get; set; }
public void StartProxy() public void StartProxy()
{ {
ProxyServer.BeforeRequest += OnRequest; ProxyServer.BeforeRequest += OnRequest;
ProxyServer.BeforeResponse += OnResponse; ProxyServer.BeforeResponse += OnResponse;
ProxyServer.EnableSsl = EnableSsl;
ProxyServer.SetAsSystemProxy = SetAsSystemProxy;
//Exclude Https addresses you don't want to proxy //Exclude Https addresses you don't want to proxy
//Usefull for clients that use certificate pinning //Usefull for clients that use certificate pinning
//for example dropbox.com //for example dropbox.com
ProxyServer.ExcludedHttpsHostNameRegex.Add(".dropbox.com"); var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Loopback, 8000, true){
ExcludedHostNameRegex = new List<string>() { "dropbox.com" }
};
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 8001, true);
ProxyServer.AddEndPoint(explicitEndPoint);
ProxyServer.AddEndPoint(transparentEndPoint);
ProxyServer.Start(); ProxyServer.Start();
ProxyServer.ListeningPort = ProxyServer.ListeningPort; foreach (var endPoint in ProxyServer.ProxyEndPoints)
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
ProxyServer.SetAsSystemProxy(explicitEndPoint);
Console.WriteLine("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort);
} }
public void Stop() public void Stop()
......
...@@ -8,8 +8,14 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -8,8 +8,14 @@ namespace Titanium.Web.Proxy.Extensions
{ {
public static Encoding GetResponseEncoding(this HttpWebSession response) public static Encoding GetResponseEncoding(this HttpWebSession response)
{ {
if (string.IsNullOrEmpty(response.Response.CharacterSet)) return Encoding.GetEncoding("ISO-8859-1"); if (string.IsNullOrEmpty(response.Response.CharacterSet))
return Encoding.GetEncoding("ISO-8859-1");
try
{
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty)); return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
} }
catch { return Encoding.GetEncoding("ISO-8859-1"); }
}
} }
} }
\ No newline at end of file
...@@ -7,14 +7,18 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -7,14 +7,18 @@ namespace Titanium.Web.Proxy.Extensions
public static class StreamHelper public static class StreamHelper
{ {
public static void CopyToAsync(this Stream input, string initialData, Stream output, int bufferSize) public static void CopyToAsync(this Stream input, string initialData, Stream output, int bufferSize)
{
if(!string.IsNullOrEmpty(initialData))
{ {
var bytes = Encoding.ASCII.GetBytes(initialData); var bytes = Encoding.ASCII.GetBytes(initialData);
output.Write(bytes, 0, bytes.Length); output.Write(bytes, 0, bytes.Length);
}
CopyToAsync(input, output, bufferSize); CopyToAsync(input, output, bufferSize);
} }
//http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write //http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
public static void CopyToAsync(this Stream input, Stream output, int bufferSize) private static void CopyToAsync(this Stream input, Stream output, int bufferSize)
{ {
try try
{ {
......
...@@ -51,7 +51,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -51,7 +51,7 @@ namespace Titanium.Web.Proxy.Helpers
try try
{ {
sslStream = new SslStream(tunnelStream); sslStream = new SslStream(tunnelStream);
sslStream.AuthenticateAsClient(hostName); sslStream.AuthenticateAsClient(hostName, null, ProxyServer.SupportedProtocols, false);
tunnelStream = sslStream; tunnelStream = sslStream;
} }
catch catch
...@@ -69,10 +69,10 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -69,10 +69,10 @@ namespace Titanium.Web.Proxy.Helpers
if (sb != null) if (sb != null)
clientStream.CopyToAsync(sb.ToString(), tunnelStream, BUFFER_SIZE); clientStream.CopyToAsync(sb.ToString(), tunnelStream, BUFFER_SIZE);
else else
clientStream.CopyToAsync(tunnelStream, BUFFER_SIZE); clientStream.CopyToAsync(string.Empty, tunnelStream, BUFFER_SIZE);
}); });
var receiveRelay = Task.Factory.StartNew(() => tunnelStream.CopyToAsync(clientStream, BUFFER_SIZE)); var receiveRelay = Task.Factory.StartNew(() => tunnelStream.CopyToAsync(string.Empty, clientStream, BUFFER_SIZE));
Task.WaitAll(sendRelay, receiveRelay); Task.WaitAll(sendRelay, receiveRelay);
} }
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Titanium.Web.Proxy.Models
{
public abstract class ProxyEndPoint
{
public ProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
{
this.IpAddress = IpAddress;
this.Port = Port;
this.EnableSsl = EnableSsl;
}
public IPAddress IpAddress { get; internal set; }
public int Port { get; internal set; }
public bool EnableSsl { get; internal set; }
internal TcpListener listener { get; set; }
}
public class ExplicitProxyEndPoint : ProxyEndPoint
{
internal bool IsSystemProxy { get; set; }
public List<string> ExcludedHostNameRegex { get; set; }
public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
: base(IpAddress, Port, EnableSsl)
{
}
}
public class TransparentProxyEndPoint : ProxyEndPoint
{
public TransparentProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
: base(IpAddress, Port, EnableSsl)
{
}
}
}
...@@ -9,6 +9,7 @@ using System.IO; ...@@ -9,6 +9,7 @@ using System.IO;
using System.Net.Security; using System.Net.Security;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
using System.Threading; using System.Threading;
using System.Security.Authentication;
namespace Titanium.Web.Proxy.Network namespace Titanium.Web.Proxy.Network
{ {
...@@ -76,7 +77,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -76,7 +77,7 @@ namespace Titanium.Web.Proxy.Network
try try
{ {
sslStream = new SslStream(stream); sslStream = new SslStream(stream);
sslStream.AuthenticateAsClient(Hostname); sslStream.AuthenticateAsClient(Hostname, null, ProxyServer.SupportedProtocols , false);
stream = (Stream)sslStream; stream = (Stream)sslStream;
} }
catch catch
......
...@@ -9,7 +9,10 @@ using System.Text.RegularExpressions; ...@@ -9,7 +9,10 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network; using Titanium.Web.Proxy.Network;
using System.Linq;
using System.Security.Authentication;
namespace Titanium.Web.Proxy namespace Titanium.Web.Proxy
{ {
...@@ -31,64 +34,69 @@ namespace Titanium.Web.Proxy ...@@ -31,64 +34,69 @@ namespace Titanium.Web.Proxy
private static readonly byte[] ChunkEnd = private static readonly byte[] ChunkEnd =
Encoding.ASCII.GetBytes(0.ToString("x2") + Environment.NewLine + Environment.NewLine); Encoding.ASCII.GetBytes(0.ToString("x2") + Environment.NewLine + Environment.NewLine);
private static TcpListener _listener; #if NET45
internal static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3 ;
public static List<string> ExcludedHttpsHostNameRegex = new List<string>(); #else
public static SslProtocols SupportedProtocols = SslProtocols.Tls | SslProtocols.Ssl3;
#endif
static ProxyServer() static ProxyServer()
{ {
CertManager = new CertificateManager("Titanium", CertManager = new CertificateManager("Titanium",
"Titanium Root Certificate Authority"); "Titanium Root Certificate Authority");
ListeningIpAddress = IPAddress.Any; ProxyEndPoints = new List<ProxyEndPoint>();
ListeningPort = 0;
Initialize(); Initialize();
} }
private static CertificateManager CertManager { get; set; } private static CertificateManager CertManager { get; set; }
private static bool EnableSsl { get; set; }
private static bool certTrusted { get; set; }
private static bool proxyStarted { get; set; }
public static string RootCertificateName { get; set; } public static string RootCertificateName { get; set; }
public static bool EnableSsl { get; set; }
public static bool SetAsSystemProxy { get; set; }
public static int ListeningPort { get; set; }
public static IPAddress ListeningIpAddress { get; set; }
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 List<ProxyEndPoint> ProxyEndPoints { get; set; }
public static void Initialize() public static void Initialize()
{ {
Task.Factory.StartNew(()=>TcpConnectionManager.ClearIdleConnections()); Task.Factory.StartNew(() => TcpConnectionManager.ClearIdleConnections());
}
public static void AddEndPoint(ProxyEndPoint endPoint)
{
if (proxyStarted)
throw new Exception("Cannot add end points after proxy started.");
ProxyEndPoints.Add(endPoint);
} }
public static bool Start() public static void SetAsSystemProxy(ExplicitProxyEndPoint endPoint)
{ {
_listener = new TcpListener(ListeningIpAddress, ListeningPort); if (ProxyEndPoints.Contains(endPoint) == false)
_listener.Start(); throw new Exception("Cannot set endPoints not added to proxy as system proxy");
ListeningPort = ((IPEndPoint)_listener.LocalEndpoint).Port; if (!proxyStarted)
// accept clients asynchronously throw new Exception("Cannot set system proxy settings before proxy has been started.");
_listener.BeginAcceptTcpClient(OnAcceptConnection, _listener);
var certTrusted = false; //clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemProxy = false);
if (EnableSsl) endPoint.IsSystemProxy = true;
certTrusted = CertManager.CreateTrustedRootCertificate();
if (SetAsSystemProxy)
{
SystemProxyHelper.EnableProxyHttp( SystemProxyHelper.EnableProxyHttp(
Equals(ListeningIpAddress, IPAddress.Any) ? "127.0.0.1" : ListeningIpAddress.ToString(), ListeningPort); Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), endPoint.Port);
#if !DEBUG #if !DEBUG
FireFoxHelper.AddFirefox(); FireFoxHelper.AddFirefox();
#endif #endif
if (endPoint.EnableSsl)
if (EnableSsl)
{ {
RootCertificateName = RootCertificateName ?? "Titanium_Proxy_Test_Root"; RootCertificateName = RootCertificateName ?? "Titanium_Proxy_Test_Root";
...@@ -96,34 +104,63 @@ namespace Titanium.Web.Proxy ...@@ -96,34 +104,63 @@ namespace Titanium.Web.Proxy
if (certTrusted) if (certTrusted)
{ {
SystemProxyHelper.EnableProxyHttps( SystemProxyHelper.EnableProxyHttps(
Equals(ListeningIpAddress, IPAddress.Any) ? "127.0.0.1" : ListeningIpAddress.ToString(), Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
ListeningPort); endPoint.Port);
} }
} }
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
}
public static void Start()
{
EnableSsl = ProxyEndPoints.Any(x => x.EnableSsl);
if (EnableSsl)
certTrusted = CertManager.CreateTrustedRootCertificate();
foreach (var endPoint in ProxyEndPoints)
{
endPoint.listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
endPoint.listener.Start();
endPoint.Port = ((IPEndPoint)endPoint.listener.LocalEndpoint).Port;
// accept clients asynchronously
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
} }
return true; proxyStarted = true;
} }
private static void OnAcceptConnection(IAsyncResult asyn) private static void OnAcceptConnection(IAsyncResult asyn)
{ {
var endPoint = (ProxyEndPoint)asyn.AsyncState;
var client = endPoint.listener.EndAcceptTcpClient(asyn);
try try
{ {
// Get the listener that handles the client request. if (endPoint.GetType() == typeof(TransparentProxyEndPoint))
_listener.BeginAcceptTcpClient(OnAcceptConnection, _listener); Task.Factory.StartNew(() => HandleClient(endPoint as TransparentProxyEndPoint, client));
else
var client = _listener.EndAcceptTcpClient(asyn); Task.Factory.StartNew(() => HandleClient(endPoint as ExplicitProxyEndPoint, client));
Task.Factory.StartNew(() => HandleClient(client));
} }
catch catch
{ {
// ignored // ignored
} }
// Get the listener that handles the client request.
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
} }
public static void Stop() public static void Stop()
{ {
var SetAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemProxy);
if (SetAsSystemProxy) if (SetAsSystemProxy)
{ {
SystemProxyHelper.DisableAllProxy(); SystemProxyHelper.DisableAllProxy();
...@@ -132,7 +169,11 @@ namespace Titanium.Web.Proxy ...@@ -132,7 +169,11 @@ namespace Titanium.Web.Proxy
#endif #endif
} }
_listener.Stop(); foreach (var endPoint in ProxyEndPoints)
{
endPoint.listener.Stop();
}
CertManager.Dispose(); CertManager.Dispose();
} }
} }
......
...@@ -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 HandleClient(TcpClient client) private static void HandleClient(ExplicitProxyEndPoint endPoint, TcpClient client)
{ {
Stream clientStream = client.GetStream(); Stream clientStream = client.GetStream();
var clientStreamReader = new CustomBinaryReader(clientStream, Encoding.ASCII); var clientStreamReader = new CustomBinaryReader(clientStream, Encoding.ASCII);
...@@ -51,10 +51,10 @@ namespace Titanium.Web.Proxy ...@@ -51,10 +51,10 @@ namespace Titanium.Web.Proxy
var httpVersion = httpCmdSplit[2]; var httpVersion = httpCmdSplit[2];
var excluded = ExcludedHttpsHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)); var excluded = endPoint.ExcludedHostNameRegex != null ? endPoint.ExcludedHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)) : false;
//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 == 443) if (httpVerb.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port!=80)
{ {
httpRemoteUri = new Uri("https://" + httpCmdSplit[1]); httpRemoteUri = new Uri("https://" + httpCmdSplit[1]);
clientStreamReader.ReadAllLines(); clientStreamReader.ReadAllLines();
...@@ -68,9 +68,10 @@ namespace Titanium.Web.Proxy ...@@ -68,9 +68,10 @@ namespace Titanium.Web.Proxy
try try
{ {
sslStream = new SslStream(clientStream, true); sslStream = new SslStream(clientStream, true);
//Successfully managed to authenticate the client using the fake certificate //Successfully managed to authenticate the client using the fake certificate
sslStream.AuthenticateAsServer(certificate, false, sslStream.AuthenticateAsServer(certificate, false,
SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, false); SupportedProtocols, false);
clientStreamReader = new CustomBinaryReader(sslStream, Encoding.ASCII); clientStreamReader = new CustomBinaryReader(sslStream, Encoding.ASCII);
clientStreamWriter = new StreamWriter(sslStream); clientStreamWriter = new StreamWriter(sslStream);
...@@ -95,15 +96,18 @@ namespace Titanium.Web.Proxy ...@@ -95,15 +96,18 @@ namespace Titanium.Web.Proxy
{ {
clientStreamReader.ReadAllLines(); clientStreamReader.ReadAllLines();
WriteConnectResponse(clientStreamWriter, httpVersion); WriteConnectResponse(clientStreamWriter, httpVersion);
TcpHelper.SendRaw(clientStreamReader.BaseStream, null, null, httpRemoteUri.Host, httpRemoteUri.Port,
TcpHelper.SendRaw(clientStream, null, null, httpRemoteUri.Host, httpRemoteUri.Port,
false); false);
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null); Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
return; return;
} }
//Now create the request //Now create the request
HandleHttpSessionRequest(client, httpCmd, clientStream, clientStreamReader, clientStreamWriter, HandleHttpSessionRequest(client, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.OriginalString : null); httpRemoteUri.Scheme == Uri.UriSchemeHttps ? true : false);
} }
catch catch
{ {
...@@ -111,9 +115,43 @@ namespace Titanium.Web.Proxy ...@@ -111,9 +115,43 @@ namespace Titanium.Web.Proxy
} }
} }
private static void HandleClient(TransparentProxyEndPoint endPoint, TcpClient client)
{
var sslStream = new SslStream(client.GetStream(), true);
CustomBinaryReader clientStreamReader = null;
StreamWriter clientStreamWriter = null;
var certificate = CertManager.CreateCertificate("127.0.0.1");
try
{
//Successfully managed to authenticate the client using the fake certificate
sslStream.AuthenticateAsServer(certificate, false,
SslProtocols.Tls, false);
clientStreamReader = new CustomBinaryReader(sslStream, Encoding.ASCII);
clientStreamWriter = new StreamWriter(sslStream);
//HTTPS server created - we can now decrypt the client's traffic
}
catch (Exception e)
{
if (sslStream != null)
sslStream.Dispose();
Dispose(client, sslStream, clientStreamReader, clientStreamWriter, null);
return;
}
var httpCmd = clientStreamReader.ReadLine();
//Now create the request
HandleHttpSessionRequest(client, httpCmd, sslStream, clientStreamReader, clientStreamWriter,
true);
}
private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream, private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream,
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string secureTunnelHostName) CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, bool IsHttps)
{ {
TcpConnection connection = null; TcpConnection connection = null;
string lastRequestHostName = null; string lastRequestHostName = null;
...@@ -135,8 +173,8 @@ namespace Titanium.Web.Proxy ...@@ -135,8 +173,8 @@ namespace Titanium.Web.Proxy
var httpCmdSplit = httpCmd.Split(SpaceSplit, 3); var httpCmdSplit = httpCmd.Split(SpaceSplit, 3);
var httpMethod = httpCmdSplit[0]; var httpMethod = httpCmdSplit[0];
var httpRemoteUri =
new Uri(secureTunnelHostName == null ? httpCmdSplit[1] : (secureTunnelHostName + httpCmdSplit[1]));
var httpVersion = httpCmdSplit[2]; var httpVersion = httpCmdSplit[2];
Version version; Version version;
...@@ -149,11 +187,6 @@ namespace Titanium.Web.Proxy ...@@ -149,11 +187,6 @@ namespace Titanium.Web.Proxy
version = new Version(1, 0); version = new Version(1, 0);
} }
if (httpRemoteUri.Scheme == Uri.UriSchemeHttps)
{
args.IsHttps = true;
}
args.ProxySession.Request.RequestHeaders = new List<HttpHeader>(); args.ProxySession.Request.RequestHeaders = new List<HttpHeader>();
...@@ -166,10 +199,13 @@ namespace Titanium.Web.Proxy ...@@ -166,10 +199,13 @@ namespace Titanium.Web.Proxy
SetRequestHeaders(args.ProxySession.Request.RequestHeaders, args.ProxySession); SetRequestHeaders(args.ProxySession.Request.RequestHeaders, args.ProxySession);
var httpRemoteUri = new Uri(!IsHttps ? httpCmdSplit[1] : (string.Concat("https://", args.ProxySession.Request.Hostname, httpCmdSplit[1])));
args.IsHttps = IsHttps;
if (args.ProxySession.Request.UpgradeToWebSocket) if (args.ProxySession.Request.UpgradeToWebSocket)
{ {
TcpHelper.SendRaw(clientStreamReader.BaseStream, httpCmd, args.ProxySession.Request.RequestHeaders, TcpHelper.SendRaw(clientStream, httpCmd, args.ProxySession.Request.RequestHeaders,
httpRemoteUri.Host, httpRemoteUri.Port, httpRemoteUri.Scheme == Uri.UriSchemeHttps); httpRemoteUri.Host, httpRemoteUri.Port, args.IsHttps);
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
return; return;
} }
......
...@@ -103,7 +103,7 @@ namespace Titanium.Web.Proxy ...@@ -103,7 +103,7 @@ namespace Titanium.Web.Proxy
if (response.Response.ResponseHeaders[i].Value.Contains(";")) if (response.Response.ResponseHeaders[i].Value.Contains(";"))
{ {
response.Response.ContentType = response.Response.ResponseHeaders[i].Value.Split(';')[0].Trim(); response.Response.ContentType = response.Response.ResponseHeaders[i].Value.Split(';')[0].Trim();
response.Response.CharacterSet = response.Response.ResponseHeaders[i].Value.Split(';')[1].ToLower().Replace("charset=", string.Empty).Trim(); response.Response.CharacterSet = response.Response.ResponseHeaders[i].Value.Split(';')[1].Substring(9).Trim();
} }
else else
response.Response.ContentType = response.Response.ResponseHeaders[i].Value.ToLower().Trim(); response.Response.ContentType = response.Response.ResponseHeaders[i].Value.ToLower().Trim();
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug-Net45|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug-Net45|AnyCPU'">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug-Net45\</OutputPath> <OutputPath>bin\Debug-Net45\</OutputPath>
<DefineConstants>DEBUG</DefineConstants> <DefineConstants>DEBUG;NET45</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<DefineConstants>NET45</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Ionic.Zip, Version=1.9.7.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL"> <Reference Include="Ionic.Zip, Version=1.9.7.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
...@@ -85,6 +86,7 @@ ...@@ -85,6 +86,7 @@
<Compile Include="Helpers\CertificateManager.cs" /> <Compile Include="Helpers\CertificateManager.cs" />
<Compile Include="Helpers\Firefox.cs" /> <Compile Include="Helpers\Firefox.cs" />
<Compile Include="Helpers\SystemProxy.cs" /> <Compile Include="Helpers\SystemProxy.cs" />
<Compile Include="Models\EndPoint.cs" />
<Compile Include="Network\TcpExtensions.cs" /> <Compile Include="Network\TcpExtensions.cs" />
<Compile Include="Network\TcpConnectionManager.cs" /> <Compile Include="Network\TcpConnectionManager.cs" />
<Compile Include="Models\HttpHeader.cs" /> <Compile Include="Models\HttpHeader.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