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
e91a6122
Commit
e91a6122
authored
May 09, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
private methods => camelCase
parent
27b90f25
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
462 additions
and
398 deletions
+462
-398
LimitedStream.cs
Titanium.Web.Proxy/EventArguments/LimitedStream.cs
+2
-2
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+19
-19
ServerConnectionException.cs
Titanium.Web.Proxy/Exceptions/ServerConnectionException.cs
+24
-0
ExplicitClientHandler.cs
Titanium.Web.Proxy/ExplicitClientHandler.cs
+10
-10
FuncExtensions.cs
Titanium.Web.Proxy/Extensions/FuncExtensions.cs
+2
-2
StreamExtensions.cs
Titanium.Web.Proxy/Extensions/StreamExtensions.cs
+2
-2
HttpHelper.cs
Titanium.Web.Proxy/Helpers/HttpHelper.cs
+3
-3
HttpWriter.cs
Titanium.Web.Proxy/Helpers/HttpWriter.cs
+10
-10
ProxyInfo.cs
Titanium.Web.Proxy/Helpers/ProxyInfo.cs
+9
-9
SystemProxy.cs
Titanium.Web.Proxy/Helpers/SystemProxy.cs
+18
-18
TcpHelper.cs
Titanium.Web.Proxy/Helpers/TcpHelper.cs
+8
-8
WinHttpWebProxyFinder.cs
Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs
+23
-23
HeaderParser.cs
Titanium.Web.Proxy/Http/HeaderParser.cs
+1
-1
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+2
-1
Request.cs
Titanium.Web.Proxy/Http/Request.cs
+2
-2
BCCertificateMaker.cs
Titanium.Web.Proxy/Network/Certificate/BCCertificateMaker.cs
+10
-10
WinCertificateMaker.cs
...nium.Web.Proxy/Network/Certificate/WinCertificateMaker.cs
+5
-5
CertificateManager.cs
Titanium.Web.Proxy/Network/CertificateManager.cs
+32
-32
TcpClientConnection.cs
Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs
+0
-1
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+6
-6
ProxyAuthorizationHandler.cs
Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
+7
-7
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+18
-18
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+154
-106
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+86
-93
TransparentClientHandler.cs
Titanium.Web.Proxy/TransparentClientHandler.cs
+6
-7
WinAuthHandler.cs
Titanium.Web.Proxy/WinAuthHandler.cs
+3
-3
No files found.
Titanium.Web.Proxy/EventArguments/LimitedStream.cs
View file @
e91a6122
...
@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy.EventArguments
set
=>
throw
new
NotSupportedException
();
set
=>
throw
new
NotSupportedException
();
}
}
private
void
G
etNextChunk
()
private
void
g
etNextChunk
()
{
{
if
(
readChunkTrail
)
if
(
readChunkTrail
)
{
{
...
@@ -96,7 +96,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -96,7 +96,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
{
if
(
isChunked
)
if
(
isChunked
)
{
{
G
etNextChunk
();
g
etNextChunk
();
}
}
else
else
{
{
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
e91a6122
...
@@ -69,12 +69,12 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -69,12 +69,12 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
/// </summary>
public
event
EventHandler
<
MultipartRequestPartSentEventArgs
>
MultipartRequestPartSent
;
public
event
EventHandler
<
MultipartRequestPartSentEventArgs
>
MultipartRequestPartSent
;
private
ICustomStreamReader
G
etStreamReader
(
bool
isRequest
)
private
ICustomStreamReader
g
etStreamReader
(
bool
isRequest
)
{
{
return
isRequest
?
ProxyClient
.
ClientStream
:
WebSession
.
ServerConnection
.
Stream
;
return
isRequest
?
ProxyClient
.
ClientStream
:
WebSession
.
ServerConnection
.
Stream
;
}
}
private
HttpWriter
G
etStreamWriter
(
bool
isRequest
)
private
HttpWriter
g
etStreamWriter
(
bool
isRequest
)
{
{
return
isRequest
?
(
HttpWriter
)
ProxyClient
.
ClientStreamWriter
:
WebSession
.
ServerConnection
.
StreamWriter
;
return
isRequest
?
(
HttpWriter
)
ProxyClient
.
ClientStreamWriter
:
WebSession
.
ServerConnection
.
StreamWriter
;
}
}
...
@@ -82,7 +82,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -82,7 +82,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// <summary>
/// Read request body content as bytes[] for current session
/// Read request body content as bytes[] for current session
/// </summary>
/// </summary>
private
async
Task
R
eadRequestBodyAsync
(
CancellationToken
cancellationToken
)
private
async
Task
r
eadRequestBodyAsync
(
CancellationToken
cancellationToken
)
{
{
WebSession
.
Request
.
EnsureBodyAvailable
(
false
);
WebSession
.
Request
.
EnsureBodyAvailable
(
false
);
...
@@ -91,7 +91,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -91,7 +91,7 @@ namespace Titanium.Web.Proxy.EventArguments
// If not already read (not cached yet)
// If not already read (not cached yet)
if
(!
request
.
IsBodyRead
)
if
(!
request
.
IsBodyRead
)
{
{
var
body
=
await
R
eadBodyAsync
(
true
,
cancellationToken
);
var
body
=
await
r
eadBodyAsync
(
true
,
cancellationToken
);
request
.
Body
=
body
;
request
.
Body
=
body
;
// Now set the flag to true
// Now set the flag to true
...
@@ -126,7 +126,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -126,7 +126,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// <summary>
/// <summary>
/// Read response body as byte[] for current response
/// Read response body as byte[] for current response
/// </summary>
/// </summary>
private
async
Task
R
eadResponseBodyAsync
(
CancellationToken
cancellationToken
)
private
async
Task
r
eadResponseBodyAsync
(
CancellationToken
cancellationToken
)
{
{
if
(!
WebSession
.
Request
.
Locked
)
if
(!
WebSession
.
Request
.
Locked
)
{
{
...
@@ -142,7 +142,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -142,7 +142,7 @@ namespace Titanium.Web.Proxy.EventArguments
// If not already read (not cached yet)
// If not already read (not cached yet)
if
(!
response
.
IsBodyRead
)
if
(!
response
.
IsBodyRead
)
{
{
var
body
=
await
R
eadBodyAsync
(
false
,
cancellationToken
);
var
body
=
await
r
eadBodyAsync
(
false
,
cancellationToken
);
response
.
Body
=
body
;
response
.
Body
=
body
;
// Now set the flag to true
// Now set the flag to true
...
@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
}
}
private
async
Task
<
byte
[
]>
R
eadBodyAsync
(
bool
isRequest
,
CancellationToken
cancellationToken
)
private
async
Task
<
byte
[
]>
r
eadBodyAsync
(
bool
isRequest
,
CancellationToken
cancellationToken
)
{
{
using
(
var
bodyStream
=
new
MemoryStream
())
using
(
var
bodyStream
=
new
MemoryStream
())
{
{
...
@@ -182,7 +182,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -182,7 +182,7 @@ namespace Titanium.Web.Proxy.EventArguments
using
(
var
bodyStream
=
new
MemoryStream
())
using
(
var
bodyStream
=
new
MemoryStream
())
{
{
var
writer
=
new
HttpWriter
(
bodyStream
,
BufferSize
);
var
writer
=
new
HttpWriter
(
bodyStream
,
BufferSize
);
await
C
opyBodyAsync
(
isRequest
,
writer
,
TransformationMode
.
None
,
null
,
cancellationToken
);
await
c
opyBodyAsync
(
isRequest
,
writer
,
TransformationMode
.
None
,
null
,
cancellationToken
);
}
}
}
}
...
@@ -199,14 +199,14 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -199,14 +199,14 @@ namespace Titanium.Web.Proxy.EventArguments
// send the request body bytes to server
// send the request body bytes to server
if
(
contentLength
>
0
&&
hasMulipartEventSubscribers
&&
request
.
IsMultipartFormData
)
if
(
contentLength
>
0
&&
hasMulipartEventSubscribers
&&
request
.
IsMultipartFormData
)
{
{
var
reader
=
G
etStreamReader
(
true
);
var
reader
=
g
etStreamReader
(
true
);
string
boundary
=
HttpHelper
.
GetBoundaryFromContentType
(
request
.
ContentType
);
string
boundary
=
HttpHelper
.
GetBoundaryFromContentType
(
request
.
ContentType
);
using
(
var
copyStream
=
new
CopyStream
(
reader
,
writer
,
BufferSize
))
using
(
var
copyStream
=
new
CopyStream
(
reader
,
writer
,
BufferSize
))
{
{
while
(
contentLength
>
copyStream
.
ReadBytes
)
while
(
contentLength
>
copyStream
.
ReadBytes
)
{
{
long
read
=
await
R
eadUntilBoundaryAsync
(
copyStream
,
contentLength
,
boundary
,
cancellationToken
);
long
read
=
await
r
eadUntilBoundaryAsync
(
copyStream
,
contentLength
,
boundary
,
cancellationToken
);
if
(
read
==
0
)
if
(
read
==
0
)
{
{
break
;
break
;
...
@@ -225,18 +225,18 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -225,18 +225,18 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
else
else
{
{
await
C
opyBodyAsync
(
true
,
writer
,
transformation
,
OnDataSent
,
cancellationToken
);
await
c
opyBodyAsync
(
true
,
writer
,
transformation
,
OnDataSent
,
cancellationToken
);
}
}
}
}
internal
async
Task
CopyResponseBodyAsync
(
HttpWriter
writer
,
TransformationMode
transformation
,
CancellationToken
cancellationToken
)
internal
async
Task
CopyResponseBodyAsync
(
HttpWriter
writer
,
TransformationMode
transformation
,
CancellationToken
cancellationToken
)
{
{
await
C
opyBodyAsync
(
false
,
writer
,
transformation
,
OnDataReceived
,
cancellationToken
);
await
c
opyBodyAsync
(
false
,
writer
,
transformation
,
OnDataReceived
,
cancellationToken
);
}
}
private
async
Task
C
opyBodyAsync
(
bool
isRequest
,
HttpWriter
writer
,
TransformationMode
transformation
,
Action
<
byte
[],
int
,
int
>
onCopy
,
CancellationToken
cancellationToken
)
private
async
Task
c
opyBodyAsync
(
bool
isRequest
,
HttpWriter
writer
,
TransformationMode
transformation
,
Action
<
byte
[],
int
,
int
>
onCopy
,
CancellationToken
cancellationToken
)
{
{
var
stream
=
G
etStreamReader
(
isRequest
);
var
stream
=
g
etStreamReader
(
isRequest
);
var
requestResponse
=
isRequest
?
(
RequestResponseBase
)
WebSession
.
Request
:
WebSession
.
Response
;
var
requestResponse
=
isRequest
?
(
RequestResponseBase
)
WebSession
.
Request
:
WebSession
.
Response
;
...
@@ -280,7 +280,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -280,7 +280,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// Read a line from the byte stream
/// Read a line from the byte stream
/// </summary>
/// </summary>
/// <returns></returns>
/// <returns></returns>
private
async
Task
<
long
>
R
eadUntilBoundaryAsync
(
ICustomStreamReader
reader
,
long
totalBytesToRead
,
string
boundary
,
CancellationToken
cancellationToken
)
private
async
Task
<
long
>
r
eadUntilBoundaryAsync
(
ICustomStreamReader
reader
,
long
totalBytesToRead
,
string
boundary
,
CancellationToken
cancellationToken
)
{
{
int
bufferDataLength
=
0
;
int
bufferDataLength
=
0
;
...
@@ -347,7 +347,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -347,7 +347,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
{
if
(!
WebSession
.
Request
.
IsBodyRead
)
if
(!
WebSession
.
Request
.
IsBodyRead
)
{
{
await
R
eadRequestBodyAsync
(
cancellationToken
);
await
r
eadRequestBodyAsync
(
cancellationToken
);
}
}
return
WebSession
.
Request
.
Body
;
return
WebSession
.
Request
.
Body
;
...
@@ -362,7 +362,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -362,7 +362,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
{
if
(!
WebSession
.
Request
.
IsBodyRead
)
if
(!
WebSession
.
Request
.
IsBodyRead
)
{
{
await
R
eadRequestBodyAsync
(
cancellationToken
);
await
r
eadRequestBodyAsync
(
cancellationToken
);
}
}
return
WebSession
.
Request
.
BodyString
;
return
WebSession
.
Request
.
BodyString
;
...
@@ -407,7 +407,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -407,7 +407,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
{
if
(!
WebSession
.
Response
.
IsBodyRead
)
if
(!
WebSession
.
Response
.
IsBodyRead
)
{
{
await
R
eadResponseBodyAsync
(
cancellationToken
);
await
r
eadResponseBodyAsync
(
cancellationToken
);
}
}
return
WebSession
.
Response
.
Body
;
return
WebSession
.
Response
.
Body
;
...
@@ -422,7 +422,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -422,7 +422,7 @@ namespace Titanium.Web.Proxy.EventArguments
{
{
if
(!
WebSession
.
Response
.
IsBodyRead
)
if
(!
WebSession
.
Response
.
IsBodyRead
)
{
{
await
R
eadResponseBodyAsync
(
cancellationToken
);
await
r
eadResponseBodyAsync
(
cancellationToken
);
}
}
return
WebSession
.
Response
.
BodyString
;
return
WebSession
.
Response
.
BodyString
;
...
...
Titanium.Web.Proxy/Exceptions/ServerConnectionException.cs
0 → 100644
View file @
e91a6122
using
System
;
namespace
Titanium.Web.Proxy.Exceptions
{
/// <summary>
/// The server connection was closed.
/// </summary>
public
class
ServerConnectionException
:
ProxyException
{
internal
ServerConnectionException
(
string
message
)
:
base
(
message
)
{
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="message"></param>
/// <param name="e"></param>
internal
ServerConnectionException
(
string
message
,
Exception
e
)
:
base
(
message
,
e
)
{
}
}
}
Titanium.Web.Proxy/ExplicitClientHandler.cs
View file @
e91a6122
...
@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy
...
@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy
/// <param name="endPoint">The explicit endpoint.</param>
/// <param name="endPoint">The explicit endpoint.</param>
/// <param name="clientConnection">The client connection.</param>
/// <param name="clientConnection">The client connection.</param>
/// <returns>The task.</returns>
/// <returns>The task.</returns>
private
async
Task
H
andleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClientConnection
clientConnection
)
private
async
Task
h
andleClient
(
ExplicitProxyEndPoint
endPoint
,
TcpClientConnection
clientConnection
)
{
{
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
cancellationToken
=
cancellationTokenSource
.
Token
;
var
cancellationToken
=
cancellationTokenSource
.
Token
;
...
@@ -95,7 +95,7 @@ namespace Titanium.Web.Proxy
...
@@ -95,7 +95,7 @@ namespace Titanium.Web.Proxy
return
;
return
;
}
}
if
(
await
C
heckAuthorization
(
connectArgs
)
==
false
)
if
(
await
c
heckAuthorization
(
connectArgs
)
==
false
)
{
{
await
endPoint
.
InvokeBeforeTunnectConnectResponse
(
this
,
connectArgs
,
ExceptionFunc
);
await
endPoint
.
InvokeBeforeTunnectConnectResponse
(
this
,
connectArgs
,
ExceptionFunc
);
...
@@ -136,7 +136,7 @@ namespace Titanium.Web.Proxy
...
@@ -136,7 +136,7 @@ namespace Titanium.Web.Proxy
{
{
// test server HTTP/2 support
// test server HTTP/2 support
// todo: this is a hack, because Titanium does not support HTTP protocol changing currently
// todo: this is a hack, because Titanium does not support HTTP protocol changing currently
var
connection
=
await
G
etServerConnection
(
connectArgs
,
true
,
var
connection
=
await
g
etServerConnection
(
connectArgs
,
true
,
SslExtensions
.
Http2ProtocolAsList
,
cancellationToken
);
SslExtensions
.
Http2ProtocolAsList
,
cancellationToken
);
http2Supproted
=
connection
.
NegotiatedApplicationProtocol
==
SslApplicationProtocol
.
Http2
;
http2Supproted
=
connection
.
NegotiatedApplicationProtocol
==
SslApplicationProtocol
.
Http2
;
...
@@ -201,7 +201,7 @@ namespace Titanium.Web.Proxy
...
@@ -201,7 +201,7 @@ namespace Titanium.Web.Proxy
if
(!
decryptSsl
||
!
isClientHello
)
if
(!
decryptSsl
||
!
isClientHello
)
{
{
// create new connection
// create new connection
var
connection
=
await
G
etServerConnection
(
connectArgs
,
true
,
var
connection
=
await
g
etServerConnection
(
connectArgs
,
true
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
if
(
isClientHello
)
if
(
isClientHello
)
...
@@ -266,7 +266,7 @@ namespace Titanium.Web.Proxy
...
@@ -266,7 +266,7 @@ namespace Titanium.Web.Proxy
}
}
// create new connection
// create new connection
var
connection
=
await
G
etServerConnection
(
connectArgs
,
true
,
SslExtensions
.
Http2ProtocolAsList
,
var
connection
=
await
g
etServerConnection
(
connectArgs
,
true
,
SslExtensions
.
Http2ProtocolAsList
,
cancellationToken
);
cancellationToken
);
await
connection
.
StreamWriter
.
WriteLineAsync
(
"PRI * HTTP/2.0"
,
cancellationToken
);
await
connection
.
StreamWriter
.
WriteLineAsync
(
"PRI * HTTP/2.0"
,
cancellationToken
);
...
@@ -285,24 +285,24 @@ namespace Titanium.Web.Proxy
...
@@ -285,24 +285,24 @@ namespace Titanium.Web.Proxy
}
}
// Now create the request
// Now create the request
await
H
andleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
await
h
andleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
WebSession
.
ConnectRequest
);
cancellationTokenSource
,
connectHostname
,
connectArgs
?.
WebSession
.
ConnectRequest
);
}
}
catch
(
ProxyException
e
)
catch
(
ProxyException
e
)
{
{
O
nException
(
clientStream
,
e
);
o
nException
(
clientStream
,
e
);
}
}
catch
(
IOException
e
)
catch
(
IOException
e
)
{
{
O
nException
(
clientStream
,
new
Exception
(
"Connection was aborted"
,
e
));
o
nException
(
clientStream
,
new
Exception
(
"Connection was aborted"
,
e
));
}
}
catch
(
SocketException
e
)
catch
(
SocketException
e
)
{
{
O
nException
(
clientStream
,
new
Exception
(
"Could not connect"
,
e
));
o
nException
(
clientStream
,
new
Exception
(
"Could not connect"
,
e
));
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
O
nException
(
clientStream
,
new
Exception
(
"Error occured in whilst handling the client"
,
e
));
o
nException
(
clientStream
,
new
Exception
(
"Error occured in whilst handling the client"
,
e
));
}
}
finally
finally
{
{
...
...
Titanium.Web.Proxy/Extensions/FuncExtensions.cs
View file @
e91a6122
...
@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Extensions
foreach
(
var
@delegate
in
invocationList
)
foreach
(
var
@delegate
in
invocationList
)
{
{
await
I
nternalInvokeAsync
((
AsyncEventHandler
<
T
>)
@delegate
,
sender
,
args
,
exceptionFunc
);
await
i
nternalInvokeAsync
((
AsyncEventHandler
<
T
>)
@delegate
,
sender
,
args
,
exceptionFunc
);
}
}
}
}
private
static
async
Task
I
nternalInvokeAsync
<
T
>(
AsyncEventHandler
<
T
>
callback
,
object
sender
,
T
args
,
private
static
async
Task
i
nternalInvokeAsync
<
T
>(
AsyncEventHandler
<
T
>
callback
,
object
sender
,
T
args
,
ExceptionHandler
exceptionFunc
)
ExceptionHandler
exceptionFunc
)
{
{
try
try
...
...
Titanium.Web.Proxy/Extensions/StreamExtensions.cs
View file @
e91a6122
...
@@ -43,7 +43,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -43,7 +43,7 @@ namespace Titanium.Web.Proxy.Extensions
// cancellation is not working on Socket ReadAsync
// cancellation is not working on Socket ReadAsync
// https://github.com/dotnet/corefx/issues/15033
// https://github.com/dotnet/corefx/issues/15033
int
num
=
await
input
.
ReadAsync
(
buffer
,
0
,
buffer
.
Length
,
CancellationToken
.
None
)
int
num
=
await
input
.
ReadAsync
(
buffer
,
0
,
buffer
.
Length
,
CancellationToken
.
None
)
.
W
ithCancellation
(
cancellationToken
);
.
w
ithCancellation
(
cancellationToken
);
int
bytesRead
;
int
bytesRead
;
if
((
bytesRead
=
num
)
!=
0
&&
!
cancellationToken
.
IsCancellationRequested
)
if
((
bytesRead
=
num
)
!=
0
&&
!
cancellationToken
.
IsCancellationRequested
)
{
{
...
@@ -62,7 +62,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -62,7 +62,7 @@ namespace Titanium.Web.Proxy.Extensions
}
}
}
}
private
static
async
Task
<
T
>
W
ithCancellation
<
T
>(
this
Task
<
T
>
task
,
CancellationToken
cancellationToken
)
private
static
async
Task
<
T
>
w
ithCancellation
<
T
>(
this
Task
<
T
>
task
,
CancellationToken
cancellationToken
)
{
{
var
tcs
=
new
TaskCompletionSource
<
bool
>();
var
tcs
=
new
TaskCompletionSource
<
bool
>();
using
(
cancellationToken
.
Register
(
s
=>
((
TaskCompletionSource
<
bool
>)
s
).
TrySetResult
(
true
),
tcs
))
using
(
cancellationToken
.
Register
(
s
=>
((
TaskCompletionSource
<
bool
>)
s
).
TrySetResult
(
true
),
tcs
))
...
...
Titanium.Web.Proxy/Helpers/HttpHelper.cs
View file @
e91a6122
...
@@ -122,7 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -122,7 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns>1: when CONNECT, 0: when valid HTTP method, -1: otherwise</returns>
/// <returns>1: when CONNECT, 0: when valid HTTP method, -1: otherwise</returns>
internal
static
Task
<
int
>
IsConnectMethod
(
ICustomStreamReader
clientStreamReader
)
internal
static
Task
<
int
>
IsConnectMethod
(
ICustomStreamReader
clientStreamReader
)
{
{
return
S
tartsWith
(
clientStreamReader
,
"CONNECT"
);
return
s
tartsWith
(
clientStreamReader
,
"CONNECT"
);
}
}
/// <summary>
/// <summary>
...
@@ -132,7 +132,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -132,7 +132,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns>1: when PRI, 0: when valid HTTP method, -1: otherwise</returns>
/// <returns>1: when PRI, 0: when valid HTTP method, -1: otherwise</returns>
internal
static
Task
<
int
>
IsPriMethod
(
ICustomStreamReader
clientStreamReader
)
internal
static
Task
<
int
>
IsPriMethod
(
ICustomStreamReader
clientStreamReader
)
{
{
return
S
tartsWith
(
clientStreamReader
,
"PRI"
);
return
s
tartsWith
(
clientStreamReader
,
"PRI"
);
}
}
/// <summary>
/// <summary>
...
@@ -143,7 +143,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -143,7 +143,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns>
/// <returns>
/// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise
/// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise
/// </returns>
/// </returns>
private
static
async
Task
<
int
>
S
tartsWith
(
ICustomStreamReader
clientStreamReader
,
string
expectedStart
)
private
static
async
Task
<
int
>
s
tartsWith
(
ICustomStreamReader
clientStreamReader
,
string
expectedStart
)
{
{
bool
isExpected
=
true
;
bool
isExpected
=
true
;
int
legthToCheck
=
10
;
int
legthToCheck
=
10
;
...
...
Titanium.Web.Proxy/Helpers/HttpWriter.cs
View file @
e91a6122
...
@@ -44,10 +44,10 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -44,10 +44,10 @@ namespace Titanium.Web.Proxy.Helpers
internal
Task
WriteAsync
(
string
value
,
CancellationToken
cancellationToken
=
default
)
internal
Task
WriteAsync
(
string
value
,
CancellationToken
cancellationToken
=
default
)
{
{
return
W
riteAsyncInternal
(
value
,
false
,
cancellationToken
);
return
w
riteAsyncInternal
(
value
,
false
,
cancellationToken
);
}
}
private
Task
W
riteAsyncInternal
(
string
value
,
bool
addNewLine
,
CancellationToken
cancellationToken
)
private
Task
w
riteAsyncInternal
(
string
value
,
bool
addNewLine
,
CancellationToken
cancellationToken
)
{
{
int
newLineChars
=
addNewLine
?
newLine
.
Length
:
0
;
int
newLineChars
=
addNewLine
?
newLine
.
Length
:
0
;
int
charCount
=
value
.
Length
;
int
charCount
=
value
.
Length
;
...
@@ -91,7 +91,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -91,7 +91,7 @@ namespace Titanium.Web.Proxy.Helpers
internal
Task
WriteLineAsync
(
string
value
,
CancellationToken
cancellationToken
=
default
)
internal
Task
WriteLineAsync
(
string
value
,
CancellationToken
cancellationToken
=
default
)
{
{
return
W
riteAsyncInternal
(
value
,
true
,
cancellationToken
);
return
w
riteAsyncInternal
(
value
,
true
,
cancellationToken
);
}
}
/// <summary>
/// <summary>
...
@@ -149,7 +149,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -149,7 +149,7 @@ namespace Titanium.Web.Proxy.Helpers
{
{
if
(
isChunked
)
if
(
isChunked
)
{
{
return
W
riteBodyChunkedAsync
(
data
,
cancellationToken
);
return
w
riteBodyChunkedAsync
(
data
,
cancellationToken
);
}
}
return
WriteAsync
(
data
,
cancellationToken
:
cancellationToken
);
return
WriteAsync
(
data
,
cancellationToken
:
cancellationToken
);
...
@@ -171,7 +171,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -171,7 +171,7 @@ namespace Titanium.Web.Proxy.Helpers
// For chunked request we need to read data as they arrive, until we reach a chunk end symbol
// For chunked request we need to read data as they arrive, until we reach a chunk end symbol
if
(
isChunked
)
if
(
isChunked
)
{
{
return
C
opyBodyChunkedAsync
(
streamReader
,
onCopy
,
cancellationToken
);
return
c
opyBodyChunkedAsync
(
streamReader
,
onCopy
,
cancellationToken
);
}
}
// http 1.0 or the stream reader limits the stream
// http 1.0 or the stream reader limits the stream
...
@@ -181,7 +181,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -181,7 +181,7 @@ namespace Titanium.Web.Proxy.Helpers
}
}
// If not chunked then its easy just read the amount of bytes mentioned in content length header
// If not chunked then its easy just read the amount of bytes mentioned in content length header
return
C
opyBytesFromStream
(
streamReader
,
contentLength
,
onCopy
,
cancellationToken
);
return
c
opyBytesFromStream
(
streamReader
,
contentLength
,
onCopy
,
cancellationToken
);
}
}
/// <summary>
/// <summary>
...
@@ -190,7 +190,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -190,7 +190,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="data"></param>
/// <param name="data"></param>
/// <param name="cancellationToken"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
W
riteBodyChunkedAsync
(
byte
[]
data
,
CancellationToken
cancellationToken
)
private
async
Task
w
riteBodyChunkedAsync
(
byte
[]
data
,
CancellationToken
cancellationToken
)
{
{
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
data
.
Length
.
ToString
(
"x2"
));
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
data
.
Length
.
ToString
(
"x2"
));
...
@@ -210,7 +210,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -210,7 +210,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="onCopy"></param>
/// <param name="onCopy"></param>
/// <param name="cancellationToken"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
C
opyBodyChunkedAsync
(
ICustomStreamReader
reader
,
Action
<
byte
[],
int
,
int
>
onCopy
,
private
async
Task
c
opyBodyChunkedAsync
(
ICustomStreamReader
reader
,
Action
<
byte
[],
int
,
int
>
onCopy
,
CancellationToken
cancellationToken
)
CancellationToken
cancellationToken
)
{
{
while
(
true
)
while
(
true
)
...
@@ -228,7 +228,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -228,7 +228,7 @@ namespace Titanium.Web.Proxy.Helpers
if
(
chunkSize
!=
0
)
if
(
chunkSize
!=
0
)
{
{
await
C
opyBytesFromStream
(
reader
,
chunkSize
,
onCopy
,
cancellationToken
);
await
c
opyBytesFromStream
(
reader
,
chunkSize
,
onCopy
,
cancellationToken
);
}
}
await
WriteLineAsync
(
cancellationToken
);
await
WriteLineAsync
(
cancellationToken
);
...
@@ -251,7 +251,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -251,7 +251,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="onCopy"></param>
/// <param name="onCopy"></param>
/// <param name="cancellationToken"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
C
opyBytesFromStream
(
ICustomStreamReader
reader
,
long
count
,
Action
<
byte
[],
int
,
int
>
onCopy
,
private
async
Task
c
opyBytesFromStream
(
ICustomStreamReader
reader
,
long
count
,
Action
<
byte
[],
int
,
int
>
onCopy
,
CancellationToken
cancellationToken
)
CancellationToken
cancellationToken
)
{
{
var
buffer
=
BufferPool
.
GetBuffer
(
BufferSize
);
var
buffer
=
BufferPool
.
GetBuffer
(
BufferSize
);
...
...
Titanium.Web.Proxy/Helpers/ProxyInfo.cs
View file @
e91a6122
...
@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy.Helpers
}
}
else
else
{
{
overrides2
.
Add
(
B
ypassStringEscape
(
overrideHost
));
overrides2
.
Add
(
b
ypassStringEscape
(
overrideHost
));
}
}
}
}
...
@@ -70,7 +70,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -70,7 +70,7 @@ namespace Titanium.Web.Proxy.Helpers
internal
string
[]
BypassList
{
get
;
}
internal
string
[]
BypassList
{
get
;
}
private
static
string
B
ypassStringEscape
(
string
rawString
)
private
static
string
b
ypassStringEscape
(
string
rawString
)
{
{
var
match
=
var
match
=
new
Regex
(
"^(?<scheme>.*://)?(?<host>[^:]*)(?<port>:[0-9]{1,5})?$"
,
new
Regex
(
"^(?<scheme>.*://)?(?<host>[^:]*)(?<port>:[0-9]{1,5})?$"
,
...
@@ -91,9 +91,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -91,9 +91,9 @@ namespace Titanium.Web.Proxy.Helpers
empty2
=
string
.
Empty
;
empty2
=
string
.
Empty
;
}
}
string
str1
=
C
onvertRegexReservedChars
(
empty1
);
string
str1
=
c
onvertRegexReservedChars
(
empty1
);
string
str2
=
C
onvertRegexReservedChars
(
rawString1
);
string
str2
=
c
onvertRegexReservedChars
(
rawString1
);
string
str3
=
C
onvertRegexReservedChars
(
empty2
);
string
str3
=
c
onvertRegexReservedChars
(
empty2
);
if
(
str1
==
string
.
Empty
)
if
(
str1
==
string
.
Empty
)
{
{
str1
=
"(?:.*://)?"
;
str1
=
"(?:.*://)?"
;
...
@@ -107,7 +107,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -107,7 +107,7 @@ namespace Titanium.Web.Proxy.Helpers
return
"^"
+
str1
+
str2
+
str3
+
"$"
;
return
"^"
+
str1
+
str2
+
str3
+
"$"
;
}
}
private
static
string
C
onvertRegexReservedChars
(
string
rawString
)
private
static
string
c
onvertRegexReservedChars
(
string
rawString
)
{
{
if
(
rawString
.
Length
==
0
)
if
(
rawString
.
Length
==
0
)
{
{
...
@@ -171,11 +171,11 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -171,11 +171,11 @@ namespace Titanium.Web.Proxy.Helpers
if
(
proxyValues
.
Length
>
0
)
if
(
proxyValues
.
Length
>
0
)
{
{
result
.
AddRange
(
proxyValues
.
Select
(
P
arseProxyValue
).
Where
(
parsedValue
=>
parsedValue
!=
null
));
result
.
AddRange
(
proxyValues
.
Select
(
p
arseProxyValue
).
Where
(
parsedValue
=>
parsedValue
!=
null
));
}
}
else
else
{
{
var
parsedValue
=
P
arseProxyValue
(
proxyServerValues
);
var
parsedValue
=
p
arseProxyValue
(
proxyServerValues
);
if
(
parsedValue
!=
null
)
if
(
parsedValue
!=
null
)
{
{
result
.
Add
(
parsedValue
);
result
.
Add
(
parsedValue
);
...
@@ -190,7 +190,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -190,7 +190,7 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
/// </summary>
/// <param name="value"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <returns></returns>
private
static
HttpSystemProxyValue
P
arseProxyValue
(
string
value
)
private
static
HttpSystemProxyValue
p
arseProxyValue
(
string
value
)
{
{
string
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
();
string
tmp
=
Regex
.
Replace
(
value
,
@"\s+"
,
" "
).
Trim
();
...
...
Titanium.Web.Proxy/Helpers/SystemProxy.cs
View file @
e91a6122
...
@@ -84,8 +84,8 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -84,8 +84,8 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
S
aveOriginalProxyConfiguration
(
reg
);
s
aveOriginalProxyConfiguration
(
reg
);
P
repareRegistry
(
reg
);
p
repareRegistry
(
reg
);
string
exisitingContent
=
reg
.
GetValue
(
regProxyServer
)
as
string
;
string
exisitingContent
=
reg
.
GetValue
(
regProxyServer
)
as
string
;
var
existingSystemProxyValues
=
ProxyInfo
.
GetSystemProxyValues
(
exisitingContent
);
var
existingSystemProxyValues
=
ProxyInfo
.
GetSystemProxyValues
(
exisitingContent
);
...
@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy.Helpers
reg
.
SetValue
(
regProxyServer
,
reg
.
SetValue
(
regProxyServer
,
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
string
.
Join
(
";"
,
existingSystemProxyValues
.
Select
(
x
=>
x
.
ToString
()).
ToArray
()));
R
efresh
();
r
efresh
();
}
}
}
}
...
@@ -129,7 +129,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -129,7 +129,7 @@ namespace Titanium.Web.Proxy.Helpers
{
{
if
(
saveOriginalConfig
)
if
(
saveOriginalConfig
)
{
{
S
aveOriginalProxyConfiguration
(
reg
);
s
aveOriginalProxyConfiguration
(
reg
);
}
}
if
(
reg
.
GetValue
(
regProxyServer
)
!=
null
)
if
(
reg
.
GetValue
(
regProxyServer
)
!=
null
)
...
@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.Helpers
}
}
}
}
R
efresh
();
r
efresh
();
}
}
}
}
...
@@ -165,12 +165,12 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -165,12 +165,12 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
S
aveOriginalProxyConfiguration
(
reg
);
s
aveOriginalProxyConfiguration
(
reg
);
reg
.
SetValue
(
regProxyEnable
,
0
);
reg
.
SetValue
(
regProxyEnable
,
0
);
reg
.
SetValue
(
regProxyServer
,
string
.
Empty
);
reg
.
SetValue
(
regProxyServer
,
string
.
Empty
);
R
efresh
();
r
efresh
();
}
}
}
}
...
@@ -180,9 +180,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -180,9 +180,9 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
S
aveOriginalProxyConfiguration
(
reg
);
s
aveOriginalProxyConfiguration
(
reg
);
reg
.
SetValue
(
regAutoConfigUrl
,
url
);
reg
.
SetValue
(
regAutoConfigUrl
,
url
);
R
efresh
();
r
efresh
();
}
}
}
}
...
@@ -192,9 +192,9 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -192,9 +192,9 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
S
aveOriginalProxyConfiguration
(
reg
);
s
aveOriginalProxyConfiguration
(
reg
);
reg
.
SetValue
(
regProxyOverride
,
proxyOverride
);
reg
.
SetValue
(
regProxyOverride
,
proxyOverride
);
R
efresh
();
r
efresh
();
}
}
}
}
...
@@ -247,7 +247,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -247,7 +247,7 @@ namespace Titanium.Web.Proxy.Helpers
}
}
originalValues
=
null
;
originalValues
=
null
;
R
efresh
();
r
efresh
();
}
}
}
}
...
@@ -257,13 +257,13 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -257,13 +257,13 @@ namespace Titanium.Web.Proxy.Helpers
if
(
reg
!=
null
)
if
(
reg
!=
null
)
{
{
return
G
etProxyInfoFromRegistry
(
reg
);
return
g
etProxyInfoFromRegistry
(
reg
);
}
}
return
null
;
return
null
;
}
}
private
ProxyInfo
G
etProxyInfoFromRegistry
(
RegistryKey
reg
)
private
ProxyInfo
g
etProxyInfoFromRegistry
(
RegistryKey
reg
)
{
{
var
pi
=
new
ProxyInfo
(
null
,
reg
.
GetValue
(
regAutoConfigUrl
)
as
string
,
reg
.
GetValue
(
regProxyEnable
)
as
int
?,
var
pi
=
new
ProxyInfo
(
null
,
reg
.
GetValue
(
regAutoConfigUrl
)
as
string
,
reg
.
GetValue
(
regProxyEnable
)
as
int
?,
reg
.
GetValue
(
regProxyServer
)
as
string
,
reg
.
GetValue
(
regProxyServer
)
as
string
,
...
@@ -272,21 +272,21 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -272,21 +272,21 @@ namespace Titanium.Web.Proxy.Helpers
return
pi
;
return
pi
;
}
}
private
void
S
aveOriginalProxyConfiguration
(
RegistryKey
reg
)
private
void
s
aveOriginalProxyConfiguration
(
RegistryKey
reg
)
{
{
if
(
originalValues
!=
null
)
if
(
originalValues
!=
null
)
{
{
return
;
return
;
}
}
originalValues
=
G
etProxyInfoFromRegistry
(
reg
);
originalValues
=
g
etProxyInfoFromRegistry
(
reg
);
}
}
/// <summary>
/// <summary>
/// Prepares the proxy server registry (create empty values if they don't exist)
/// Prepares the proxy server registry (create empty values if they don't exist)
/// </summary>
/// </summary>
/// <param name="reg"></param>
/// <param name="reg"></param>
private
static
void
P
repareRegistry
(
RegistryKey
reg
)
private
static
void
p
repareRegistry
(
RegistryKey
reg
)
{
{
if
(
reg
.
GetValue
(
regProxyEnable
)
==
null
)
if
(
reg
.
GetValue
(
regProxyEnable
)
==
null
)
{
{
...
@@ -302,7 +302,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -302,7 +302,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <summary>
/// <summary>
/// Refresh the settings so that the system know about a change in proxy setting
/// Refresh the settings so that the system know about a change in proxy setting
/// </summary>
/// </summary>
private
void
R
efresh
()
private
static
void
r
efresh
()
{
{
NativeMethods
.
InternetSetOption
(
IntPtr
.
Zero
,
InternetOptionSettingsChanged
,
IntPtr
.
Zero
,
0
);
NativeMethods
.
InternetSetOption
(
IntPtr
.
Zero
,
InternetOptionSettingsChanged
,
IntPtr
.
Zero
,
0
);
NativeMethods
.
InternetSetOption
(
IntPtr
.
Zero
,
InternetOptionRefresh
,
IntPtr
.
Zero
,
0
);
NativeMethods
.
InternetSetOption
(
IntPtr
.
Zero
,
InternetOptionRefresh
,
IntPtr
.
Zero
,
0
);
...
...
Titanium.Web.Proxy/Helpers/TcpHelper.cs
View file @
e91a6122
...
@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -39,7 +39,7 @@ namespace Titanium.Web.Proxy.Helpers
0
)
==
0
)
0
)
==
0
)
{
{
int
rowCount
=
*(
int
*)
tcpTable
;
int
rowCount
=
*(
int
*)
tcpTable
;
uint
portInNetworkByteOrder
=
T
oNetworkByteOrder
((
uint
)
localPort
);
uint
portInNetworkByteOrder
=
t
oNetworkByteOrder
((
uint
)
localPort
);
if
(
ipVersion
==
IpVersion
.
Ipv4
)
if
(
ipVersion
==
IpVersion
.
Ipv4
)
{
{
...
@@ -90,7 +90,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -90,7 +90,7 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary>
/// </summary>
/// <param name="port"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <returns></returns>
private
static
uint
T
oNetworkByteOrder
(
uint
port
)
private
static
uint
t
oNetworkByteOrder
(
uint
port
)
{
{
return
((
port
>>
8
)
&
0x00FF00FFu
)
|
((
port
<<
8
)
&
0xFF00FF00u
);
return
((
port
>>
8
)
&
0x00FF00FFu
)
|
((
port
<<
8
)
&
0xFF00FF00u
);
}
}
...
@@ -122,8 +122,8 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -122,8 +122,8 @@ namespace Titanium.Web.Proxy.Helpers
var
serverBuffer
=
BufferPool
.
GetBuffer
(
bufferSize
);
var
serverBuffer
=
BufferPool
.
GetBuffer
(
bufferSize
);
try
try
{
{
B
eginRead
(
clientStream
,
serverStream
,
clientBuffer
,
onDataSend
,
cancellationTokenSource
,
exceptionFunc
);
b
eginRead
(
clientStream
,
serverStream
,
clientBuffer
,
onDataSend
,
cancellationTokenSource
,
exceptionFunc
);
B
eginRead
(
serverStream
,
clientStream
,
serverBuffer
,
onDataReceive
,
cancellationTokenSource
,
b
eginRead
(
serverStream
,
clientStream
,
serverBuffer
,
onDataReceive
,
cancellationTokenSource
,
exceptionFunc
);
exceptionFunc
);
await
taskCompletionSource
.
Task
;
await
taskCompletionSource
.
Task
;
}
}
...
@@ -134,7 +134,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -134,7 +134,7 @@ namespace Titanium.Web.Proxy.Helpers
}
}
}
}
private
static
void
B
eginRead
(
Stream
inputStream
,
Stream
outputStream
,
byte
[]
buffer
,
private
static
void
b
eginRead
(
Stream
inputStream
,
Stream
outputStream
,
byte
[]
buffer
,
Action
<
byte
[],
int
,
int
>
onCopy
,
CancellationTokenSource
cancellationTokenSource
,
Action
<
byte
[],
int
,
int
>
onCopy
,
CancellationTokenSource
cancellationTokenSource
,
ExceptionHandler
exceptionFunc
)
ExceptionHandler
exceptionFunc
)
{
{
...
@@ -174,7 +174,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -174,7 +174,7 @@ namespace Titanium.Web.Proxy.Helpers
try
try
{
{
outputStream
.
EndWrite
(
ar2
);
outputStream
.
EndWrite
(
ar2
);
B
eginRead
(
inputStream
,
outputStream
,
buffer
,
onCopy
,
cancellationTokenSource
,
b
eginRead
(
inputStream
,
outputStream
,
buffer
,
onCopy
,
cancellationTokenSource
,
exceptionFunc
);
exceptionFunc
);
}
}
catch
(
IOException
ex
)
catch
(
IOException
ex
)
...
@@ -214,7 +214,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -214,7 +214,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="cancellationTokenSource"></param>
/// <param name="cancellationTokenSource"></param>
/// <param name="exceptionFunc"></param>
/// <param name="exceptionFunc"></param>
/// <returns></returns>
/// <returns></returns>
private
static
async
Task
S
endRawTap
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
private
static
async
Task
s
endRawTap
(
Stream
clientStream
,
Stream
serverStream
,
int
bufferSize
,
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
Action
<
byte
[],
int
,
int
>
onDataSend
,
Action
<
byte
[],
int
,
int
>
onDataReceive
,
CancellationTokenSource
cancellationTokenSource
,
CancellationTokenSource
cancellationTokenSource
,
ExceptionHandler
exceptionFunc
)
ExceptionHandler
exceptionFunc
)
...
@@ -250,7 +250,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -250,7 +250,7 @@ namespace Titanium.Web.Proxy.Helpers
ExceptionHandler
exceptionFunc
)
ExceptionHandler
exceptionFunc
)
{
{
// todo: fix APM mode
// todo: fix APM mode
return
S
endRawTap
(
clientStream
,
serverStream
,
bufferSize
,
onDataSend
,
onDataReceive
,
return
s
endRawTap
(
clientStream
,
serverStream
,
bufferSize
,
onDataSend
,
onDataReceive
,
cancellationTokenSource
,
cancellationTokenSource
,
exceptionFunc
);
exceptionFunc
);
}
}
...
...
Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs
View file @
e91a6122
...
@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
session
=
NativeMethods
.
WinHttp
.
WinHttpOpen
(
null
,
NativeMethods
.
WinHttp
.
AccessType
.
NoProxy
,
null
,
null
,
0
);
session
=
NativeMethods
.
WinHttp
.
WinHttpOpen
(
null
,
NativeMethods
.
WinHttp
.
AccessType
.
NoProxy
,
null
,
null
,
0
);
if
(
session
==
null
||
session
.
IsInvalid
)
if
(
session
==
null
||
session
.
IsInvalid
)
{
{
int
lastWin32Error
=
G
etLastWin32Error
();
int
lastWin32Error
=
g
etLastWin32Error
();
}
}
else
else
{
{
...
@@ -30,7 +30,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -30,7 +30,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
return
;
return
;
}
}
int
lastWin32Error
=
G
etLastWin32Error
();
int
lastWin32Error
=
g
etLastWin32Error
();
}
}
}
}
...
@@ -50,7 +50,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -50,7 +50,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
public
void
Dispose
()
public
void
Dispose
()
{
{
D
ispose
(
true
);
d
ispose
(
true
);
}
}
public
bool
GetAutoProxies
(
Uri
destination
,
out
IList
<
string
>
proxyList
)
public
bool
GetAutoProxies
(
Uri
destination
,
out
IList
<
string
>
proxyList
)
...
@@ -65,8 +65,8 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -65,8 +65,8 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
var
errorCode
=
NativeMethods
.
WinHttp
.
ErrorCodes
.
AudodetectionFailed
;
var
errorCode
=
NativeMethods
.
WinHttp
.
ErrorCodes
.
AudodetectionFailed
;
if
(
AutomaticallyDetectSettings
&&
!
autoDetectFailed
)
if
(
AutomaticallyDetectSettings
&&
!
autoDetectFailed
)
{
{
errorCode
=
(
NativeMethods
.
WinHttp
.
ErrorCodes
)
G
etAutoProxies
(
destination
,
null
,
out
proxyListString
);
errorCode
=
(
NativeMethods
.
WinHttp
.
ErrorCodes
)
g
etAutoProxies
(
destination
,
null
,
out
proxyListString
);
autoDetectFailed
=
I
sErrorFatalForAutoDetect
(
errorCode
);
autoDetectFailed
=
i
sErrorFatalForAutoDetect
(
errorCode
);
if
(
errorCode
==
NativeMethods
.
WinHttp
.
ErrorCodes
.
UnrecognizedScheme
)
if
(
errorCode
==
NativeMethods
.
WinHttp
.
ErrorCodes
.
UnrecognizedScheme
)
{
{
state
=
AutoWebProxyState
.
UnrecognizedScheme
;
state
=
AutoWebProxyState
.
UnrecognizedScheme
;
...
@@ -74,13 +74,13 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -74,13 +74,13 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
}
}
}
}
if
(
AutomaticConfigurationScript
!=
null
&&
I
sRecoverableAutoProxyError
(
errorCode
))
if
(
AutomaticConfigurationScript
!=
null
&&
i
sRecoverableAutoProxyError
(
errorCode
))
{
{
errorCode
=
(
NativeMethods
.
WinHttp
.
ErrorCodes
)
G
etAutoProxies
(
destination
,
AutomaticConfigurationScript
,
errorCode
=
(
NativeMethods
.
WinHttp
.
ErrorCodes
)
g
etAutoProxies
(
destination
,
AutomaticConfigurationScript
,
out
proxyListString
);
out
proxyListString
);
}
}
state
=
G
etStateFromErrorCode
(
errorCode
);
state
=
g
etStateFromErrorCode
(
errorCode
);
if
(
state
!=
AutoWebProxyState
.
Completed
)
if
(
state
!=
AutoWebProxyState
.
Completed
)
{
{
return
false
;
return
false
;
...
@@ -88,7 +88,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -88,7 +88,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
if
(!
string
.
IsNullOrEmpty
(
proxyListString
))
if
(!
string
.
IsNullOrEmpty
(
proxyListString
))
{
{
proxyListString
=
R
emoveWhitespaces
(
proxyListString
);
proxyListString
=
r
emoveWhitespaces
(
proxyListString
);
proxyList
=
proxyListString
.
Split
(
';'
);
proxyList
=
proxyListString
.
Split
(
';'
);
}
}
...
@@ -149,7 +149,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -149,7 +149,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
public
void
LoadFromIE
()
public
void
LoadFromIE
()
{
{
var
pi
=
G
etProxyInfo
();
var
pi
=
g
etProxyInfo
();
ProxyInfo
=
pi
;
ProxyInfo
=
pi
;
AutomaticallyDetectSettings
=
pi
.
AutoDetect
==
true
;
AutomaticallyDetectSettings
=
pi
.
AutoDetect
==
true
;
AutomaticConfigurationScript
=
pi
.
AutoConfigUrl
==
null
?
null
:
new
Uri
(
pi
.
AutoConfigUrl
);
AutomaticConfigurationScript
=
pi
.
AutoConfigUrl
==
null
?
null
:
new
Uri
(
pi
.
AutoConfigUrl
);
...
@@ -158,7 +158,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -158,7 +158,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
proxy
=
new
WebProxy
(
new
Uri
(
"http://localhost"
),
BypassOnLocal
,
pi
.
BypassList
);
proxy
=
new
WebProxy
(
new
Uri
(
"http://localhost"
),
BypassOnLocal
,
pi
.
BypassList
);
}
}
private
ProxyInfo
G
etProxyInfo
()
private
ProxyInfo
g
etProxyInfo
()
{
{
var
proxyConfig
=
new
NativeMethods
.
WinHttp
.
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
();
var
proxyConfig
=
new
NativeMethods
.
WinHttp
.
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
();
RuntimeHelpers
.
PrepareConstrainedRegions
();
RuntimeHelpers
.
PrepareConstrainedRegions
();
...
@@ -200,7 +200,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -200,7 +200,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
autoDetectFailed
=
false
;
autoDetectFailed
=
false
;
}
}
private
void
D
ispose
(
bool
disposing
)
private
void
d
ispose
(
bool
disposing
)
{
{
if
(!
disposing
||
session
==
null
||
session
.
IsInvalid
)
if
(!
disposing
||
session
==
null
||
session
.
IsInvalid
)
{
{
...
@@ -210,7 +210,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -210,7 +210,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
session
.
Close
();
session
.
Close
();
}
}
private
int
G
etAutoProxies
(
Uri
destination
,
Uri
scriptLocation
,
out
string
proxyListString
)
private
int
g
etAutoProxies
(
Uri
destination
,
Uri
scriptLocation
,
out
string
proxyListString
)
{
{
int
num
=
0
;
int
num
=
0
;
var
autoProxyOptions
=
new
NativeMethods
.
WinHttp
.
WINHTTP_AUTOPROXY_OPTIONS
();
var
autoProxyOptions
=
new
NativeMethods
.
WinHttp
.
WINHTTP_AUTOPROXY_OPTIONS
();
...
@@ -229,16 +229,16 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -229,16 +229,16 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
autoProxyOptions
.
AutoDetectFlags
=
NativeMethods
.
WinHttp
.
AutoDetectType
.
None
;
autoProxyOptions
.
AutoDetectFlags
=
NativeMethods
.
WinHttp
.
AutoDetectType
.
None
;
}
}
if
(!
W
inHttpGetProxyForUrl
(
destination
.
ToString
(),
ref
autoProxyOptions
,
out
proxyListString
))
if
(!
w
inHttpGetProxyForUrl
(
destination
.
ToString
(),
ref
autoProxyOptions
,
out
proxyListString
))
{
{
num
=
G
etLastWin32Error
();
num
=
g
etLastWin32Error
();
if
(
num
==
(
int
)
NativeMethods
.
WinHttp
.
ErrorCodes
.
LoginFailure
&&
Credentials
!=
null
)
if
(
num
==
(
int
)
NativeMethods
.
WinHttp
.
ErrorCodes
.
LoginFailure
&&
Credentials
!=
null
)
{
{
autoProxyOptions
.
AutoLogonIfChallenged
=
true
;
autoProxyOptions
.
AutoLogonIfChallenged
=
true
;
if
(!
W
inHttpGetProxyForUrl
(
destination
.
ToString
(),
ref
autoProxyOptions
,
out
proxyListString
))
if
(!
w
inHttpGetProxyForUrl
(
destination
.
ToString
(),
ref
autoProxyOptions
,
out
proxyListString
))
{
{
num
=
G
etLastWin32Error
();
num
=
g
etLastWin32Error
();
}
}
}
}
}
}
...
@@ -246,7 +246,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -246,7 +246,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
return
num
;
return
num
;
}
}
private
bool
W
inHttpGetProxyForUrl
(
string
destination
,
private
bool
w
inHttpGetProxyForUrl
(
string
destination
,
ref
NativeMethods
.
WinHttp
.
WINHTTP_AUTOPROXY_OPTIONS
autoProxyOptions
,
out
string
proxyListString
)
ref
NativeMethods
.
WinHttp
.
WINHTTP_AUTOPROXY_OPTIONS
autoProxyOptions
,
out
string
proxyListString
)
{
{
proxyListString
=
null
;
proxyListString
=
null
;
...
@@ -271,7 +271,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -271,7 +271,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
return
flag
;
return
flag
;
}
}
private
static
int
G
etLastWin32Error
()
private
static
int
g
etLastWin32Error
()
{
{
int
lastWin32Error
=
Marshal
.
GetLastWin32Error
();
int
lastWin32Error
=
Marshal
.
GetLastWin32Error
();
if
(
lastWin32Error
==
8
)
if
(
lastWin32Error
==
8
)
...
@@ -282,7 +282,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -282,7 +282,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
return
lastWin32Error
;
return
lastWin32Error
;
}
}
private
static
bool
I
sRecoverableAutoProxyError
(
NativeMethods
.
WinHttp
.
ErrorCodes
errorCode
)
private
static
bool
i
sRecoverableAutoProxyError
(
NativeMethods
.
WinHttp
.
ErrorCodes
errorCode
)
{
{
switch
(
errorCode
)
switch
(
errorCode
)
{
{
...
@@ -300,7 +300,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -300,7 +300,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
}
}
}
}
private
static
AutoWebProxyState
G
etStateFromErrorCode
(
NativeMethods
.
WinHttp
.
ErrorCodes
errorCode
)
private
static
AutoWebProxyState
g
etStateFromErrorCode
(
NativeMethods
.
WinHttp
.
ErrorCodes
errorCode
)
{
{
if
(
errorCode
==
0L
)
if
(
errorCode
==
0L
)
{
{
...
@@ -324,7 +324,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -324,7 +324,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
}
}
}
}
private
static
string
R
emoveWhitespaces
(
string
value
)
private
static
string
r
emoveWhitespaces
(
string
value
)
{
{
var
stringBuilder
=
new
StringBuilder
();
var
stringBuilder
=
new
StringBuilder
();
foreach
(
char
c
in
value
)
foreach
(
char
c
in
value
)
...
@@ -338,7 +338,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
...
@@ -338,7 +338,7 @@ namespace Titanium.Web.Proxy.Helpers.WinHttp
return
stringBuilder
.
ToString
();
return
stringBuilder
.
ToString
();
}
}
private
static
bool
I
sErrorFatalForAutoDetect
(
NativeMethods
.
WinHttp
.
ErrorCodes
errorCode
)
private
static
bool
i
sErrorFatalForAutoDetect
(
NativeMethods
.
WinHttp
.
ErrorCodes
errorCode
)
{
{
switch
(
errorCode
)
switch
(
errorCode
)
{
{
...
...
Titanium.Web.Proxy/Http/HeaderParser.cs
View file @
e91a6122
...
@@ -24,7 +24,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -24,7 +24,7 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
/// </summary>
/// <param name="buffer"></param>
/// <param name="buffer"></param>
/// <param name="size"></param>
/// <param name="size"></param>
private
static
void
R
esizeBuffer
(
ref
byte
[]
buffer
,
long
size
)
private
static
void
r
esizeBuffer
(
ref
byte
[]
buffer
,
long
size
)
{
{
var
newBuffer
=
new
byte
[
size
];
var
newBuffer
=
new
byte
[
size
];
Buffer
.
BlockCopy
(
buffer
,
0
,
newBuffer
,
0
,
buffer
.
Length
);
Buffer
.
BlockCopy
(
buffer
,
0
,
newBuffer
,
0
,
buffer
.
Length
);
...
...
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
e91a6122
...
@@ -4,6 +4,7 @@ using System.Net;
...
@@ -4,6 +4,7 @@ using System.Net;
using
System.Text
;
using
System.Text
;
using
System.Threading
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Exceptions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Network.Tcp
;
using
Titanium.Web.Proxy.Network.Tcp
;
...
@@ -160,7 +161,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -160,7 +161,7 @@ namespace Titanium.Web.Proxy.Http
string
httpStatus
=
await
ServerConnection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
string
httpStatus
=
await
ServerConnection
.
Stream
.
ReadLineAsync
(
cancellationToken
);
if
(
httpStatus
==
null
)
if
(
httpStatus
==
null
)
{
{
throw
new
IOException
(
);
throw
new
ServerConnectionException
(
"Server connection was closed."
);
}
}
if
(
httpStatus
==
string
.
Empty
)
if
(
httpStatus
==
string
.
Empty
)
...
...
Titanium.Web.Proxy/Http/Request.cs
View file @
e91a6122
...
@@ -199,7 +199,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -199,7 +199,7 @@ namespace Titanium.Web.Proxy.Http
// Find the request Verb
// Find the request Verb
httpMethod
=
httpCmdSplit
[
0
];
httpMethod
=
httpCmdSplit
[
0
];
if
(!
I
sAllUpper
(
httpMethod
))
if
(!
i
sAllUpper
(
httpMethod
))
{
{
httpMethod
=
httpMethod
.
ToUpper
();
httpMethod
=
httpMethod
.
ToUpper
();
}
}
...
@@ -219,7 +219,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -219,7 +219,7 @@ namespace Titanium.Web.Proxy.Http
}
}
}
}
private
static
bool
I
sAllUpper
(
string
input
)
private
static
bool
i
sAllUpper
(
string
input
)
{
{
for
(
int
i
=
0
;
i
<
input
.
Length
;
i
++)
for
(
int
i
=
0
;
i
<
input
.
Length
;
i
++)
{
{
...
...
Titanium.Web.Proxy/Network/Certificate/BCCertificateMaker.cs
View file @
e91a6122
...
@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -49,7 +49,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// <returns>X509Certificate2 instance.</returns>
/// <returns>X509Certificate2 instance.</returns>
public
X509Certificate2
MakeCertificate
(
string
sSubjectCn
,
bool
isRoot
,
X509Certificate2
signingCert
=
null
)
public
X509Certificate2
MakeCertificate
(
string
sSubjectCn
,
bool
isRoot
,
X509Certificate2
signingCert
=
null
)
{
{
return
M
akeCertificateInternal
(
sSubjectCn
,
isRoot
,
true
,
signingCert
);
return
m
akeCertificateInternal
(
sSubjectCn
,
isRoot
,
true
,
signingCert
);
}
}
/// <summary>
/// <summary>
...
@@ -65,7 +65,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -65,7 +65,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// <param name="hostName">The host name</param>
/// <param name="hostName">The host name</param>
/// <returns>X509Certificate2 instance.</returns>
/// <returns>X509Certificate2 instance.</returns>
/// <exception cref="PemException">Malformed sequence in RSA private key</exception>
/// <exception cref="PemException">Malformed sequence in RSA private key</exception>
private
static
X509Certificate2
G
enerateCertificate
(
string
hostName
,
private
static
X509Certificate2
g
enerateCertificate
(
string
hostName
,
string
subjectName
,
string
subjectName
,
string
issuerName
,
DateTime
validFrom
,
string
issuerName
,
DateTime
validFrom
,
DateTime
validTo
,
int
keyStrength
=
2048
,
DateTime
validTo
,
int
keyStrength
=
2048
,
...
@@ -145,7 +145,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -145,7 +145,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
var
x509Certificate
=
new
X509Certificate2
(
certificate
.
GetEncoded
());
var
x509Certificate
=
new
X509Certificate2
(
certificate
.
GetEncoded
());
x509Certificate
.
PrivateKey
=
DotNetUtilities
.
ToRSA
(
rsaparams
);
x509Certificate
.
PrivateKey
=
DotNetUtilities
.
ToRSA
(
rsaparams
);
#else
#else
var
x509Certificate
=
W
ithPrivateKey
(
certificate
,
rsaparams
);
var
x509Certificate
=
w
ithPrivateKey
(
certificate
,
rsaparams
);
x509Certificate
.
FriendlyName
=
subjectName
;
x509Certificate
.
FriendlyName
=
subjectName
;
#endif
#endif
...
@@ -164,7 +164,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -164,7 +164,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
return
x509Certificate
;
return
x509Certificate
;
}
}
private
static
X509Certificate2
W
ithPrivateKey
(
X509Certificate
certificate
,
AsymmetricKeyParameter
privateKey
)
private
static
X509Certificate2
w
ithPrivateKey
(
X509Certificate
certificate
,
AsymmetricKeyParameter
privateKey
)
{
{
const
string
password
=
"password"
;
const
string
password
=
"password"
;
var
store
=
new
Pkcs12Store
();
var
store
=
new
Pkcs12Store
();
...
@@ -194,7 +194,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -194,7 +194,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// You must specify a Signing Certificate if and only if you are not creating a
/// You must specify a Signing Certificate if and only if you are not creating a
/// root.
/// root.
/// </exception>
/// </exception>
private
X509Certificate2
M
akeCertificateInternal
(
bool
isRoot
,
private
X509Certificate2
m
akeCertificateInternal
(
bool
isRoot
,
string
hostName
,
string
subjectName
,
string
hostName
,
string
subjectName
,
DateTime
validFrom
,
DateTime
validTo
,
X509Certificate2
signingCertificate
)
DateTime
validFrom
,
DateTime
validTo
,
X509Certificate2
signingCertificate
)
{
{
...
@@ -207,11 +207,11 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -207,11 +207,11 @@ namespace Titanium.Web.Proxy.Network.Certificate
if
(
isRoot
)
if
(
isRoot
)
{
{
return
G
enerateCertificate
(
null
,
subjectName
,
subjectName
,
validFrom
,
validTo
);
return
g
enerateCertificate
(
null
,
subjectName
,
subjectName
,
validFrom
,
validTo
);
}
}
var
kp
=
DotNetUtilities
.
GetKeyPair
(
signingCertificate
.
PrivateKey
);
var
kp
=
DotNetUtilities
.
GetKeyPair
(
signingCertificate
.
PrivateKey
);
return
G
enerateCertificate
(
hostName
,
subjectName
,
signingCertificate
.
Subject
,
validFrom
,
validTo
,
return
g
enerateCertificate
(
hostName
,
subjectName
,
signingCertificate
.
Subject
,
validFrom
,
validTo
,
issuerPrivateKey
:
kp
.
Private
);
issuerPrivateKey
:
kp
.
Private
);
}
}
...
@@ -224,7 +224,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -224,7 +224,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// <param name="signingCert">The signing cert.</param>
/// <param name="signingCert">The signing cert.</param>
/// <param name="cancellationToken">Task cancellation token</param>
/// <param name="cancellationToken">Task cancellation token</param>
/// <returns>X509Certificate2.</returns>
/// <returns>X509Certificate2.</returns>
private
X509Certificate2
M
akeCertificateInternal
(
string
subject
,
bool
isRoot
,
private
X509Certificate2
m
akeCertificateInternal
(
string
subject
,
bool
isRoot
,
bool
switchToMtaIfNeeded
,
X509Certificate2
signingCert
=
null
,
bool
switchToMtaIfNeeded
,
X509Certificate2
signingCert
=
null
,
CancellationToken
cancellationToken
=
default
)
CancellationToken
cancellationToken
=
default
)
{
{
...
@@ -238,7 +238,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -238,7 +238,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
{
{
try
try
{
{
certificate
=
M
akeCertificateInternal
(
subject
,
isRoot
,
false
,
signingCert
);
certificate
=
m
akeCertificateInternal
(
subject
,
isRoot
,
false
,
signingCert
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
...
@@ -258,7 +258,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -258,7 +258,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
}
}
#endif
#endif
return
M
akeCertificateInternal
(
isRoot
,
subject
,
$"CN=
{
subject
}
"
,
return
m
akeCertificateInternal
(
isRoot
,
subject
,
$"CN=
{
subject
}
"
,
DateTime
.
UtcNow
.
AddDays
(-
certificateGraceDays
),
DateTime
.
UtcNow
.
AddDays
(
certificateValidDays
),
DateTime
.
UtcNow
.
AddDays
(-
certificateGraceDays
),
DateTime
.
UtcNow
.
AddDays
(
certificateValidDays
),
isRoot
?
null
:
signingCert
);
isRoot
?
null
:
signingCert
);
}
}
...
...
Titanium.Web.Proxy/Network/Certificate/WinCertificateMaker.cs
View file @
e91a6122
...
@@ -80,10 +80,10 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -80,10 +80,10 @@ namespace Titanium.Web.Proxy.Network.Certificate
/// <returns></returns>
/// <returns></returns>
public
X509Certificate2
MakeCertificate
(
string
sSubjectCN
,
bool
isRoot
,
X509Certificate2
signingCert
=
null
)
public
X509Certificate2
MakeCertificate
(
string
sSubjectCN
,
bool
isRoot
,
X509Certificate2
signingCert
=
null
)
{
{
return
M
akeCertificateInternal
(
sSubjectCN
,
isRoot
,
true
,
signingCert
);
return
m
akeCertificateInternal
(
sSubjectCN
,
isRoot
,
true
,
signingCert
);
}
}
private
X509Certificate2
M
akeCertificate
(
bool
isRoot
,
string
subject
,
string
fullSubject
,
private
X509Certificate2
m
akeCertificate
(
bool
isRoot
,
string
subject
,
string
fullSubject
,
int
privateKeyLength
,
string
hashAlg
,
DateTime
validFrom
,
DateTime
validTo
,
int
privateKeyLength
,
string
hashAlg
,
DateTime
validFrom
,
DateTime
validTo
,
X509Certificate2
signingCertificate
)
X509Certificate2
signingCertificate
)
{
{
...
@@ -274,13 +274,13 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -274,13 +274,13 @@ namespace Titanium.Web.Proxy.Network.Certificate
return
new
X509Certificate2
(
Convert
.
FromBase64String
(
empty
),
string
.
Empty
,
X509KeyStorageFlags
.
Exportable
);
return
new
X509Certificate2
(
Convert
.
FromBase64String
(
empty
),
string
.
Empty
,
X509KeyStorageFlags
.
Exportable
);
}
}
private
X509Certificate2
M
akeCertificateInternal
(
string
sSubjectCN
,
bool
isRoot
,
private
X509Certificate2
m
akeCertificateInternal
(
string
sSubjectCN
,
bool
isRoot
,
bool
switchToMTAIfNeeded
,
X509Certificate2
signingCert
=
null
,
bool
switchToMTAIfNeeded
,
X509Certificate2
signingCert
=
null
,
CancellationToken
cancellationToken
=
default
)
CancellationToken
cancellationToken
=
default
)
{
{
if
(
switchToMTAIfNeeded
&&
Thread
.
CurrentThread
.
GetApartmentState
()
!=
ApartmentState
.
MTA
)
if
(
switchToMTAIfNeeded
&&
Thread
.
CurrentThread
.
GetApartmentState
()
!=
ApartmentState
.
MTA
)
{
{
return
Task
.
Run
(()
=>
M
akeCertificateInternal
(
sSubjectCN
,
isRoot
,
false
,
signingCert
),
return
Task
.
Run
(()
=>
m
akeCertificateInternal
(
sSubjectCN
,
isRoot
,
false
,
signingCert
),
cancellationToken
).
Result
;
cancellationToken
).
Result
;
}
}
...
@@ -301,7 +301,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
...
@@ -301,7 +301,7 @@ namespace Titanium.Web.Proxy.Network.Certificate
var
graceTime
=
DateTime
.
Now
.
AddDays
(
graceDays
);
var
graceTime
=
DateTime
.
Now
.
AddDays
(
graceDays
);
var
now
=
DateTime
.
Now
;
var
now
=
DateTime
.
Now
;
var
certificate
=
M
akeCertificate
(
isRoot
,
sSubjectCN
,
fullSubject
,
keyLength
,
hashAlgo
,
graceTime
,
var
certificate
=
m
akeCertificate
(
isRoot
,
sSubjectCN
,
fullSubject
,
keyLength
,
hashAlgo
,
graceTime
,
now
.
AddDays
(
validDays
),
isRoot
?
null
:
signingCert
);
now
.
AddDays
(
validDays
),
isRoot
?
null
:
signingCert
);
return
certificate
;
return
certificate
;
}
}
...
...
Titanium.Web.Proxy/Network/CertificateManager.cs
View file @
e91a6122
This diff is collapsed.
Click to expand it.
Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs
View file @
e91a6122
using
System
;
using
System
;
using
System.IO
;
using
System.IO
;
using
System.Net
;
using
System.Net
;
using
System.Net.Security
;
using
System.Net.Sockets
;
using
System.Net.Sockets
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
...
...
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
e91a6122
...
@@ -40,7 +40,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -40,7 +40,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal
TcpConnectionFactory
(
ProxyServer
server
)
internal
TcpConnectionFactory
(
ProxyServer
server
)
{
{
this
.
server
=
server
;
this
.
server
=
server
;
Task
.
Run
(
async
()
=>
await
C
learOutdatedConnections
());
Task
.
Run
(
async
()
=>
await
c
learOutdatedConnections
());
}
}
/// <summary>
/// <summary>
...
@@ -88,7 +88,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -88,7 +88,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
var
cutOff
=
DateTime
.
Now
.
AddSeconds
(-
1
*
proxyServer
.
ConnectionTimeOutSeconds
+
3
);
var
cutOff
=
DateTime
.
Now
.
AddSeconds
(-
1
*
proxyServer
.
ConnectionTimeOutSeconds
+
3
);
if
(
recentConnection
.
LastAccess
>
cutOff
if
(
recentConnection
.
LastAccess
>
cutOff
&&
I
sGoodConnection
(
recentConnection
.
TcpClient
))
&&
i
sGoodConnection
(
recentConnection
.
TcpClient
))
{
{
return
recentConnection
;
return
recentConnection
;
}
}
...
@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
}
}
}
var
connection
=
await
C
reateClient
(
remoteHostName
,
remotePort
,
httpVersion
,
isHttps
,
var
connection
=
await
c
reateClient
(
remoteHostName
,
remotePort
,
httpVersion
,
isHttps
,
applicationProtocols
,
isConnect
,
proxyServer
,
upStreamEndPoint
,
externalProxy
,
cancellationToken
);
applicationProtocols
,
isConnect
,
proxyServer
,
upStreamEndPoint
,
externalProxy
,
cancellationToken
);
connection
.
CacheKey
=
cacheKey
;
connection
.
CacheKey
=
cacheKey
;
...
@@ -121,7 +121,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -121,7 +121,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <param name="externalProxy">The external proxy to make request via.</param>
/// <param name="externalProxy">The external proxy to make request via.</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
<
TcpServerConnection
>
C
reateClient
(
string
remoteHostName
,
int
remotePort
,
private
async
Task
<
TcpServerConnection
>
c
reateClient
(
string
remoteHostName
,
int
remotePort
,
Version
httpVersion
,
bool
isHttps
,
List
<
SslApplicationProtocol
>
applicationProtocols
,
bool
isConnect
,
Version
httpVersion
,
bool
isHttps
,
List
<
SslApplicationProtocol
>
applicationProtocols
,
bool
isConnect
,
ProxyServer
proxyServer
,
IPEndPoint
upStreamEndPoint
,
ExternalProxy
externalProxy
,
ProxyServer
proxyServer
,
IPEndPoint
upStreamEndPoint
,
ExternalProxy
externalProxy
,
CancellationToken
cancellationToken
)
CancellationToken
cancellationToken
)
...
@@ -280,7 +280,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -280,7 +280,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
@lock
.
Release
();
@lock
.
Release
();
}
}
private
async
Task
C
learOutdatedConnections
()
private
async
Task
c
learOutdatedConnections
()
{
{
while
(
runCleanUpTask
)
while
(
runCleanUpTask
)
{
{
...
@@ -326,7 +326,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -326,7 +326,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// </summary>
/// </summary>
/// <param name="client"></param>
/// <param name="client"></param>
/// <returns></returns>
/// <returns></returns>
private
static
bool
I
sGoodConnection
(
TcpClient
client
)
private
static
bool
i
sGoodConnection
(
TcpClient
client
)
{
{
var
socket
=
client
.
Client
;
var
socket
=
client
.
Client
;
...
...
Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
View file @
e91a6122
...
@@ -18,7 +18,7 @@ namespace Titanium.Web.Proxy
...
@@ -18,7 +18,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="session">The session event arguments.</param>
/// <param name="session">The session event arguments.</param>
/// <returns>True if authorized.</returns>
/// <returns>True if authorized.</returns>
private
async
Task
<
bool
>
C
heckAuthorization
(
SessionEventArgsBase
session
)
private
async
Task
<
bool
>
c
heckAuthorization
(
SessionEventArgsBase
session
)
{
{
// If we are not authorizing clients return true
// If we are not authorizing clients return true
if
(
AuthenticateUserFunc
==
null
)
if
(
AuthenticateUserFunc
==
null
)
...
@@ -33,7 +33,7 @@ namespace Titanium.Web.Proxy
...
@@ -33,7 +33,7 @@ namespace Titanium.Web.Proxy
var
header
=
httpHeaders
.
GetFirstHeader
(
KnownHeaders
.
ProxyAuthorization
);
var
header
=
httpHeaders
.
GetFirstHeader
(
KnownHeaders
.
ProxyAuthorization
);
if
(
header
==
null
)
if
(
header
==
null
)
{
{
session
.
WebSession
.
Response
=
C
reateAuthentication407Response
(
"Proxy Authentication Required"
);
session
.
WebSession
.
Response
=
c
reateAuthentication407Response
(
"Proxy Authentication Required"
);
return
false
;
return
false
;
}
}
...
@@ -42,7 +42,7 @@ namespace Titanium.Web.Proxy
...
@@ -42,7 +42,7 @@ namespace Titanium.Web.Proxy
!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
))
!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
))
{
{
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
C
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
WebSession
.
Response
=
c
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
...
@@ -51,7 +51,7 @@ namespace Titanium.Web.Proxy
...
@@ -51,7 +51,7 @@ namespace Titanium.Web.Proxy
if
(
colonIndex
==
-
1
)
if
(
colonIndex
==
-
1
)
{
{
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
C
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
WebSession
.
Response
=
c
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
...
@@ -60,7 +60,7 @@ namespace Titanium.Web.Proxy
...
@@ -60,7 +60,7 @@ namespace Titanium.Web.Proxy
bool
authenticated
=
await
AuthenticateUserFunc
(
username
,
password
);
bool
authenticated
=
await
AuthenticateUserFunc
(
username
,
password
);
if
(!
authenticated
)
if
(!
authenticated
)
{
{
session
.
WebSession
.
Response
=
C
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
WebSession
.
Response
=
c
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
}
}
return
authenticated
;
return
authenticated
;
...
@@ -71,7 +71,7 @@ namespace Titanium.Web.Proxy
...
@@ -71,7 +71,7 @@ namespace Titanium.Web.Proxy
httpHeaders
));
httpHeaders
));
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
C
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
WebSession
.
Response
=
c
reateAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
}
}
...
@@ -81,7 +81,7 @@ namespace Titanium.Web.Proxy
...
@@ -81,7 +81,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="description">Response description.</param>
/// <param name="description">Response description.</param>
/// <returns></returns>
/// <returns></returns>
private
Response
C
reateAuthentication407Response
(
string
description
)
private
Response
c
reateAuthentication407Response
(
string
description
)
{
{
var
response
=
new
Response
var
response
=
new
Response
{
{
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
e91a6122
...
@@ -314,7 +314,7 @@ namespace Titanium.Web.Proxy
...
@@ -314,7 +314,7 @@ namespace Titanium.Web.Proxy
if
(
ProxyRunning
)
if
(
ProxyRunning
)
{
{
L
isten
(
endPoint
);
l
isten
(
endPoint
);
}
}
}
}
...
@@ -334,7 +334,7 @@ namespace Titanium.Web.Proxy
...
@@ -334,7 +334,7 @@ namespace Titanium.Web.Proxy
if
(
ProxyRunning
)
if
(
ProxyRunning
)
{
{
Q
uitListen
(
endPoint
);
q
uitListen
(
endPoint
);
}
}
}
}
...
@@ -368,7 +368,7 @@ namespace Titanium.Web.Proxy
...
@@ -368,7 +368,7 @@ namespace Titanium.Web.Proxy
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
throw
new
Exception
(
"Mono Runtime do not support system proxy settings."
);
}
}
V
alidateEndPointAsSystemProxy
(
endPoint
);
v
alidateEndPointAsSystemProxy
(
endPoint
);
bool
isHttp
=
(
protocolType
&
ProxyProtocolType
.
Http
)
>
0
;
bool
isHttp
=
(
protocolType
&
ProxyProtocolType
.
Http
)
>
0
;
bool
isHttps
=
(
protocolType
&
ProxyProtocolType
.
Https
)
>
0
;
bool
isHttps
=
(
protocolType
&
ProxyProtocolType
.
Https
)
>
0
;
...
@@ -523,7 +523,7 @@ namespace Titanium.Web.Proxy
...
@@ -523,7 +523,7 @@ namespace Titanium.Web.Proxy
systemProxyResolver
=
new
WinHttpWebProxyFinder
();
systemProxyResolver
=
new
WinHttpWebProxyFinder
();
systemProxyResolver
.
LoadFromIE
();
systemProxyResolver
.
LoadFromIE
();
GetCustomUpStreamProxyFunc
=
G
etSystemUpStreamProxy
;
GetCustomUpStreamProxyFunc
=
g
etSystemUpStreamProxy
;
}
}
ProxyRunning
=
true
;
ProxyRunning
=
true
;
...
@@ -532,7 +532,7 @@ namespace Titanium.Web.Proxy
...
@@ -532,7 +532,7 @@ namespace Titanium.Web.Proxy
foreach
(
var
endPoint
in
ProxyEndPoints
)
foreach
(
var
endPoint
in
ProxyEndPoints
)
{
{
L
isten
(
endPoint
);
l
isten
(
endPoint
);
}
}
}
}
...
@@ -559,7 +559,7 @@ namespace Titanium.Web.Proxy
...
@@ -559,7 +559,7 @@ namespace Titanium.Web.Proxy
foreach
(
var
endPoint
in
ProxyEndPoints
)
foreach
(
var
endPoint
in
ProxyEndPoints
)
{
{
Q
uitListen
(
endPoint
);
q
uitListen
(
endPoint
);
}
}
ProxyEndPoints
.
Clear
();
ProxyEndPoints
.
Clear
();
...
@@ -574,7 +574,7 @@ namespace Titanium.Web.Proxy
...
@@ -574,7 +574,7 @@ namespace Titanium.Web.Proxy
/// Listen on given end point of local machine.
/// Listen on given end point of local machine.
/// </summary>
/// </summary>
/// <param name="endPoint">The end point to listen.</param>
/// <param name="endPoint">The end point to listen.</param>
private
void
L
isten
(
ProxyEndPoint
endPoint
)
private
void
l
isten
(
ProxyEndPoint
endPoint
)
{
{
endPoint
.
Listener
=
new
TcpListener
(
endPoint
.
IpAddress
,
endPoint
.
Port
);
endPoint
.
Listener
=
new
TcpListener
(
endPoint
.
IpAddress
,
endPoint
.
Port
);
try
try
...
@@ -584,7 +584,7 @@ namespace Titanium.Web.Proxy
...
@@ -584,7 +584,7 @@ namespace Titanium.Web.Proxy
endPoint
.
Port
=
((
IPEndPoint
)
endPoint
.
Listener
.
LocalEndpoint
).
Port
;
endPoint
.
Port
=
((
IPEndPoint
)
endPoint
.
Listener
.
LocalEndpoint
).
Port
;
// accept clients asynchronously
// accept clients asynchronously
endPoint
.
Listener
.
BeginAcceptTcpClient
(
O
nAcceptConnection
,
endPoint
);
endPoint
.
Listener
.
BeginAcceptTcpClient
(
o
nAcceptConnection
,
endPoint
);
}
}
catch
(
SocketException
ex
)
catch
(
SocketException
ex
)
{
{
...
@@ -600,7 +600,7 @@ namespace Titanium.Web.Proxy
...
@@ -600,7 +600,7 @@ namespace Titanium.Web.Proxy
/// Verify if its safe to set this end point as system proxy.
/// Verify if its safe to set this end point as system proxy.
/// </summary>
/// </summary>
/// <param name="endPoint">The end point to validate.</param>
/// <param name="endPoint">The end point to validate.</param>
private
void
V
alidateEndPointAsSystemProxy
(
ExplicitProxyEndPoint
endPoint
)
private
void
v
alidateEndPointAsSystemProxy
(
ExplicitProxyEndPoint
endPoint
)
{
{
if
(
endPoint
==
null
)
if
(
endPoint
==
null
)
{
{
...
@@ -623,7 +623,7 @@ namespace Titanium.Web.Proxy
...
@@ -623,7 +623,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="sessionEventArgs">The session.</param>
/// <param name="sessionEventArgs">The session.</param>
/// <returns>The external proxy as task result.</returns>
/// <returns>The external proxy as task result.</returns>
private
Task
<
ExternalProxy
>
G
etSystemUpStreamProxy
(
SessionEventArgsBase
sessionEventArgs
)
private
Task
<
ExternalProxy
>
g
etSystemUpStreamProxy
(
SessionEventArgsBase
sessionEventArgs
)
{
{
var
proxy
=
systemProxyResolver
.
GetProxy
(
sessionEventArgs
.
WebSession
.
Request
.
RequestUri
);
var
proxy
=
systemProxyResolver
.
GetProxy
(
sessionEventArgs
.
WebSession
.
Request
.
RequestUri
);
return
Task
.
FromResult
(
proxy
);
return
Task
.
FromResult
(
proxy
);
...
@@ -632,7 +632,7 @@ namespace Titanium.Web.Proxy
...
@@ -632,7 +632,7 @@ namespace Titanium.Web.Proxy
/// <summary>
/// <summary>
/// Act when a connection is received from client.
/// Act when a connection is received from client.
/// </summary>
/// </summary>
private
void
O
nAcceptConnection
(
IAsyncResult
asyn
)
private
void
o
nAcceptConnection
(
IAsyncResult
asyn
)
{
{
var
endPoint
=
(
ProxyEndPoint
)
asyn
.
AsyncState
;
var
endPoint
=
(
ProxyEndPoint
)
asyn
.
AsyncState
;
...
@@ -657,11 +657,11 @@ namespace Titanium.Web.Proxy
...
@@ -657,11 +657,11 @@ namespace Titanium.Web.Proxy
if
(
tcpClient
!=
null
)
if
(
tcpClient
!=
null
)
{
{
Task
.
Run
(
async
()
=>
{
await
H
andleClient
(
tcpClient
,
endPoint
);
});
Task
.
Run
(
async
()
=>
{
await
h
andleClient
(
tcpClient
,
endPoint
);
});
}
}
// Get the listener that handles the client request.
// Get the listener that handles the client request.
endPoint
.
Listener
.
BeginAcceptTcpClient
(
O
nAcceptConnection
,
endPoint
);
endPoint
.
Listener
.
BeginAcceptTcpClient
(
o
nAcceptConnection
,
endPoint
);
}
}
/// <summary>
/// <summary>
...
@@ -670,7 +670,7 @@ namespace Titanium.Web.Proxy
...
@@ -670,7 +670,7 @@ namespace Titanium.Web.Proxy
/// <param name="tcpClient">The client.</param>
/// <param name="tcpClient">The client.</param>
/// <param name="endPoint">The proxy endpoint.</param>
/// <param name="endPoint">The proxy endpoint.</param>
/// <returns>The task.</returns>
/// <returns>The task.</returns>
private
async
Task
H
andleClient
(
TcpClient
tcpClient
,
ProxyEndPoint
endPoint
)
private
async
Task
h
andleClient
(
TcpClient
tcpClient
,
ProxyEndPoint
endPoint
)
{
{
tcpClient
.
ReceiveTimeout
=
ConnectionTimeOutSeconds
*
1000
;
tcpClient
.
ReceiveTimeout
=
ConnectionTimeOutSeconds
*
1000
;
tcpClient
.
SendTimeout
=
ConnectionTimeOutSeconds
*
1000
;
tcpClient
.
SendTimeout
=
ConnectionTimeOutSeconds
*
1000
;
...
@@ -683,11 +683,11 @@ namespace Titanium.Web.Proxy
...
@@ -683,11 +683,11 @@ namespace Titanium.Web.Proxy
{
{
if
(
endPoint
is
TransparentProxyEndPoint
tep
)
if
(
endPoint
is
TransparentProxyEndPoint
tep
)
{
{
await
H
andleClient
(
tep
,
clientConnection
);
await
h
andleClient
(
tep
,
clientConnection
);
}
}
else
else
{
{
await
H
andleClient
((
ExplicitProxyEndPoint
)
endPoint
,
clientConnection
);
await
h
andleClient
((
ExplicitProxyEndPoint
)
endPoint
,
clientConnection
);
}
}
}
}
}
}
...
@@ -697,7 +697,7 @@ namespace Titanium.Web.Proxy
...
@@ -697,7 +697,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="clientStream">The client stream.</param>
/// <param name="clientStream">The client stream.</param>
/// <param name="exception">The exception.</param>
/// <param name="exception">The exception.</param>
private
void
O
nException
(
CustomBufferedStream
clientStream
,
Exception
exception
)
private
void
o
nException
(
CustomBufferedStream
clientStream
,
Exception
exception
)
{
{
#if DEBUG
#if DEBUG
if
(
clientStream
is
DebugCustomBufferedStream
debugStream
)
if
(
clientStream
is
DebugCustomBufferedStream
debugStream
)
...
@@ -712,7 +712,7 @@ namespace Titanium.Web.Proxy
...
@@ -712,7 +712,7 @@ namespace Titanium.Web.Proxy
/// <summary>
/// <summary>
/// Quit listening on the given end point.
/// Quit listening on the given end point.
/// </summary>
/// </summary>
private
void
Q
uitListen
(
ProxyEndPoint
endPoint
)
private
void
q
uitListen
(
ProxyEndPoint
endPoint
)
{
{
endPoint
.
Listener
.
Stop
();
endPoint
.
Listener
.
Stop
();
endPoint
.
Listener
.
Server
.
Dispose
();
endPoint
.
Listener
.
Server
.
Dispose
();
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
e91a6122
This diff is collapsed.
Click to expand it.
Titanium.Web.Proxy/ResponseHandler.cs
View file @
e91a6122
using
System
;
using
System.Net
;
using
System.Net
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.EventArguments
;
using
Titanium.Web.Proxy.Exceptions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Network.WinAuth.Security
;
using
Titanium.Web.Proxy.Network.WinAuth.Security
;
...
@@ -18,10 +16,9 @@ namespace Titanium.Web.Proxy
...
@@ -18,10 +16,9 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="args">The session event arguments.</param>
/// <param name="args">The session event arguments.</param>
/// <returns> The task.</returns>
/// <returns> The task.</returns>
private
async
Task
HandleHttpSessionResponse
(
SessionEventArgs
args
)
private
async
Task
handleHttpSessionResponse
(
SessionEventArgs
args
)
{
try
{
{
var
cancellationToken
=
args
.
CancellationTokenSource
.
Token
;
var
cancellationToken
=
args
.
CancellationTokenSource
.
Token
;
// read response & headers from server
// read response & headers from server
...
@@ -35,7 +32,7 @@ namespace Titanium.Web.Proxy
...
@@ -35,7 +32,7 @@ namespace Titanium.Web.Proxy
{
{
if
(
response
.
StatusCode
==
(
int
)
HttpStatusCode
.
Unauthorized
)
if
(
response
.
StatusCode
==
(
int
)
HttpStatusCode
.
Unauthorized
)
{
{
await
H
andle401UnAuthorized
(
args
);
await
h
andle401UnAuthorized
(
args
);
}
}
else
else
{
{
...
@@ -48,7 +45,7 @@ namespace Titanium.Web.Proxy
...
@@ -48,7 +45,7 @@ namespace Titanium.Web.Proxy
// if user requested call back then do it
// if user requested call back then do it
if
(!
response
.
Locked
)
if
(!
response
.
Locked
)
{
{
await
I
nvokeBeforeResponse
(
args
);
await
i
nvokeBeforeResponse
(
args
);
}
}
// it may changed in the user event
// it may changed in the user event
...
@@ -80,7 +77,7 @@ namespace Titanium.Web.Proxy
...
@@ -80,7 +77,7 @@ namespace Titanium.Web.Proxy
{
{
// clear current response
// clear current response
await
args
.
ClearResponse
(
cancellationToken
);
await
args
.
ClearResponse
(
cancellationToken
);
await
H
andleHttpSessionRequestInternal
(
args
.
WebSession
.
ServerConnection
,
args
);
await
h
andleHttpSessionRequestInternal
(
args
.
WebSession
.
ServerConnection
,
args
);
return
;
return
;
}
}
...
@@ -123,11 +120,7 @@ namespace Titanium.Web.Proxy
...
@@ -123,11 +120,7 @@ namespace Titanium.Web.Proxy
cancellationToken
);
cancellationToken
);
}
}
}
}
}
catch
(
Exception
e
)
when
(!(
e
is
ProxyHttpException
))
{
throw
new
ProxyHttpException
(
"Error occured whilst handling session response"
,
e
,
args
);
}
}
}
/// <summary>
/// <summary>
...
@@ -135,7 +128,7 @@ namespace Titanium.Web.Proxy
...
@@ -135,7 +128,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="args"></param>
/// <param name="args"></param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
I
nvokeBeforeResponse
(
SessionEventArgs
args
)
private
async
Task
i
nvokeBeforeResponse
(
SessionEventArgs
args
)
{
{
if
(
BeforeResponse
!=
null
)
if
(
BeforeResponse
!=
null
)
{
{
...
@@ -148,7 +141,7 @@ namespace Titanium.Web.Proxy
...
@@ -148,7 +141,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="args"></param>
/// <param name="args"></param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
I
nvokeAfterResponse
(
SessionEventArgs
args
)
private
async
Task
i
nvokeAfterResponse
(
SessionEventArgs
args
)
{
{
if
(
AfterResponse
!=
null
)
if
(
AfterResponse
!=
null
)
{
{
...
...
Titanium.Web.Proxy/TransparentClientHandler.cs
View file @
e91a6122
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.IO
;
using
System.Net.Security
;
using
System.Net.Security
;
using
System.Net.Sockets
;
using
System.Net.Sockets
;
...
@@ -27,7 +26,7 @@ namespace Titanium.Web.Proxy
...
@@ -27,7 +26,7 @@ namespace Titanium.Web.Proxy
/// <param name="endPoint">The transparent endpoint.</param>
/// <param name="endPoint">The transparent endpoint.</param>
/// <param name="clientConnection">The client connection.</param>
/// <param name="clientConnection">The client connection.</param>
/// <returns></returns>
/// <returns></returns>
private
async
Task
H
andleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClientConnection
clientConnection
)
private
async
Task
h
andleClient
(
TransparentProxyEndPoint
endPoint
,
TcpClientConnection
clientConnection
)
{
{
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
cancellationTokenSource
=
new
CancellationTokenSource
();
var
cancellationToken
=
cancellationTokenSource
.
Token
;
var
cancellationToken
=
cancellationTokenSource
.
Token
;
...
@@ -125,24 +124,24 @@ namespace Titanium.Web.Proxy
...
@@ -125,24 +124,24 @@ 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
H
andleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
await
h
andleHttpSessionRequest
(
endPoint
,
clientConnection
,
clientStream
,
clientStreamWriter
,
cancellationTokenSource
,
isHttps
?
httpsHostName
:
null
,
null
);
cancellationTokenSource
,
isHttps
?
httpsHostName
:
null
,
null
);
}
}
catch
(
ProxyException
e
)
catch
(
ProxyException
e
)
{
{
O
nException
(
clientStream
,
e
);
o
nException
(
clientStream
,
e
);
}
}
catch
(
IOException
e
)
catch
(
IOException
e
)
{
{
O
nException
(
clientStream
,
new
Exception
(
"Connection was aborted"
,
e
));
o
nException
(
clientStream
,
new
Exception
(
"Connection was aborted"
,
e
));
}
}
catch
(
SocketException
e
)
catch
(
SocketException
e
)
{
{
O
nException
(
clientStream
,
new
Exception
(
"Could not connect"
,
e
));
o
nException
(
clientStream
,
new
Exception
(
"Could not connect"
,
e
));
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
O
nException
(
clientStream
,
new
Exception
(
"Error occured in whilst handling the client"
,
e
));
o
nException
(
clientStream
,
new
Exception
(
"Error occured in whilst handling the client"
,
e
));
}
}
finally
finally
{
{
...
...
Titanium.Web.Proxy/WinAuthHandler.cs
View file @
e91a6122
...
@@ -45,7 +45,7 @@ namespace Titanium.Web.Proxy
...
@@ -45,7 +45,7 @@ namespace Titanium.Web.Proxy
/// User to server to authenticate requests.
/// User to server to authenticate requests.
/// To disable this set ProxyServer.EnableWinAuth to false.
/// To disable this set ProxyServer.EnableWinAuth to false.
/// </summary>
/// </summary>
internal
async
Task
H
andle401UnAuthorized
(
SessionEventArgs
args
)
private
async
Task
h
andle401UnAuthorized
(
SessionEventArgs
args
)
{
{
string
headerName
=
null
;
string
headerName
=
null
;
HttpHeader
authHeader
=
null
;
HttpHeader
authHeader
=
null
;
...
@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy
...
@@ -99,7 +99,7 @@ namespace Titanium.Web.Proxy
if
(!
WinAuthEndPoint
.
ValidateWinAuthState
(
args
.
WebSession
.
Data
,
expectedAuthState
))
if
(!
WinAuthEndPoint
.
ValidateWinAuthState
(
args
.
WebSession
.
Data
,
expectedAuthState
))
{
{
// Invalid state, create proper error message to client
// Invalid state, create proper error message to client
await
R
ewriteUnauthorizedResponse
(
args
);
await
r
ewriteUnauthorizedResponse
(
args
);
return
;
return
;
}
}
...
@@ -163,7 +163,7 @@ namespace Titanium.Web.Proxy
...
@@ -163,7 +163,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
/// <param name="args"></param>
/// <param name="args"></param>
/// <returns></returns>
/// <returns></returns>
internal
async
Task
R
ewriteUnauthorizedResponse
(
SessionEventArgs
args
)
private
async
Task
r
ewriteUnauthorizedResponse
(
SessionEventArgs
args
)
{
{
var
response
=
args
.
WebSession
.
Response
;
var
response
=
args
.
WebSession
.
Response
;
...
...
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