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
b343ce05
Commit
b343ce05
authored
May 09, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
align lines
parent
d2814921
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
37 deletions
+39
-37
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+17
-16
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+22
-21
No files found.
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
b343ce05
...
@@ -16,7 +16,6 @@ using Titanium.Web.Proxy.Models;
...
@@ -16,7 +16,6 @@ using Titanium.Web.Proxy.Models;
namespace
Titanium.Web.Proxy.Network.Tcp
namespace
Titanium.Web.Proxy.Network.Tcp
{
{
/// <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>
...
@@ -28,21 +27,21 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -28,21 +27,21 @@ namespace Titanium.Web.Proxy.Network.Tcp
//Tcp connections waiting to be disposed by cleanup task
//Tcp connections waiting to be disposed by cleanup task
private
readonly
ConcurrentBag
<
TcpServerConnection
>
disposalBag
=
private
readonly
ConcurrentBag
<
TcpServerConnection
>
disposalBag
=
new
ConcurrentBag
<
TcpServerConnection
>();
new
ConcurrentBag
<
TcpServerConnection
>();
//cache object race operations lock
//cache object race operations lock
private
readonly
SemaphoreSlim
@lock
=
new
SemaphoreSlim
(
1
);
private
readonly
SemaphoreSlim
@lock
=
new
SemaphoreSlim
(
1
);
private
bool
runCleanUpTask
=
true
;
private
bool
runCleanUpTask
=
true
;
internal
ProxyServer
server
{
get
;
set
;
}
internal
TcpConnectionFactory
(
ProxyServer
server
)
internal
TcpConnectionFactory
(
ProxyServer
server
)
{
{
this
.
server
=
server
;
this
.
server
=
server
;
Task
.
Run
(
async
()
=>
await
clearOutdatedConnections
());
Task
.
Run
(
async
()
=>
await
clearOutdatedConnections
());
}
}
internal
ProxyServer
server
{
get
;
set
;
}
/// <summary>
/// <summary>
/// Gets a TCP connection to server from connection pool.
/// Gets a TCP connection to server from connection pool.
/// </summary>
/// </summary>
...
@@ -73,10 +72,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -73,10 +72,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
}
}
}
cacheKeyBuilder
.
Append
(
upStreamEndPoint
!=
null
?
$"
{
upStreamEndPoint
.
Address
}
-
{
upStreamEndPoint
.
Port
}
-"
:
string
.
Empty
);
cacheKeyBuilder
.
Append
(
upStreamEndPoint
!=
null
?
$"
{
upStreamEndPoint
.
Address
}
-
{
upStreamEndPoint
.
Port
}
-"
:
string
.
Empty
);
cacheKeyBuilder
.
Append
(
externalProxy
!=
null
?
$"
{
externalProxy
.
GetCacheKey
()}
-"
:
string
.
Empty
);
cacheKeyBuilder
.
Append
(
externalProxy
!=
null
?
$"
{
externalProxy
.
GetCacheKey
()}
-"
:
string
.
Empty
);
var
cacheKey
=
cacheKeyBuilder
.
ToString
();
string
cacheKey
=
cacheKeyBuilder
.
ToString
();
if
(
proxyServer
.
EnableConnectionPool
)
if
(
proxyServer
.
EnableConnectionPool
)
{
{
...
@@ -95,7 +96,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -95,7 +96,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
disposalBag
.
Add
(
recentConnection
);
disposalBag
.
Add
(
recentConnection
);
}
}
}
}
}
}
...
@@ -126,7 +126,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -126,7 +126,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
ProxyServer
proxyServer
,
IPEndPoint
upStreamEndPoint
,
ExternalProxy
externalProxy
,
ProxyServer
proxyServer
,
IPEndPoint
upStreamEndPoint
,
ExternalProxy
externalProxy
,
CancellationToken
cancellationToken
)
CancellationToken
cancellationToken
)
{
{
bool
useUpstreamProxy
=
false
;
bool
useUpstreamProxy
=
false
;
// check if external proxy is set for HTTP/HTTPS
// check if external proxy is set for HTTP/HTTPS
...
@@ -223,7 +222,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -223,7 +222,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
negotiatedApplicationProtocol
=
sslStream
.
NegotiatedApplicationProtocol
;
negotiatedApplicationProtocol
=
sslStream
.
NegotiatedApplicationProtocol
;
#endif
#endif
}
}
}
}
catch
(
Exception
)
catch
(
Exception
)
{
{
...
@@ -248,7 +246,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -248,7 +246,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
}
/// <summary>
/// <summary>
/// Release connection back to cache.
///
Release connection back to cache.
/// </summary>
/// </summary>
/// <param name="connection">The Tcp server connection to return.</param>
/// <param name="connection">The Tcp server connection to return.</param>
/// <param name="close">Should we just close the connection instead of reusing?</param>
/// <param name="close">Should we just close the connection instead of reusing?</param>
...
@@ -274,9 +272,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -274,9 +272,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
if
(
cache
.
TryAdd
(
connection
.
CacheKey
,
new
ConcurrentQueue
<
TcpServerConnection
>(
new
[]
{
connection
})))
if
(
cache
.
TryAdd
(
connection
.
CacheKey
,
new
ConcurrentQueue
<
TcpServerConnection
>(
new
[]
{
connection
})))
{
{
break
;
break
;
}
;
}
;
}
}
@lock
.
Release
();
@lock
.
Release
();
}
}
...
@@ -295,6 +295,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -295,6 +295,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
disposalBag
.
Add
(
connection
);
disposalBag
.
Add
(
connection
);
continue
;
continue
;
}
}
queue
.
Enqueue
(
connection
);
queue
.
Enqueue
(
connection
);
break
;
break
;
}
}
...
@@ -303,13 +304,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -303,13 +304,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
//clear empty queues
//clear empty queues
await
@lock
.
WaitAsync
();
await
@lock
.
WaitAsync
();
var
emptyKeys
=
cache
.
Where
(
x
=>
x
.
Value
.
Count
==
0
).
Select
(
x
=>
x
.
Key
).
ToList
();
var
emptyKeys
=
cache
.
Where
(
x
=>
x
.
Value
.
Count
==
0
).
Select
(
x
=>
x
.
Key
).
ToList
();
foreach
(
var
key
in
emptyKeys
)
foreach
(
string
key
in
emptyKeys
)
{
{
cache
.
TryRemove
(
key
,
out
var
_
);
cache
.
TryRemove
(
key
,
out
var
_
);
}
}
@lock
.
Release
();
@lock
.
Release
();
while
(
disposalBag
.
TryTake
(
out
TcpServerConnection
connection
))
while
(
disposalBag
.
TryTake
(
out
var
connection
))
{
{
connection
?.
Dispose
();
connection
?.
Dispose
();
}
}
...
@@ -317,12 +319,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -317,12 +319,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
//cleanup every ten seconds by default
//cleanup every ten seconds by default
await
Task
.
Delay
(
1000
*
10
);
await
Task
.
Delay
(
1000
*
10
);
}
}
}
}
/// <summary>
/// <summary>
/// Check if a TcpClient is good to be used.
///
Check if a TcpClient is good to be used.
/// https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected(v=vs.110).aspx
///
https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected(v=vs.110).aspx
/// </summary>
/// </summary>
/// <param name="client"></param>
/// <param name="client"></param>
/// <returns></returns>
/// <returns></returns>
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
b343ce05
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Linq
;
using
System.Net
;
using
System.Net
;
using
System.Text
;
using
System.Text.RegularExpressions
;
using
System.Text.RegularExpressions
;
using
System.Threading
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
...
@@ -26,11 +24,12 @@ namespace Titanium.Web.Proxy
...
@@ -26,11 +24,12 @@ namespace Titanium.Web.Proxy
private
static
readonly
Regex
uriSchemeRegex
=
private
static
readonly
Regex
uriSchemeRegex
=
new
Regex
(
"^[a-z]*://"
,
RegexOptions
.
IgnoreCase
|
RegexOptions
.
Compiled
);
new
Regex
(
"^[a-z]*://"
,
RegexOptions
.
IgnoreCase
|
RegexOptions
.
Compiled
);
private
static
readonly
HashSet
<
string
>
proxySupportedCompressions
=
new
HashSet
<
string
>(
StringComparer
.
OrdinalIgnoreCase
)
private
static
readonly
HashSet
<
string
>
proxySupportedCompressions
=
{
new
HashSet
<
string
>(
StringComparer
.
OrdinalIgnoreCase
)
"gzip"
,
{
"deflate"
"gzip"
,
};
"deflate"
};
private
bool
isWindowsAuthenticationEnabledAndSupported
=>
private
bool
isWindowsAuthenticationEnabledAndSupported
=>
EnableWinAuth
&&
RunTime
.
IsWindows
&&
!
RunTime
.
IsRunningOnMono
;
EnableWinAuth
&&
RunTime
.
IsWindows
&&
!
RunTime
.
IsRunningOnMono
;
...
@@ -178,6 +177,7 @@ namespace Titanium.Web.Proxy
...
@@ -178,6 +177,7 @@ namespace Titanium.Web.Proxy
continue
;
continue
;
}
}
bool
connectionChanged
=
false
;
// create a new connection if hostname/upstream end point changes
// create a new connection if hostname/upstream end point changes
if
(
serverConnection
!=
null
if
(
serverConnection
!=
null
&&
(!
serverConnection
.
HostName
.
EqualsIgnoreCase
(
request
.
RequestUri
.
Host
)
&&
(!
serverConnection
.
HostName
.
EqualsIgnoreCase
(
request
.
RequestUri
.
Host
)
...
@@ -186,17 +186,18 @@ namespace Titanium.Web.Proxy
...
@@ -186,17 +186,18 @@ namespace Titanium.Web.Proxy
{
{
tcpConnectionFactory
.
Release
(
serverConnection
,
true
);
tcpConnectionFactory
.
Release
(
serverConnection
,
true
);
serverConnection
=
null
;
serverConnection
=
null
;
connectionChanged
=
true
;
}
}
var
connectionChanged
=
false
;
if
(
serverConnection
==
null
)
if
(
serverConnection
==
null
)
{
{
serverConnection
=
await
getServerConnection
(
args
,
false
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
serverConnection
=
await
getServerConnection
(
args
,
false
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
connectionChanged
=
true
;
connectionChanged
=
true
;
}
}
//for connection pool retry attempt until we get a good connection
//for connection pool retry attempt until we get a good connection
var
attemt
=
0
;
int
attemt
=
0
;
while
(
attemt
<
3
)
while
(
attemt
<
3
)
{
{
try
try
...
@@ -223,7 +224,8 @@ namespace Titanium.Web.Proxy
...
@@ -223,7 +224,8 @@ namespace Titanium.Web.Proxy
//get new connection from pool
//get new connection from pool
tcpConnectionFactory
.
Release
(
serverConnection
,
true
);
tcpConnectionFactory
.
Release
(
serverConnection
,
true
);
serverConnection
=
await
getServerConnection
(
args
,
false
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
serverConnection
=
await
getServerConnection
(
args
,
false
,
clientConnection
.
NegotiatedApplicationProtocol
,
cancellationToken
);
attemt
++;
attemt
++;
continue
;
continue
;
}
}
...
@@ -348,7 +350,6 @@ namespace Titanium.Web.Proxy
...
@@ -348,7 +350,6 @@ namespace Titanium.Web.Proxy
{
{
await
handleHttpSessionResponse
(
args
);
await
handleHttpSessionResponse
(
args
);
}
}
}
}
/// <summary>
/// <summary>
...
@@ -380,7 +381,7 @@ namespace Titanium.Web.Proxy
...
@@ -380,7 +381,7 @@ namespace Titanium.Web.Proxy
/// <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
>
getServerConnection
(
SessionEventArgsBase
args
,
bool
isConnect
,
private
async
Task
<
TcpServerConnection
>
getServerConnection
(
SessionEventArgsBase
args
,
bool
isConnect
,
List
<
SslApplicationProtocol
>
applicationProtocols
,
CancellationToken
cancellationToken
)
List
<
SslApplicationProtocol
>
applicationProtocols
,
CancellationToken
cancellationToken
)
{
{
ExternalProxy
customUpStreamProxy
=
null
;
ExternalProxy
customUpStreamProxy
=
null
;
...
@@ -407,7 +408,7 @@ namespace Titanium.Web.Proxy
...
@@ -407,7 +408,7 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
private
void
prepareRequestHeaders
(
HeaderCollection
requestHeaders
)
private
void
prepareRequestHeaders
(
HeaderCollection
requestHeaders
)
{
{
var
acceptEncoding
=
requestHeaders
.
GetHeaderValueOrNull
(
KnownHeaders
.
AcceptEncoding
);
string
acceptEncoding
=
requestHeaders
.
GetHeaderValueOrNull
(
KnownHeaders
.
AcceptEncoding
);
if
(
acceptEncoding
!=
null
)
if
(
acceptEncoding
!=
null
)
{
{
...
@@ -421,22 +422,22 @@ namespace Titanium.Web.Proxy
...
@@ -421,22 +422,22 @@ namespace Titanium.Web.Proxy
//uncompressed is always supported by proxy
//uncompressed is always supported by proxy
supportedAcceptEncoding
.
Add
(
"identity"
);
supportedAcceptEncoding
.
Add
(
"identity"
);
requestHeaders
.
SetOrAddHeaderValue
(
KnownHeaders
.
AcceptEncoding
,
string
.
Join
(
","
,
supportedAcceptEncoding
));
requestHeaders
.
SetOrAddHeaderValue
(
KnownHeaders
.
AcceptEncoding
,
string
.
Join
(
","
,
supportedAcceptEncoding
));
}
}
requestHeaders
.
FixProxyHeaders
();
requestHeaders
.
FixProxyHeaders
();
}
}
/// <summary>
/// <summary>
/// Handle upgrade to websocket
///
Handle upgrade to websocket
/// </summary>
/// </summary>
private
async
Task
handleWebSocketUpgrade
(
string
httpCmd
,
private
async
Task
handleWebSocketUpgrade
(
string
httpCmd
,
SessionEventArgs
args
,
Request
request
,
Response
response
,
SessionEventArgs
args
,
Request
request
,
Response
response
,
CustomBufferedStream
clientStream
,
HttpResponseWriter
clientStreamWriter
,
CustomBufferedStream
clientStream
,
HttpResponseWriter
clientStreamWriter
,
TcpServerConnection
serverConnection
,
TcpServerConnection
serverConnection
,
CancellationTokenSource
cancellationTokenSource
,
CancellationToken
cancellationToken
)
CancellationTokenSource
cancellationTokenSource
,
CancellationToken
cancellationToken
)
{
{
// prepare the prefix content
// prepare the prefix content
await
serverConnection
.
StreamWriter
.
WriteLineAsync
(
httpCmd
,
cancellationToken
);
await
serverConnection
.
StreamWriter
.
WriteLineAsync
(
httpCmd
,
cancellationToken
);
await
serverConnection
.
StreamWriter
.
WriteHeadersAsync
(
request
.
Headers
,
await
serverConnection
.
StreamWriter
.
WriteHeadersAsync
(
request
.
Headers
,
...
...
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