Commit a68e4eda authored by justcoding121's avatar justcoding121

align lines

parent ecff9ef1
......@@ -16,7 +16,6 @@ using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Network.Tcp
{
/// <summary>
/// A class that manages Tcp Connection to server used by this proxy server.
/// </summary>
......@@ -28,21 +27,21 @@ namespace Titanium.Web.Proxy.Network.Tcp
//Tcp connections waiting to be disposed by cleanup task
private readonly ConcurrentBag<TcpServerConnection> disposalBag =
new ConcurrentBag<TcpServerConnection>();
new ConcurrentBag<TcpServerConnection>();
//cache object race operations lock
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1);
private bool runCleanUpTask = true;
internal ProxyServer server { get; set; }
internal TcpConnectionFactory(ProxyServer server)
{
this.server = server;
Task.Run(async () => await clearOutdatedConnections());
}
internal ProxyServer server { get; set; }
/// <summary>
/// Gets a TCP connection to server from connection pool.
/// </summary>
......@@ -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);
var cacheKey = cacheKeyBuilder.ToString();
string cacheKey = cacheKeyBuilder.ToString();
if (proxyServer.EnableConnectionPool)
{
......@@ -95,7 +96,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
disposalBag.Add(recentConnection);
}
}
}
......@@ -126,7 +126,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
CancellationToken cancellationToken)
{
bool useUpstreamProxy = false;
// check if external proxy is set for HTTP/HTTPS
......@@ -223,7 +222,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
negotiatedApplicationProtocol = sslStream.NegotiatedApplicationProtocol;
#endif
}
}
catch (Exception)
{
......@@ -248,7 +246,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
/// <summary>
/// Release connection back to cache.
/// Release connection back to cache.
/// </summary>
/// <param name="connection">The Tcp server connection to return.</param>
/// <param name="close">Should we just close the connection instead of reusing?</param>
......@@ -274,9 +272,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
if (cache.TryAdd(connection.CacheKey, new ConcurrentQueue<TcpServerConnection>(new[] { connection })))
{
break;
};
}
;
}
@lock.Release();
}
......@@ -295,6 +295,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
disposalBag.Add(connection);
continue;
}
queue.Enqueue(connection);
break;
}
......@@ -303,13 +304,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
//clear empty queues
await @lock.WaitAsync();
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 _);
}
@lock.Release();
while (disposalBag.TryTake(out TcpServerConnection connection))
while (disposalBag.TryTake(out var connection))
{
connection?.Dispose();
}
......@@ -317,12 +319,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
//cleanup every ten seconds by default
await Task.Delay(1000 * 10);
}
}
/// <summary>
/// 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
/// 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
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
......@@ -26,11 +24,12 @@ namespace Titanium.Web.Proxy
private static readonly Regex uriSchemeRegex =
new Regex("^[a-z]*://", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly HashSet<string> proxySupportedCompressions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"gzip",
"deflate"
};
private static readonly HashSet<string> proxySupportedCompressions =
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"gzip",
"deflate"
};
private bool isWindowsAuthenticationEnabledAndSupported =>
EnableWinAuth && RunTime.IsWindows && !RunTime.IsRunningOnMono;
......@@ -178,6 +177,7 @@ namespace Titanium.Web.Proxy
continue;
}
bool connectionChanged = false;
// create a new connection if hostname/upstream end point changes
if (serverConnection != null
&& (!serverConnection.HostName.EqualsIgnoreCase(request.RequestUri.Host)
......@@ -186,17 +186,18 @@ namespace Titanium.Web.Proxy
{
tcpConnectionFactory.Release(serverConnection, true);
serverConnection = null;
connectionChanged = true;
}
var connectionChanged = false;
if (serverConnection == null)
{
serverConnection = await getServerConnection(args, false, clientConnection.NegotiatedApplicationProtocol, cancellationToken);
serverConnection = await getServerConnection(args, false,
clientConnection.NegotiatedApplicationProtocol, cancellationToken);
connectionChanged = true;
}
//for connection pool retry attempt until we get a good connection
var attemt = 0;
int attemt = 0;
while (attemt < 3)
{
try
......@@ -223,7 +224,8 @@ namespace Titanium.Web.Proxy
//get new connection from pool
tcpConnectionFactory.Release(serverConnection, true);
serverConnection = await getServerConnection(args, false, clientConnection.NegotiatedApplicationProtocol, cancellationToken);
serverConnection = await getServerConnection(args, false,
clientConnection.NegotiatedApplicationProtocol, cancellationToken);
attemt++;
continue;
}
......@@ -348,7 +350,6 @@ namespace Titanium.Web.Proxy
{
await handleHttpSessionResponse(args);
}
}
/// <summary>
......@@ -380,7 +381,7 @@ namespace Titanium.Web.Proxy
/// <param name="cancellationToken">The cancellation token for this async task.</param>
/// <returns></returns>
private async Task<TcpServerConnection> getServerConnection(SessionEventArgsBase args, bool isConnect,
List<SslApplicationProtocol> applicationProtocols, CancellationToken cancellationToken)
List<SslApplicationProtocol> applicationProtocols, CancellationToken cancellationToken)
{
ExternalProxy customUpStreamProxy = null;
......@@ -407,7 +408,7 @@ namespace Titanium.Web.Proxy
/// </summary>
private void prepareRequestHeaders(HeaderCollection requestHeaders)
{
var acceptEncoding = requestHeaders.GetHeaderValueOrNull(KnownHeaders.AcceptEncoding);
string acceptEncoding = requestHeaders.GetHeaderValueOrNull(KnownHeaders.AcceptEncoding);
if (acceptEncoding != null)
{
......@@ -421,22 +422,22 @@ namespace Titanium.Web.Proxy
//uncompressed is always supported by proxy
supportedAcceptEncoding.Add("identity");
requestHeaders.SetOrAddHeaderValue(KnownHeaders.AcceptEncoding, string.Join(",", supportedAcceptEncoding));
requestHeaders.SetOrAddHeaderValue(KnownHeaders.AcceptEncoding,
string.Join(",", supportedAcceptEncoding));
}
requestHeaders.FixProxyHeaders();
}
/// <summary>
/// Handle upgrade to websocket
/// Handle upgrade to websocket
/// </summary>
private async Task handleWebSocketUpgrade(string httpCmd,
SessionEventArgs args, Request request, Response response,
CustomBufferedStream clientStream, HttpResponseWriter clientStreamWriter,
TcpServerConnection serverConnection,
CancellationTokenSource cancellationTokenSource, CancellationToken cancellationToken)
SessionEventArgs args, Request request, Response response,
CustomBufferedStream clientStream, HttpResponseWriter clientStreamWriter,
TcpServerConnection serverConnection,
CancellationTokenSource cancellationTokenSource, CancellationToken cancellationToken)
{
// prepare the prefix content
await serverConnection.StreamWriter.WriteLineAsync(httpCmd, cancellationToken);
await serverConnection.StreamWriter.WriteHeadersAsync(request.Headers,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment