Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
989c8831
Commit
989c8831
authored
May 10, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prefetch server connection async during client ssl authentication
parent
190da80f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
19 deletions
+66
-19
ExplicitClientHandler.cs
Titanium.Web.Proxy/ExplicitClientHandler.cs
+18
-7
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+5
-0
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+24
-6
TransparentClientHandler.cs
Titanium.Web.Proxy/TransparentClientHandler.cs
+19
-6
No files found.
Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
989c8831
...
...
@@ -14,7 +14,6 @@ using Titanium.Web.Proxy.Extensions;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network
;
using
Titanium.Web.Proxy.Network.Tcp
;
namespace
Titanium.Web.Proxy
...
...
@@ -34,13 +33,16 @@ namespace Titanium.Web.Proxy
var
cancellationToken
=
cancellationTokenSource
.
Token
;
var
clientStream
=
new
CustomBufferedStream
(
clientConnection
.
GetStream
(),
BufferPool
,
BufferSize
);
var
clientStreamWriter
=
new
HttpResponseWriter
(
clientStream
,
BufferPool
,
BufferSize
);
Task
<
TcpServerConnection
>
prefetchConnectionTask
=
null
;
bool
closeServerConnection
=
false
;
try
{
string
connectHostname
=
null
;
TunnelConnectSessionEventArgs
connectArgs
=
null
;
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
if
(
await
HttpHelper
.
IsConnectMethod
(
clientStream
)
==
1
)
...
...
@@ -135,9 +137,6 @@ namespace Titanium.Web.Proxy
{
// test server HTTP/2 support
// todo: this is a hack, because Titanium does not support HTTP protocol changing currently
// When connection pool becomes stable we can connect to server preemptively in a separate task here without awaiting
// using HTTPS CONNECT hostname. Then by releasing server connection
// back to connection pool we can authenticate against client/server concurrently and save time.
var
connection
=
await
getServerConnection
(
connectArgs
,
true
,
SslExtensions
.
Http2ProtocolAsList
,
cancellationToken
);
...
...
@@ -148,7 +147,8 @@ namespace Titanium.Web.Proxy
}
SslStream
sslStream
=
null
;
prefetchConnectionTask
=
getServerConnection
(
connectArgs
,
true
,
null
,
cancellationToken
);
try
{
sslStream
=
new
SslStream
(
clientStream
);
...
...
@@ -292,27 +292,38 @@ namespace Titanium.Web.Proxy
// Now create the request
await
handleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
WebSession
.
ConnectRequest
);
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
WebSession
.
ConnectRequest
,
prefetchConnectionTask
);
}
catch
(
ProxyException
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
e
);
}
catch
(
IOException
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
new
Exception
(
"Connection was aborted"
,
e
));
}
catch
(
SocketException
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
new
Exception
(
"Could not connect"
,
e
));
}
catch
(
Exception
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
new
Exception
(
"Error occured in whilst handling the client"
,
e
));
}
finally
{
clientStream
.
Dispose
();
if
(
prefetchConnectionTask
!=
null
)
{
var
connection
=
await
prefetchConnectionTask
;
await
tcpConnectionFactory
.
Release
(
connection
,
closeServerConnection
||
!
EnableConnectionPool
);
}
if
(!
cancellationTokenSource
.
IsCancellationRequested
)
{
cancellationTokenSource
.
Cancel
();
...
...
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
989c8831
...
...
@@ -114,6 +114,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <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
(
close
||
connection
.
IsWinAuthenticated
)
{
disposalBag
.
Add
(
connection
);
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
989c8831
...
...
@@ -49,13 +49,16 @@ namespace Titanium.Web.Proxy
/// explicit endpoint.
/// </param>
/// <param name="connectRequest">The Connect request if this is a HTTPS request from explicit endpoint.</param>
/// <param name="prefetchConnectionTask">Prefected server connection for current client using Connect/SNI headers.</param>
private
async
Task
handleHttpSessionRequest
(
ProxyEndPoint
endPoint
,
TcpClientConnection
clientConnection
,
CustomBufferedStream
clientStream
,
HttpResponseWriter
clientStreamWriter
,
CancellationTokenSource
cancellationTokenSource
,
string
httpsConnectHostname
,
ConnectRequest
connectRequest
)
CancellationTokenSource
cancellationTokenSource
,
string
httpsConnectHostname
,
ConnectRequest
connectRequest
,
Task
<
TcpServerConnection
>
prefetchConnectionTask
=
null
)
{
var
cancellationToken
=
cancellationTokenSource
.
Token
;
TcpServerConnection
serverConnection
=
null
;
bool
serverConnectionClose
=
false
;
bool
closeServerConnection
=
false
;
try
{
...
...
@@ -178,6 +181,13 @@ namespace Titanium.Web.Proxy
}
bool
newConnection
=
false
;
if
(
serverConnection
==
null
&&
prefetchConnectionTask
!=
null
)
{
serverConnection
=
await
prefetchConnectionTask
;
newConnection
=
true
;
}
// create a new connection if hostname/upstream end point changes
if
(
serverConnection
!=
null
&&
(!
serverConnection
.
HostName
.
EqualsIgnoreCase
(
request
.
RequestUri
.
Host
)
...
...
@@ -230,7 +240,7 @@ namespace Titanium.Web.Proxy
await
tcpConnectionFactory
.
Release
(
serverConnection
,
true
);
serverConnection
=
await
getServerConnection
(
args
,
false
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
continue
;
}
...
...
@@ -245,7 +255,7 @@ namespace Titanium.Web.Proxy
// if connection is closing exit
if
(!
response
.
KeepAlive
)
{
serverConnectionClose
=
true
;
closeServerConnection
=
true
;
return
;
}
...
...
@@ -262,7 +272,7 @@ namespace Titanium.Web.Proxy
catch
(
Exception
e
)
{
args
.
Exception
=
e
;
serverConnectionClose
=
true
;
closeServerConnection
=
true
;
throw
;
}
finally
...
...
@@ -274,7 +284,15 @@ namespace Titanium.Web.Proxy
}
finally
{
await
tcpConnectionFactory
.
Release
(
serverConnection
,
serverConnectionClose
||
!
EnableConnectionPool
);
//don't release prefetched connection here.
//it will be handled by the parent method which created it.
if
(
prefetchConnectionTask
==
null
||
serverConnection
!=
await
prefetchConnectionTask
)
{
await
tcpConnectionFactory
.
Release
(
serverConnection
,
closeServerConnection
||
!
EnableConnectionPool
);
}
}
}
...
...
Titanium.Web.Proxy/TransparentClientHandler.cs
View file @
989c8831
...
...
@@ -31,9 +31,11 @@ namespace Titanium.Web.Proxy
var
cancellationToken
=
cancellationTokenSource
.
Token
;
var
clientStream
=
new
CustomBufferedStream
(
clientConnection
.
GetStream
(),
BufferPool
,
BufferSize
);
var
clientStreamWriter
=
new
HttpResponseWriter
(
clientStream
,
BufferPool
,
BufferSize
);
Task
<
TcpServerConnection
>
prefetchConnectionTask
=
null
;
bool
closeServerConnection
=
false
;
try
{
var
clientHelloInfo
=
await
SslTools
.
PeekClientHello
(
clientStream
,
BufferPool
,
cancellationToken
);
...
...
@@ -59,11 +61,11 @@ namespace Titanium.Web.Proxy
if
(
endPoint
.
DecryptSsl
&&
args
.
DecryptSsl
)
{
SslStream
sslStream
=
null
;
prefetchConnectionTask
=
tcpConnectionFactory
.
GetClient
(
httpsHostName
,
endPoint
.
Port
,
null
,
true
,
null
,
false
,
this
,
UpStreamEndPoint
,
UpStreamHttpsProxy
,
cancellationToken
);
// When connection pool becomes stable we can connect to server preemptively in a separate task here without awaiting
// for all HTTPS requests using SNI hostname. Then by releasing server connection
// back to connection pool we can authenticate against client/server concurrently and save time.
SslStream
sslStream
=
null
;
//do client authentication using fake certificate
try
...
...
@@ -129,27 +131,38 @@ namespace Titanium.Web.Proxy
// HTTPS server created - we can now decrypt the client's traffic
// Now create the request
await
handleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
isHttps
?
httpsHostName
:
null
,
null
);
cancellationTokenSource
,
isHttps
?
httpsHostName
:
null
,
null
,
prefetchConnectionTask
);
}
catch
(
ProxyException
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
e
);
}
catch
(
IOException
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
new
Exception
(
"Connection was aborted"
,
e
));
}
catch
(
SocketException
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
new
Exception
(
"Could not connect"
,
e
));
}
catch
(
Exception
e
)
{
closeServerConnection
=
true
;
onException
(
clientStream
,
new
Exception
(
"Error occured in whilst handling the client"
,
e
));
}
finally
{
clientStream
.
Dispose
();
if
(
prefetchConnectionTask
!=
null
)
{
var
connection
=
await
prefetchConnectionTask
;
await
tcpConnectionFactory
.
Release
(
connection
,
closeServerConnection
||
!
EnableConnectionPool
);
}
if
(!
cancellationTokenSource
.
IsCancellationRequested
)
{
cancellationTokenSource
.
Cancel
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment