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
687a8740
Commit
687a8740
authored
Apr 30, 2018
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TcpClientConnection class
parent
c882467b
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
145 additions
and
80 deletions
+145
-80
MainWindow.xaml.cs
Examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
+0
-1
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+1
-0
SessionEventArgsBase.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
+1
-1
ExplicitClientHandler.cs
Titanium.Web.Proxy/ExplicitClientHandler.cs
+9
-7
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+2
-3
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+5
-5
DebugCustomBufferedStream.cs
Titanium.Web.Proxy/Network/DebugCustomBufferedStream.cs
+30
-4
ProxyClient.cs
Titanium.Web.Proxy/Network/ProxyClient.cs
+4
-4
TcpClientConnection.cs
Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs
+46
-0
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+10
-11
TcpServerConnection.cs
Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs
+5
-4
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+3
-11
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+23
-24
TransparentClientHandler.cs
Titanium.Web.Proxy/TransparentClientHandler.cs
+5
-4
WinAuthHandler.cs
Titanium.Web.Proxy/WinAuthHandler.cs
+1
-1
No files found.
Examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
View file @
687a8740
...
...
@@ -9,7 +9,6 @@ using System.Windows;
using
System.Windows.Controls
;
using
System.Windows.Input
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
687a8740
...
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.Helpers;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http.Responses
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network
;
namespace
Titanium.Web.Proxy.EventArguments
{
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
View file @
687a8740
...
...
@@ -87,7 +87,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// Client End Point.
/// </summary>
public
IPEndPoint
ClientEndPoint
=>
(
IPEndPoint
)
ProxyClient
.
TcpClient
.
Client
.
RemoteEndPoint
;
public
IPEndPoint
ClientEndPoint
=>
(
IPEndPoint
)
ProxyClient
.
ClientConnection
.
RemoteEndPoint
;
/// <summary>
/// A web session corresponding to a single request/response sequence
...
...
Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
687a8740
...
...
@@ -15,6 +15,8 @@ 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
{
...
...
@@ -25,14 +27,14 @@ namespace Titanium.Web.Proxy
/// So for HTTPS requests client would send CONNECT header to negotiate a secure tcp tunnel via proxy
/// </summary>
/// <param name="endPoint">The explicit endpoint.</param>
/// <param name="
tcpClient">The client
.</param>
/// <param name="
clientConnection">The client connection
.</param>
/// <returns>The task.</returns>
private
async
Task
HandleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
private
async
Task
HandleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClient
Connection
clientConnection
)
{
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
cancellationToken
=
cancellationTokenSource
.
Token
;
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
var
clientStream
=
new
CustomBufferedStream
(
clientConnection
.
GetStream
(),
BufferSize
);
var
clientStreamWriter
=
new
HttpResponseWriter
(
clientStream
,
BufferSize
);
...
...
@@ -67,7 +69,7 @@ namespace Titanium.Web.Proxy
connectArgs
=
new
TunnelConnectSessionEventArgs
(
BufferSize
,
endPoint
,
connectRequest
,
cancellationTokenSource
,
ExceptionFunc
);
connectArgs
.
ProxyClient
.
TcpClient
=
tcpClient
;
connectArgs
.
ProxyClient
.
ClientConnection
=
clientConnection
;
connectArgs
.
ProxyClient
.
ClientStream
=
clientStream
;
await
endPoint
.
InvokeBeforeTunnelConnectRequest
(
this
,
connectArgs
,
ExceptionFunc
);
...
...
@@ -158,7 +160,7 @@ namespace Titanium.Web.Proxy
options
.
ApplicationProtocols
=
clientHelloInfo
.
GetAlpn
();
if
(
options
.
ApplicationProtocols
==
null
||
options
.
ApplicationProtocols
.
Count
==
0
)
{
options
.
ApplicationProtocols
=
Titanium
.
Web
.
Proxy
.
Extensions
.
SslExtensions
.
Http11ProtocolAsList
;
options
.
ApplicationProtocols
=
SslExtensions
.
Http11ProtocolAsList
;
}
}
...
...
@@ -270,13 +272,13 @@ namespace Titanium.Web.Proxy
await
TcpHelper
.
SendHttp2
(
clientStream
,
connection
.
Stream
,
BufferSize
,
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
connectArgs
.
CancellationTokenSource
,
ExceptionFunc
);
connectArgs
.
CancellationTokenSource
,
clientConnection
.
Id
,
ExceptionFunc
);
}
}
}
//Now create the request
await
HandleHttpSessionRequest
(
endPoint
,
tcpClient
,
clientStream
,
await
HandleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
WebSession
.
ConnectRequest
);
}
...
...
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
687a8740
...
...
@@ -265,15 +265,14 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="onDataSend"></param>
/// <param name="onDataReceive"></param>
/// <param name="cancellationTokenSource"></param>
/// <param name="connectionId"></param>
/// <param name="exceptionFunc"></param>
/// <returns></returns>
internal
static
async
Task
SendHttp2
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
CancellationTokenSource
cancellationTokenSource
,
CancellationTokenSource
cancellationTokenSource
,
Guid
connectionId
,
ExceptionHandler
exceptionFunc
)
{
var
connectionId
=
Guid
.
NewGuid
();
//Now async relay all server=>client & client=>server data
var
sendRelay
=
CopyHttp2FrameAsync
(
clientStream
,
serverStream
,
onDataSend
,
bufferSize
,
connectionId
,
...
...
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
687a8740
...
...
@@ -28,7 +28,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// Connection to server
/// </summary>
internal
TcpConnection
ServerConnection
{
get
;
set
;
}
internal
Tcp
Server
Connection
ServerConnection
{
get
;
set
;
}
/// <summary>
/// Request ID.
...
...
@@ -69,11 +69,11 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// Set the tcp connection to server used by this webclient
/// </summary>
/// <param name="
connection">Instance of <see cref="Tcp
Connection" /></param>
internal
void
SetConnection
(
Tcp
Connection
c
onnection
)
/// <param name="
serverConnection">Instance of <see cref="TcpServer
Connection" /></param>
internal
void
SetConnection
(
Tcp
ServerConnection
serverC
onnection
)
{
c
onnection
.
LastAccess
=
DateTime
.
Now
;
ServerConnection
=
c
onnection
;
serverC
onnection
.
LastAccess
=
DateTime
.
Now
;
ServerConnection
=
serverC
onnection
;
}
/// <summary>
...
...
Titanium.Web.Proxy/Network/DebugCustomBufferedStream.cs
View file @
687a8740
#
if
DEBUG
using
System
;
using
System.IO
;
using
System.Text
;
using
System.Threading
;
using
StreamExtended.Network
;
...
...
@@ -15,11 +17,11 @@ namespace Titanium.Web.Proxy.Network
private
readonly
FileStream
fileStreamSent
;
public
DebugCustomBufferedStream
(
Stream
baseStream
,
int
bufferSize
)
:
base
(
baseStream
,
bufferSize
)
public
DebugCustomBufferedStream
(
Guid
connectionId
,
string
type
,
Stream
baseStream
,
int
bufferSize
,
bool
leaveOpen
=
false
)
:
base
(
baseStream
,
bufferSize
,
leaveOpen
)
{
Counter
=
Interlocked
.
Increment
(
ref
counter
);
fileStreamSent
=
new
FileStream
(
Path
.
Combine
(
basePath
,
$"
{
Counter
}
_sent.dat"
),
FileMode
.
Create
);
fileStreamReceived
=
new
FileStream
(
Path
.
Combine
(
basePath
,
$"
{
Counter
}
_received.dat"
),
FileMode
.
Create
);
fileStreamSent
=
new
FileStream
(
Path
.
Combine
(
basePath
,
$"
{
connectionId
}
_
{
type
}
_
{
Counter
}
_sent.dat"
),
FileMode
.
Create
);
fileStreamReceived
=
new
FileStream
(
Path
.
Combine
(
basePath
,
$"
{
connectionId
}
_
{
type
}
_
{
Counter
}
_received.dat"
),
FileMode
.
Create
);
}
public
int
Counter
{
get
;
}
...
...
@@ -27,18 +29,42 @@ namespace Titanium.Web.Proxy.Network
protected
override
void
OnDataWrite
(
byte
[]
buffer
,
int
offset
,
int
count
)
{
fileStreamSent
.
Write
(
buffer
,
offset
,
count
);
Flush
();
}
protected
override
void
OnDataRead
(
byte
[]
buffer
,
int
offset
,
int
count
)
{
fileStreamReceived
.
Write
(
buffer
,
offset
,
count
);
Flush
();
}
public
void
LogException
(
Exception
ex
)
{
var
data
=
Encoding
.
UTF8
.
GetBytes
(
"EXCEPTION: "
+
ex
);
fileStreamReceived
.
Write
(
data
,
0
,
data
.
Length
);
fileStreamReceived
.
Flush
();
}
public
override
void
Flush
()
{
fileStreamSent
.
Flush
(
true
);
fileStreamReceived
.
Flush
(
true
);
base
.
Flush
();
if
(
CanWrite
)
{
base
.
Flush
();
}
}
protected
override
void
Dispose
(
bool
disposing
)
{
if
(
disposing
)
{
Flush
();
fileStreamSent
.
Dispose
();
fileStreamReceived
.
Dispose
();
}
base
.
Dispose
(
disposing
);
}
}
}
...
...
Titanium.Web.Proxy/Network/ProxyClient.cs
View file @
687a8740
using
System.Net.Sockets
;
using
StreamExtended.Network
;
using
StreamExtended.Network
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Network.Tcp
;
namespace
Titanium.Web.Proxy.Network
{
...
...
@@ -10,9 +10,9 @@ namespace Titanium.Web.Proxy.Network
internal
class
ProxyClient
{
/// <summary>
/// TcpClient used to communicate with client
/// TcpClient
connection
used to communicate with client
/// </summary>
internal
TcpClient
TcpClient
{
get
;
set
;
}
internal
TcpClient
Connection
ClientConnection
{
get
;
set
;
}
/// <summary>
/// Holds the stream to client
...
...
Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs
0 → 100644
View file @
687a8740
using
System
;
using
System.IO
;
using
System.Net
;
using
System.Net.Sockets
;
using
Titanium.Web.Proxy.Extensions
;
namespace
Titanium.Web.Proxy.Network.Tcp
{
/// <summary>
/// An object that holds TcpConnection to a particular server and port
/// </summary>
internal
class
TcpClientConnection
:
IDisposable
{
internal
TcpClientConnection
(
ProxyServer
proxyServer
,
TcpClient
tcpClient
)
{
this
.
tcpClient
=
tcpClient
;
this
.
proxyServer
=
proxyServer
;
this
.
proxyServer
.
UpdateClientConnectionCount
(
true
);
}
private
ProxyServer
proxyServer
{
get
;
}
public
Guid
Id
{
get
;
}
=
Guid
.
NewGuid
();
public
EndPoint
LocalEndPoint
=>
tcpClient
.
Client
.
LocalEndPoint
;
public
EndPoint
RemoteEndPoint
=>
tcpClient
.
Client
.
RemoteEndPoint
;
private
readonly
TcpClient
tcpClient
;
public
Stream
GetStream
()
{
return
tcpClient
.
GetStream
();
}
/// <summary>
/// Dispose.
/// </summary>
public
void
Dispose
()
{
tcpClient
.
CloseSocket
();
proxyServer
.
UpdateClientConnectionCount
(
false
);
}
}
}
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
687a8740
...
...
@@ -32,7 +32,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <param name="externalProxy"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
internal
async
Task
<
TcpConnection
>
CreateClient
(
string
remoteHostName
,
int
remotePort
,
internal
async
Task
<
Tcp
Server
Connection
>
CreateClient
(
string
remoteHostName
,
int
remotePort
,
List
<
SslApplicationProtocol
>
applicationProtocols
,
Version
httpVersion
,
bool
decryptSsl
,
bool
isConnect
,
ProxyServer
proxyServer
,
IPEndPoint
upStreamEndPoint
,
ExternalProxy
externalProxy
,
CancellationToken
cancellationToken
)
...
...
@@ -52,26 +52,26 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
}
TcpClient
c
lient
=
null
;
TcpClient
tcpC
lient
=
null
;
CustomBufferedStream
stream
=
null
;
bool
http2Supported
=
false
;
try
{
c
lient
=
new
TcpClient
(
upStreamEndPoint
);
tcpC
lient
=
new
TcpClient
(
upStreamEndPoint
);
//If this proxy uses another external proxy then create a tunnel request for HTTP/HTTPS connections
if
(
useUpstreamProxy
)
{
await
c
lient
.
ConnectAsync
(
externalProxy
.
HostName
,
externalProxy
.
Port
);
await
tcpC
lient
.
ConnectAsync
(
externalProxy
.
HostName
,
externalProxy
.
Port
);
}
else
{
await
c
lient
.
ConnectAsync
(
remoteHostName
,
remotePort
);
await
tcpC
lient
.
ConnectAsync
(
remoteHostName
,
remotePort
);
}
stream
=
new
CustomBufferedStream
(
c
lient
.
GetStream
(),
proxyServer
.
BufferSize
);
stream
=
new
CustomBufferedStream
(
tcpC
lient
.
GetStream
(),
proxyServer
.
BufferSize
);
if
(
useUpstreamProxy
&&
(
isConnect
||
decryptSsl
))
{
...
...
@@ -129,17 +129,17 @@ namespace Titanium.Web.Proxy.Network.Tcp
#endif
}
c
lient
.
ReceiveTimeout
=
proxyServer
.
ConnectionTimeOutSeconds
*
1000
;
c
lient
.
SendTimeout
=
proxyServer
.
ConnectionTimeOutSeconds
*
1000
;
tcpC
lient
.
ReceiveTimeout
=
proxyServer
.
ConnectionTimeOutSeconds
*
1000
;
tcpC
lient
.
SendTimeout
=
proxyServer
.
ConnectionTimeOutSeconds
*
1000
;
}
catch
(
Exception
)
{
stream
?.
Dispose
();
c
lient
?.
Close
();
tcpC
lient
?.
Close
();
throw
;
}
return
new
Tcp
Connection
(
proxyServer
)
return
new
Tcp
ServerConnection
(
proxyServer
,
tcpClient
)
{
UpStreamProxy
=
externalProxy
,
UpStreamEndPoint
=
upStreamEndPoint
,
...
...
@@ -148,7 +148,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
IsHttps
=
decryptSsl
,
IsHttp2Supported
=
http2Supported
,
UseUpstreamProxy
=
useUpstreamProxy
,
TcpClient
=
client
,
StreamWriter
=
new
HttpRequestWriter
(
stream
,
proxyServer
.
BufferSize
),
Stream
=
stream
,
Version
=
httpVersion
...
...
Titanium.Web.Proxy/Network/Tcp/TcpConnection.cs
→
Titanium.Web.Proxy/Network/Tcp/Tcp
Server
Connection.cs
View file @
687a8740
...
...
@@ -11,10 +11,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <summary>
/// An object that holds TcpConnection to a particular server and port
/// </summary>
internal
class
TcpConnection
:
IDisposable
internal
class
Tcp
Server
Connection
:
IDisposable
{
internal
Tcp
Connection
(
ProxyServer
proxyServer
)
internal
Tcp
ServerConnection
(
ProxyServer
proxyServer
,
TcpClient
tcpClient
)
{
this
.
tcpClient
=
tcpClient
;
LastAccess
=
DateTime
.
Now
;
this
.
proxyServer
=
proxyServer
;
this
.
proxyServer
.
UpdateServerConnectionCount
(
true
);
...
...
@@ -44,7 +45,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// </summary>
internal
Version
Version
{
get
;
set
;
}
internal
TcpClient
TcpClient
{
private
get
;
set
;
}
private
readonly
TcpClient
tcpClient
;
/// <summary>
/// Used to write lines to server
...
...
@@ -68,7 +69,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
{
Stream
?.
Dispose
();
T
cpClient
.
CloseSocket
();
t
cpClient
.
CloseSocket
();
proxyServer
.
UpdateServerConnectionCount
(
false
);
}
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
687a8740
...
...
@@ -8,7 +8,6 @@ using System.Security.Cryptography.X509Certificates;
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.Helpers.WinHttp
;
using
Titanium.Web.Proxy.Models
;
...
...
@@ -648,27 +647,20 @@ namespace Titanium.Web.Proxy
private
async
Task
HandleClient
(
TcpClient
tcpClient
,
ProxyEndPoint
endPoint
)
{
UpdateClientConnectionCount
(
true
);
tcpClient
.
ReceiveTimeout
=
ConnectionTimeOutSeconds
*
1000
;
tcpClient
.
SendTimeout
=
ConnectionTimeOutSeconds
*
1000
;
try
using
(
var
clientConnection
=
new
TcpClientConnection
(
this
,
tcpClient
))
{
if
(
endPoint
is
TransparentProxyEndPoint
tep
)
{
await
HandleClient
(
tep
,
tcpClient
);
await
HandleClient
(
tep
,
clientConnection
);
}
else
{
await
HandleClient
((
ExplicitProxyEndPoint
)
endPoint
,
tcpClient
);
await
HandleClient
((
ExplicitProxyEndPoint
)
endPoint
,
clientConnection
);
}
}
finally
{
UpdateClientConnectionCount
(
false
);
tcpClient
.
CloseSocket
();
}
}
/// <summary>
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
687a8740
using
System
;
using
System.Net
;
using
System.Net.Sockets
;
using
System.Text.RegularExpressions
;
using
System.Threading
;
using
System.Threading.Tasks
;
...
...
@@ -32,7 +31,7 @@ namespace Titanium.Web.Proxy
/// client/server abruptly terminates connection or by normal HTTP termination.
/// </summary>
/// <param name="endPoint">The proxy endpoint.</param>
/// <param name="client
">The client
.</param>
/// <param name="client
Connection">The client connection
.</param>
/// <param name="clientStream">The client stream.</param>
/// <param name="clientStreamWriter">The client stream writer.</param>
/// <param name="cancellationTokenSource">The cancellation token source for this async task.</param>
...
...
@@ -43,13 +42,13 @@ namespace Titanium.Web.Proxy
/// <param name="connectRequest">The Connect request if this is a HTTPS request from explicit endpoint.</param>
/// <param name="isTransparentEndPoint">Is this a request from transparent endpoint?</param>
/// <returns></returns>
private
async
Task
HandleHttpSessionRequest
(
ProxyEndPoint
endPoint
,
TcpClient
client
,
private
async
Task
HandleHttpSessionRequest
(
ProxyEndPoint
endPoint
,
TcpClient
Connection
clientConnection
,
CustomBufferedStream
clientStream
,
HttpResponseWriter
clientStreamWriter
,
CancellationTokenSource
cancellationTokenSource
,
string
httpsConnectHostname
,
ConnectRequest
connectRequest
,
bool
isTransparentEndPoint
=
false
)
{
var
cancellationToken
=
cancellationTokenSource
.
Token
;
Tcp
Connection
c
onnection
=
null
;
Tcp
ServerConnection
serverC
onnection
=
null
;
try
{
...
...
@@ -67,7 +66,7 @@ namespace Titanium.Web.Proxy
var
args
=
new
SessionEventArgs
(
BufferSize
,
endPoint
,
cancellationTokenSource
,
ExceptionFunc
)
{
ProxyClient
=
{
TcpClient
=
client
},
ProxyClient
=
{
ClientConnection
=
clientConnection
},
WebSession
=
{
ConnectRequest
=
connectRequest
}
};
...
...
@@ -173,29 +172,29 @@ namespace Titanium.Web.Proxy
}
//create a new connection if hostname/upstream end point changes
if
(
c
onnection
!=
null
&&
(!
c
onnection
.
HostName
.
Equals
(
request
.
RequestUri
.
Host
,
if
(
serverC
onnection
!=
null
&&
(!
serverC
onnection
.
HostName
.
Equals
(
request
.
RequestUri
.
Host
,
StringComparison
.
OrdinalIgnoreCase
)
||
args
.
WebSession
.
UpStreamEndPoint
!=
null
&&
!
args
.
WebSession
.
UpStreamEndPoint
.
Equals
(
c
onnection
.
UpStreamEndPoint
)))
&&
!
args
.
WebSession
.
UpStreamEndPoint
.
Equals
(
serverC
onnection
.
UpStreamEndPoint
)))
{
c
onnection
.
Dispose
();
c
onnection
=
null
;
serverC
onnection
.
Dispose
();
serverC
onnection
=
null
;
}
if
(
c
onnection
==
null
)
if
(
serverC
onnection
==
null
)
{
c
onnection
=
await
GetServerConnection
(
args
,
false
,
cancellationToken
);
serverC
onnection
=
await
GetServerConnection
(
args
,
false
,
cancellationToken
);
}
//if upgrading to websocket then relay the requet without reading the contents
if
(
request
.
UpgradeToWebSocket
)
{
//prepare the prefix content
await
c
onnection
.
StreamWriter
.
WriteLineAsync
(
httpCmd
,
cancellationToken
);
await
c
onnection
.
StreamWriter
.
WriteHeadersAsync
(
request
.
Headers
,
await
serverC
onnection
.
StreamWriter
.
WriteLineAsync
(
httpCmd
,
cancellationToken
);
await
serverC
onnection
.
StreamWriter
.
WriteHeadersAsync
(
request
.
Headers
,
cancellationToken
:
cancellationToken
);
string
httpStatus
=
await
c
onnection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
string
httpStatus
=
await
serverC
onnection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
Response
.
ParseResponseLine
(
httpStatus
,
out
var
responseVersion
,
out
int
responseStatusCode
,
...
...
@@ -204,7 +203,7 @@ namespace Titanium.Web.Proxy
response
.
StatusCode
=
responseStatusCode
;
response
.
StatusDescription
=
responseStatusDescription
;
await
HeaderParser
.
ReadHeaders
(
c
onnection
.
Stream
,
response
.
Headers
,
await
HeaderParser
.
ReadHeaders
(
serverC
onnection
.
Stream
,
response
.
Headers
,
cancellationToken
);
if
(!
args
.
IsTransparent
)
...
...
@@ -219,7 +218,7 @@ namespace Titanium.Web.Proxy
await
InvokeBeforeResponse
(
args
);
}
await
TcpHelper
.
SendRaw
(
clientStream
,
c
onnection
.
Stream
,
BufferSize
,
await
TcpHelper
.
SendRaw
(
clientStream
,
serverC
onnection
.
Stream
,
BufferSize
,
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
cancellationTokenSource
,
ExceptionFunc
);
...
...
@@ -228,7 +227,7 @@ namespace Titanium.Web.Proxy
}
//construct the web request that we are going to issue on behalf of the client.
await
HandleHttpSessionRequestInternal
(
c
onnection
,
args
);
await
HandleHttpSessionRequestInternal
(
serverC
onnection
,
args
);
if
(
args
.
WebSession
.
ServerConnection
==
null
)
{
...
...
@@ -265,17 +264,17 @@ namespace Titanium.Web.Proxy
}
finally
{
c
onnection
?.
Dispose
();
serverC
onnection
?.
Dispose
();
}
}
/// <summary>
/// Handle a specific session (request/response sequence)
/// </summary>
/// <param name="
c
onnection">The tcp connection.</param>
/// <param name="
serverC
onnection">The tcp connection.</param>
/// <param name="args">The session event arguments.</param>
/// <returns></returns>
private
async
Task
HandleHttpSessionRequestInternal
(
Tcp
Connection
c
onnection
,
SessionEventArgs
args
)
private
async
Task
HandleHttpSessionRequestInternal
(
Tcp
ServerConnection
serverC
onnection
,
SessionEventArgs
args
)
{
try
{
...
...
@@ -289,7 +288,7 @@ namespace Titanium.Web.Proxy
//and see if server would return 100 conitinue
if
(
request
.
ExpectContinue
)
{
args
.
WebSession
.
SetConnection
(
c
onnection
);
args
.
WebSession
.
SetConnection
(
serverC
onnection
);
await
args
.
WebSession
.
SendRequest
(
Enable100ContinueBehaviour
,
args
.
IsTransparent
,
cancellationToken
);
}
...
...
@@ -316,7 +315,7 @@ namespace Titanium.Web.Proxy
//If expect continue is not enabled then set the connectio and send request headers
if
(!
request
.
ExpectContinue
)
{
args
.
WebSession
.
SetConnection
(
c
onnection
);
args
.
WebSession
.
SetConnection
(
serverC
onnection
);
await
args
.
WebSession
.
SendRequest
(
Enable100ContinueBehaviour
,
args
.
IsTransparent
,
cancellationToken
);
}
...
...
@@ -361,7 +360,7 @@ namespace Titanium.Web.Proxy
/// <param name="isConnect">Is this a CONNECT request.</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
private
async
Task
<
TcpConnection
>
GetServerConnection
(
SessionEventArgsBase
args
,
bool
isConnect
,
private
async
Task
<
Tcp
Server
Connection
>
GetServerConnection
(
SessionEventArgsBase
args
,
bool
isConnect
,
CancellationToken
cancellationToken
)
{
ExternalProxy
customUpStreamProxy
=
null
;
...
...
Titanium.Web.Proxy/TransparentClientHandler.cs
View file @
687a8740
...
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.EventArguments;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network.Tcp
;
namespace
Titanium.Web.Proxy
{
...
...
@@ -21,14 +22,14 @@ namespace Titanium.Web.Proxy
/// So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client
/// </summary>
/// <param name="endPoint">The transparent endpoint.</param>
/// <param name="
tcpClient">The client
.</param>
/// <param name="
clientConnection">The client connection
.</param>
/// <returns></returns>
private
async
Task
HandleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
private
async
Task
HandleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClient
Connection
clientConnection
)
{
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
cancellationToken
=
cancellationTokenSource
.
Token
;
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
var
clientStream
=
new
CustomBufferedStream
(
clientConnection
.
GetStream
(),
BufferSize
);
var
clientStreamWriter
=
new
HttpResponseWriter
(
clientStream
,
BufferSize
);
...
...
@@ -123,7 +124,7 @@ namespace Titanium.Web.Proxy
//HTTPS server created - we can now decrypt the client's traffic
//Now create the request
await
HandleHttpSessionRequest
(
endPoint
,
tcpClient
,
clientStream
,
clientStreamWriter
,
await
HandleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
isHttps
?
httpsHostName
:
null
,
null
,
true
);
}
finally
...
...
Titanium.Web.Proxy/WinAuthHandler.cs
View file @
687a8740
...
...
@@ -172,7 +172,7 @@ namespace Titanium.Web.Proxy
// Add custom div to body to clarify that the proxy (not the client browser) failed authentication
string
authErrorMessage
=
"<div class=\"inserted-by-proxy\"><h2>NTLM authentication through Titanium.Web.Proxy ("
+
args
.
ProxyClient
.
TcpClient
.
Client
.
LocalEndPoint
+
args
.
ProxyClient
.
ClientConnection
.
LocalEndPoint
+
") failed. Please check credentials.</h2></div>"
;
string
originalErrorMessage
=
"<div class=\"inserted-by-proxy\"><h3>Response from remote web server below.</h3></div><br/>"
;
...
...
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