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
44ba131c
Commit
44ba131c
authored
Jul 16, 2016
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor params
parent
3ea9f89a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
68 deletions
+78
-68
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+23
-9
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/TcpConnectionFactory.cs
+32
-40
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+23
-19
No files found.
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
44ba131c
...
@@ -16,21 +16,30 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -16,21 +16,30 @@ namespace Titanium.Web.Proxy.Helpers
internal
class
TcpHelper
internal
class
TcpHelper
{
{
/// <summary>
/// <summary>
/// relays the input clientStream to the server at the specified host name & port with the given httpCmd & headers as prefix
/// relays the input clientStream to the server at the specified host name & port with the given httpCmd & headers as prefix
/// Usefull for websocket requests
/// Usefull for websocket requests
/// </summary>
/// </summary>
/// <param name="clientStream"></param>
/// <param name="bufferSize"></param>
/// <param name="connectionTimeOutSeconds"></param>
/// <param name="remoteHostName"></param>
/// <param name="httpCmd"></param>
/// <param name="httpCmd"></param>
/// <param name="httpVersion"></param>
/// <param name="requestHeaders"></param>
/// <param name="requestHeaders"></param>
/// <param name="hostName"></param>
/// <param name="tunnelPort"></param>
/// <param name="isHttps"></param>
/// <param name="isHttps"></param>
/// <param name="remotePort"></param>
/// <param name="supportedProtocols"></param>
/// <param name="remoteCertificateValidationCallback"></param>
/// <param name="localCertificateSelectionCallback"></param>
/// <param name="clientStream"></param>
/// <param name="tcpConnectionFactory"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
SendRaw
(
TcpConnectionFactory
tcpConnectionFactory
,
Stream
clientStream
,
string
httpCmd
,
Version
version
,
internal
static
async
Task
SendRaw
(
int
bufferSize
,
int
connectionTimeOutSeconds
,
Dictionary
<
string
,
HttpHeader
>
requestHeaders
,
string
hostName
,
int
bufferSize
,
string
remoteHostName
,
int
remotePort
,
string
httpCmd
,
Version
httpVersion
,
Dictionary
<
string
,
HttpHeader
>
requestHeaders
,
int
tunnelPort
,
bool
isHttps
,
SslProtocols
supportedProtocols
,
int
connectionTimeOutSeconds
,
bool
isHttps
,
SslProtocols
supportedProtocols
,
RemoteCertificateValidationCallback
remoteCertificateValidationCallback
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
)
RemoteCertificateValidationCallback
remoteCertificateValidationCallback
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
,
Stream
clientStream
,
TcpConnectionFactory
tcpConnectionFactory
)
{
{
//prepare the prefix content
//prepare the prefix content
StringBuilder
sb
=
null
;
StringBuilder
sb
=
null
;
...
@@ -56,8 +65,13 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -56,8 +65,13 @@ namespace Titanium.Web.Proxy.Helpers
sb
.
Append
(
Environment
.
NewLine
);
sb
.
Append
(
Environment
.
NewLine
);
}
}
var
tcpConnection
=
await
tcpConnectionFactory
.
GetClient
(
hostName
,
tunnelPort
,
isHttps
,
version
,
null
,
null
,
bufferSize
,
supportedProtocols
,
connectionTimeOutSeconds
,
remoteCertificateValidationCallback
,
localCertificateSelectionCallback
);
var
tcpConnection
=
await
tcpConnectionFactory
.
CreateClient
(
bufferSize
,
connectionTimeOutSeconds
,
remoteHostName
,
remotePort
,
httpVersion
,
isHttps
,
supportedProtocols
,
remoteCertificateValidationCallback
,
localCertificateSelectionCallback
,
null
,
null
,
clientStream
);
TcpClient
tunnelClient
=
tcpConnection
.
TcpClient
;
TcpClient
tunnelClient
=
tcpConnection
.
TcpClient
;
Stream
tunnelStream
=
tcpConnection
.
Stream
;
Stream
tunnelStream
=
tcpConnection
.
Stream
;
...
...
Titanium.Web.Proxy/Network/TcpConnectionFactory.cs
View file @
44ba131c
...
@@ -17,36 +17,28 @@ namespace Titanium.Web.Proxy.Network
...
@@ -17,36 +17,28 @@ namespace Titanium.Web.Proxy.Network
{
{
/// <summary>
/// <summary>
///
Get a TcpConnection to the specified host, port optionally HTTPS and a particular HTTP version
///
Creates a TCP connection to server
/// </summary>
/// </summary>
/// <param name="hostname"></param>
/// <param name="bufferSize"></param>
/// <param name="port"></param>
/// <param name="connectionTimeOutSeconds"></param>
/// <param name="remoteHostName"></param>
/// <param name="httpCmd"></param>
/// <param name="httpVersion"></param>
/// <param name="isHttps"></param>
/// <param name="isHttps"></param>
/// <param name="version"></param>
/// <param name="remotePort"></param>
/// <param name="supportedSslProtocols"></param>
/// <param name="remoteCertificateValidationCallback"></param>
/// <param name="localCertificateSelectionCallback"></param>
/// <param name="externalHttpProxy"></param>
/// <param name="externalHttpsProxy"></param>
/// <param name="clientStream"></param>
/// <returns></returns>
/// <returns></returns>
internal
async
Task
<
TcpConnection
>
GetClient
(
string
hostname
,
int
port
,
bool
isHttps
,
Version
version
,
internal
async
Task
<
TcpConnection
>
CreateClient
(
int
bufferSize
,
int
connectionTimeOutSeconds
,
ExternalProxy
upStreamHttpProxy
,
ExternalProxy
upStreamHttpsProxy
,
int
bufferSize
,
SslProtocols
supportedSslProtocols
,
string
remoteHostName
,
int
remotePort
,
Version
httpVersion
,
int
connectionTimeOutSeconds
,
bool
isHttps
,
SslProtocols
supportedSslProtocols
,
RemoteCertificateValidationCallback
remoteCertificateValidationCallBack
,
RemoteCertificateValidationCallback
remoteCertificateValidationCallback
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
)
ExternalProxy
externalHttpProxy
,
ExternalProxy
externalHttpsProxy
,
{
Stream
clientStream
)
//not in cache so create and return
return
await
CreateClient
(
hostname
,
port
,
isHttps
,
version
,
connectionTimeOutSeconds
,
upStreamHttpProxy
,
upStreamHttpsProxy
,
bufferSize
,
supportedSslProtocols
,
remoteCertificateValidationCallBack
,
localCertificateSelectionCallback
);
}
/// <summary>
/// Create connection to a particular host/port optionally with SSL and a particular HTTP version
/// </summary>
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <param name="isHttps"></param>
/// <param name="version"></param>
/// <returns></returns>
private
async
Task
<
TcpConnection
>
CreateClient
(
string
hostname
,
int
port
,
bool
isHttps
,
Version
version
,
int
connectionTimeOutSeconds
,
ExternalProxy
upStreamHttpProxy
,
ExternalProxy
upStreamHttpsProxy
,
int
bufferSize
,
SslProtocols
supportedSslProtocols
,
RemoteCertificateValidationCallback
remoteCertificateValidationCallBack
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
)
{
{
TcpClient
client
;
TcpClient
client
;
Stream
stream
;
Stream
stream
;
...
@@ -56,15 +48,15 @@ namespace Titanium.Web.Proxy.Network
...
@@ -56,15 +48,15 @@ namespace Titanium.Web.Proxy.Network
SslStream
sslStream
=
null
;
SslStream
sslStream
=
null
;
//If this proxy uses another external proxy then create a tunnel request for HTTPS connections
//If this proxy uses another external proxy then create a tunnel request for HTTPS connections
if
(
upStream
HttpsProxy
!=
null
)
if
(
external
HttpsProxy
!=
null
)
{
{
client
=
new
TcpClient
(
upStreamHttpsProxy
.
HostName
,
upStream
HttpsProxy
.
Port
);
client
=
new
TcpClient
(
externalHttpsProxy
.
HostName
,
external
HttpsProxy
.
Port
);
stream
=
client
.
GetStream
();
stream
=
client
.
GetStream
();
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
bufferSize
,
true
))
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
bufferSize
,
true
))
{
{
await
writer
.
WriteLineAsync
(
string
.
Format
(
"CONNECT {0}:{1} {2}"
,
hostname
,
port
,
v
ersion
));
await
writer
.
WriteLineAsync
(
string
.
Format
(
"CONNECT {0}:{1} {2}"
,
remoteHostName
,
remotePort
,
httpV
ersion
));
await
writer
.
WriteLineAsync
(
string
.
Format
(
"Host: {0}:{1}"
,
hostname
,
p
ort
));
await
writer
.
WriteLineAsync
(
string
.
Format
(
"Host: {0}:{1}"
,
remoteHostName
,
remoteP
ort
));
await
writer
.
WriteLineAsync
(
"Connection: Keep-Alive"
);
await
writer
.
WriteLineAsync
(
"Connection: Keep-Alive"
);
await
writer
.
WriteLineAsync
();
await
writer
.
WriteLineAsync
();
await
writer
.
FlushAsync
();
await
writer
.
FlushAsync
();
...
@@ -85,16 +77,16 @@ namespace Titanium.Web.Proxy.Network
...
@@ -85,16 +77,16 @@ namespace Titanium.Web.Proxy.Network
}
}
else
else
{
{
client
=
new
TcpClient
(
hostname
,
p
ort
);
client
=
new
TcpClient
(
remoteHostName
,
remoteP
ort
);
stream
=
client
.
GetStream
();
stream
=
client
.
GetStream
();
}
}
try
try
{
{
sslStream
=
new
SslStream
(
stream
,
true
,
remoteCertificateValidationCall
B
ack
,
sslStream
=
new
SslStream
(
stream
,
true
,
remoteCertificateValidationCall
b
ack
,
localCertificateSelectionCallback
);
localCertificateSelectionCallback
);
await
sslStream
.
AuthenticateAsClientAsync
(
hostn
ame
,
null
,
supportedSslProtocols
,
false
);
await
sslStream
.
AuthenticateAsClientAsync
(
remoteHostN
ame
,
null
,
supportedSslProtocols
,
false
);
stream
=
sslStream
;
stream
=
sslStream
;
}
}
...
@@ -110,14 +102,14 @@ namespace Titanium.Web.Proxy.Network
...
@@ -110,14 +102,14 @@ namespace Titanium.Web.Proxy.Network
}
}
else
else
{
{
if
(
upStream
HttpProxy
!=
null
)
if
(
external
HttpProxy
!=
null
)
{
{
client
=
new
TcpClient
(
upStreamHttpProxy
.
HostName
,
upStream
HttpProxy
.
Port
);
client
=
new
TcpClient
(
externalHttpProxy
.
HostName
,
external
HttpProxy
.
Port
);
stream
=
client
.
GetStream
();
stream
=
client
.
GetStream
();
}
}
else
else
{
{
client
=
new
TcpClient
(
hostname
,
p
ort
);
client
=
new
TcpClient
(
remoteHostName
,
remoteP
ort
);
stream
=
client
.
GetStream
();
stream
=
client
.
GetStream
();
}
}
}
}
...
@@ -130,13 +122,13 @@ namespace Titanium.Web.Proxy.Network
...
@@ -130,13 +122,13 @@ namespace Titanium.Web.Proxy.Network
return
new
TcpConnection
()
return
new
TcpConnection
()
{
{
HostName
=
hostn
ame
,
HostName
=
remoteHostN
ame
,
port
=
p
ort
,
port
=
remoteP
ort
,
IsHttps
=
isHttps
,
IsHttps
=
isHttps
,
TcpClient
=
client
,
TcpClient
=
client
,
StreamReader
=
new
CustomBinaryReader
(
stream
),
StreamReader
=
new
CustomBinaryReader
(
stream
),
Stream
=
stream
,
Stream
=
stream
,
Version
=
v
ersion
Version
=
httpV
ersion
};
};
}
}
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
44ba131c
...
@@ -125,11 +125,13 @@ namespace Titanium.Web.Proxy
...
@@ -125,11 +125,13 @@ namespace Titanium.Web.Proxy
//write back successfull CONNECT response
//write back successfull CONNECT response
await
WriteConnectResponse
(
clientStreamWriter
,
version
);
await
WriteConnectResponse
(
clientStreamWriter
,
version
);
//Just relay the request/response without decrypting it
await
TcpHelper
.
SendRaw
(
tcpConnectionFactory
,
clientStream
,
null
,
version
,
null
,
httpRemoteUri
.
Host
,
BUFFER_SIZE
,
await
TcpHelper
.
SendRaw
(
BUFFER_SIZE
,
ConnectionTimeOutSeconds
,
httpRemoteUri
.
Host
,
httpRemoteUri
.
Port
,
httpRemoteUri
.
Port
,
false
,
SupportedSslProtocols
,
ConnectionTimeOutSeconds
,
httpCmd
,
version
,
null
,
new
RemoteCertificateValidationCallback
(
ValidateServerCertificate
),
false
,
SupportedSslProtocols
,
new
LocalCertificateSelectionCallback
(
SelectClientCertificate
));
new
RemoteCertificateValidationCallback
(
ValidateServerCertificate
),
new
LocalCertificateSelectionCallback
(
SelectClientCertificate
),
clientStream
,
tcpConnectionFactory
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
return
;
return
;
...
@@ -214,7 +216,7 @@ namespace Titanium.Web.Proxy
...
@@ -214,7 +216,7 @@ namespace Titanium.Web.Proxy
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
httpsHostName
)
CustomBinaryReader
clientStreamReader
,
StreamWriter
clientStreamWriter
,
string
httpsHostName
)
{
{
TcpConnection
connection
=
null
;
TcpConnection
connection
=
null
;
string
lastConnectionKey
=
null
;
//Loop through each subsequest request on this particular client connection
//Loop through each subsequest request on this particular client connection
//(assuming HTTP connection is kept alive by client)
//(assuming HTTP connection is kept alive by client)
while
(
true
)
while
(
true
)
...
@@ -236,14 +238,14 @@ namespace Titanium.Web.Proxy
...
@@ -236,14 +238,14 @@ namespace Titanium.Web.Proxy
var
httpMethod
=
httpCmdSplit
[
0
];
var
httpMethod
=
httpCmdSplit
[
0
];
//find the request HTTP version
//find the request HTTP version
Version
v
ersion
=
new
Version
(
1
,
1
);
Version
httpV
ersion
=
new
Version
(
1
,
1
);
if
(
httpCmdSplit
.
Length
==
3
)
if
(
httpCmdSplit
.
Length
==
3
)
{
{
var
httpVersion
=
httpCmdSplit
[
2
].
ToLower
().
Trim
();
var
httpVersion
String
=
httpCmdSplit
[
2
].
ToLower
().
Trim
();
if
(
httpVersion
==
"http/1.0"
)
if
(
httpVersion
String
==
"http/1.0"
)
{
{
v
ersion
=
new
Version
(
1
,
0
);
httpV
ersion
=
new
Version
(
1
,
0
);
}
}
}
}
...
@@ -287,7 +289,7 @@ namespace Titanium.Web.Proxy
...
@@ -287,7 +289,7 @@ namespace Titanium.Web.Proxy
args
.
WebSession
.
Request
.
RequestUri
=
httpRemoteUri
;
args
.
WebSession
.
Request
.
RequestUri
=
httpRemoteUri
;
args
.
WebSession
.
Request
.
Method
=
httpMethod
;
args
.
WebSession
.
Request
.
Method
=
httpMethod
;
args
.
WebSession
.
Request
.
HttpVersion
=
v
ersion
;
args
.
WebSession
.
Request
.
HttpVersion
=
httpV
ersion
;
args
.
ProxyClient
.
ClientStream
=
clientStream
;
args
.
ProxyClient
.
ClientStream
=
clientStream
;
args
.
ProxyClient
.
ClientStreamReader
=
clientStreamReader
;
args
.
ProxyClient
.
ClientStreamReader
=
clientStreamReader
;
args
.
ProxyClient
.
ClientStreamWriter
=
clientStreamWriter
;
args
.
ProxyClient
.
ClientStreamWriter
=
clientStreamWriter
;
...
@@ -312,10 +314,11 @@ namespace Titanium.Web.Proxy
...
@@ -312,10 +314,11 @@ namespace Titanium.Web.Proxy
//if upgrading to websocket then relay the requet without reading the contents
//if upgrading to websocket then relay the requet without reading the contents
if
(
args
.
WebSession
.
Request
.
UpgradeToWebSocket
)
if
(
args
.
WebSession
.
Request
.
UpgradeToWebSocket
)
{
{
await
TcpHelper
.
SendRaw
(
tcpConnectionFactory
,
clientStream
,
httpCmd
,
version
,
args
.
WebSession
.
Request
.
RequestHeaders
,
await
TcpHelper
.
SendRaw
(
BUFFER_SIZE
,
ConnectionTimeOutSeconds
,
httpRemoteUri
.
Host
,
httpRemoteUri
.
Port
,
httpRemoteUri
.
Host
,
BUFFER_SIZE
,
httpRemoteUri
.
Port
,
args
.
IsHttps
,
SupportedSslProtocols
,
ConnectionTimeOutSeconds
,
httpCmd
,
httpVersion
,
args
.
WebSession
.
Request
.
RequestHeaders
,
args
.
IsHttps
,
new
RemoteCertificateValidationCallback
(
ValidateServerCertificate
),
SupportedSslProtocols
,
new
RemoteCertificateValidationCallback
(
ValidateServerCertificate
),
new
LocalCertificateSelectionCallback
(
SelectClientCertificate
));
new
LocalCertificateSelectionCallback
(
SelectClientCertificate
),
clientStream
,
tcpConnectionFactory
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
args
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
args
);
break
;
break
;
...
@@ -324,11 +327,12 @@ namespace Titanium.Web.Proxy
...
@@ -324,11 +327,12 @@ namespace Titanium.Web.Proxy
//construct the web request that we are going to issue on behalf of the client.
//construct the web request that we are going to issue on behalf of the client.
if
(
connection
==
null
)
if
(
connection
==
null
)
{
{
connection
=
await
tcpConnectionFactory
.
GetClient
(
args
.
WebSession
.
Request
.
RequestUri
.
Host
,
connection
=
await
tcpConnectionFactory
.
CreateClient
(
BUFFER_SIZE
,
ConnectionTimeOutSeconds
,
args
.
WebSession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
,
v
ersion
,
args
.
WebSession
.
Request
.
RequestUri
.
Host
,
args
.
WebSession
.
Request
.
RequestUri
.
Port
,
httpV
ersion
,
UpStreamHttpProxy
,
UpStreamHttpsProxy
,
BUFFER_SIZE
,
SupportedSslProtocols
,
ConnectionTimeOutSecond
s
,
args
.
IsHttps
,
SupportedSslProtocol
s
,
new
RemoteCertificateValidationCallback
(
ValidateServerCertificate
),
new
RemoteCertificateValidationCallback
(
ValidateServerCertificate
),
new
LocalCertificateSelectionCallback
(
SelectClientCertificate
));
new
LocalCertificateSelectionCallback
(
SelectClientCertificate
),
UpStreamHttpProxy
,
UpStreamHttpsProxy
,
clientStream
);
}
}
args
.
WebSession
.
Request
.
RequestLocked
=
true
;
args
.
WebSession
.
Request
.
RequestLocked
=
true
;
...
...
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