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
6d0ff956
Commit
6d0ff956
authored
May 15, 2017
by
justcoding121
Committed by
justcoding121
May 15, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Server Connection Count; Reduce Params
parent
67a3fb16
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
59 additions
and
64 deletions
+59
-64
CertificateHandler.cs
Titanium.Web.Proxy/CertificateHandler.cs
+2
-2
Tcp.cs
Titanium.Web.Proxy/Helpers/Tcp.cs
+9
-15
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+25
-31
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+9
-0
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+12
-16
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+2
-0
No files found.
Titanium.Web.Proxy/CertificateHandler.cs
View file @
6d0ff956
...
@@ -16,7 +16,7 @@ namespace Titanium.Web.Proxy
...
@@ -16,7 +16,7 @@ namespace Titanium.Web.Proxy
/// <param name="chain"></param>
/// <param name="chain"></param>
/// <param name="sslPolicyErrors"></param>
/// <param name="sslPolicyErrors"></param>
/// <returns></returns>
/// <returns></returns>
private
bool
ValidateServerCertificate
(
internal
bool
ValidateServerCertificate
(
object
sender
,
object
sender
,
X509Certificate
certificate
,
X509Certificate
certificate
,
X509Chain
chain
,
X509Chain
chain
,
...
@@ -65,7 +65,7 @@ namespace Titanium.Web.Proxy
...
@@ -65,7 +65,7 @@ namespace Titanium.Web.Proxy
/// <param name="remoteCertificate"></param>
/// <param name="remoteCertificate"></param>
/// <param name="acceptableIssuers"></param>
/// <param name="acceptableIssuers"></param>
/// <returns></returns>
/// <returns></returns>
private
X509Certificate
SelectClientCertificate
(
internal
X509Certificate
SelectClientCertificate
(
object
sender
,
object
sender
,
string
targetHost
,
string
targetHost
,
X509CertificateCollection
localCertificates
,
X509CertificateCollection
localCertificates
,
...
...
Titanium.Web.Proxy/Helpers/Tcp.cs
View file @
6d0ff956
...
@@ -127,26 +127,21 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -127,26 +127,21 @@ namespace Titanium.Web.Proxy.Helpers
/// relays the input clientStream to the server at the specified host name and port with the given httpCmd and headers as prefix
/// relays the input clientStream to the server at the specified host name and port with the given httpCmd and headers as prefix
/// Usefull for websocket requests
/// Usefull for websocket requests
/// </summary>
/// </summary>
/// <param name="bufferSize"></param>
/// <param name="server"></param>
/// <param name="connectionTimeOutSeconds"></param>
/// <param name="remoteHostName"></param>
/// <param name="remoteHostName"></param>
/// <param name="remotePort"></param>
/// <param name="httpCmd"></param>
/// <param name="httpCmd"></param>
/// <param name="httpVersion"></param>
/// <param name="httpVersion"></param>
/// <param name="requestHeaders"></param>
/// <param name="requestHeaders"></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="clientStream"></param>
/// <param name="tcpConnectionFactory"></param>
/// <param name="tcpConnectionFactory"></param>
/// <param name="upStreamEndPoint"></param>
/// <returns></returns>
/// <returns></returns>
internal
static
async
Task
SendRaw
(
int
bufferSize
,
int
connectionTimeOutSeconds
,
internal
static
async
Task
SendRaw
(
ProxyServer
server
,
string
remoteHostName
,
int
remotePort
,
string
httpCmd
,
Version
httpVersion
,
Dictionary
<
string
,
HttpHeader
>
requestHeaders
,
string
remoteHostName
,
int
remotePort
,
bool
isHttps
,
SslProtocols
supportedProtocol
s
,
string
httpCmd
,
Version
httpVersion
,
Dictionary
<
string
,
HttpHeader
>
requestHeader
s
,
RemoteCertificateValidationCallback
remoteCertificateValidationCallback
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
,
bool
isHttps
,
Stream
clientStream
,
TcpConnectionFactory
tcpConnectionFactory
,
IPEndPoint
upStreamEndPoint
)
Stream
clientStream
,
TcpConnectionFactory
tcpConnectionFactory
)
{
{
//prepare the prefix content
//prepare the prefix content
StringBuilder
sb
=
null
;
StringBuilder
sb
=
null
;
...
@@ -172,11 +167,10 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -172,11 +167,10 @@ namespace Titanium.Web.Proxy.Helpers
sb
.
Append
(
ProxyConstants
.
NewLine
);
sb
.
Append
(
ProxyConstants
.
NewLine
);
}
}
var
tcpConnection
=
await
tcpConnectionFactory
.
CreateClient
(
bufferSize
,
connectionTimeOutSeconds
,
var
tcpConnection
=
await
tcpConnectionFactory
.
CreateClient
(
server
,
remoteHostName
,
remotePort
,
remoteHostName
,
remotePort
,
httpVersion
,
isHttps
,
httpVersion
,
isHttps
,
supportedProtocols
,
remoteCertificateValidationCallback
,
localCertificateSelectionCallback
,
null
,
null
,
clientStream
);
null
,
null
,
clientStream
,
upStreamEndPoint
);
try
try
{
{
...
...
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
6d0ff956
...
@@ -6,43 +6,35 @@ using System.IO;
...
@@ -6,43 +6,35 @@ using System.IO;
using
System.Net.Security
;
using
System.Net.Security
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
System.Security.Authentication
;
using
System.Linq
;
using
System.Linq
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Shared
;
using
Titanium.Web.Proxy.Shared
;
namespace
Titanium.Web.Proxy.Network.Tcp
namespace
Titanium.Web.Proxy.Network.Tcp
{
{
using
System.Net
;
/// <summary>
/// <summary>
/// A class that manages Tcp Connection to server used by this proxy server
/// A class that manages Tcp Connection to server used by this proxy server
/// </summary>
/// </summary>
internal
class
TcpConnectionFactory
internal
class
TcpConnectionFactory
{
{
/// <summary>
/// <summary>
/// Creates a TCP connection to server
/// Creates a TCP connection to server
/// </summary>
/// </summary>
/// <param name="bufferSize"></param>
/// <param name="server"></param>
/// <param name="connectionTimeOutSeconds"></param>
/// <param name="remoteHostName"></param>
/// <param name="remoteHostName"></param>
/// <param name="remotePort"></param>
/// <param name="httpVersion"></param>
/// <param name="httpVersion"></param>
/// <param name="isHttps"></param>
/// <param name="isHttps"></param>
/// <param name="remotePort"></param>
/// <param name="supportedSslProtocols"></param>
/// <param name="remoteCertificateValidationCallback"></param>
/// <param name="localCertificateSelectionCallback"></param>
/// <param name="externalHttpProxy"></param>
/// <param name="externalHttpProxy"></param>
/// <param name="externalHttpsProxy"></param>
/// <param name="externalHttpsProxy"></param>
/// <param name="clientStream"></param>
/// <param name="clientStream"></param>
/// <param name="upStreamEndPoint"></param>
/// <returns></returns>
/// <returns></returns>
internal
async
Task
<
TcpConnection
>
CreateClient
(
int
bufferSize
,
int
connectionTimeOutSeconds
,
internal
async
Task
<
TcpConnection
>
CreateClient
(
ProxyServer
server
,
string
remoteHostName
,
int
remotePort
,
Version
httpVersion
,
string
remoteHostName
,
int
remotePort
,
Version
httpVersion
,
bool
isHttps
,
SslProtocols
supportedSslProtocols
,
bool
isHttps
,
RemoteCertificateValidationCallback
remoteCertificateValidationCallback
,
LocalCertificateSelectionCallback
localCertificateSelectionCallback
,
ExternalProxy
externalHttpProxy
,
ExternalProxy
externalHttpsProxy
,
ExternalProxy
externalHttpProxy
,
ExternalProxy
externalHttpsProxy
,
Stream
clientStream
,
IPEndPoint
upStreamEndPoint
)
Stream
clientStream
)
{
{
TcpClient
client
;
TcpClient
client
;
CustomBufferedStream
stream
;
CustomBufferedStream
stream
;
...
@@ -59,11 +51,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -59,11 +51,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
//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
(
useHttpsProxy
)
if
(
useHttpsProxy
)
{
{
client
=
new
TcpClient
(
u
pStreamEndPoint
);
client
=
new
TcpClient
(
server
.
U
pStreamEndPoint
);
await
client
.
ConnectAsync
(
externalHttpsProxy
.
HostName
,
externalHttpsProxy
.
Port
);
await
client
.
ConnectAsync
(
externalHttpsProxy
.
HostName
,
externalHttpsProxy
.
Port
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
b
ufferSize
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
server
.
B
ufferSize
);
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
b
ufferSize
,
true
)
{
NewLine
=
ProxyConstants
.
NewLine
})
using
(
var
writer
=
new
StreamWriter
(
stream
,
Encoding
.
ASCII
,
server
.
B
ufferSize
,
true
)
{
NewLine
=
ProxyConstants
.
NewLine
})
{
{
await
writer
.
WriteLineAsync
(
$"CONNECT
{
remoteHostName
}
:
{
remotePort
}
HTTP/
{
httpVersion
}
"
);
await
writer
.
WriteLineAsync
(
$"CONNECT
{
remoteHostName
}
:
{
remotePort
}
HTTP/
{
httpVersion
}
"
);
await
writer
.
WriteLineAsync
(
$"Host:
{
remoteHostName
}
:
{
remotePort
}
"
);
await
writer
.
WriteLineAsync
(
$"Host:
{
remoteHostName
}
:
{
remotePort
}
"
);
...
@@ -79,7 +71,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -79,7 +71,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
writer
.
Close
();
writer
.
Close
();
}
}
using
(
var
reader
=
new
CustomBinaryReader
(
stream
,
b
ufferSize
))
using
(
var
reader
=
new
CustomBinaryReader
(
stream
,
server
.
B
ufferSize
))
{
{
var
result
=
await
reader
.
ReadLineAsync
();
var
result
=
await
reader
.
ReadLineAsync
();
...
@@ -93,19 +85,19 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -93,19 +85,19 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
}
else
else
{
{
client
=
new
TcpClient
(
u
pStreamEndPoint
);
client
=
new
TcpClient
(
server
.
U
pStreamEndPoint
);
await
client
.
ConnectAsync
(
remoteHostName
,
remotePort
);
await
client
.
ConnectAsync
(
remoteHostName
,
remotePort
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
b
ufferSize
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
server
.
B
ufferSize
);
}
}
try
try
{
{
sslStream
=
new
SslStream
(
stream
,
true
,
remoteCertificateValidationCallback
,
sslStream
=
new
SslStream
(
stream
,
true
,
server
.
ValidateServerCertificate
,
localCertificateSelectionCallback
);
server
.
SelectClientCertificate
);
await
sslStream
.
AuthenticateAsClientAsync
(
remoteHostName
,
null
,
supportedSslProtocols
,
false
);
await
sslStream
.
AuthenticateAsClientAsync
(
remoteHostName
,
null
,
s
erver
.
S
upportedSslProtocols
,
false
);
stream
=
new
CustomBufferedStream
(
sslStream
,
b
ufferSize
);
stream
=
new
CustomBufferedStream
(
sslStream
,
server
.
B
ufferSize
);
}
}
catch
catch
{
{
...
@@ -118,23 +110,25 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -118,23 +110,25 @@ namespace Titanium.Web.Proxy.Network.Tcp
{
{
if
(
useHttpProxy
)
if
(
useHttpProxy
)
{
{
client
=
new
TcpClient
(
u
pStreamEndPoint
);
client
=
new
TcpClient
(
server
.
U
pStreamEndPoint
);
await
client
.
ConnectAsync
(
externalHttpProxy
.
HostName
,
externalHttpProxy
.
Port
);
await
client
.
ConnectAsync
(
externalHttpProxy
.
HostName
,
externalHttpProxy
.
Port
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
b
ufferSize
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
server
.
B
ufferSize
);
}
}
else
else
{
{
client
=
new
TcpClient
(
u
pStreamEndPoint
);
client
=
new
TcpClient
(
server
.
U
pStreamEndPoint
);
await
client
.
ConnectAsync
(
remoteHostName
,
remotePort
);
await
client
.
ConnectAsync
(
remoteHostName
,
remotePort
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
b
ufferSize
);
stream
=
new
CustomBufferedStream
(
client
.
GetStream
(),
server
.
B
ufferSize
);
}
}
}
}
client
.
ReceiveTimeout
=
c
onnectionTimeOutSeconds
*
1000
;
client
.
ReceiveTimeout
=
server
.
C
onnectionTimeOutSeconds
*
1000
;
client
.
SendTimeout
=
c
onnectionTimeOutSeconds
*
1000
;
client
.
SendTimeout
=
server
.
C
onnectionTimeOutSeconds
*
1000
;
client
.
LingerState
=
new
LingerOption
(
true
,
0
);
client
.
LingerState
=
new
LingerOption
(
true
,
0
);
server
.
ServerConnectionCount
++;
return
new
TcpConnection
return
new
TcpConnection
{
{
UpStreamHttpProxy
=
externalHttpProxy
,
UpStreamHttpProxy
=
externalHttpProxy
,
...
@@ -143,7 +137,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -143,7 +137,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
Port
=
remotePort
,
Port
=
remotePort
,
IsHttps
=
isHttps
,
IsHttps
=
isHttps
,
TcpClient
=
client
,
TcpClient
=
client
,
StreamReader
=
new
CustomBinaryReader
(
stream
,
b
ufferSize
),
StreamReader
=
new
CustomBinaryReader
(
stream
,
server
.
B
ufferSize
),
Stream
=
stream
,
Stream
=
stream
,
Version
=
httpVersion
Version
=
httpVersion
};
};
...
...
Titanium.Web.Proxy/ProxyServer.cs
View file @
6d0ff956
...
@@ -226,8 +226,17 @@ namespace Titanium.Web.Proxy
...
@@ -226,8 +226,17 @@ namespace Titanium.Web.Proxy
public
SslProtocols
SupportedSslProtocols
{
get
;
set
;
}
=
SslProtocols
.
Tls
public
SslProtocols
SupportedSslProtocols
{
get
;
set
;
}
=
SslProtocols
.
Tls
|
SslProtocols
.
Tls11
|
SslProtocols
.
Tls12
|
SslProtocols
.
Ssl3
;
|
SslProtocols
.
Tls11
|
SslProtocols
.
Tls12
|
SslProtocols
.
Ssl3
;
/// <summary>
/// Total number of active client connections
/// </summary>
public
int
ClientConnectionCount
{
get
;
private
set
;
}
public
int
ClientConnectionCount
{
get
;
private
set
;
}
/// <summary>
/// Total number of active server connections
/// </summary>
public
int
ServerConnectionCount
{
get
;
internal
set
;
}
/// <summary>
/// <summary>
/// Constructor
/// Constructor
/// </summary>
/// </summary>
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
6d0ff956
...
@@ -141,12 +141,11 @@ namespace Titanium.Web.Proxy
...
@@ -141,12 +141,11 @@ namespace Titanium.Web.Proxy
//write back successfull CONNECT response
//write back successfull CONNECT response
await
WriteConnectResponse
(
clientStreamWriter
,
version
);
await
WriteConnectResponse
(
clientStreamWriter
,
version
);
await
TcpHelper
.
SendRaw
(
BufferSize
,
ConnectionTimeOutSeconds
,
httpRemoteUri
.
Host
,
httpRemoteUri
.
Port
,
await
TcpHelper
.
SendRaw
(
this
,
httpRemoteUri
.
Host
,
httpRemoteUri
.
Port
,
null
,
version
,
null
,
null
,
version
,
null
,
false
,
SupportedSslProtocols
,
false
,
ValidateServerCertificate
,
clientStream
,
tcpConnectionFactory
);
SelectClientCertificate
,
clientStream
,
tcpConnectionFactory
,
UpStreamEndPoint
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamWriter
,
null
);
return
;
return
;
...
@@ -243,15 +242,13 @@ namespace Titanium.Web.Proxy
...
@@ -243,15 +242,13 @@ namespace Titanium.Web.Proxy
args
.
CustomUpStreamHttpProxyUsed
=
customUpStreamHttpProxy
;
args
.
CustomUpStreamHttpProxyUsed
=
customUpStreamHttpProxy
;
args
.
CustomUpStreamHttpsProxyUsed
=
customUpStreamHttpsProxy
;
args
.
CustomUpStreamHttpsProxyUsed
=
customUpStreamHttpsProxy
;
connection
=
await
tcpConnectionFactory
.
CreateClient
(
BufferSize
,
ConnectionTimeOutSeconds
,
connection
=
await
tcpConnectionFactory
.
CreateClient
(
this
,
args
.
WebSession
.
Request
.
RequestUri
.
Host
,
args
.
WebSession
.
Request
.
RequestUri
.
Port
,
args
.
WebSession
.
Request
.
HttpVersion
,
args
.
WebSession
.
Request
.
RequestUri
.
Host
,
args
.
IsHttps
,
SupportedSslProtocols
,
args
.
WebSession
.
Request
.
RequestUri
.
Port
,
args
.
WebSession
.
Request
.
HttpVersion
,
ValidateServerCertificate
,
args
.
IsHttps
,
SelectClientCertificate
,
customUpStreamHttpProxy
??
UpStreamHttpProxy
,
customUpStreamHttpProxy
??
UpStreamHttpProxy
,
customUpStreamHttpsProxy
??
UpStreamHttpsProxy
,
customUpStreamHttpsProxy
??
UpStreamHttpsProxy
,
args
.
ProxyClient
.
ClientStream
,
args
.
ProxyClient
.
ClientStream
);
UpStreamEndPoint
);
}
}
args
.
WebSession
.
Request
.
RequestLocked
=
true
;
args
.
WebSession
.
Request
.
RequestLocked
=
true
;
...
@@ -519,11 +516,10 @@ namespace Titanium.Web.Proxy
...
@@ -519,11 +516,10 @@ 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
(
BufferSize
,
ConnectionTimeOutSeconds
,
httpRemoteUri
.
Host
,
httpRemoteUri
.
Port
,
await
TcpHelper
.
SendRaw
(
this
,
httpRemoteUri
.
Host
,
httpRemoteUri
.
Port
,
httpCmd
,
httpVersion
,
args
.
WebSession
.
Request
.
RequestHeaders
,
args
.
IsHttps
,
httpCmd
,
httpVersion
,
args
.
WebSession
.
Request
.
RequestHeaders
,
args
.
IsHttps
,
SupportedSslProtocols
,
ValidateServerCertificate
,
clientStream
,
tcpConnectionFactory
);
SelectClientCertificate
,
clientStream
,
tcpConnectionFactory
,
UpStreamEndPoint
);
Dispose
(
clientStream
,
Dispose
(
clientStream
,
clientStreamReader
,
clientStreamReader
,
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
6d0ff956
...
@@ -234,6 +234,8 @@ namespace Titanium.Web.Proxy
...
@@ -234,6 +234,8 @@ namespace Titanium.Web.Proxy
StreamWriter
clientStreamWriter
,
StreamWriter
clientStreamWriter
,
TcpConnection
serverConnection
)
TcpConnection
serverConnection
)
{
{
ServerConnectionCount
--;
clientStream
?.
Close
();
clientStream
?.
Close
();
clientStream
?.
Dispose
();
clientStream
?.
Dispose
();
...
...
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