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