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

Commit 8fa9ebd

Browse files
authored
Merge pull request #677 from justcoding121/master
beta
2 parents a994cb3 + 0366b34 commit 8fa9ebd

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,38 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
348348
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
349349
}
350350

351-
await tcpClient.ConnectAsync(ipAddress, port);
351+
var connectTask = tcpClient.ConnectAsync(ipAddress, port);
352+
await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000));
353+
if (!connectTask.IsCompleted || !tcpClient.Connected)
354+
{
355+
// here we can just do some cleanup and let the loop continue since
356+
// we will either get a connection or wind up with a null tcpClient
357+
// which will throw
358+
try
359+
{
360+
connectTask.Dispose();
361+
362+
}
363+
catch
364+
{
365+
// ignore
366+
}
367+
try
368+
{
369+
#if NET45
370+
tcpClient?.Close();
371+
#else
372+
tcpClient?.Dispose();
373+
#endif
374+
tcpClient = null;
375+
}
376+
catch
377+
{
378+
// ignore
379+
}
380+
381+
continue;
382+
}
352383
break;
353384
}
354385
catch (Exception e)

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
196196
/// </summary>
197197
public int ConnectionTimeOutSeconds { get; set; } = 60;
198198

199+
/// <summary>
200+
/// Seconds server connection are to wait for connection to be established.
201+
/// Default value is 20 seconds.
202+
/// </summary>
203+
public int ConnectTimeOutSeconds { get; set; } = 20;
204+
199205
/// <summary>
200206
/// Maximum number of concurrent connections per remote host in cache.
201207
/// Only valid when connection pooling is enabled.

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ private async Task<RetryResult> handleHttpSessionRequest(string requestHttpMetho
257257
TcpServerConnection? serverConnection, SslApplicationProtocol sslApplicationProtocol,
258258
CancellationToken cancellationToken, CancellationTokenSource cancellationTokenSource)
259259
{
260+
args.HttpClient.Request.Locked = true;
261+
260262
// a connection generator task with captured parameters via closure.
261263
Func<Task<TcpServerConnection>> generator = () =>
262264
tcpConnectionFactory.GetServerConnection(this, args, isConnect: false,
@@ -266,6 +268,9 @@ private async Task<RetryResult> handleHttpSessionRequest(string requestHttpMetho
266268
// for connection pool, retry fails until cache is exhausted.
267269
return await retryPolicy<ServerConnectionException>().ExecuteAsync(async (connection) =>
268270
{
271+
// set the connection and send request headers
272+
args.HttpClient.SetConnection(connection);
273+
269274
args.TimeLine["Connection Ready"] = DateTime.Now;
270275

271276
if (args.HttpClient.Request.UpgradeToWebSocket)
@@ -290,12 +295,9 @@ private async Task handleHttpSessionRequest(TcpServerConnection connection, Sess
290295
{
291296
var cancellationToken = args.CancellationTokenSource.Token;
292297
var request = args.HttpClient.Request;
293-
request.Locked = true;
294298

295299
var body = request.CompressBodyAndUpdateContentLength();
296300

297-
// set the connection and send request headers
298-
args.HttpClient.SetConnection(connection);
299301
await args.HttpClient.SendRequest(Enable100ContinueBehaviour, args.IsTransparent,
300302
cancellationToken);
301303

0 commit comments

Comments
 (0)