Commit 70d144f3 authored by ilushka85's avatar ilushka85

add connect headers to the headers passed into the upstreamproxy decider function

parent 4c75ae15
...@@ -25,7 +25,7 @@ namespace Titanium.Web.Proxy ...@@ -25,7 +25,7 @@ namespace Titanium.Web.Proxy
partial class ProxyServer partial class ProxyServer
{ {
private async Task<bool> CheckAuthorization( StreamWriter clientStreamWriter, IEnumerable<HttpHeader> Headers) private async Task<bool> CheckAuthorization(StreamWriter clientStreamWriter, IEnumerable<HttpHeader> Headers)
{ {
if (AuthenticateUserFunc == null) if (AuthenticateUserFunc == null)
{ {
...@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy ...@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy
return await AuthenticateUserFunc(username, password).ConfigureAwait(false); return await AuthenticateUserFunc(username, password).ConfigureAwait(false);
} }
} }
catch(Exception e) catch (Exception e)
{ {
//Return not authorized //Return not authorized
await WriteResponseStatus(new Version(1, 1), "407", await WriteResponseStatus(new Version(1, 1), "407",
...@@ -156,20 +156,23 @@ namespace Titanium.Web.Proxy ...@@ -156,20 +156,23 @@ namespace Titanium.Web.Proxy
var excluded = endPoint.ExcludedHttpsHostNameRegex != null ? var excluded = endPoint.ExcludedHttpsHostNameRegex != null ?
endPoint.ExcludedHttpsHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)) : false; endPoint.ExcludedHttpsHostNameRegex.Any(x => Regex.IsMatch(httpRemoteUri.Host, x)) : false;
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.ToUpper() == "CONNECT" && !excluded && httpRemoteUri.Port != 80)
{ {
httpRemoteUri = new Uri("https://" + httpCmdSplit[1]); httpRemoteUri = new Uri("https://" + httpCmdSplit[1]);
string tmpLine = null; string tmpLine = null;
List<HttpHeader> headers = new List<HttpHeader>(); connectRequestHeaders = new List<HttpHeader>();
while (!string.IsNullOrEmpty(tmpLine = await clientStreamReader.ReadLineAsync())) while (!string.IsNullOrEmpty(tmpLine = await clientStreamReader.ReadLineAsync()))
{ {
var header = tmpLine.Split(ProxyConstants.ColonSplit, 2); var header = tmpLine.Split(ProxyConstants.ColonSplit, 2);
var newHeader = new HttpHeader(header[0], header[1]); var newHeader = new HttpHeader(header[0], header[1]);
headers.Add(newHeader); connectRequestHeaders.Add(newHeader);
} }
if (await CheckAuthorization(clientStreamWriter,headers) == false) if (await CheckAuthorization(clientStreamWriter, connectRequestHeaders) == false)
{ {
Dispose(clientStream, clientStreamReader, clientStreamWriter, null); Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
return; return;
...@@ -230,7 +233,7 @@ namespace Titanium.Web.Proxy ...@@ -230,7 +233,7 @@ namespace Titanium.Web.Proxy
} }
//Now create the request //Now create the request
await HandleHttpSessionRequest(client, httpCmd, clientStream, clientStreamReader, clientStreamWriter, await HandleHttpSessionRequest(client, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.Host : null, null, null); httpRemoteUri.Scheme == Uri.UriSchemeHttps ? httpRemoteUri.Host : null, connectRequestHeaders, null, null);
} }
catch (Exception ex) catch (Exception ex)
{ {
...@@ -291,7 +294,7 @@ namespace Titanium.Web.Proxy ...@@ -291,7 +294,7 @@ namespace Titanium.Web.Proxy
//Now create the request //Now create the request
await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter, await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
endPoint.EnableSsl ? endPoint.GenericCertificateName : null); endPoint.EnableSsl ? endPoint.GenericCertificateName : null,null);
} }
/// <summary> /// <summary>
/// This is the core request handler method for a particular connection from client /// This is the core request handler method for a particular connection from client
...@@ -304,7 +307,7 @@ namespace Titanium.Web.Proxy ...@@ -304,7 +307,7 @@ namespace Titanium.Web.Proxy
/// <param name="httpsHostName"></param> /// <param name="httpsHostName"></param>
/// <returns></returns> /// <returns></returns>
private async Task HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream, private async Task HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream,
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string httpsHostName, ExternalProxy customUpStreamHttpProxy = null, ExternalProxy customUpStreamHttpsProxy = null) CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string httpsHostName, List<HttpHeader> connectHeaders, ExternalProxy customUpStreamHttpProxy = null, ExternalProxy customUpStreamHttpsProxy = null)
{ {
TcpConnection connection = null; TcpConnection connection = null;
...@@ -386,7 +389,7 @@ namespace Titanium.Web.Proxy ...@@ -386,7 +389,7 @@ namespace Titanium.Web.Proxy
args.ProxyClient.ClientStreamReader = clientStreamReader; args.ProxyClient.ClientStreamReader = clientStreamReader;
args.ProxyClient.ClientStreamWriter = clientStreamWriter; args.ProxyClient.ClientStreamWriter = clientStreamWriter;
if (httpsHostName == null && (await CheckAuthorization(clientStreamWriter,args.WebSession.Request.RequestHeaders.Values) == false)) if (httpsHostName == null && (await CheckAuthorization(clientStreamWriter, args.WebSession.Request.RequestHeaders.Values) == false))
{ {
Dispose(clientStream, clientStreamReader, clientStreamWriter, args); Dispose(clientStream, clientStreamReader, clientStreamWriter, args);
...@@ -428,12 +431,24 @@ namespace Titanium.Web.Proxy ...@@ -428,12 +431,24 @@ namespace Titanium.Web.Proxy
//construct the web request that we are going to issue on behalf of the client. //construct the web request that we are going to issue on behalf of the client.
if (connection == null) if (connection == null)
{ {
if (httpsHostName == null)
{
if (GetCustomUpStreamHttpProxyFunc != null) if (GetCustomUpStreamHttpProxyFunc != null)
{
customUpStreamHttpProxy = GetCustomUpStreamHttpProxyFunc(args.WebSession.Request.RequestHeaders.Values); customUpStreamHttpProxy = GetCustomUpStreamHttpProxyFunc(args.WebSession.Request.RequestHeaders.Values);
}
}
else
{
if (GetCustomUpStreamHttpsProxyFunc != null) if (GetCustomUpStreamHttpsProxyFunc != null)
customUpStreamHttpsProxy = GetCustomUpStreamHttpsProxyFunc(args.WebSession.Request.RequestHeaders.Values); {
foreach (var header in args.WebSession.Request.RequestHeaders.Values)
{
connectHeaders.Add(header);
}
customUpStreamHttpsProxy = GetCustomUpStreamHttpsProxyFunc(connectHeaders);
}
}
connection = await tcpConnectionFactory.CreateClient(BUFFER_SIZE, ConnectionTimeOutSeconds, connection = await tcpConnectionFactory.CreateClient(BUFFER_SIZE, ConnectionTimeOutSeconds,
args.WebSession.Request.RequestUri.Host, args.WebSession.Request.RequestUri.Port, httpVersion, args.WebSession.Request.RequestUri.Host, args.WebSession.Request.RequestUri.Port, httpVersion,
......
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