Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 4477f6d

Browse files
authored
Merge pull request #377 from justcoding121/develop
Beta
2 parents 63ce4b3 + 80de6ea commit 4477f6d

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public bool ReRequest
9696
/// </summary>
9797
public event EventHandler<MultipartRequestPartSentEventArgs> MultipartRequestPartSent;
9898

99-
public ProxyEndPoint LocalEndPoint;
99+
public ProxyEndPoint LocalEndPoint { get; }
100+
101+
public bool IsTransparent => LocalEndPoint is TransparentProxyEndPoint;
100102

101103
/// <summary>
102104
/// Constructor to initialize the proxy

Titanium.Web.Proxy/Helpers/HttpResponseWriter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public HttpResponseWriter(Stream stream, int bufferSize) : base(stream, bufferSi
2020
public async Task WriteResponseAsync(Response response, bool flush = true)
2121
{
2222
await WriteResponseStatusAsync(response.HttpVersion, response.StatusCode, response.StatusDescription);
23-
response.Headers.FixProxyHeaders();
2423
await WriteHeadersAsync(response.Headers, flush);
2524
}
2625

Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal void SetConnection(TcpConnection connection)
7979
/// Prepare and send the http(s) request
8080
/// </summary>
8181
/// <returns></returns>
82-
internal async Task SendRequest(bool enable100ContinueBehaviour)
82+
internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTransparent)
8383
{
8484
var upstreamProxy = ServerConnection.UpStreamProxy;
8585

@@ -89,12 +89,12 @@ internal async Task SendRequest(bool enable100ContinueBehaviour)
8989

9090
//prepare the request & headers
9191
await writer.WriteLineAsync(Request.CreateRequestLine(Request.Method,
92-
useUpstreamProxy ? Request.OriginalUrl : Request.RequestUri.PathAndQuery,
92+
useUpstreamProxy || isTransparent ? Request.OriginalUrl : Request.RequestUri.PathAndQuery,
9393
Request.HttpVersion));
9494

9595

9696
//Send Authentication to Upstream proxy if needed
97-
if (upstreamProxy != null
97+
if (!isTransparent && upstreamProxy != null
9898
&& ServerConnection.IsHttps == false
9999
&& !string.IsNullOrEmpty(upstreamProxy.UserName)
100100
&& upstreamProxy.Password != null)
@@ -106,7 +106,7 @@ await writer.WriteLineAsync(Request.CreateRequestLine(Request.Method,
106106
//write request headers
107107
foreach (var header in Request.Headers)
108108
{
109-
if (header.Name != KnownHeaders.ProxyAuthorization)
109+
if (isTransparent || header.Name != KnownHeaders.ProxyAuthorization)
110110
{
111111
await header.WriteToStreamAsync(writer);
112112
}

Titanium.Web.Proxy/ProxyAuthorizationHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ private async Task<Response> SendAuthentication407Response(HttpResponseWriter cl
7676
response.Headers.AddHeader(KnownHeaders.ProxyAuthenticate, $"Basic realm=\"{ProxyRealm}\"");
7777
response.Headers.AddHeader(KnownHeaders.ProxyConnection, KnownHeaders.ProxyConnectionClose);
7878

79+
response.Headers.FixProxyHeaders();
7980
await clientStreamWriter.WriteResponseAsync(response);
8081
return response;
8182
}

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClient tcpCli
106106
}
107107

108108
//write back successfull CONNECT response
109-
connectArgs.WebSession.Response = ConnectResponse.CreateSuccessfullConnectResponse(version);
110-
await clientStreamWriter.WriteResponseAsync(connectArgs.WebSession.Response);
109+
var response = ConnectResponse.CreateSuccessfullConnectResponse(version);
110+
response.Headers.FixProxyHeaders();
111+
connectArgs.WebSession.Response = response;
112+
113+
await clientStreamWriter.WriteResponseAsync(response);
111114

112115
var clientHelloInfo = await SslTools.PeekClientHello(clientStream);
113116
bool isClientHello = clientHelloInfo != null;
@@ -401,14 +404,14 @@ private async Task HandleHttpSessionRequest(TcpClient client, CustomBufferedStre
401404
args.ProxyClient.ClientStreamWriter = clientStreamWriter;
402405

403406
//proxy authorization check
404-
if (httpsConnectHostname == null && await CheckAuthorization(clientStreamWriter, args) == false)
407+
if (!args.IsTransparent && httpsConnectHostname == null && await CheckAuthorization(clientStreamWriter, args) == false)
405408
{
406409
break;
407410
}
408411

409-
PrepareRequestHeaders(args.WebSession.Request.Headers);
410412
if (!isTransparentEndPoint)
411413
{
414+
PrepareRequestHeaders(args.WebSession.Request.Headers);
412415
args.WebSession.Request.Host = args.WebSession.Request.RequestUri.Authority;
413416
}
414417

@@ -426,10 +429,18 @@ private async Task HandleHttpSessionRequest(TcpClient client, CustomBufferedStre
426429
await BeforeRequest.InvokeAsync(this, args, ExceptionFunc);
427430
}
428431

432+
var response = args.WebSession.Response;
433+
429434
if (args.WebSession.Request.CancelRequest)
430435
{
431436
await HandleHttpSessionResponse(args);
432-
break;
437+
438+
if (!response.KeepAlive)
439+
{
440+
break;
441+
}
442+
443+
continue;
433444
}
434445

435446
//create a new connection if hostname/upstream end point changes
@@ -457,13 +468,16 @@ private async Task HandleHttpSessionRequest(TcpClient client, CustomBufferedStre
457468
string httpStatus = await connection.StreamReader.ReadLineAsync();
458469

459470
Response.ParseResponseLine(httpStatus, out var responseVersion, out int responseStatusCode, out string responseStatusDescription);
460-
args.WebSession.Response.HttpVersion = responseVersion;
461-
args.WebSession.Response.StatusCode = responseStatusCode;
462-
args.WebSession.Response.StatusDescription = responseStatusDescription;
471+
response.HttpVersion = responseVersion;
472+
response.StatusCode = responseStatusCode;
473+
response.StatusDescription = responseStatusDescription;
463474

464-
await HeaderParser.ReadHeaders(connection.StreamReader, args.WebSession.Response.Headers);
475+
await HeaderParser.ReadHeaders(connection.StreamReader, response.Headers);
465476

466-
await clientStreamWriter.WriteResponseAsync(args.WebSession.Response);
477+
if (!args.IsTransparent)
478+
{
479+
await clientStreamWriter.WriteResponseAsync(response);
480+
}
467481

468482
//If user requested call back then do it
469483
if (BeforeResponse != null && !args.WebSession.Response.ResponseLocked)
@@ -483,7 +497,7 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferSize,
483497
await HandleHttpSessionRequestInternal(connection, args);
484498

485499
//if connection is closing exit
486-
if (args.WebSession.Response.KeepAlive == false)
500+
if (!response.KeepAlive)
487501
{
488502
break;
489503
}
@@ -522,7 +536,7 @@ private async Task HandleHttpSessionRequestInternal(TcpConnection connection, Se
522536
if (request.ExpectContinue)
523537
{
524538
args.WebSession.SetConnection(connection);
525-
await args.WebSession.SendRequest(Enable100ContinueBehaviour);
539+
await args.WebSession.SendRequest(Enable100ContinueBehaviour, args.IsTransparent);
526540
}
527541

528542
//If 100 continue was the response inform that to the client
@@ -546,7 +560,7 @@ private async Task HandleHttpSessionRequestInternal(TcpConnection connection, Se
546560
if (!request.ExpectContinue)
547561
{
548562
args.WebSession.SetConnection(connection);
549-
await args.WebSession.SendRequest(Enable100ContinueBehaviour);
563+
await args.WebSession.SendRequest(Enable100ContinueBehaviour, args.IsTransparent);
550564
}
551565

552566
//check if content-length is > 0

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ private async Task HandleHttpSessionResponse(SessionEventArgs args)
7070
//Write back response status to client
7171
await clientStreamWriter.WriteResponseStatusAsync(response.HttpVersion, response.StatusCode, response.StatusDescription);
7272

73-
response.Headers.FixProxyHeaders();
73+
if (!args.IsTransparent)
74+
{
75+
response.Headers.FixProxyHeaders();
76+
}
77+
7478
if (response.IsBodyRead)
7579
{
7680
bool isChunked = response.IsChunked;

0 commit comments

Comments
 (0)