1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
2
using System . Net . Sockets ;
5
3
using System . Text ;
6
4
using System . Threading . Tasks ;
7
5
using System . IO ;
8
6
using System . Net . Security ;
9
7
using Titanium . Web . Proxy . Helpers ;
10
- using System . Threading ;
11
- using Titanium . Web . Proxy . Extensions ;
12
8
using Titanium . Web . Proxy . Models ;
13
9
using System . Security . Authentication ;
14
10
@@ -19,38 +15,30 @@ namespace Titanium.Web.Proxy.Network
19
15
/// </summary>
20
16
internal class TcpConnectionFactory
21
17
{
22
- /// <summary>
23
- /// Get a TcpConnection to the specified host, port optionally HTTPS and a particular HTTP version
24
- /// </summary>
25
- /// <param name="hostname"></param>
26
- /// <param name="port"></param>
27
- /// <param name="isHttps"></param>
28
- /// <param name="version"></param>
29
- /// <returns></returns>
30
- internal async Task < TcpConnection > GetClient ( string hostname , int port , bool isHttps , Version version ,
31
- ExternalProxy upStreamHttpProxy , ExternalProxy upStreamHttpsProxy , int bufferSize , SslProtocols supportedSslProtocols ,
32
- int connectionTimeOutSeconds ,
33
- RemoteCertificateValidationCallback remoteCertificateValidationCallBack ,
34
- LocalCertificateSelectionCallback localCertificateSelectionCallback )
35
- {
36
- //not in cache so create and return
37
- return await CreateClient ( hostname , port , isHttps , version , connectionTimeOutSeconds , upStreamHttpProxy , upStreamHttpsProxy , bufferSize , supportedSslProtocols ,
38
- remoteCertificateValidationCallBack , localCertificateSelectionCallback ) ;
39
-
40
- }
41
-
42
18
43
19
/// <summary>
44
- /// Create connection to a particular host/port optionally with SSL and a particular HTTP version
20
+ /// Creates a TCP connection to server
45
21
/// </summary>
46
- /// <param name="hostname"></param>
47
- /// <param name="port"></param>
22
+ /// <param name="bufferSize"></param>
23
+ /// <param name="connectionTimeOutSeconds"></param>
24
+ /// <param name="remoteHostName"></param>
25
+ /// <param name="httpCmd"></param>
26
+ /// <param name="httpVersion"></param>
48
27
/// <param name="isHttps"></param>
49
- /// <param name="version"></param>
28
+ /// <param name="remotePort"></param>
29
+ /// <param name="supportedSslProtocols"></param>
30
+ /// <param name="remoteCertificateValidationCallback"></param>
31
+ /// <param name="localCertificateSelectionCallback"></param>
32
+ /// <param name="externalHttpProxy"></param>
33
+ /// <param name="externalHttpsProxy"></param>
34
+ /// <param name="clientStream"></param>
50
35
/// <returns></returns>
51
- private async Task < TcpConnection > CreateClient ( string hostname , int port , bool isHttps , Version version , int connectionTimeOutSeconds ,
52
- ExternalProxy upStreamHttpProxy , ExternalProxy upStreamHttpsProxy , int bufferSize , SslProtocols supportedSslProtocols ,
53
- RemoteCertificateValidationCallback remoteCertificateValidationCallBack , LocalCertificateSelectionCallback localCertificateSelectionCallback )
36
+ internal async Task < TcpConnection > CreateClient ( int bufferSize , int connectionTimeOutSeconds ,
37
+ string remoteHostName , int remotePort , Version httpVersion ,
38
+ bool isHttps , SslProtocols supportedSslProtocols ,
39
+ RemoteCertificateValidationCallback remoteCertificateValidationCallback , LocalCertificateSelectionCallback localCertificateSelectionCallback ,
40
+ ExternalProxy externalHttpProxy , ExternalProxy externalHttpsProxy ,
41
+ Stream clientStream )
54
42
{
55
43
TcpClient client ;
56
44
Stream stream ;
@@ -60,15 +48,15 @@ private async Task<TcpConnection> CreateClient(string hostname, int port, bool i
60
48
SslStream sslStream = null ;
61
49
62
50
//If this proxy uses another external proxy then create a tunnel request for HTTPS connections
63
- if ( upStreamHttpsProxy != null )
51
+ if ( externalHttpsProxy != null )
64
52
{
65
- client = new TcpClient ( upStreamHttpsProxy . HostName , upStreamHttpsProxy . Port ) ;
53
+ client = new TcpClient ( externalHttpsProxy . HostName , externalHttpsProxy . Port ) ;
66
54
stream = client . GetStream ( ) ;
67
55
68
56
using ( var writer = new StreamWriter ( stream , Encoding . ASCII , bufferSize , true ) )
69
57
{
70
- await writer . WriteLineAsync ( string . Format ( "CONNECT {0}:{1} {2}" , hostname , port , version ) ) ;
71
- await writer . WriteLineAsync ( string . Format ( "Host: {0}:{1}" , hostname , port ) ) ;
58
+ await writer . WriteLineAsync ( string . Format ( "CONNECT {0}:{1} {2}" , remoteHostName , remotePort , httpVersion ) ) ;
59
+ await writer . WriteLineAsync ( string . Format ( "Host: {0}:{1}" , remoteHostName , remotePort ) ) ;
72
60
await writer . WriteLineAsync ( "Connection: Keep-Alive" ) ;
73
61
await writer . WriteLineAsync ( ) ;
74
62
await writer . FlushAsync ( ) ;
@@ -89,16 +77,16 @@ private async Task<TcpConnection> CreateClient(string hostname, int port, bool i
89
77
}
90
78
else
91
79
{
92
- client = new TcpClient ( hostname , port ) ;
80
+ client = new TcpClient ( remoteHostName , remotePort ) ;
93
81
stream = client . GetStream ( ) ;
94
82
}
95
83
96
84
try
97
85
{
98
- sslStream = new SslStream ( stream , true , remoteCertificateValidationCallBack ,
86
+ sslStream = new SslStream ( stream , true , remoteCertificateValidationCallback ,
99
87
localCertificateSelectionCallback ) ;
100
88
101
- await sslStream . AuthenticateAsClientAsync ( hostname , null , supportedSslProtocols , false ) ;
89
+ await sslStream . AuthenticateAsClientAsync ( remoteHostName , null , supportedSslProtocols , false ) ;
102
90
103
91
stream = sslStream ;
104
92
}
@@ -114,14 +102,14 @@ private async Task<TcpConnection> CreateClient(string hostname, int port, bool i
114
102
}
115
103
else
116
104
{
117
- if ( upStreamHttpProxy != null )
105
+ if ( externalHttpProxy != null )
118
106
{
119
- client = new TcpClient ( upStreamHttpProxy . HostName , upStreamHttpProxy . Port ) ;
107
+ client = new TcpClient ( externalHttpProxy . HostName , externalHttpProxy . Port ) ;
120
108
stream = client . GetStream ( ) ;
121
109
}
122
110
else
123
111
{
124
- client = new TcpClient ( hostname , port ) ;
112
+ client = new TcpClient ( remoteHostName , remotePort ) ;
125
113
stream = client . GetStream ( ) ;
126
114
}
127
115
}
@@ -132,15 +120,17 @@ private async Task<TcpConnection> CreateClient(string hostname, int port, bool i
132
120
stream . ReadTimeout = connectionTimeOutSeconds * 1000 ;
133
121
stream . WriteTimeout = connectionTimeOutSeconds * 1000 ;
134
122
123
+ client . NoDelay = true ;
124
+
135
125
return new TcpConnection ( )
136
126
{
137
- HostName = hostname ,
138
- port = port ,
127
+ HostName = remoteHostName ,
128
+ port = remotePort ,
139
129
IsHttps = isHttps ,
140
130
TcpClient = client ,
141
131
StreamReader = new CustomBinaryReader ( stream ) ,
142
132
Stream = stream ,
143
- Version = version
133
+ Version = httpVersion
144
134
} ;
145
135
}
146
136
0 commit comments