This repository was archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathTcpConnectionFactory.cs
803 lines (680 loc) · 33.4 KB
/
TcpConnectionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.ProxySocket;
namespace Titanium.Web.Proxy.Network.Tcp;
/// <summary>
/// A class that manages Tcp Connection to server used by this proxy server.
/// </summary>
internal class TcpConnectionFactory : IDisposable
{
// Tcp server connection pool cache
private readonly ConcurrentDictionary<string, ConcurrentQueue<TcpServerConnection>> cache = new();
// Tcp connections waiting to be disposed by cleanup task
private readonly ConcurrentBag<TcpServerConnection> disposalBag = new();
// cache object race operations lock
private readonly SemaphoreSlim @lock = new(1);
private bool disposed;
private volatile bool runCleanUpTask = true;
internal TcpConnectionFactory(ProxyServer server)
{
Server = server;
Task.Run(async () => await ClearOutdatedConnections());
}
internal ProxyServer Server { get; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
internal string GetConnectionCacheKey(string remoteHostName, int remotePort,
bool isHttps, List<SslApplicationProtocol>? applicationProtocols,
IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy)
{
// http version is ignored since its an application level decision b/w HTTP 1.0/1.1
// also when doing connect request MS Edge browser sends http 1.0 but uses 1.1 after server sends 1.1 its response.
// That can create cache miss for same server connection unnecessarily especially when prefetching with Connect.
// http version 2 is separated using applicationProtocols below.
var cacheKeyBuilder = new StringBuilder();
cacheKeyBuilder.Append(remoteHostName);
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(remotePort);
cacheKeyBuilder.Append("-");
// when creating Tcp client isConnect won't matter
cacheKeyBuilder.Append(isHttps);
if (applicationProtocols != null)
foreach (var protocol in applicationProtocols.OrderBy(x => x))
{
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(protocol);
}
if (upStreamEndPoint != null)
{
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(upStreamEndPoint.Address);
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(upStreamEndPoint.Port);
}
if (externalProxy != null)
{
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(externalProxy.HostName);
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(externalProxy.Port);
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(externalProxy.ProxyType);
if (externalProxy.UseDefaultCredentials)
{
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(externalProxy.UserName);
cacheKeyBuilder.Append("-");
cacheKeyBuilder.Append(externalProxy.Password);
}
}
return cacheKeyBuilder.ToString();
}
/// <summary>
/// Gets the connection cache key.
/// </summary>
/// <param name="server">The server.</param>
/// <param name="session">The session event arguments.</param>
/// <param name="applicationProtocol">The application protocol.</param>
/// <returns></returns>
internal async Task<string> GetConnectionCacheKey(ProxyServer server, SessionEventArgsBase session,
SslApplicationProtocol applicationProtocol)
{
List<SslApplicationProtocol>? applicationProtocols = null;
if (applicationProtocol != default)
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
var customUpStreamProxy = session.CustomUpStreamProxy;
var isHttps = session.IsHttps;
if (customUpStreamProxy == null && server.GetCustomUpStreamProxyFunc != null)
customUpStreamProxy = await server.GetCustomUpStreamProxyFunc(session);
session.CustomUpStreamProxyUsed = customUpStreamProxy;
var uri = session.HttpClient.Request.RequestUri;
var upStreamEndPoint = session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint;
var upStreamProxy = customUpStreamProxy ?? (isHttps ? server.UpStreamHttpsProxy : server.UpStreamHttpProxy);
return GetConnectionCacheKey(uri.Host, uri.Port, isHttps, applicationProtocols, upStreamEndPoint,
upStreamProxy);
}
/// <summary>
/// Create a server connection.
/// </summary>
/// <param name="proxyServer">The proxy server.</param>
/// <param name="session">The session event arguments.</param>
/// <param name="isConnect">Is this a CONNECT request.</param>
/// <param name="applicationProtocol"></param>
/// <param name="noCache">if set to <c>true</c> [no cache].</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
internal Task<TcpServerConnection> GetServerConnection(ProxyServer proxyServer, SessionEventArgsBase session,
bool isConnect,
SslApplicationProtocol applicationProtocol, bool noCache, CancellationToken cancellationToken)
{
List<SslApplicationProtocol>? applicationProtocols = null;
if (applicationProtocol != default)
applicationProtocols = new List<SslApplicationProtocol> { applicationProtocol };
return GetServerConnection(proxyServer, session, isConnect, applicationProtocols, noCache, false,
cancellationToken)!;
}
/// <summary>
/// Create a server connection.
/// </summary>
/// <param name="proxyServer">The proxy server.</param>
/// <param name="session">The session event arguments.</param>
/// <param name="isConnect">Is this a CONNECT request.</param>
/// <param name="applicationProtocols"></param>
/// <param name="noCache">if set to <c>true</c> [no cache].</param>
/// <param name="prefetch">if set to <c>true</c> [prefetch].</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
internal async Task<TcpServerConnection?> GetServerConnection(ProxyServer proxyServer, SessionEventArgsBase session,
bool isConnect,
List<SslApplicationProtocol>? applicationProtocols, bool noCache, bool prefetch,
CancellationToken cancellationToken)
{
var customUpStreamProxy = session.CustomUpStreamProxy;
var isHttps = session.IsHttps;
if (customUpStreamProxy == null && proxyServer.GetCustomUpStreamProxyFunc != null)
customUpStreamProxy = await proxyServer.GetCustomUpStreamProxyFunc(session);
session.CustomUpStreamProxyUsed = customUpStreamProxy;
var request = session.HttpClient.Request;
string host;
int port;
if (request.Authority.Length > 0)
{
var authority = request.Authority;
var idx = authority.IndexOf((byte)':');
if (idx == -1)
{
host = authority.GetString();
port = 80;
}
else
{
host = authority.Slice(0, idx).GetString();
port = int.Parse(authority.Slice(idx + 1).GetString());
}
}
else
{
var uri = request.RequestUri;
host = uri.Host;
port = uri.Port;
}
if (session.IsTransparent && !string.IsNullOrEmpty(((TransparentBaseProxyEndPoint)session.ProxyEndPoint).OverrideForwardHostName))
{
host = ((TransparentBaseProxyEndPoint)session.ProxyEndPoint).OverrideForwardHostName;
if (((TransparentBaseProxyEndPoint)session.ProxyEndPoint).OverrideForwardPort > 0)
port = ((TransparentBaseProxyEndPoint)session.ProxyEndPoint).OverrideForwardPort;
}
var upStreamEndPoint = session.HttpClient.UpStreamEndPoint ?? proxyServer.UpStreamEndPoint;
var upStreamProxy = customUpStreamProxy ??
(isHttps ? proxyServer.UpStreamHttpsProxy : proxyServer.UpStreamHttpProxy);
return await GetServerConnection(proxyServer, host, port, session.HttpClient.Request.HttpVersion, isHttps,
applicationProtocols, isConnect, session, upStreamEndPoint, upStreamProxy, noCache, prefetch,
cancellationToken);
}
/// <summary>
/// Gets a TCP connection to server from connection pool.
/// </summary>
/// <param name="proxyServer">The current ProxyServer instance.</param>
/// <param name="remoteHostName">The remote hostname.</param>
/// <param name="remotePort">The remote port.</param>
/// <param name="httpVersion">The http version to use.</param>
/// <param name="isHttps">Is this a HTTPS request.</param>
/// <param name="applicationProtocols">The list of HTTPS application level protocol to negotiate if needed.</param>
/// <param name="isConnect">Is this a CONNECT request.</param>
/// <param name="sessionArgs">The session event arguments.</param>
/// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param>
/// <param name="externalProxy">The external proxy to make request via.</param>
/// <param name="noCache">Not from cache/create new connection.</param>
/// <param name="prefetch">if set to <c>true</c> [prefetch].</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
internal async Task<TcpServerConnection?> GetServerConnection(ProxyServer proxyServer, string remoteHostName,
int remotePort,
Version httpVersion, bool isHttps, List<SslApplicationProtocol>? applicationProtocols, bool isConnect,
SessionEventArgsBase sessionArgs, IPEndPoint? upStreamEndPoint, IExternalProxy? externalProxy,
bool noCache, bool prefetch, CancellationToken cancellationToken)
{
var sslProtocol = sessionArgs.ClientConnection.SslProtocol;
var cacheKey = GetConnectionCacheKey(remoteHostName, remotePort,
isHttps, applicationProtocols, upStreamEndPoint, externalProxy);
if (proxyServer.EnableConnectionPool && !noCache)
if (cache.TryGetValue(cacheKey, out var existingConnections))
lock (existingConnections)
{
// +3 seconds for potential delay after getting connection
var cutOff = DateTime.UtcNow.AddSeconds(-proxyServer.ConnectionTimeOutSeconds + 3);
while (existingConnections.Count > 0)
if (existingConnections.TryDequeue(out var recentConnection))
{
if (recentConnection.LastAccess > cutOff
&& recentConnection.TcpSocket.IsGoodConnection())
return recentConnection;
disposalBag.Add(recentConnection);
}
}
var connection = await CreateServerConnection(remoteHostName, remotePort, httpVersion, isHttps, sslProtocol,
applicationProtocols, isConnect, proxyServer, sessionArgs, upStreamEndPoint, externalProxy, cacheKey,
prefetch, cancellationToken);
return connection;
}
/// <summary>
/// Creates a TCP connection to server
/// </summary>
/// <param name="remoteHostName">The remote hostname.</param>
/// <param name="remotePort">The remote port.</param>
/// <param name="httpVersion">The http version to use.</param>
/// <param name="isHttps">Is this a HTTPS request.</param>
/// <param name="sslProtocol">The SSL protocol.</param>
/// <param name="applicationProtocols">The list of HTTPS application level protocol to negotiate if needed.</param>
/// <param name="isConnect">Is this a CONNECT request.</param>
/// <param name="proxyServer">The current ProxyServer instance.</param>
/// <param name="sessionArgs">The http session.</param>
/// <param name="upStreamEndPoint">The local upstream endpoint to make request via.</param>
/// <param name="externalProxy">The external proxy to make request via.</param>
/// <param name="cacheKey">The connection cache key</param>
/// <param name="prefetch">if set to <c>true</c> [prefetch].</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
private async Task<TcpServerConnection?> CreateServerConnection(string remoteHostName, int remotePort,
Version httpVersion, bool isHttps, SslProtocols sslProtocol, List<SslApplicationProtocol>? applicationProtocols,
bool isConnect,
ProxyServer proxyServer, SessionEventArgsBase sessionArgs, IPEndPoint? upStreamEndPoint,
IExternalProxy? externalProxy, string cacheKey,
bool prefetch, CancellationToken cancellationToken)
{
// deny connection to proxy end points to avoid infinite connection loop.
if (Server.ProxyEndPoints.Any(x => x.Port == remotePort)
&& NetworkHelper.IsLocalIpAddress(remoteHostName))
throw new Exception(
$"A client is making HTTP request to one of the listening ports of this proxy {remoteHostName}:{remotePort}");
if (externalProxy != null)
if (Server.ProxyEndPoints.Any(x => x.Port == externalProxy.Port)
&& NetworkHelper.IsLocalIpAddress(externalProxy.HostName))
throw new Exception(
$"A client is making HTTP request via external proxy to one of the listening ports of this proxy {remoteHostName}:{remotePort}");
if (proxyServer.SupportedServerSslProtocols != SslProtocols.None) sslProtocol = proxyServer.SupportedServerSslProtocols;
if (isHttps && sslProtocol == SslProtocols.None) sslProtocol = proxyServer.SupportedSslProtocols;
var useUpstreamProxy1 = false;
// check if external proxy is set for HTTP/HTTPS
if (externalProxy != null && !(externalProxy.HostName == remoteHostName && externalProxy.Port == remotePort))
{
useUpstreamProxy1 = true;
// check if we need to ByPass
if (externalProxy.BypassLocalhost &&
NetworkHelper.IsLocalIpAddress(remoteHostName, externalProxy.ProxyDnsRequests))
useUpstreamProxy1 = false;
}
if (!useUpstreamProxy1) externalProxy = null;
Socket? tcpServerSocket = null;
HttpServerStream? stream = null;
SslApplicationProtocol negotiatedApplicationProtocol = default;
var retry = true;
var enabledSslProtocols = sslProtocol;
retry:
try
{
var socks = externalProxy != null && externalProxy.ProxyType != ExternalProxyType.Http;
var hostname = remoteHostName;
var port = remotePort;
if (externalProxy != null)
{
hostname = externalProxy.HostName;
port = externalProxy.Port;
}
var ipAddresses = await Dns.GetHostAddressesAsync(hostname);
if (ipAddresses == null || ipAddresses.Length == 0)
{
if (prefetch) return null;
throw new Exception($"Could not resolve the hostname {hostname}");
}
if (sessionArgs != null) sessionArgs.TimeLine["Dns Resolved"] = DateTime.UtcNow;
Array.Sort(ipAddresses, (x, y) => x.AddressFamily.CompareTo(y.AddressFamily));
Exception? lastException = null;
for (var i = 0; i < ipAddresses.Length; i++)
try
{
var ipAddress = ipAddresses[i];
var addressFamily = upStreamEndPoint?.AddressFamily ?? ipAddress.AddressFamily;
if (socks)
{
var proxySocket =
new ProxySocket.ProxySocket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
proxySocket.ProxyType = externalProxy!.ProxyType == ExternalProxyType.Socks4
? ProxyTypes.Socks4
: ProxyTypes.Socks5;
proxySocket.ProxyEndPoint = new IPEndPoint(ipAddress, port);
if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null)
{
proxySocket.ProxyUser = externalProxy.UserName;
proxySocket.ProxyPass = externalProxy.Password;
}
tcpServerSocket = proxySocket;
}
else
{
tcpServerSocket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
}
if (upStreamEndPoint != null) tcpServerSocket.Bind(upStreamEndPoint);
tcpServerSocket.NoDelay = proxyServer.NoDelay;
tcpServerSocket.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
tcpServerSocket.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
tcpServerSocket.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds);
if (proxyServer.ReuseSocket && RunTime.IsSocketReuseAvailable())
tcpServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
Task connectTask;
if (socks)
{
if (externalProxy!.ProxyDnsRequests)
{
connectTask =
ProxySocketConnectionTaskFactory.CreateTask((ProxySocket.ProxySocket)tcpServerSocket,
remoteHostName, remotePort);
}
else
{
// todo: resolve only once when the SOCKS proxy has multiple addresses (and the first address fails)
var remoteIpAddresses = await Dns.GetHostAddressesAsync(remoteHostName);
if (remoteIpAddresses == null || remoteIpAddresses.Length == 0)
throw new Exception($"Could not resolve the SOCKS remote hostname {remoteHostName}");
// todo: use the 2nd, 3rd... remote addresses when first fails
connectTask = ProxySocketConnectionTaskFactory.CreateTask(
(ProxySocket.ProxySocket)tcpServerSocket, remoteIpAddresses[0], remotePort);
}
}
else
{
connectTask = SocketConnectionTaskFactory.CreateTask(tcpServerSocket, ipAddress, port);
}
await Task.WhenAny(connectTask,
Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000, cancellationToken));
if (!connectTask.IsCompleted || !tcpServerSocket.Connected)
{
// here we can just do some cleanup and let the loop continue since
// we will either get a connection or wind up with a null tcpClient
// which will throw
try
{
connectTask.Dispose();
}
catch
{
// ignore
}
try
{
tcpServerSocket?.Dispose();
tcpServerSocket = null;
}
catch
{
// ignore
}
continue;
}
break;
}
catch (Exception e)
{
// dispose the current TcpClient and try the next address
lastException = e;
tcpServerSocket?.Dispose();
tcpServerSocket = null;
}
if (tcpServerSocket == null)
{
if (sessionArgs != null && proxyServer.CustomUpStreamProxyFailureFunc != null)
{
var newUpstreamProxy = await proxyServer.CustomUpStreamProxyFailureFunc(sessionArgs);
if (newUpstreamProxy != null)
{
sessionArgs.CustomUpStreamProxyUsed = newUpstreamProxy;
sessionArgs.TimeLine["Retrying Upstream Proxy Connection"] = DateTime.UtcNow;
return await CreateServerConnection(remoteHostName, remotePort, httpVersion, isHttps,
sslProtocol, applicationProtocols, isConnect, proxyServer, sessionArgs, upStreamEndPoint,
externalProxy, cacheKey, prefetch, cancellationToken);
}
}
if (prefetch) return null;
throw new Exception($"Could not establish connection to {hostname}", lastException);
}
if (sessionArgs != null) sessionArgs.TimeLine["Connection Established"] = DateTime.UtcNow;
await proxyServer.InvokeServerConnectionCreateEvent(tcpServerSocket);
stream = new HttpServerStream(proxyServer, new NetworkStream(tcpServerSocket, true), proxyServer.BufferPool,
cancellationToken);
if (externalProxy != null && externalProxy.ProxyType == ExternalProxyType.Http && (isConnect || isHttps))
{
var authority = $"{remoteHostName}:{remotePort}";
var authorityBytes = authority.GetByteString();
var connectRequest = new ConnectRequest(authorityBytes)
{
IsHttps = isHttps,
RequestUriString8 = authorityBytes,
HttpVersion = httpVersion
};
connectRequest.Headers.AddHeader(KnownHeaders.Connection, KnownHeaders.ConnectionKeepAlive);
connectRequest.Headers.AddHeader(KnownHeaders.Host, authority);
if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null)
{
connectRequest.Headers.AddHeader(HttpHeader.ProxyConnectionKeepAlive);
connectRequest.Headers.AddHeader(
HttpHeader.GetProxyAuthorizationHeader(externalProxy.UserName, externalProxy.Password));
}
await proxyServer.OnBeforeUpStreamConnectRequest(connectRequest);
await stream.WriteRequestAsync(connectRequest, cancellationToken);
var httpStatus = await stream.ReadResponseStatus(cancellationToken);
var headers = new HeaderCollection();
await HeaderParser.ReadHeaders(stream, headers, cancellationToken);
if (httpStatus.StatusCode != 200 && !httpStatus.Description.EqualsIgnoreCase("OK")
&& !httpStatus.Description.EqualsIgnoreCase("Connection Established"))
throw new Exception("Upstream proxy failed to create a secure tunnel");
}
if (isHttps)
{
var sslStream = new SslStream(stream, false,
(sender, certificate, chain, sslPolicyErrors) =>
proxyServer.ValidateServerCertificate(sender, sessionArgs, certificate, chain,
sslPolicyErrors),
(sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
proxyServer.SelectClientCertificate(sender, sessionArgs, targetHost, localCertificates,
remoteCertificate, acceptableIssuers));
stream = new HttpServerStream(proxyServer, sslStream, proxyServer.BufferPool, cancellationToken);
var options = new SslClientAuthenticationOptions
{
ApplicationProtocols = applicationProtocols,
TargetHost = remoteHostName,
ClientCertificates = null!,
EnabledSslProtocols = enabledSslProtocols,
CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation
};
await sslStream.AuthenticateAsClientAsync(options, cancellationToken);
#if NET6_0_OR_GREATER
negotiatedApplicationProtocol = sslStream.NegotiatedApplicationProtocol;
#endif
if (sessionArgs != null) sessionArgs.TimeLine["HTTPS Established"] = DateTime.UtcNow;
}
}
catch (IOException ex) when (ex.HResult == unchecked((int)0x80131620) && retry &&
enabledSslProtocols >= SslProtocols.Tls11)
{
stream?.Dispose();
tcpServerSocket?.Close();
// Specifying Tls11 and/or Tls12 will disable the usage of Ssl3, even if it has been included.
// https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.tcptransportsecurity.sslprotocols?view=dotnet-plat-ext-3.1
enabledSslProtocols = proxyServer.SupportedSslProtocols & (SslProtocols)0xff;
if (enabledSslProtocols == SslProtocols.None) throw;
retry = false;
goto retry;
}
catch (AuthenticationException ex) when (ex.HResult == unchecked((int)0x80131501) && retry &&
enabledSslProtocols >= SslProtocols.Tls11)
{
stream?.Dispose();
tcpServerSocket?.Close();
// Specifying Tls11 and/or Tls12 will disable the usage of Ssl3, even if it has been included.
// https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.tcptransportsecurity.sslprotocols?view=dotnet-plat-ext-3.1
enabledSslProtocols = proxyServer.SupportedSslProtocols & (SslProtocols)0xff;
if (enabledSslProtocols == SslProtocols.None) throw;
retry = false;
goto retry;
}
catch (Exception)
{
stream?.Dispose();
tcpServerSocket?.Close();
throw;
}
return new TcpServerConnection(proxyServer, tcpServerSocket, stream, remoteHostName, remotePort, isHttps,
negotiatedApplicationProtocol, httpVersion, externalProxy, upStreamEndPoint, cacheKey);
}
/// <summary>
/// Release connection back to cache.
/// </summary>
/// <param name="connection">The Tcp server connection to return.</param>
/// <param name="close">Should we just close the connection instead of reusing?</param>
internal async Task Release(TcpServerConnection? connection, bool close = false)
{
if (connection == null) return;
if (disposalBag.Any(x => x == connection)) return;
if (close || connection.IsWinAuthenticated || !Server.EnableConnectionPool || connection.IsClosed)
{
disposalBag.Add(connection);
return;
}
connection.LastAccess = DateTime.UtcNow;
try
{
await @lock.WaitAsync();
while (true)
{
if (cache.TryGetValue(connection.CacheKey, out var existingConnections))
{
while (existingConnections.Count >= Server.MaxCachedConnections)
if (existingConnections.TryDequeue(out var staleConnection))
disposalBag.Add(staleConnection);
if (existingConnections.Any(x => x == connection)) break;
existingConnections.Enqueue(connection);
break;
}
if (cache.TryAdd(connection.CacheKey,
new ConcurrentQueue<TcpServerConnection>(new[] { connection })))
break;
}
}
finally
{
@lock.Release();
}
}
internal async Task Release(Task<TcpServerConnection?>? connectionCreateTask, bool closeServerConnection)
{
if (connectionCreateTask == null) return;
TcpServerConnection? connection = null;
try
{
connection = await connectionCreateTask;
}
catch
{
// ignore
}
finally
{
if (connection != null) await Release(connection, closeServerConnection);
}
}
private async Task ClearOutdatedConnections()
{
while (runCleanUpTask)
try
{
var cutOff = DateTime.UtcNow.AddSeconds(-Server.ConnectionTimeOutSeconds);
foreach (var item in cache)
{
var queue = item.Value;
while (queue.Count > 0)
if (queue.TryDequeue(out var connection))
{
if (!Server.EnableConnectionPool || connection.LastAccess < cutOff)
{
disposalBag.Add(connection);
}
else
{
queue.Enqueue(connection);
break;
}
}
}
try
{
await @lock.WaitAsync();
// clear empty queues
var emptyKeys = cache.ToArray().Where(x => x.Value.Count == 0).Select(x => x.Key);
foreach (var key in emptyKeys) cache.TryRemove(key, out _);
}
finally
{
@lock.Release();
}
while (!disposalBag.IsEmpty)
if (disposalBag.TryTake(out var connection))
connection?.Dispose();
}
catch (Exception e)
{
Server.ExceptionFunc?.Invoke(new Exception("An error occurred when disposing server connections.", e));
}
finally
{
// cleanup every 3 seconds by default
await Task.Delay(1000 * 3);
}
}
protected virtual void Dispose(bool disposing)
{
if (disposed) return;
runCleanUpTask = false;
if (disposing)
{
try
{
@lock.Wait();
foreach (var queue in cache.Select(x => x.Value).ToList())
while (!queue.IsEmpty)
if (queue.TryDequeue(out var connection))
disposalBag.Add(connection);
cache.Clear();
}
finally
{
@lock.Release();
}
while (!disposalBag.IsEmpty)
if (disposalBag.TryTake(out var connection))
connection?.Dispose();
}
disposed = true;
}
~TcpConnectionFactory()
{
Dispose(false);
}
private static class SocketConnectionTaskFactory
{
private static IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback,
object state)
{
return ((Socket)state).BeginConnect(address, port, requestCallback, state);
}
private static void EndConnect(IAsyncResult asyncResult)
{
((Socket)asyncResult.AsyncState).EndConnect(asyncResult);
}
public static Task CreateTask(Socket socket, IPAddress ipAddress, int port)
{
return Task.Factory.FromAsync(BeginConnect, EndConnect, ipAddress, port, socket);
}
}
private static class ProxySocketConnectionTaskFactory
{
private static IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback,
object state)
{
return ((ProxySocket.ProxySocket)state).BeginConnect(address, port, requestCallback, state);
}
private static IAsyncResult BeginConnect(string hostName, int port, AsyncCallback requestCallback, object state)
{
return ((ProxySocket.ProxySocket)state).BeginConnect(hostName, port, requestCallback, state);
}
private static void EndConnect(IAsyncResult asyncResult)
{
((ProxySocket.ProxySocket)asyncResult.AsyncState).EndConnect(asyncResult);
}
public static Task CreateTask(ProxySocket.ProxySocket socket, IPAddress ipAddress, int port)
{
return Task.Factory.FromAsync(BeginConnect, EndConnect, ipAddress, port, socket);
}
public static Task CreateTask(ProxySocket.ProxySocket socket, string hostName, int port)
{
return Task.Factory.FromAsync(BeginConnect, EndConnect, hostName, port, socket);
}
}
}