Commit a68e4eda authored by justcoding121's avatar justcoding121

align lines

parent ecff9ef1
...@@ -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>
...@@ -35,14 +34,14 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -35,14 +34,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
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)
{ {
...@@ -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,7 +319,6 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -317,7 +319,6 @@ 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>
......
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,7 +24,8 @@ namespace Titanium.Web.Proxy ...@@ -26,7 +24,8 @@ 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", "gzip",
"deflate" "deflate"
...@@ -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>
...@@ -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,7 +422,8 @@ namespace Titanium.Web.Proxy ...@@ -421,7 +422,8 @@ 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();
...@@ -436,7 +438,6 @@ namespace Titanium.Web.Proxy ...@@ -436,7 +438,6 @@ namespace Titanium.Web.Proxy
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,
......
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