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
6c07974f
Commit
6c07974f
authored
Jun 11, 2016
by
justcoding121
Committed by
justcoding121
Jun 11, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expose client IP Address #77
parent
efada5e6
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
75 additions
and
91 deletions
+75
-91
DeflateDecompression.cs
Titanium.Web.Proxy/Decompression/DeflateDecompression.cs
+1
-1
GZipDecompression.cs
Titanium.Web.Proxy/Decompression/GZipDecompression.cs
+1
-1
ZlibDecompression.cs
Titanium.Web.Proxy/Decompression/ZlibDecompression.cs
+1
-1
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+14
-4
HttpWebRequestExtensions.cs
Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
+1
-1
HttpWebResponseExtensions.cs
Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
+1
-1
StreamExtensions.cs
Titanium.Web.Proxy/Extensions/StreamExtensions.cs
+2
-2
CustomBinaryReader.cs
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
+4
-4
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+1
-1
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+3
-3
CustomSslStream.cs
Titanium.Web.Proxy/Network/CustomSslStream.cs
+0
-24
TcpConnectionManager.cs
Titanium.Web.Proxy/Network/TcpConnectionManager.cs
+4
-4
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+15
-15
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+21
-21
ProxyConstants.cs
Titanium.Web.Proxy/Shared/ProxyConstants.cs
+5
-6
Titanium.Web.Proxy.csproj
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
+1
-2
No files found.
Titanium.Web.Proxy/Decompression/DeflateDecompression.cs
View file @
6c07974f
...
@@ -13,7 +13,7 @@ namespace Titanium.Web.Proxy.Decompression
...
@@ -13,7 +13,7 @@ namespace Titanium.Web.Proxy.Decompression
using
(
var
decompressor
=
new
DeflateStream
(
stream
,
CompressionMode
.
Decompress
))
using
(
var
decompressor
=
new
DeflateStream
(
stream
,
CompressionMode
.
Decompress
))
{
{
var
buffer
=
new
byte
[
Constants
.
BUFFER_SIZE
];
var
buffer
=
new
byte
[
Proxy
Constants
.
BUFFER_SIZE
];
using
(
var
output
=
new
MemoryStream
())
using
(
var
output
=
new
MemoryStream
())
{
{
...
...
Titanium.Web.Proxy/Decompression/GZipDecompression.cs
View file @
6c07974f
...
@@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Decompression
...
@@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Decompression
{
{
using
(
var
decompressor
=
new
GZipStream
(
new
MemoryStream
(
compressedArray
),
CompressionMode
.
Decompress
))
using
(
var
decompressor
=
new
GZipStream
(
new
MemoryStream
(
compressedArray
),
CompressionMode
.
Decompress
))
{
{
var
buffer
=
new
byte
[
Constants
.
BUFFER_SIZE
];
var
buffer
=
new
byte
[
Proxy
Constants
.
BUFFER_SIZE
];
using
(
var
output
=
new
MemoryStream
())
using
(
var
output
=
new
MemoryStream
())
{
{
int
read
;
int
read
;
...
...
Titanium.Web.Proxy/Decompression/ZlibDecompression.cs
View file @
6c07974f
...
@@ -12,7 +12,7 @@ namespace Titanium.Web.Proxy.Decompression
...
@@ -12,7 +12,7 @@ namespace Titanium.Web.Proxy.Decompression
var
memoryStream
=
new
MemoryStream
(
compressedArray
);
var
memoryStream
=
new
MemoryStream
(
compressedArray
);
using
(
var
decompressor
=
new
ZlibStream
(
memoryStream
,
CompressionMode
.
Decompress
))
using
(
var
decompressor
=
new
ZlibStream
(
memoryStream
,
CompressionMode
.
Decompress
))
{
{
var
buffer
=
new
byte
[
Constants
.
BUFFER_SIZE
];
var
buffer
=
new
byte
[
Proxy
Constants
.
BUFFER_SIZE
];
using
(
var
output
=
new
MemoryStream
())
using
(
var
output
=
new
MemoryStream
())
{
{
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
6c07974f
...
@@ -8,6 +8,8 @@ using Titanium.Web.Proxy.Http.Responses;
...
@@ -8,6 +8,8 @@ using Titanium.Web.Proxy.Http.Responses;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Network
;
using
Titanium.Web.Proxy.Network
;
using
System.Net
;
using
System.Net.Sockets
;
namespace
Titanium.Web.Proxy.EventArguments
namespace
Titanium.Web.Proxy.EventArguments
{
{
...
@@ -24,26 +26,34 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -24,26 +26,34 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
/// </summary>
internal
SessionEventArgs
()
internal
SessionEventArgs
()
{
{
Client
=
new
ProxyClient
();
Proxy
Client
=
new
ProxyClient
();
WebSession
=
new
HttpWebClient
();
WebSession
=
new
HttpWebClient
();
}
}
/// <summary>
/// <summary>
/// Holds a reference to server connection
/// Holds a reference to server connection
/// </summary>
/// </summary>
internal
ProxyClient
Client
{
get
;
set
;
}
internal
ProxyClient
Proxy
Client
{
get
;
set
;
}
/// <summary>
/// <summary>
/// Does this session uses SSL
/// Does this session uses SSL
/// </summary>
/// </summary>
public
bool
IsHttps
=>
WebSession
.
Request
.
RequestUri
.
Scheme
==
Uri
.
UriSchemeHttps
;
public
bool
IsHttps
=>
WebSession
.
Request
.
RequestUri
.
Scheme
==
Uri
.
UriSchemeHttps
;
public
IPAddress
ClientIpAddress
=>
((
IPEndPoint
)
Client
.
Client
.
RemoteEndPoint
).
Address
;
/// <summary>
/// <summary>
/// A web session corresponding to a single request/response sequence
/// A web session corresponding to a single request/response sequence
/// within a proxy connection
/// within a proxy connection
/// </summary>
/// </summary>
public
HttpWebClient
WebSession
{
get
;
set
;
}
public
HttpWebClient
WebSession
{
get
;
set
;
}
/// <summary>
/// Reference to client connection
/// </summary>
internal
TcpClient
Client
{
get
;
set
;
}
/// <summary>
/// <summary>
/// implement any cleanup here
/// implement any cleanup here
...
@@ -76,7 +86,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -76,7 +86,7 @@ namespace Titanium.Web.Proxy.EventArguments
//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
(
WebSession
.
Request
.
IsChunked
)
if
(
WebSession
.
Request
.
IsChunked
)
{
{
await
this
.
Client
.
ClientStreamReader
.
CopyBytesToStreamChunked
(
requestBodyStream
).
ConfigureAwait
(
false
);
await
this
.
Proxy
Client
.
ClientStreamReader
.
CopyBytesToStreamChunked
(
requestBodyStream
).
ConfigureAwait
(
false
);
}
}
else
else
{
{
...
@@ -84,7 +94,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -84,7 +94,7 @@ namespace Titanium.Web.Proxy.EventArguments
if
(
WebSession
.
Request
.
ContentLength
>
0
)
if
(
WebSession
.
Request
.
ContentLength
>
0
)
{
{
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
await
this
.
Client
.
ClientStreamReader
.
CopyBytesToStream
(
requestBodyStream
,
WebSession
.
Request
.
ContentLength
).
ConfigureAwait
(
false
);
await
this
.
Proxy
Client
.
ClientStreamReader
.
CopyBytesToStream
(
requestBodyStream
,
WebSession
.
Request
.
ContentLength
).
ConfigureAwait
(
false
);
}
}
else
if
(
WebSession
.
Request
.
HttpVersion
.
Major
==
1
&&
WebSession
.
Request
.
HttpVersion
.
Minor
==
0
)
else
if
(
WebSession
.
Request
.
HttpVersion
.
Major
==
1
&&
WebSession
.
Request
.
HttpVersion
.
Minor
==
0
)
...
...
Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
View file @
6c07974f
...
@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Extensions
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
//extract the encoding by finding the charset
//extract the encoding by finding the charset
var
contentTypes
=
request
.
ContentType
.
Split
(
Constants
.
SemiColonSplit
);
var
contentTypes
=
request
.
ContentType
.
Split
(
Proxy
Constants
.
SemiColonSplit
);
foreach
(
var
contentType
in
contentTypes
)
foreach
(
var
contentType
in
contentTypes
)
{
{
var
encodingSplit
=
contentType
.
Split
(
'='
);
var
encodingSplit
=
contentType
.
Split
(
'='
);
...
...
Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
View file @
6c07974f
...
@@ -15,7 +15,7 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -15,7 +15,7 @@ namespace Titanium.Web.Proxy.Extensions
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
//extract the encoding by finding the charset
//extract the encoding by finding the charset
var
contentTypes
=
response
.
ContentType
.
Split
(
Constants
.
SemiColonSplit
);
var
contentTypes
=
response
.
ContentType
.
Split
(
Proxy
Constants
.
SemiColonSplit
);
foreach
(
var
contentType
in
contentTypes
)
foreach
(
var
contentType
in
contentTypes
)
{
{
var
encodingSplit
=
contentType
.
Split
(
'='
);
var
encodingSplit
=
contentType
.
Split
(
'='
);
...
...
Titanium.Web.Proxy/Extensions/StreamExtensions.cs
View file @
6c07974f
...
@@ -25,12 +25,12 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -25,12 +25,12 @@ namespace Titanium.Web.Proxy.Extensions
var
totalbytesRead
=
0
;
var
totalbytesRead
=
0
;
long
bytesToRead
;
long
bytesToRead
;
if
(
totalBytesToRead
<
Constants
.
BUFFER_SIZE
)
if
(
totalBytesToRead
<
Proxy
Constants
.
BUFFER_SIZE
)
{
{
bytesToRead
=
totalBytesToRead
;
bytesToRead
=
totalBytesToRead
;
}
}
else
else
bytesToRead
=
Constants
.
BUFFER_SIZE
;
bytesToRead
=
Proxy
Constants
.
BUFFER_SIZE
;
while
(
totalbytesRead
<
totalBytesToRead
)
while
(
totalbytesRead
<
totalBytesToRead
)
...
...
Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
View file @
6c07974f
...
@@ -78,12 +78,12 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -78,12 +78,12 @@ namespace Titanium.Web.Proxy.Helpers
internal
async
Task
<
byte
[
]>
ReadBytesAsync
(
long
totalBytesToRead
)
internal
async
Task
<
byte
[
]>
ReadBytesAsync
(
long
totalBytesToRead
)
{
{
int
bytesToRead
=
Constants
.
BUFFER_SIZE
;
int
bytesToRead
=
Proxy
Constants
.
BUFFER_SIZE
;
if
(
totalBytesToRead
<
Constants
.
BUFFER_SIZE
)
if
(
totalBytesToRead
<
Proxy
Constants
.
BUFFER_SIZE
)
bytesToRead
=
(
int
)
totalBytesToRead
;
bytesToRead
=
(
int
)
totalBytesToRead
;
var
buffer
=
new
byte
[
Constants
.
BUFFER_SIZE
];
var
buffer
=
new
byte
[
Proxy
Constants
.
BUFFER_SIZE
];
var
bytesRead
=
0
;
var
bytesRead
=
0
;
var
totalBytesRead
=
0
;
var
totalBytesRead
=
0
;
...
@@ -100,7 +100,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -100,7 +100,7 @@ namespace Titanium.Web.Proxy.Helpers
bytesRead
=
0
;
bytesRead
=
0
;
var
remainingBytes
=
(
totalBytesToRead
-
totalBytesRead
);
var
remainingBytes
=
(
totalBytesToRead
-
totalBytesRead
);
bytesToRead
=
remainingBytes
>
(
long
)
Constants
.
BUFFER_SIZE
?
Constants
.
BUFFER_SIZE
:
(
int
)
remainingBytes
;
bytesToRead
=
remainingBytes
>
(
long
)
ProxyConstants
.
BUFFER_SIZE
?
Proxy
Constants
.
BUFFER_SIZE
:
(
int
)
remainingBytes
;
}
}
return
outStream
.
ToArray
();
return
outStream
.
ToArray
();
...
...
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
6c07974f
...
@@ -50,7 +50,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -50,7 +50,7 @@ namespace Titanium.Web.Proxy.Helpers
try
try
{
{
sslStream
=
new
SslStream
(
tunnelStream
);
sslStream
=
new
SslStream
(
tunnelStream
);
await
sslStream
.
AuthenticateAsClientAsync
(
hostName
,
null
,
Constants
.
Supported
Protocols
,
false
);
await
sslStream
.
AuthenticateAsClientAsync
(
hostName
,
null
,
ProxyConstants
.
SupportedSsl
Protocols
,
false
);
tunnelStream
=
sslStream
;
tunnelStream
=
sslStream
;
}
}
catch
catch
...
...
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
6c07974f
...
@@ -64,7 +64,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -64,7 +64,7 @@ namespace Titanium.Web.Proxy.Http
if
(
ProxyServer
.
Enable100ContinueBehaviour
)
if
(
ProxyServer
.
Enable100ContinueBehaviour
)
if
(
this
.
Request
.
ExpectContinue
)
if
(
this
.
Request
.
ExpectContinue
)
{
{
var
httpResult
=
(
await
ServerConnection
.
StreamReader
.
ReadLineAsync
()).
Split
(
Constants
.
SpaceSplit
,
3
);
var
httpResult
=
(
await
ServerConnection
.
StreamReader
.
ReadLineAsync
()).
Split
(
Proxy
Constants
.
SpaceSplit
,
3
);
var
responseStatusCode
=
httpResult
[
1
].
Trim
();
var
responseStatusCode
=
httpResult
[
1
].
Trim
();
var
responseStatusDescription
=
httpResult
[
2
].
Trim
();
var
responseStatusDescription
=
httpResult
[
2
].
Trim
();
...
@@ -89,7 +89,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -89,7 +89,7 @@ namespace Titanium.Web.Proxy.Http
//return if this is already read
//return if this is already read
if
(
this
.
Response
.
ResponseStatusCode
!=
null
)
return
;
if
(
this
.
Response
.
ResponseStatusCode
!=
null
)
return
;
var
httpResult
=
(
await
ServerConnection
.
StreamReader
.
ReadLineAsync
()).
Split
(
Constants
.
SpaceSplit
,
3
);
var
httpResult
=
(
await
ServerConnection
.
StreamReader
.
ReadLineAsync
()).
Split
(
Proxy
Constants
.
SpaceSplit
,
3
);
if
(
string
.
IsNullOrEmpty
(
httpResult
[
0
]))
if
(
string
.
IsNullOrEmpty
(
httpResult
[
0
]))
{
{
...
@@ -131,7 +131,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -131,7 +131,7 @@ namespace Titanium.Web.Proxy.Http
for
(
int
index
=
0
;
index
<
responseLines
.
Count
;
++
index
)
for
(
int
index
=
0
;
index
<
responseLines
.
Count
;
++
index
)
{
{
string
[]
strArray
=
responseLines
[
index
].
Split
(
Constants
.
ColonSplit
,
2
);
string
[]
strArray
=
responseLines
[
index
].
Split
(
Proxy
Constants
.
ColonSplit
,
2
);
this
.
Response
.
ResponseHeaders
.
Add
(
new
HttpHeader
(
strArray
[
0
],
strArray
[
1
]));
this
.
Response
.
ResponseHeaders
.
Add
(
new
HttpHeader
(
strArray
[
0
],
strArray
[
1
]));
}
}
}
}
...
...
Titanium.Web.Proxy/Network/CustomSslStream.cs
deleted
100644 → 0
View file @
efada5e6
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Net.Security
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.EventArguments
;
namespace
Titanium.Web.Proxy.Network
{
/// <summary>
/// Used to pass in Session object for ServerCertificateValidation Callback
/// </summary>
internal
class
CustomSslStream
:
SslStream
{
internal
CustomSslStream
(
Stream
innerStream
,
bool
leaveInnerStreamOpen
,
RemoteCertificateValidationCallback
userCertificateValidationCallback
,
LocalCertificateSelectionCallback
clientCertificateSelectionCallback
)
:
base
(
innerStream
,
leaveInnerStreamOpen
,
userCertificateValidationCallback
,
clientCertificateSelectionCallback
)
{
}
}
}
Titanium.Web.Proxy/Network/TcpConnectionManager.cs
View file @
6c07974f
...
@@ -102,14 +102,14 @@ namespace Titanium.Web.Proxy.Network
...
@@ -102,14 +102,14 @@ namespace Titanium.Web.Proxy.Network
if
(
isHttps
)
if
(
isHttps
)
{
{
Custom
SslStream
sslStream
=
null
;
SslStream
sslStream
=
null
;
if
(
ProxyServer
.
UpStreamHttpsProxy
!=
null
)
if
(
ProxyServer
.
UpStreamHttpsProxy
!=
null
)
{
{
client
=
new
TcpClient
(
ProxyServer
.
UpStreamHttpsProxy
.
HostName
,
ProxyServer
.
UpStreamHttpsProxy
.
Port
);
client
=
new
TcpClient
(
ProxyServer
.
UpStreamHttpsProxy
.
HostName
,
ProxyServer
.
UpStreamHttpsProxy
.
Port
);
stream
=
(
Stream
)
client
.
GetStream
();
stream
=
(
Stream
)
client
.
GetStream
();
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
Constants
.
BUFFER_SIZE
,
true
))
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
Proxy
Constants
.
BUFFER_SIZE
,
true
))
{
{
await
writer
.
WriteLineAsync
(
string
.
Format
(
"CONNECT {0}:{1} {2}"
,
hostname
,
port
,
version
)).
ConfigureAwait
(
false
);
await
writer
.
WriteLineAsync
(
string
.
Format
(
"CONNECT {0}:{1} {2}"
,
hostname
,
port
,
version
)).
ConfigureAwait
(
false
);
await
writer
.
WriteLineAsync
(
string
.
Format
(
"Host: {0}:{1}"
,
hostname
,
port
)).
ConfigureAwait
(
false
);
await
writer
.
WriteLineAsync
(
string
.
Format
(
"Host: {0}:{1}"
,
hostname
,
port
)).
ConfigureAwait
(
false
);
...
@@ -137,9 +137,9 @@ namespace Titanium.Web.Proxy.Network
...
@@ -137,9 +137,9 @@ namespace Titanium.Web.Proxy.Network
try
try
{
{
sslStream
=
new
Custom
SslStream
(
stream
,
true
,
new
RemoteCertificateValidationCallback
(
ProxyServer
.
ValidateServerCertificate
),
sslStream
=
new
SslStream
(
stream
,
true
,
new
RemoteCertificateValidationCallback
(
ProxyServer
.
ValidateServerCertificate
),
new
LocalCertificateSelectionCallback
(
ProxyServer
.
SelectClientCertificate
));
new
LocalCertificateSelectionCallback
(
ProxyServer
.
SelectClientCertificate
));
await
sslStream
.
AuthenticateAsClientAsync
(
hostname
,
null
,
Constants
.
Supported
Protocols
,
false
).
ConfigureAwait
(
false
);
await
sslStream
.
AuthenticateAsClientAsync
(
hostname
,
null
,
ProxyConstants
.
SupportedSsl
Protocols
,
false
).
ConfigureAwait
(
false
);
stream
=
(
Stream
)
sslStream
;
stream
=
(
Stream
)
sslStream
;
}
}
catch
catch
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
6c07974f
...
@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy
...
@@ -41,7 +41,7 @@ namespace Titanium.Web.Proxy
}
}
//break up the line into three components (method, remote URL & Http Version)
//break up the line into three components (method, remote URL & Http Version)
var
httpCmdSplit
=
httpCmd
.
Split
(
Constants
.
SpaceSplit
,
3
);
var
httpCmdSplit
=
httpCmd
.
Split
(
Proxy
Constants
.
SpaceSplit
,
3
);
var
httpVerb
=
httpCmdSplit
[
0
];
var
httpVerb
=
httpCmdSplit
[
0
];
...
@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy
...
@@ -86,7 +86,7 @@ namespace Titanium.Web.Proxy
var
certificate
=
await
CertManager
.
CreateCertificate
(
httpRemoteUri
.
Host
,
false
);
var
certificate
=
await
CertManager
.
CreateCertificate
(
httpRemoteUri
.
Host
,
false
);
//Successfully managed to authenticate the client using the fake certificate
//Successfully managed to authenticate the client using the fake certificate
await
sslStream
.
AuthenticateAsServerAsync
(
certificate
,
false
,
await
sslStream
.
AuthenticateAsServerAsync
(
certificate
,
false
,
Constants
.
Supported
Protocols
,
false
).
ConfigureAwait
(
false
);
ProxyConstants
.
SupportedSsl
Protocols
,
false
).
ConfigureAwait
(
false
);
//HTTPS server created - we can now decrypt the client's traffic
//HTTPS server created - we can now decrypt the client's traffic
clientStream
=
sslStream
;
clientStream
=
sslStream
;
...
@@ -198,12 +198,12 @@ namespace Titanium.Web.Proxy
...
@@ -198,12 +198,12 @@ namespace Titanium.Web.Proxy
}
}
var
args
=
new
SessionEventArgs
();
var
args
=
new
SessionEventArgs
();
args
.
Client
.
TcpClient
=
client
;
args
.
Client
=
client
;
try
try
{
{
//break up the line into three components (method, remote URL & Http Version)
//break up the line into three components (method, remote URL & Http Version)
var
httpCmdSplit
=
httpCmd
.
Split
(
Constants
.
SpaceSplit
,
3
);
var
httpCmdSplit
=
httpCmd
.
Split
(
Proxy
Constants
.
SpaceSplit
,
3
);
var
httpMethod
=
httpCmdSplit
[
0
];
var
httpMethod
=
httpCmdSplit
[
0
];
...
@@ -223,7 +223,7 @@ namespace Titanium.Web.Proxy
...
@@ -223,7 +223,7 @@ namespace Titanium.Web.Proxy
string
tmpLine
;
string
tmpLine
;
while
(!
string
.
IsNullOrEmpty
(
tmpLine
=
await
clientStreamReader
.
ReadLineAsync
().
ConfigureAwait
(
false
)))
while
(!
string
.
IsNullOrEmpty
(
tmpLine
=
await
clientStreamReader
.
ReadLineAsync
().
ConfigureAwait
(
false
)))
{
{
var
header
=
tmpLine
.
Split
(
Constants
.
ColonSplit
,
2
);
var
header
=
tmpLine
.
Split
(
Proxy
Constants
.
ColonSplit
,
2
);
args
.
WebSession
.
Request
.
RequestHeaders
.
Add
(
new
HttpHeader
(
header
[
0
],
header
[
1
]));
args
.
WebSession
.
Request
.
RequestHeaders
.
Add
(
new
HttpHeader
(
header
[
0
],
header
[
1
]));
}
}
...
@@ -240,9 +240,9 @@ namespace Titanium.Web.Proxy
...
@@ -240,9 +240,9 @@ namespace Titanium.Web.Proxy
args
.
WebSession
.
Request
.
Method
=
httpMethod
;
args
.
WebSession
.
Request
.
Method
=
httpMethod
;
args
.
WebSession
.
Request
.
HttpVersion
=
version
;
args
.
WebSession
.
Request
.
HttpVersion
=
version
;
args
.
Client
.
ClientStream
=
clientStream
;
args
.
Proxy
Client
.
ClientStream
=
clientStream
;
args
.
Client
.
ClientStreamReader
=
clientStreamReader
;
args
.
Proxy
Client
.
ClientStreamReader
=
clientStreamReader
;
args
.
Client
.
ClientStreamWriter
=
clientStreamWriter
;
args
.
Proxy
Client
.
ClientStreamWriter
=
clientStreamWriter
;
PrepareRequestHeaders
(
args
.
WebSession
.
Request
.
RequestHeaders
,
args
.
WebSession
);
PrepareRequestHeaders
(
args
.
WebSession
.
Request
.
RequestHeaders
,
args
.
WebSession
);
...
@@ -292,14 +292,14 @@ namespace Titanium.Web.Proxy
...
@@ -292,14 +292,14 @@ namespace Titanium.Web.Proxy
if
(
args
.
WebSession
.
Request
.
Is100Continue
)
if
(
args
.
WebSession
.
Request
.
Is100Continue
)
{
{
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"100"
,
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"100"
,
"Continue"
,
args
.
Client
.
ClientStreamWriter
);
"Continue"
,
args
.
Proxy
Client
.
ClientStreamWriter
);
await
args
.
Client
.
ClientStreamWriter
.
WriteLineAsync
();
await
args
.
Proxy
Client
.
ClientStreamWriter
.
WriteLineAsync
();
}
}
else
if
(
args
.
WebSession
.
Request
.
ExpectationFailed
)
else
if
(
args
.
WebSession
.
Request
.
ExpectationFailed
)
{
{
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"417"
,
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"417"
,
"Expectation Failed"
,
args
.
Client
.
ClientStreamWriter
);
"Expectation Failed"
,
args
.
Proxy
Client
.
ClientStreamWriter
);
await
args
.
Client
.
ClientStreamWriter
.
WriteLineAsync
();
await
args
.
Proxy
Client
.
ClientStreamWriter
.
WriteLineAsync
();
}
}
if
(!
args
.
WebSession
.
Request
.
ExpectContinue
)
if
(!
args
.
WebSession
.
Request
.
ExpectContinue
)
...
@@ -415,7 +415,7 @@ namespace Titanium.Web.Proxy
...
@@ -415,7 +415,7 @@ namespace Titanium.Web.Proxy
{
{
try
try
{
{
await
args
.
Client
.
ClientStreamReader
.
CopyBytesToStream
(
postStream
,
args
.
WebSession
.
Request
.
ContentLength
).
ConfigureAwait
(
false
);
await
args
.
Proxy
Client
.
ClientStreamReader
.
CopyBytesToStream
(
postStream
,
args
.
WebSession
.
Request
.
ContentLength
).
ConfigureAwait
(
false
);
}
}
catch
catch
{
{
...
@@ -427,7 +427,7 @@ namespace Titanium.Web.Proxy
...
@@ -427,7 +427,7 @@ namespace Titanium.Web.Proxy
{
{
try
try
{
{
await
args
.
Client
.
ClientStreamReader
.
CopyBytesToStreamChunked
(
postStream
).
ConfigureAwait
(
false
);
await
args
.
Proxy
Client
.
ClientStreamReader
.
CopyBytesToStreamChunked
(
postStream
).
ConfigureAwait
(
false
);
}
}
catch
catch
{
{
...
@@ -496,7 +496,7 @@ namespace Titanium.Web.Proxy
...
@@ -496,7 +496,7 @@ namespace Titanium.Web.Proxy
string
[]
acceptableIssuers
)
string
[]
acceptableIssuers
)
{
{
X509Certificate
clientCertificate
=
null
;
X509Certificate
clientCertificate
=
null
;
var
customSslStream
=
sender
as
Custom
SslStream
;
var
customSslStream
=
sender
as
SslStream
;
if
(
acceptableIssuers
!=
null
&&
if
(
acceptableIssuers
!=
null
&&
acceptableIssuers
.
Length
>
0
&&
acceptableIssuers
.
Length
>
0
&&
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
6c07974f
...
@@ -45,18 +45,18 @@ namespace Titanium.Web.Proxy
...
@@ -45,18 +45,18 @@ namespace Titanium.Web.Proxy
if
(
args
.
WebSession
.
Response
.
Is100Continue
)
if
(
args
.
WebSession
.
Response
.
Is100Continue
)
{
{
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"100"
,
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"100"
,
"Continue"
,
args
.
Client
.
ClientStreamWriter
);
"Continue"
,
args
.
Proxy
Client
.
ClientStreamWriter
);
await
args
.
Client
.
ClientStreamWriter
.
WriteLineAsync
();
await
args
.
Proxy
Client
.
ClientStreamWriter
.
WriteLineAsync
();
}
}
else
if
(
args
.
WebSession
.
Response
.
ExpectationFailed
)
else
if
(
args
.
WebSession
.
Response
.
ExpectationFailed
)
{
{
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"417"
,
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
"417"
,
"Expectation Failed"
,
args
.
Client
.
ClientStreamWriter
);
"Expectation Failed"
,
args
.
Proxy
Client
.
ClientStreamWriter
);
await
args
.
Client
.
ClientStreamWriter
.
WriteLineAsync
();
await
args
.
Proxy
Client
.
ClientStreamWriter
.
WriteLineAsync
();
}
}
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
args
.
WebSession
.
Response
.
ResponseStatusCode
,
await
WriteResponseStatus
(
args
.
WebSession
.
Response
.
HttpVersion
,
args
.
WebSession
.
Response
.
ResponseStatusCode
,
args
.
WebSession
.
Response
.
ResponseStatusDescription
,
args
.
Client
.
ClientStreamWriter
);
args
.
WebSession
.
Response
.
ResponseStatusDescription
,
args
.
Proxy
Client
.
ClientStreamWriter
);
if
(
args
.
WebSession
.
Response
.
ResponseBodyRead
)
if
(
args
.
WebSession
.
Response
.
ResponseBodyRead
)
{
{
...
@@ -73,24 +73,24 @@ namespace Titanium.Web.Proxy
...
@@ -73,24 +73,24 @@ namespace Titanium.Web.Proxy
args
.
WebSession
.
Response
.
ContentLength
=
-
1
;
args
.
WebSession
.
Response
.
ContentLength
=
-
1
;
}
}
await
WriteResponseHeaders
(
args
.
Client
.
ClientStreamWriter
,
args
.
WebSession
.
Response
.
ResponseHeaders
).
ConfigureAwait
(
false
);
await
WriteResponseHeaders
(
args
.
Proxy
Client
.
ClientStreamWriter
,
args
.
WebSession
.
Response
.
ResponseHeaders
).
ConfigureAwait
(
false
);
await
WriteResponseBody
(
args
.
Client
.
ClientStream
,
args
.
WebSession
.
Response
.
ResponseBody
,
isChunked
).
ConfigureAwait
(
false
);
await
WriteResponseBody
(
args
.
Proxy
Client
.
ClientStream
,
args
.
WebSession
.
Response
.
ResponseBody
,
isChunked
).
ConfigureAwait
(
false
);
}
}
else
else
{
{
await
WriteResponseHeaders
(
args
.
Client
.
ClientStreamWriter
,
args
.
WebSession
.
Response
.
ResponseHeaders
);
await
WriteResponseHeaders
(
args
.
Proxy
Client
.
ClientStreamWriter
,
args
.
WebSession
.
Response
.
ResponseHeaders
);
if
(
args
.
WebSession
.
Response
.
IsChunked
||
args
.
WebSession
.
Response
.
ContentLength
>
0
||
if
(
args
.
WebSession
.
Response
.
IsChunked
||
args
.
WebSession
.
Response
.
ContentLength
>
0
||
(
args
.
WebSession
.
Response
.
HttpVersion
.
Major
==
1
&&
args
.
WebSession
.
Response
.
HttpVersion
.
Minor
==
0
))
(
args
.
WebSession
.
Response
.
HttpVersion
.
Major
==
1
&&
args
.
WebSession
.
Response
.
HttpVersion
.
Minor
==
0
))
await
WriteResponseBody
(
args
.
WebSession
.
ServerConnection
.
StreamReader
,
args
.
Client
.
ClientStream
,
args
.
WebSession
.
Response
.
IsChunked
,
args
.
WebSession
.
Response
.
ContentLength
).
ConfigureAwait
(
false
);
await
WriteResponseBody
(
args
.
WebSession
.
ServerConnection
.
StreamReader
,
args
.
Proxy
Client
.
ClientStream
,
args
.
WebSession
.
Response
.
IsChunked
,
args
.
WebSession
.
Response
.
ContentLength
).
ConfigureAwait
(
false
);
}
}
await
args
.
Client
.
ClientStream
.
FlushAsync
();
await
args
.
Proxy
Client
.
ClientStream
.
FlushAsync
();
}
}
catch
catch
{
{
Dispose
(
args
.
Client
.
TcpClient
,
args
.
Client
.
ClientStream
,
args
.
Client
.
ClientStreamReader
,
args
.
Client
.
ClientStreamWriter
,
args
);
Dispose
(
args
.
ProxyClient
.
TcpClient
,
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStreamReader
,
args
.
Proxy
Client
.
ClientStreamWriter
,
args
);
}
}
finally
finally
{
{
...
@@ -164,12 +164,12 @@ namespace Titanium.Web.Proxy
...
@@ -164,12 +164,12 @@ namespace Titanium.Web.Proxy
if
(
ContentLength
==
-
1
)
if
(
ContentLength
==
-
1
)
ContentLength
=
long
.
MaxValue
;
ContentLength
=
long
.
MaxValue
;
int
bytesToRead
=
Constants
.
BUFFER_SIZE
;
int
bytesToRead
=
Proxy
Constants
.
BUFFER_SIZE
;
if
(
ContentLength
<
Constants
.
BUFFER_SIZE
)
if
(
ContentLength
<
Proxy
Constants
.
BUFFER_SIZE
)
bytesToRead
=
(
int
)
ContentLength
;
bytesToRead
=
(
int
)
ContentLength
;
var
buffer
=
new
byte
[
Constants
.
BUFFER_SIZE
];
var
buffer
=
new
byte
[
Proxy
Constants
.
BUFFER_SIZE
];
var
bytesRead
=
0
;
var
bytesRead
=
0
;
var
totalBytesRead
=
0
;
var
totalBytesRead
=
0
;
...
@@ -184,7 +184,7 @@ namespace Titanium.Web.Proxy
...
@@ -184,7 +184,7 @@ namespace Titanium.Web.Proxy
bytesRead
=
0
;
bytesRead
=
0
;
var
remainingBytes
=
(
ContentLength
-
totalBytesRead
);
var
remainingBytes
=
(
ContentLength
-
totalBytesRead
);
bytesToRead
=
remainingBytes
>
(
long
)
Constants
.
BUFFER_SIZE
?
Constants
.
BUFFER_SIZE
:
(
int
)
remainingBytes
;
bytesToRead
=
remainingBytes
>
(
long
)
ProxyConstants
.
BUFFER_SIZE
?
Proxy
Constants
.
BUFFER_SIZE
:
(
int
)
remainingBytes
;
}
}
}
}
else
else
...
@@ -206,17 +206,17 @@ namespace Titanium.Web.Proxy
...
@@ -206,17 +206,17 @@ namespace Titanium.Web.Proxy
var
chunkHeadBytes
=
Encoding
.
ASCII
.
GetBytes
(
chunkSize
.
ToString
(
"x2"
));
var
chunkHeadBytes
=
Encoding
.
ASCII
.
GetBytes
(
chunkSize
.
ToString
(
"x2"
));
await
outStream
.
WriteAsync
(
chunkHeadBytes
,
0
,
chunkHeadBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
chunkHeadBytes
,
0
,
chunkHeadBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
Constants
.
NewLineBytes
,
0
,
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
Proxy
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
buffer
,
0
,
chunkSize
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
buffer
,
0
,
chunkSize
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
Constants
.
NewLineBytes
,
0
,
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
Proxy
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
inStreamReader
.
ReadLineAsync
().
ConfigureAwait
(
false
);
await
inStreamReader
.
ReadLineAsync
().
ConfigureAwait
(
false
);
}
}
else
else
{
{
await
inStreamReader
.
ReadLineAsync
().
ConfigureAwait
(
false
);
await
inStreamReader
.
ReadLineAsync
().
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
Constants
.
ChunkEnd
,
0
,
Constants
.
ChunkEnd
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
ProxyConstants
.
ChunkEnd
,
0
,
Proxy
Constants
.
ChunkEnd
.
Length
).
ConfigureAwait
(
false
);
break
;
break
;
}
}
}
}
...
@@ -227,11 +227,11 @@ namespace Titanium.Web.Proxy
...
@@ -227,11 +227,11 @@ namespace Titanium.Web.Proxy
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
data
.
Length
.
ToString
(
"x2"
));
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
data
.
Length
.
ToString
(
"x2"
));
await
outStream
.
WriteAsync
(
chunkHead
,
0
,
chunkHead
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
chunkHead
,
0
,
chunkHead
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
Constants
.
NewLineBytes
,
0
,
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
Proxy
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
data
,
0
,
data
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
data
,
0
,
data
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
Constants
.
NewLineBytes
,
0
,
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
Proxy
Constants
.
NewLineBytes
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
Constants
.
ChunkEnd
,
0
,
Constants
.
ChunkEnd
.
Length
).
ConfigureAwait
(
false
);
await
outStream
.
WriteAsync
(
ProxyConstants
.
ChunkEnd
,
0
,
Proxy
Constants
.
ChunkEnd
.
Length
).
ConfigureAwait
(
false
);
}
}
...
...
Titanium.Web.Proxy/Shared/Constants.cs
→
Titanium.Web.Proxy/Shared/
Proxy
Constants.cs
View file @
6c07974f
using
System
;
using
System
;
using
System.Security.Authentication
;
using
System.Security.Authentication
;
using
System.Text
;
using
System.Text
;
using
System.Text.RegularExpressions
;
namespace
Titanium.Web.Proxy.Shared
namespace
Titanium.Web.Proxy.Shared
{
{
/// <summary>
/// <summary>
/// Literals shared by Proxy Server
/// Literals shared by Proxy Server
/// </summary>
/// </summary>
internal
class
Constants
internal
class
ProxyConstants
{
{
internal
static
readonly
int
BUFFER_SIZE
=
8192
;
internal
static
readonly
char
[]
SpaceSplit
=
{
' '
};
internal
static
readonly
char
[]
SpaceSplit
=
{
' '
};
internal
static
readonly
char
[]
ColonSplit
=
{
':'
};
internal
static
readonly
char
[]
ColonSplit
=
{
':'
};
internal
static
readonly
char
[]
SemiColonSplit
=
{
';'
};
internal
static
readonly
char
[]
SemiColonSplit
=
{
';'
};
...
@@ -21,6 +18,8 @@ namespace Titanium.Web.Proxy.Shared
...
@@ -21,6 +18,8 @@ namespace Titanium.Web.Proxy.Shared
internal
static
readonly
byte
[]
ChunkEnd
=
internal
static
readonly
byte
[]
ChunkEnd
=
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
Environment
.
NewLine
+
Environment
.
NewLine
);
internal
static
SslProtocols
SupportedProtocols
=
SslProtocols
.
Tls
|
SslProtocols
.
Tls11
|
SslProtocols
.
Tls12
|
SslProtocols
.
Ssl3
;
public
static
SslProtocols
SupportedSslProtocols
=
SslProtocols
.
Tls
|
SslProtocols
.
Tls11
|
SslProtocols
.
Tls12
|
SslProtocols
.
Ssl3
;
public
static
readonly
int
BUFFER_SIZE
=
8192
;
}
}
}
}
Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
View file @
6c07974f
...
@@ -74,7 +74,6 @@
...
@@ -74,7 +74,6 @@
<Compile
Include=
"Http\Request.cs"
/>
<Compile
Include=
"Http\Request.cs"
/>
<Compile
Include=
"Http\Response.cs"
/>
<Compile
Include=
"Http\Response.cs"
/>
<Compile
Include=
"Models\ExternalProxy.cs"
/>
<Compile
Include=
"Models\ExternalProxy.cs"
/>
<Compile
Include=
"Network\CustomSslStream.cs"
/>
<Compile
Include=
"Network\TcpConnectionManager.cs"
/>
<Compile
Include=
"Network\TcpConnectionManager.cs"
/>
<Compile
Include=
"Models\HttpHeader.cs"
/>
<Compile
Include=
"Models\HttpHeader.cs"
/>
<Compile
Include=
"Http\HttpWebClient.cs"
/>
<Compile
Include=
"Http\HttpWebClient.cs"
/>
...
@@ -88,7 +87,7 @@
...
@@ -88,7 +87,7 @@
<Compile
Include=
"Extensions\StreamExtensions.cs"
/>
<Compile
Include=
"Extensions\StreamExtensions.cs"
/>
<Compile
Include=
"Http\Responses\OkResponse.cs"
/>
<Compile
Include=
"Http\Responses\OkResponse.cs"
/>
<Compile
Include=
"Http\Responses\RedirectResponse.cs"
/>
<Compile
Include=
"Http\Responses\RedirectResponse.cs"
/>
<Compile
Include=
"Shared\Constants.cs"
/>
<Compile
Include=
"Shared\
Proxy
Constants.cs"
/>
</ItemGroup>
</ItemGroup>
<ItemGroup
/>
<ItemGroup
/>
<ItemGroup>
<ItemGroup>
...
...
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