Commit d49448ea authored by Honfika's avatar Honfika

Fix for #289

parent 70e1a0dd
...@@ -76,7 +76,16 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -76,7 +76,16 @@ namespace Titanium.Web.Proxy.Examples.Wpf
{ {
//IncludedHttpsHostNameRegex = new string[0], //IncludedHttpsHostNameRegex = new string[0],
}; };
proxyServer.AddEndPoint(explicitEndPoint); proxyServer.AddEndPoint(explicitEndPoint);
//proxyServer.UpStreamHttpProxy = new ExternalProxy
//{
// HostName = "158.69.115.45",
// Port = 3128,
// UserName = "Titanium",
// Password = "Titanium",
//};
proxyServer.BeforeRequest += ProxyServer_BeforeRequest; proxyServer.BeforeRequest += ProxyServer_BeforeRequest;
proxyServer.BeforeResponse += ProxyServer_BeforeResponse; proxyServer.BeforeResponse += ProxyServer_BeforeResponse;
proxyServer.TunnelConnectRequest += ProxyServer_TunnelConnectRequest; proxyServer.TunnelConnectRequest += ProxyServer_TunnelConnectRequest;
......
...@@ -69,7 +69,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -69,7 +69,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary> /// <summary>
/// Does this session uses SSL /// Does this session uses SSL
/// </summary> /// </summary>
public bool IsHttps => WebSession.Request.RequestUri.Scheme == ProxyServer.UriSchemeHttps; public bool IsHttps => WebSession.Request.IsHttps;
/// <summary> /// <summary>
/// Client End Point. /// Client End Point.
......
...@@ -49,8 +49,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -49,8 +49,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary> /// <summary>
/// Is Https? /// Is Https?
/// </summary> /// </summary>
public bool IsHttps => Request.RequestUri.Scheme == ProxyServer.UriSchemeHttps; public bool IsHttps => Request.IsHttps;
internal HttpWebClient() internal HttpWebClient()
{ {
...@@ -81,7 +80,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -81,7 +80,7 @@ namespace Titanium.Web.Proxy.Http
var requestLines = new StringBuilder(); var requestLines = new StringBuilder();
//prepare the request & headers //prepare the request & headers
requestLines.AppendLine($"{Request.Method} {Request.RequestUri.PathAndQuery} HTTP/{Request.HttpVersion.Major}.{Request.HttpVersion.Minor}"); requestLines.AppendLine($"{Request.Method} {Request.OriginalRequestUrl} HTTP/{Request.HttpVersion.Major}.{Request.HttpVersion.Minor}");
//Send Authentication to Upstream proxy if needed //Send Authentication to Upstream proxy if needed
......
...@@ -23,6 +23,16 @@ namespace Titanium.Web.Proxy.Http ...@@ -23,6 +23,16 @@ namespace Titanium.Web.Proxy.Http
/// </summary> /// </summary>
public Uri RequestUri { get; set; } public Uri RequestUri { get; set; }
/// <summary>
/// Is Https?
/// </summary>
public bool IsHttps => RequestUri.Scheme == ProxyServer.UriSchemeHttps;
/// <summary>
/// The original request Url.
/// </summary>
public string OriginalRequestUrl { get; set; }
/// <summary> /// <summary>
/// Request Http Version /// Request Http Version
/// </summary> /// </summary>
...@@ -219,7 +229,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -219,7 +229,7 @@ namespace Titanium.Web.Proxy.Http
get get
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine($"{Method} {RequestUri.PathAndQuery} HTTP/{HttpVersion.Major}.{HttpVersion.Minor}"); sb.AppendLine($"{Method} {OriginalRequestUrl} HTTP/{HttpVersion.Major}.{HttpVersion.Minor}");
foreach (var header in RequestHeaders) foreach (var header in RequestHeaders)
{ {
sb.AppendLine(header.ToString()); sb.AppendLine(header.ToString());
......
...@@ -63,36 +63,39 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -63,36 +63,39 @@ namespace Titanium.Web.Proxy.Network.Tcp
await client.ConnectAsync(externalProxy.HostName, externalProxy.Port); await client.ConnectAsync(externalProxy.HostName, externalProxy.Port);
stream = new CustomBufferedStream(client.GetStream(), server.BufferSize); stream = new CustomBufferedStream(client.GetStream(), server.BufferSize);
using (var writer = new StreamWriter(stream, Encoding.ASCII, server.BufferSize, true) if (isHttps)
{ {
NewLine = ProxyConstants.NewLine using (var writer = new StreamWriter(stream, Encoding.ASCII, server.BufferSize, true)
})
{
await writer.WriteLineAsync($"CONNECT {remoteHostName}:{remotePort} HTTP/{httpVersion}");
await writer.WriteLineAsync($"Host: {remoteHostName}:{remotePort}");
await writer.WriteLineAsync("Connection: Keep-Alive");
if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null)
{ {
await writer.WriteLineAsync("Proxy-Connection: keep-alive"); NewLine = ProxyConstants.NewLine
await writer.WriteLineAsync("Proxy-Authorization" + ": Basic " + })
Convert.ToBase64String(Encoding.UTF8.GetBytes( {
externalProxy.UserName + ":" + externalProxy.Password))); await writer.WriteLineAsync($"CONNECT {remoteHostName}:{remotePort} HTTP/{httpVersion}");
} await writer.WriteLineAsync($"Host: {remoteHostName}:{remotePort}");
await writer.WriteLineAsync(); await writer.WriteLineAsync("Connection: Keep-Alive");
await writer.FlushAsync();
}
using (var reader = new CustomBinaryReader(stream, server.BufferSize)) if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null)
{ {
string result = await reader.ReadLineAsync(); await writer.WriteLineAsync("Proxy-Connection: keep-alive");
await writer.WriteLineAsync("Proxy-Authorization" + ": Basic " +
Convert.ToBase64String(Encoding.UTF8.GetBytes(
externalProxy.UserName + ":" + externalProxy.Password)));
}
await writer.WriteLineAsync();
await writer.FlushAsync();
}
if (!new[] { "200 OK", "connection established" }.Any(s => result.ContainsIgnoreCase(s))) using (var reader = new CustomBinaryReader(stream, server.BufferSize))
{ {
throw new Exception("Upstream proxy failed to create a secure tunnel"); string result = await reader.ReadLineAsync();
}
await reader.ReadAndIgnoreAllLinesAsync(); if (!new[] { "200 OK", "connection established" }.Any(s => result.ContainsIgnoreCase(s)))
{
throw new Exception("Upstream proxy failed to create a secure tunnel");
}
await reader.ReadAndIgnoreAllLinesAsync();
}
} }
} }
else else
......
...@@ -85,6 +85,7 @@ namespace Titanium.Web.Proxy ...@@ -85,6 +85,7 @@ namespace Titanium.Web.Proxy
connectRequest = new ConnectRequest connectRequest = new ConnectRequest
{ {
RequestUri = httpRemoteUri, RequestUri = httpRemoteUri,
OriginalRequestUrl = httpUrl,
HttpVersion = version, HttpVersion = version,
Method = httpMethod, Method = httpMethod,
}; };
...@@ -303,6 +304,7 @@ namespace Titanium.Web.Proxy ...@@ -303,6 +304,7 @@ namespace Titanium.Web.Proxy
: string.Concat("https://", args.WebSession.Request.Host ?? httpsConnectHostname, httpUrl)); : string.Concat("https://", args.WebSession.Request.Host ?? httpsConnectHostname, httpUrl));
args.WebSession.Request.RequestUri = httpRemoteUri; args.WebSession.Request.RequestUri = httpRemoteUri;
args.WebSession.Request.OriginalRequestUrl = httpUrl;
args.WebSession.Request.Method = httpMethod; args.WebSession.Request.Method = httpMethod;
args.WebSession.Request.HttpVersion = version; args.WebSession.Request.HttpVersion = version;
...@@ -318,7 +320,6 @@ namespace Titanium.Web.Proxy ...@@ -318,7 +320,6 @@ namespace Titanium.Web.Proxy
} }
PrepareRequestHeaders(args.WebSession.Request.RequestHeaders); PrepareRequestHeaders(args.WebSession.Request.RequestHeaders);
args.WebSession.Request.Host = args.WebSession.Request.RequestUri.Authority;
#if NET45 #if NET45
//if win auth is enabled //if win auth is enabled
...@@ -572,18 +573,18 @@ namespace Titanium.Web.Proxy ...@@ -572,18 +573,18 @@ namespace Titanium.Web.Proxy
ExternalProxy customUpStreamHttpProxy = null; ExternalProxy customUpStreamHttpProxy = null;
ExternalProxy customUpStreamHttpsProxy = null; ExternalProxy customUpStreamHttpsProxy = null;
if (args.WebSession.Request.RequestUri.Scheme == UriSchemeHttp) if (args.WebSession.Request.IsHttps)
{ {
if (GetCustomUpStreamHttpProxyFunc != null) if (GetCustomUpStreamHttpsProxyFunc != null)
{ {
customUpStreamHttpProxy = await GetCustomUpStreamHttpProxyFunc(args); customUpStreamHttpsProxy = await GetCustomUpStreamHttpsProxyFunc(args);
} }
} }
else else
{ {
if (GetCustomUpStreamHttpsProxyFunc != null) if (GetCustomUpStreamHttpProxyFunc != null)
{ {
customUpStreamHttpsProxy = await GetCustomUpStreamHttpsProxyFunc(args); customUpStreamHttpProxy = await GetCustomUpStreamHttpProxyFunc(args);
} }
} }
......
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