Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 87497a0

Browse files
justcoding121justcoding121
authored andcommitted
Cleanup connection manager
1 parent b72fad9 commit 87497a0

11 files changed

+112
-80
lines changed

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Text;
99
using Titanium.Web.Proxy.Exceptions;
1010
using Titanium.Web.Proxy.Helpers;
11-
using Titanium.Web.Proxy.Http;
11+
using Titanium.Web.Proxy.Network;
1212
using Titanium.Web.Proxy.Models;
1313

1414
namespace Titanium.Web.Proxy.EventArguments
@@ -32,7 +32,7 @@ internal SessionEventArgs(int bufferSize)
3232
{
3333
_bufferSize = bufferSize;
3434
Client = new Client();
35-
ProxySession = new Http.HttpWebSession();
35+
ProxySession = new HttpWebSession();
3636
}
3737

3838
public Client Client { get; set; }

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Net;
22
using System.Text;
3-
using Titanium.Web.Proxy.Http;
3+
using Titanium.Web.Proxy.Network;
44

55
namespace Titanium.Web.Proxy.Extensions
66
{

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Net;
22
using System.Text;
3-
using Titanium.Web.Proxy.Http;
3+
using Titanium.Web.Proxy.Network;
44

55
namespace Titanium.Web.Proxy.Extensions
66
{

Titanium.Web.Proxy/Http/HttpStreamReader.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

Titanium.Web.Proxy/Http/HttpWebClient.cs renamed to Titanium.Web.Proxy/Network/HttpWebClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Titanium.Web.Proxy.Helpers;
1111
using Titanium.Web.Proxy.Models;
1212

13-
namespace Titanium.Web.Proxy.Http
13+
namespace Titanium.Web.Proxy.Network
1414
{
1515
public class Request
1616
{
@@ -127,7 +127,6 @@ public void SendRequest()
127127
}
128128

129129
requestLines.AppendLine();
130-
//requestLines.AppendLine();
131130

132131
string request = requestLines.ToString();
133132
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
@@ -139,6 +138,10 @@ public void ReceiveResponse()
139138
{
140139
var httpResult = ProxyClient.ServerStreamReader.ReadLine().Split(new char[] { ' ' }, 3);
141140

141+
if(string.IsNullOrEmpty(httpResult[0]))
142+
{
143+
var s = ProxyClient.ServerStreamReader.ReadLine();
144+
}
142145
var httpVersion = httpResult[0];
143146

144147
Version version;

Titanium.Web.Proxy/Http/TcpConnectionManager.cs renamed to Titanium.Web.Proxy/Network/TcpConnectionManager.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,26 @@
88
using System.IO;
99
using System.Net.Security;
1010
using Titanium.Web.Proxy.Helpers;
11+
using System.Threading;
1112

12-
namespace Titanium.Web.Proxy.Http
13+
namespace Titanium.Web.Proxy.Network
1314
{
1415
public class TcpConnection
1516
{
1617
public string HostName { get; set; }
1718
public int port { get; set; }
1819
public bool IsSecure { get; set; }
1920

20-
public TcpClient Client { get; set; }
21+
public TcpClient TcpClient { get; set; }
2122
public CustomBinaryReader ServerStreamReader { get; set; }
2223
public Stream Stream { get; set; }
24+
25+
public DateTime LastAccess { get; set; }
26+
27+
public TcpConnection()
28+
{
29+
LastAccess = DateTime.Now;
30+
}
2331
}
2432

2533
internal class TcpConnectionManager
@@ -28,15 +36,23 @@ internal class TcpConnectionManager
2836

2937
public static TcpConnection GetClient(string Hostname, int port, bool IsSecure)
3038
{
31-
lock (ConnectionCache)
32-
{
33-
var cached = ConnectionCache.FirstOrDefault(x => x.HostName == Hostname && x.port == port && x.IsSecure == IsSecure && x.Client.Connected);
3439

35-
if (cached != null)
40+
while (true)
41+
{
42+
TcpConnection cached = null;
43+
lock (ConnectionCache)
3644
{
37-
ConnectionCache.Remove(cached);
38-
return cached;
45+
cached = ConnectionCache.FirstOrDefault(x => x.HostName == Hostname && x.port == port && x.IsSecure == IsSecure && x.TcpClient.Connected);
46+
47+
if (cached != null)
48+
ConnectionCache.Remove(cached);
3949
}
50+
51+
if (cached != null && !cached.TcpClient.Client.IsConnected())
52+
continue;
53+
54+
if (cached == null)
55+
break;
4056
}
4157

4258
return CreateClient(Hostname, port, IsSecure);
@@ -69,16 +85,39 @@ private static TcpConnection CreateClient(string Hostname, int port, bool IsSecu
6985
HostName = Hostname,
7086
port = port,
7187
IsSecure = IsSecure,
72-
Client = client,
88+
TcpClient = client,
7389
ServerStreamReader = new CustomBinaryReader(stream, Encoding.ASCII),
7490
Stream = stream
7591
};
7692
}
7793

7894
public static void ReleaseClient(TcpConnection Connection)
7995
{
96+
Connection.LastAccess = DateTime.Now;
8097
ConnectionCache.Add(Connection);
8198
}
8299

100+
public static void ClearIdleConnections()
101+
{
102+
while (true)
103+
{
104+
lock (ConnectionCache)
105+
{
106+
var cutOff = DateTime.Now.AddSeconds(-60);
107+
108+
ConnectionCache
109+
.Where(x => x.LastAccess < cutOff)
110+
.ToList()
111+
.ForEach(x => x.TcpClient.Close());
112+
113+
ConnectionCache.RemoveAll(x => x.LastAccess < cutOff);
114+
}
115+
116+
Thread.Sleep(1000 * 60 * 3);
117+
}
118+
119+
}
120+
121+
83122
}
84123
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net.Sockets;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
10+
namespace Titanium.Web.Proxy.Network
11+
{
12+
13+
internal static class TcpExtensions
14+
{
15+
public static bool IsConnected(this Socket client)
16+
{
17+
// This is how you can determine whether a socket is still connected.
18+
bool blockingState = client.Blocking;
19+
try
20+
{
21+
byte[] tmp = new byte[1];
22+
23+
client.Blocking = false;
24+
client.Send(tmp, 0, 0);
25+
return true;
26+
}
27+
catch (SocketException e)
28+
{
29+
// 10035 == WSAEWOULDBLOCK
30+
if (e.NativeErrorCode.Equals(10035))
31+
return true;
32+
else
33+
{
34+
return false;
35+
}
36+
}
37+
finally
38+
{
39+
client.Blocking = blockingState;
40+
}
41+
}
42+
}
43+
44+
}

Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Titanium.Web.Proxy.EventArguments;
1111
using Titanium.Web.Proxy.Helpers;
12+
using Titanium.Web.Proxy.Network;
1213

1314
namespace Titanium.Web.Proxy
1415
{
@@ -79,6 +80,7 @@ public static void Initialize()
7980
//useUnsafeHeaderParsing
8081
#endif
8182
NetFrameworkHelper.ToggleAllowUnsafeHeaderParsing(true);
83+
Task.Factory.StartNew(()=>TcpConnectionManager.ClearIdleConnections());
8284
}
8385

8486

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using Titanium.Web.Proxy.EventArguments;
1414
using Titanium.Web.Proxy.Extensions;
1515
using Titanium.Web.Proxy.Helpers;
16-
using Titanium.Web.Proxy.Http;
16+
using Titanium.Web.Proxy.Network;
1717
using Titanium.Web.Proxy.Models;
1818

1919
namespace Titanium.Web.Proxy
@@ -115,7 +115,7 @@ private static void HandleClient(TcpClient client)
115115
private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream,
116116
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string secureTunnelHostName)
117117
{
118-
118+
119119
while (true)
120120
{
121121
if (string.IsNullOrEmpty(httpCmd))
@@ -201,10 +201,11 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
201201
return;
202202
}
203203

204-
204+
205205
//construct the web request that we are going to issue on behalf of the client.
206206
var connection = TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps);
207207
args.ProxySession.SetConnection(connection);
208+
208209
args.ProxySession.SendRequest();
209210

210211
//If request was modified by user
@@ -228,7 +229,7 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
228229
//if connection is closing exit
229230
if (args.ProxySession.Response.ResponseKeepAlive == false)
230231
{
231-
connection.Client.Close();
232+
connection.TcpClient.Close();
232233
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
233234
return;
234235
}

Titanium.Web.Proxy/ResponseHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Titanium.Web.Proxy.EventArguments;
1111
using Titanium.Web.Proxy.Extensions;
1212
using Titanium.Web.Proxy.Helpers;
13-
using Titanium.Web.Proxy.Http;
13+
using Titanium.Web.Proxy.Network;
1414
using Titanium.Web.Proxy.Models;
1515

1616
namespace Titanium.Web.Proxy

Titanium.Web.Proxy/Titanium.Web.Proxy.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@
8585
<Compile Include="Helpers\CertificateManager.cs" />
8686
<Compile Include="Helpers\Firefox.cs" />
8787
<Compile Include="Helpers\SystemProxy.cs" />
88-
<Compile Include="Http\TcpConnectionManager.cs" />
88+
<Compile Include="Network\TcpExtensions.cs" />
89+
<Compile Include="Network\TcpConnectionManager.cs" />
8990
<Compile Include="Models\HttpHeader.cs" />
90-
<Compile Include="Http\HttpWebClient.cs" />
91+
<Compile Include="Network\HttpWebClient.cs" />
9192
<Compile Include="Properties\AssemblyInfo.cs" />
9293
<Compile Include="RequestHandler.cs" />
9394
<Compile Include="ResponseHandler.cs" />

0 commit comments

Comments
 (0)