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
2bbf9bc7
Commit
2bbf9bc7
authored
Apr 14, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#410 implement task cancellation
parent
de8d70bc
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
77 additions
and
47 deletions
+77
-47
BeforeSslAuthenticateEventArgs.cs
...eb.Proxy/EventArguments/BeforeSslAuthenticateEventArgs.cs
+11
-1
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+5
-4
SessionEventArgsBase.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
+15
-8
TunnelConnectEventArgs.cs
Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
+3
-2
ExplicitClientHandler.cs
Titanium.Web.Proxy/ExplicitClientHandler.cs
+10
-4
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+16
-19
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+5
-4
TransparentClientHandler.cs
Titanium.Web.Proxy/TransparentClientHandler.cs
+12
-5
No files found.
Titanium.Web.Proxy/EventArguments/BeforeSslAuthenticateEventArgs.cs
View file @
2bbf9bc7
using
System
;
using
System
;
using
System.Threading
;
namespace
Titanium.Web.Proxy.EventArguments
namespace
Titanium.Web.Proxy.EventArguments
{
{
public
class
BeforeSslAuthenticateEventArgs
:
EventArgs
public
class
BeforeSslAuthenticateEventArgs
:
EventArgs
{
{
internal
CancellationTokenSource
TaskCancellationSource
;
internal
BeforeSslAuthenticateEventArgs
(
CancellationTokenSource
taskCancellationSource
)
{
this
.
TaskCancellationSource
=
taskCancellationSource
;
}
public
string
SniHostName
{
get
;
internal
set
;
}
public
string
SniHostName
{
get
;
internal
set
;
}
public
bool
DecryptSsl
{
get
;
set
;
}
=
true
;
public
bool
DecryptSsl
{
get
;
set
;
}
=
true
;
public
bool
TerminateSession
{
get
;
set
;
}
public
void
TerminateSession
()
{
TaskCancellationSource
.
Cancel
();
}
}
}
}
}
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
2bbf9bc7
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.Http;
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.Http;
using
Titanium.Web.Proxy.Http.Responses
;
using
Titanium.Web.Proxy.Http.Responses
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network
;
using
Titanium.Web.Proxy.Network
;
using
System.Threading
;
namespace
Titanium.Web.Proxy.EventArguments
namespace
Titanium.Web.Proxy.EventArguments
{
{
...
@@ -56,13 +57,13 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -56,13 +57,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// <summary>
/// Constructor to initialize the proxy
/// Constructor to initialize the proxy
/// </summary>
/// </summary>
internal
SessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
)
internal
SessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
,
CancellationTokenSource
cancellationTokenSource
)
:
this
(
bufferSize
,
endPoint
,
exceptionFunc
,
null
)
:
this
(
bufferSize
,
endPoint
,
exceptionFunc
,
null
,
cancellationTokenSource
)
{
{
}
}
protected
SessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
,
Request
request
)
protected
SessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
,
Request
request
,
CancellationTokenSource
cancellationTokenSource
)
:
base
(
bufferSize
,
endPoint
,
exceptionFunc
,
request
)
:
base
(
bufferSize
,
endPoint
,
exceptionFunc
,
request
,
cancellationTokenSource
)
{
{
}
}
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs
View file @
2bbf9bc7
using
System
;
using
System
;
using
System.Net
;
using
System.Net
;
using
System.Threading
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
...
@@ -22,6 +23,8 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -22,6 +23,8 @@ namespace Titanium.Web.Proxy.EventArguments
protected
readonly
ExceptionHandler
ExceptionFunc
;
protected
readonly
ExceptionHandler
ExceptionFunc
;
internal
CancellationTokenSource
cancellationTokenSource
;
/// <summary>
/// <summary>
/// Holds a reference to client
/// Holds a reference to client
/// </summary>
/// </summary>
...
@@ -60,11 +63,6 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -60,11 +63,6 @@ namespace Titanium.Web.Proxy.EventArguments
public
ProxyEndPoint
LocalEndPoint
{
get
;
}
public
ProxyEndPoint
LocalEndPoint
{
get
;
}
/// <summary>
/// Terminates the session abruptly by terminating client/server connections
/// </summary>
public
bool
TerminateSession
{
get
;
set
;
}
public
bool
IsTransparent
=>
LocalEndPoint
is
TransparentProxyEndPoint
;
public
bool
IsTransparent
=>
LocalEndPoint
is
TransparentProxyEndPoint
;
public
Exception
Exception
{
get
;
internal
set
;
}
public
Exception
Exception
{
get
;
internal
set
;
}
...
@@ -72,15 +70,17 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -72,15 +70,17 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// <summary>
/// Constructor to initialize the proxy
/// Constructor to initialize the proxy
/// </summary>
/// </summary>
internal
SessionEventArgsBase
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
)
internal
SessionEventArgsBase
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
,
CancellationTokenSource
cancellationTokenSource
)
:
this
(
bufferSize
,
endPoint
,
exceptionFunc
,
null
)
:
this
(
bufferSize
,
endPoint
,
exceptionFunc
,
null
,
cancellationTokenSource
)
{
{
}
}
protected
SessionEventArgsBase
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
,
Request
request
)
protected
SessionEventArgsBase
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ExceptionHandler
exceptionFunc
,
Request
request
,
CancellationTokenSource
cancellationTokenSource
)
{
{
this
.
BufferSize
=
bufferSize
;
this
.
BufferSize
=
bufferSize
;
this
.
ExceptionFunc
=
exceptionFunc
;
this
.
ExceptionFunc
=
exceptionFunc
;
this
.
cancellationTokenSource
=
cancellationTokenSource
;
ProxyClient
=
new
ProxyClient
();
ProxyClient
=
new
ProxyClient
();
WebSession
=
new
HttpWebClient
(
bufferSize
,
request
);
WebSession
=
new
HttpWebClient
(
bufferSize
,
request
);
...
@@ -130,6 +130,13 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -130,6 +130,13 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
}
}
/// <summary>
/// Terminates the session abruptly by terminating client/server connections
/// </summary>
public
void
TerminateSession
()
{
cancellationTokenSource
.
Cancel
();
}
/// <summary>
/// <summary>
/// implement any cleanup here
/// implement any cleanup here
/// </summary>
/// </summary>
...
...
Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
View file @
2bbf9bc7
using
System
;
using
System
;
using
System.Threading
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
...
@@ -15,8 +16,8 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -15,8 +16,8 @@ namespace Titanium.Web.Proxy.EventArguments
public
bool
IsHttpsConnect
{
get
;
internal
set
;
}
public
bool
IsHttpsConnect
{
get
;
internal
set
;
}
internal
TunnelConnectSessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ConnectRequest
connectRequest
,
ExceptionHandler
exceptionFunc
)
internal
TunnelConnectSessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ConnectRequest
connectRequest
,
ExceptionHandler
exceptionFunc
,
CancellationTokenSource
cancellationTokenSource
)
:
base
(
bufferSize
,
endPoint
,
exceptionFunc
,
connectRequest
)
:
base
(
bufferSize
,
endPoint
,
exceptionFunc
,
connectRequest
,
cancellationTokenSource
)
{
{
}
}
}
}
...
...
Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
2bbf9bc7
...
@@ -12,6 +12,7 @@ using Titanium.Web.Proxy.Exceptions;
...
@@ -12,6 +12,7 @@ using Titanium.Web.Proxy.Exceptions;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
System.Threading
;
namespace
Titanium.Web.Proxy
namespace
Titanium.Web.Proxy
{
{
...
@@ -26,6 +27,7 @@ namespace Titanium.Web.Proxy
...
@@ -26,6 +27,7 @@ namespace Titanium.Web.Proxy
/// <returns></returns>
/// <returns></returns>
private
async
Task
HandleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
private
async
Task
HandleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
{
{
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
var
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
BufferSize
);
var
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
BufferSize
);
...
@@ -60,7 +62,7 @@ namespace Titanium.Web.Proxy
...
@@ -60,7 +62,7 @@ namespace Titanium.Web.Proxy
await
HeaderParser
.
ReadHeaders
(
clientStreamReader
,
connectRequest
.
Headers
);
await
HeaderParser
.
ReadHeaders
(
clientStreamReader
,
connectRequest
.
Headers
);
var
connectArgs
=
new
TunnelConnectSessionEventArgs
(
BufferSize
,
endPoint
,
connectRequest
,
ExceptionFunc
);
var
connectArgs
=
new
TunnelConnectSessionEventArgs
(
BufferSize
,
endPoint
,
connectRequest
,
ExceptionFunc
,
cancellationTokenSource
);
connectArgs
.
ProxyClient
.
TcpClient
=
tcpClient
;
connectArgs
.
ProxyClient
.
TcpClient
=
tcpClient
;
connectArgs
.
ProxyClient
.
ClientStream
=
clientStream
;
connectArgs
.
ProxyClient
.
ClientStream
=
clientStream
;
...
@@ -152,7 +154,7 @@ namespace Titanium.Web.Proxy
...
@@ -152,7 +154,7 @@ namespace Titanium.Web.Proxy
}
}
}
}
if
(
connectArgs
.
TerminateSession
)
if
(
connectArgs
.
cancellationTokenSource
.
IsCancellationRequested
)
{
{
throw
new
Exception
(
"Session was terminated by user."
);
throw
new
Exception
(
"Session was terminated by user."
);
}
}
...
@@ -190,7 +192,7 @@ namespace Titanium.Web.Proxy
...
@@ -190,7 +192,7 @@ namespace Titanium.Web.Proxy
await
TcpHelper
.
SendRaw
(
clientStream
,
connection
.
Stream
,
BufferSize
,
await
TcpHelper
.
SendRaw
(
clientStream
,
connection
.
Stream
,
BufferSize
,
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
connectArgs
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
ExceptionFunc
);
ExceptionFunc
,
connectArgs
.
cancellationTokenSource
);
}
}
return
;
return
;
...
@@ -198,7 +200,7 @@ namespace Titanium.Web.Proxy
...
@@ -198,7 +200,7 @@ namespace Titanium.Web.Proxy
}
}
//Now create the request
//Now create the request
await
HandleHttpSessionRequest
(
tcpClient
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connectHostname
,
endPoint
,
connectRequest
);
await
HandleHttpSessionRequest
(
tcpClient
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
connectHostname
,
endPoint
,
connectRequest
,
cancellationTokenSource
);
}
}
catch
(
ProxyHttpException
e
)
catch
(
ProxyHttpException
e
)
{
{
...
@@ -220,6 +222,10 @@ namespace Titanium.Web.Proxy
...
@@ -220,6 +222,10 @@ namespace Titanium.Web.Proxy
{
{
clientStreamReader
.
Dispose
();
clientStreamReader
.
Dispose
();
clientStream
.
Dispose
();
clientStream
.
Dispose
();
if
(!
cancellationTokenSource
.
IsCancellationRequested
)
{
cancellationTokenSource
.
Cancel
();
}
}
}
}
}
...
...
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
2bbf9bc7
...
@@ -122,10 +122,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -122,10 +122,9 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="exceptionFunc"></param>
/// <param name="exceptionFunc"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
SendRawApm
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
internal
static
async
Task
SendRawApm
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
ExceptionHandler
exceptionFunc
)
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
ExceptionHandler
exceptionFunc
,
CancellationTokenSource
cts
)
{
{
var
tcs
=
new
TaskCompletionSource
<
bool
>();
var
tcs
=
new
TaskCompletionSource
<
bool
>();
var
cts
=
new
CancellationTokenSource
();
cts
.
Token
.
Register
(()
=>
tcs
.
TrySetResult
(
true
));
cts
.
Token
.
Register
(()
=>
tcs
.
TrySetResult
(
true
));
//Now async relay all server=>client & client=>server data
//Now async relay all server=>client & client=>server data
...
@@ -144,10 +143,10 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -144,10 +143,10 @@ namespace Titanium.Web.Proxy.Helpers
}
}
}
}
private
static
void
BeginRead
(
Stream
inputStream
,
Stream
outputStream
,
byte
[]
buffer
,
CancellationTokenSource
c
ts
,
Action
<
byte
[],
int
,
int
>
onCopy
,
private
static
void
BeginRead
(
Stream
inputStream
,
Stream
outputStream
,
byte
[]
buffer
,
CancellationTokenSource
c
ancellationTokenSource
,
Action
<
byte
[],
int
,
int
>
onCopy
,
ExceptionHandler
exceptionFunc
)
ExceptionHandler
exceptionFunc
)
{
{
if
(
c
ts
.
IsCancellationRequested
)
if
(
c
ancellationTokenSource
.
IsCancellationRequested
)
{
{
return
;
return
;
}
}
...
@@ -155,7 +154,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -155,7 +154,7 @@ namespace Titanium.Web.Proxy.Helpers
bool
readFlag
=
false
;
bool
readFlag
=
false
;
var
readCallback
=
(
AsyncCallback
)(
ar
=>
var
readCallback
=
(
AsyncCallback
)(
ar
=>
{
{
if
(
c
ts
.
IsCancellationRequested
||
readFlag
)
if
(
c
ancellationTokenSource
.
IsCancellationRequested
||
readFlag
)
{
{
return
;
return
;
}
}
...
@@ -167,7 +166,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -167,7 +166,7 @@ namespace Titanium.Web.Proxy.Helpers
int
read
=
inputStream
.
EndRead
(
ar
);
int
read
=
inputStream
.
EndRead
(
ar
);
if
(
read
<=
0
)
if
(
read
<=
0
)
{
{
c
ts
.
Cancel
();
c
ancellationTokenSource
.
Cancel
();
return
;
return
;
}
}
...
@@ -175,7 +174,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -175,7 +174,7 @@ namespace Titanium.Web.Proxy.Helpers
var
writeCallback
=
(
AsyncCallback
)(
ar2
=>
var
writeCallback
=
(
AsyncCallback
)(
ar2
=>
{
{
if
(
c
ts
.
IsCancellationRequested
)
if
(
c
ancellationTokenSource
.
IsCancellationRequested
)
{
{
return
;
return
;
}
}
...
@@ -183,11 +182,11 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -183,11 +182,11 @@ namespace Titanium.Web.Proxy.Helpers
try
try
{
{
outputStream
.
EndWrite
(
ar2
);
outputStream
.
EndWrite
(
ar2
);
BeginRead
(
inputStream
,
outputStream
,
buffer
,
c
ts
,
onCopy
,
exceptionFunc
);
BeginRead
(
inputStream
,
outputStream
,
buffer
,
c
ancellationTokenSource
,
onCopy
,
exceptionFunc
);
}
}
catch
(
IOException
ex
)
catch
(
IOException
ex
)
{
{
c
ts
.
Cancel
();
c
ancellationTokenSource
.
Cancel
();
exceptionFunc
(
ex
);
exceptionFunc
(
ex
);
}
}
});
});
...
@@ -196,7 +195,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -196,7 +195,7 @@ namespace Titanium.Web.Proxy.Helpers
}
}
catch
(
IOException
ex
)
catch
(
IOException
ex
)
{
{
c
ts
.
Cancel
();
c
ancellationTokenSource
.
Cancel
();
exceptionFunc
(
ex
);
exceptionFunc
(
ex
);
}
}
});
});
...
@@ -220,17 +219,15 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -220,17 +219,15 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="onDataReceive"></param>
/// <param name="onDataReceive"></param>
/// <param name="exceptionFunc"></param>
/// <param name="exceptionFunc"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
SendRawTap
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
private
static
async
Task
SendRawTap
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
ExceptionHandler
exceptionFunc
)
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
ExceptionHandler
exceptionFunc
,
CancellationTokenSource
cancellationTokenSource
)
{
{
var
cts
=
new
CancellationTokenSource
();
//Now async relay all server=>client & client=>server data
//Now async relay all server=>client & client=>server data
var
sendRelay
=
clientStream
.
CopyToAsync
(
serverStream
,
onDataSend
,
bufferSize
,
c
ts
.
Token
);
var
sendRelay
=
clientStream
.
CopyToAsync
(
serverStream
,
onDataSend
,
bufferSize
,
c
ancellationTokenSource
.
Token
);
var
receiveRelay
=
serverStream
.
CopyToAsync
(
clientStream
,
onDataReceive
,
bufferSize
,
c
ts
.
Token
);
var
receiveRelay
=
serverStream
.
CopyToAsync
(
clientStream
,
onDataReceive
,
bufferSize
,
c
ancellationTokenSource
.
Token
);
await
Task
.
WhenAny
(
sendRelay
,
receiveRelay
);
await
Task
.
WhenAny
(
sendRelay
,
receiveRelay
);
c
ts
.
Cancel
();
c
ancellationTokenSource
.
Cancel
();
await
Task
.
WhenAll
(
sendRelay
,
receiveRelay
);
await
Task
.
WhenAll
(
sendRelay
,
receiveRelay
);
}
}
...
@@ -247,10 +244,10 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -247,10 +244,10 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="exceptionFunc"></param>
/// <param name="exceptionFunc"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
Task
SendRaw
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
internal
static
Task
SendRaw
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
ExceptionHandler
exceptionFunc
)
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
ExceptionHandler
exceptionFunc
,
CancellationTokenSource
cancellationTokenSource
)
{
{
// todo: fix APM mode
// todo: fix APM mode
return
SendRawTap
(
clientStream
,
serverStream
,
bufferSize
,
onDataSend
,
onDataReceive
,
exceptionFunc
);
return
SendRawTap
(
clientStream
,
serverStream
,
bufferSize
,
onDataSend
,
onDataReceive
,
exceptionFunc
,
cancellationTokenSource
);
}
}
}
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/RequestHandler.cs
View file @
2bbf9bc7
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.Helpers;
...
@@ -11,6 +11,7 @@ using Titanium.Web.Proxy.Helpers;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network.Tcp
;
using
Titanium.Web.Proxy.Network.Tcp
;
using
System.Threading
;
namespace
Titanium.Web.Proxy
namespace
Titanium.Web.Proxy
{
{
...
@@ -39,7 +40,7 @@ namespace Titanium.Web.Proxy
...
@@ -39,7 +40,7 @@ namespace Titanium.Web.Proxy
/// <returns></returns>
/// <returns></returns>
private
async
Task
HandleHttpSessionRequest
(
TcpClient
client
,
CustomBufferedStream
clientStream
,
private
async
Task
HandleHttpSessionRequest
(
TcpClient
client
,
CustomBufferedStream
clientStream
,
CustomBinaryReader
clientStreamReader
,
HttpResponseWriter
clientStreamWriter
,
string
httpsConnectHostname
,
CustomBinaryReader
clientStreamReader
,
HttpResponseWriter
clientStreamWriter
,
string
httpsConnectHostname
,
ProxyEndPoint
endPoint
,
ConnectRequest
connectRequest
,
bool
isTransparentEndPoint
=
false
)
ProxyEndPoint
endPoint
,
ConnectRequest
connectRequest
,
CancellationTokenSource
cancellationTokenSource
,
bool
isTransparentEndPoint
=
false
)
{
{
TcpConnection
connection
=
null
;
TcpConnection
connection
=
null
;
...
@@ -56,7 +57,7 @@ namespace Titanium.Web.Proxy
...
@@ -56,7 +57,7 @@ namespace Titanium.Web.Proxy
return
;
return
;
}
}
var
args
=
new
SessionEventArgs
(
BufferSize
,
endPoint
,
ExceptionFunc
)
var
args
=
new
SessionEventArgs
(
BufferSize
,
endPoint
,
ExceptionFunc
,
cancellationTokenSource
)
{
{
ProxyClient
=
{
TcpClient
=
client
},
ProxyClient
=
{
TcpClient
=
client
},
WebSession
=
{
ConnectRequest
=
connectRequest
}
WebSession
=
{
ConnectRequest
=
connectRequest
}
...
@@ -204,7 +205,7 @@ namespace Titanium.Web.Proxy
...
@@ -204,7 +205,7 @@ namespace Titanium.Web.Proxy
await
TcpHelper
.
SendRaw
(
clientStream
,
connection
.
Stream
,
BufferSize
,
await
TcpHelper
.
SendRaw
(
clientStream
,
connection
.
Stream
,
BufferSize
,
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataSent
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
ExceptionFunc
);
ExceptionFunc
,
args
.
cancellationTokenSource
);
return
;
return
;
}
}
...
@@ -224,7 +225,7 @@ namespace Titanium.Web.Proxy
...
@@ -224,7 +225,7 @@ namespace Titanium.Web.Proxy
return
;
return
;
}
}
if
(
args
.
TerminateSession
)
if
(
args
.
cancellationTokenSource
.
IsCancellationRequested
)
{
{
throw
new
Exception
(
"Session was terminated by user."
);
throw
new
Exception
(
"Session was terminated by user."
);
}
}
...
...
Titanium.Web.Proxy/TransparentClientHandler.cs
View file @
2bbf9bc7
...
@@ -10,6 +10,7 @@ using Titanium.Web.Proxy.EventArguments;
...
@@ -10,6 +10,7 @@ using Titanium.Web.Proxy.EventArguments;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
System.Threading
;
namespace
Titanium.Web.Proxy
namespace
Titanium.Web.Proxy
{
{
...
@@ -25,11 +26,13 @@ namespace Titanium.Web.Proxy
...
@@ -25,11 +26,13 @@ namespace Titanium.Web.Proxy
/// <returns></returns>
/// <returns></returns>
private
async
Task
HandleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
private
async
Task
HandleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClient
tcpClient
)
{
{
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
var
clientStream
=
new
CustomBufferedStream
(
tcpClient
.
GetStream
(),
BufferSize
);
var
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
BufferSize
);
var
clientStreamReader
=
new
CustomBinaryReader
(
clientStream
,
BufferSize
);
var
clientStreamWriter
=
new
HttpResponseWriter
(
clientStream
,
BufferSize
);
var
clientStreamWriter
=
new
HttpResponseWriter
(
clientStream
,
BufferSize
);
try
try
{
{
var
clientHelloInfo
=
await
SslTools
.
PeekClientHello
(
clientStream
);
var
clientHelloInfo
=
await
SslTools
.
PeekClientHello
(
clientStream
);
...
@@ -41,11 +44,11 @@ namespace Titanium.Web.Proxy
...
@@ -41,11 +44,11 @@ namespace Titanium.Web.Proxy
{
{
httpsHostName
=
clientHelloInfo
.
GetServerName
()
??
endPoint
.
GenericCertificateName
;
httpsHostName
=
clientHelloInfo
.
GetServerName
()
??
endPoint
.
GenericCertificateName
;
var
args
=
new
BeforeSslAuthenticateEventArgs
();
var
args
=
new
BeforeSslAuthenticateEventArgs
(
cancellationTokenSource
);
args
.
SniHostName
=
httpsHostName
;
args
.
SniHostName
=
httpsHostName
;
await
endPoint
.
InvokeBeforeSslAuthenticate
(
this
,
args
,
ExceptionFunc
);
await
endPoint
.
InvokeBeforeSslAuthenticate
(
this
,
args
,
ExceptionFunc
);
if
(
args
.
T
erminateSession
)
if
(
args
.
T
askCancellationSource
.
IsCancellationRequested
)
{
{
throw
new
Exception
(
"Session was terminated by user."
);
throw
new
Exception
(
"Session was terminated by user."
);
}
}
...
@@ -112,7 +115,7 @@ namespace Titanium.Web.Proxy
...
@@ -112,7 +115,7 @@ namespace Titanium.Web.Proxy
//var serverHelloInfo = await SslTools.PeekServerHello(serverStream);
//var serverHelloInfo = await SslTools.PeekServerHello(serverStream);
await
TcpHelper
.
SendRaw
(
clientStream
,
serverStream
,
BufferSize
,
await
TcpHelper
.
SendRaw
(
clientStream
,
serverStream
,
BufferSize
,
null
,
null
,
ExceptionFunc
);
null
,
null
,
ExceptionFunc
,
args
.
TaskCancellationSource
);
}
}
}
}
}
}
...
@@ -120,12 +123,16 @@ namespace Titanium.Web.Proxy
...
@@ -120,12 +123,16 @@ namespace Titanium.Web.Proxy
//HTTPS server created - we can now decrypt the client's traffic
//HTTPS server created - we can now decrypt the client's traffic
//Now create the request
//Now create the request
await
HandleHttpSessionRequest
(
tcpClient
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
await
HandleHttpSessionRequest
(
tcpClient
,
clientStream
,
clientStreamReader
,
clientStreamWriter
,
isHttps
?
httpsHostName
:
null
,
endPoint
,
null
,
true
);
isHttps
?
httpsHostName
:
null
,
endPoint
,
null
,
cancellationTokenSource
,
true
);
}
}
finally
finally
{
{
clientStreamReader
.
Dispose
();
clientStreamReader
.
Dispose
();
clientStream
.
Dispose
();
clientStream
.
Dispose
();
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