12
12
using Titanium . Web . Proxy . Shared ;
13
13
using System . Security . Cryptography . X509Certificates ;
14
14
using Titanium . Web . Proxy . EventArguments ;
15
+ using Titanium . Web . Proxy . Models ;
15
16
16
17
namespace Titanium . Web . Proxy . Network
17
18
{
@@ -38,7 +39,12 @@ internal class TcpConnectionManager
38
39
{
39
40
static Dictionary < string , List < TcpConnection > > connectionCache = new Dictionary < string , List < TcpConnection > > ( ) ;
40
41
static SemaphoreSlim connectionAccessLock = new SemaphoreSlim ( 1 ) ;
41
- internal static async Task < TcpConnection > GetClient ( SessionEventArgs sessionArgs , string hostname , int port , bool isHttps , Version version )
42
+
43
+ internal static async Task < TcpConnection > GetClient ( string hostname , int port , bool isHttps , Version version )
44
+ {
45
+ return await GetClient ( null , hostname , port , isHttps , version ) ;
46
+ }
47
+ internal static async Task < TcpConnection > GetClient ( ConnectRequest connectRequest , string hostname , int port , bool isHttps , Version version )
42
48
{
43
49
List < TcpConnection > cachedConnections = null ;
44
50
TcpConnection cached = null ;
@@ -72,14 +78,14 @@ internal static async Task<TcpConnection> GetClient(SessionEventArgs sessionArgs
72
78
}
73
79
74
80
if ( cached == null )
75
- cached = await CreateClient ( sessionArgs , hostname , port , isHttps , version ) . ConfigureAwait ( false ) ;
81
+ cached = await CreateClient ( connectRequest , hostname , port , isHttps , version ) . ConfigureAwait ( false ) ;
76
82
77
83
78
84
//just create one more preemptively
79
85
if ( cachedConnections == null || cachedConnections . Count ( ) < 2 )
80
86
{
81
- var task = CreateClient ( sessionArgs , hostname , port , isHttps , version )
82
- . ContinueWith ( async ( x ) => { if ( x . Status == TaskStatus . RanToCompletion ) await ReleaseClient ( x . Result ) ; } ) ;
87
+ var task = CreateClient ( connectRequest , hostname , port , isHttps , version )
88
+ . ContinueWith ( async ( x ) => { if ( x . Status == TaskStatus . RanToCompletion ) await ReleaseClient ( x . Result ) ; } ) ;
83
89
}
84
90
85
91
return cached ;
@@ -90,7 +96,7 @@ internal static string GetConnectionKey(string hostname, int port, bool isHttps,
90
96
return string . Format ( "{0}:{1}:{2}:{3}:{4}" , hostname . ToLower ( ) , port , isHttps , version . Major , version . Minor ) ;
91
97
}
92
98
93
- private static async Task < TcpConnection > CreateClient ( SessionEventArgs sessionArgs , string hostname , int port , bool isHttps , Version version )
99
+ private static async Task < TcpConnection > CreateClient ( ConnectRequest connectRequest , string hostname , int port , bool isHttps , Version version )
94
100
{
95
101
TcpClient client ;
96
102
Stream stream ;
@@ -106,8 +112,8 @@ private static async Task<TcpConnection> CreateClient(SessionEventArgs sessionAr
106
112
107
113
using ( var writer = new StreamWriter ( stream , Encoding . ASCII , Constants . BUFFER_SIZE , true ) )
108
114
{
109
- await writer . WriteLineAsync ( string . Format ( "CONNECT {0}:{1} {2}" , sessionArgs . WebSession . Request . RequestUri . Host , sessionArgs . WebSession . Request . RequestUri . Port , sessionArgs . WebSession . Request . HttpVersion ) ) . ConfigureAwait ( false ) ;
110
- await writer . WriteLineAsync ( string . Format ( "Host: {0}:{1}" , sessionArgs . WebSession . Request . RequestUri . Host , sessionArgs . WebSession . Request . RequestUri . Port ) ) . ConfigureAwait ( false ) ;
115
+ await writer . WriteLineAsync ( string . Format ( "CONNECT {0}:{1} {2}" , hostname , port , version ) ) . ConfigureAwait ( false ) ;
116
+ await writer . WriteLineAsync ( string . Format ( "Host: {0}:{1}" , hostname , port ) ) . ConfigureAwait ( false ) ;
111
117
await writer . WriteLineAsync ( "Connection: Keep-Alive" ) . ConfigureAwait ( false ) ;
112
118
await writer . WriteLineAsync ( ) . ConfigureAwait ( false ) ;
113
119
await writer . FlushAsync ( ) . ConfigureAwait ( false ) ;
@@ -132,8 +138,9 @@ private static async Task<TcpConnection> CreateClient(SessionEventArgs sessionAr
132
138
133
139
try
134
140
{
135
- sslStream = new CustomSslStream ( stream , true , new RemoteCertificateValidationCallback ( ProxyServer . ValidateServerCertificate ) ) ;
136
- sslStream . Session = sessionArgs ;
141
+ sslStream = new CustomSslStream ( stream , true , new RemoteCertificateValidationCallback ( ProxyServer . ValidateServerCertificate ) ,
142
+ new LocalCertificateSelectionCallback ( ProxyServer . SelectClientCertificate ) ) ;
143
+ sslStream . Param = connectRequest ;
137
144
await sslStream . AuthenticateAsClientAsync ( hostname , null , Constants . SupportedProtocols , false ) . ConfigureAwait ( false ) ;
138
145
stream = ( Stream ) sslStream ;
139
146
}
0 commit comments