Commit 989b6cad authored by Honfika's avatar Honfika

do not change headers in transparent mode

parent f11b6b60
......@@ -96,7 +96,9 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
public event EventHandler<MultipartRequestPartSentEventArgs> MultipartRequestPartSent;
public ProxyEndPoint LocalEndPoint;
public ProxyEndPoint LocalEndPoint { get; }
public bool IsTransparent => LocalEndPoint is TransparentProxyEndPoint;
/// <summary>
/// Constructor to initialize the proxy
......
......@@ -20,7 +20,6 @@ namespace Titanium.Web.Proxy.Helpers
public async Task WriteResponseAsync(Response response, bool flush = true)
{
await WriteResponseStatusAsync(response.HttpVersion, response.StatusCode, response.StatusDescription);
response.Headers.FixProxyHeaders();
await WriteHeadersAsync(response.Headers, flush);
}
......
......@@ -79,7 +79,7 @@ namespace Titanium.Web.Proxy.Http
/// Prepare and send the http(s) request
/// </summary>
/// <returns></returns>
internal async Task SendRequest(bool enable100ContinueBehaviour)
internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTransparent)
{
var upstreamProxy = ServerConnection.UpStreamProxy;
......@@ -89,12 +89,12 @@ namespace Titanium.Web.Proxy.Http
//prepare the request & headers
await writer.WriteLineAsync(Request.CreateRequestLine(Request.Method,
useUpstreamProxy ? Request.OriginalUrl : Request.RequestUri.PathAndQuery,
useUpstreamProxy || isTransparent ? Request.OriginalUrl : Request.RequestUri.PathAndQuery,
Request.HttpVersion));
//Send Authentication to Upstream proxy if needed
if (upstreamProxy != null
if (!isTransparent && upstreamProxy != null
&& ServerConnection.IsHttps == false
&& !string.IsNullOrEmpty(upstreamProxy.UserName)
&& upstreamProxy.Password != null)
......@@ -106,7 +106,7 @@ namespace Titanium.Web.Proxy.Http
//write request headers
foreach (var header in Request.Headers)
{
if (header.Name != KnownHeaders.ProxyAuthorization)
if (isTransparent || header.Name != KnownHeaders.ProxyAuthorization)
{
await header.WriteToStreamAsync(writer);
}
......
......@@ -76,6 +76,7 @@ namespace Titanium.Web.Proxy
response.Headers.AddHeader(KnownHeaders.ProxyAuthenticate, $"Basic realm=\"{ProxyRealm}\"");
response.Headers.AddHeader(KnownHeaders.ProxyConnection, KnownHeaders.ProxyConnectionClose);
response.Headers.FixProxyHeaders();
await clientStreamWriter.WriteResponseAsync(response);
return response;
}
......
......@@ -106,8 +106,11 @@ namespace Titanium.Web.Proxy
}
//write back successfull CONNECT response
connectArgs.WebSession.Response = ConnectResponse.CreateSuccessfullConnectResponse(version);
await clientStreamWriter.WriteResponseAsync(connectArgs.WebSession.Response);
var response = ConnectResponse.CreateSuccessfullConnectResponse(version);
response.Headers.FixProxyHeaders();
connectArgs.WebSession.Response = response;
await clientStreamWriter.WriteResponseAsync(response);
var clientHelloInfo = await SslTools.PeekClientHello(clientStream);
bool isClientHello = clientHelloInfo != null;
......@@ -401,14 +404,14 @@ namespace Titanium.Web.Proxy
args.ProxyClient.ClientStreamWriter = clientStreamWriter;
//proxy authorization check
if (httpsConnectHostname == null && await CheckAuthorization(clientStreamWriter, args) == false)
if (!args.IsTransparent && httpsConnectHostname == null && await CheckAuthorization(clientStreamWriter, args) == false)
{
break;
}
PrepareRequestHeaders(args.WebSession.Request.Headers);
if (!isTransparentEndPoint)
{
PrepareRequestHeaders(args.WebSession.Request.Headers);
args.WebSession.Request.Host = args.WebSession.Request.RequestUri.Authority;
}
......@@ -447,6 +450,8 @@ namespace Titanium.Web.Proxy
connection = await GetServerConnection(args, false);
}
var response = args.WebSession.Response;
//if upgrading to websocket then relay the requet without reading the contents
if (args.WebSession.Request.UpgradeToWebSocket)
{
......@@ -457,13 +462,16 @@ namespace Titanium.Web.Proxy
string httpStatus = await connection.StreamReader.ReadLineAsync();
Response.ParseResponseLine(httpStatus, out var responseVersion, out int responseStatusCode, out string responseStatusDescription);
args.WebSession.Response.HttpVersion = responseVersion;
args.WebSession.Response.StatusCode = responseStatusCode;
args.WebSession.Response.StatusDescription = responseStatusDescription;
response.HttpVersion = responseVersion;
response.StatusCode = responseStatusCode;
response.StatusDescription = responseStatusDescription;
await HeaderParser.ReadHeaders(connection.StreamReader, args.WebSession.Response.Headers);
await HeaderParser.ReadHeaders(connection.StreamReader, response.Headers);
await clientStreamWriter.WriteResponseAsync(args.WebSession.Response);
if (!args.IsTransparent)
{
await clientStreamWriter.WriteResponseAsync(response);
}
//If user requested call back then do it
if (BeforeResponse != null && !args.WebSession.Response.ResponseLocked)
......@@ -483,7 +491,7 @@ namespace Titanium.Web.Proxy
await HandleHttpSessionRequestInternal(connection, args);
//if connection is closing exit
if (args.WebSession.Response.KeepAlive == false)
if (response.KeepAlive == false)
{
break;
}
......@@ -522,7 +530,7 @@ namespace Titanium.Web.Proxy
if (request.ExpectContinue)
{
args.WebSession.SetConnection(connection);
await args.WebSession.SendRequest(Enable100ContinueBehaviour);
await args.WebSession.SendRequest(Enable100ContinueBehaviour, args.IsTransparent);
}
//If 100 continue was the response inform that to the client
......@@ -546,7 +554,7 @@ namespace Titanium.Web.Proxy
if (!request.ExpectContinue)
{
args.WebSession.SetConnection(connection);
await args.WebSession.SendRequest(Enable100ContinueBehaviour);
await args.WebSession.SendRequest(Enable100ContinueBehaviour, args.IsTransparent);
}
//check if content-length is > 0
......
......@@ -70,7 +70,11 @@ namespace Titanium.Web.Proxy
//Write back response status to client
await clientStreamWriter.WriteResponseStatusAsync(response.HttpVersion, response.StatusCode, response.StatusDescription);
response.Headers.FixProxyHeaders();
if (!args.IsTransparent)
{
response.Headers.FixProxyHeaders();
}
if (response.IsBodyRead)
{
bool isChunked = response.IsChunked;
......
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