Commit e4dfa442 authored by titanium007's avatar titanium007

issue #13 Fix

Mark items internal; keep only MRU certificates in cache
parent dfc6d9a6
namespace Titanium.Web.Proxy.Decompression namespace Titanium.Web.Proxy.Decompression
{ {
class DecompressionFactory internal class DecompressionFactory
{ {
public IDecompression Create(string type) internal IDecompression Create(string type)
{ {
switch(type) switch(type)
{ {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Titanium.Web.Proxy.Decompression namespace Titanium.Web.Proxy.Decompression
{ {
class DefaultDecompression : IDecompression internal class DefaultDecompression : IDecompression
{ {
public Task<byte[]> Decompress(byte[] compressedArray) public Task<byte[]> Decompress(byte[] compressedArray)
{ {
......
...@@ -5,7 +5,7 @@ using Titanium.Web.Proxy.Shared; ...@@ -5,7 +5,7 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Decompression namespace Titanium.Web.Proxy.Decompression
{ {
class DeflateDecompression : IDecompression internal class DeflateDecompression : IDecompression
{ {
public async Task<byte[]> Decompress(byte[] compressedArray) public async Task<byte[]> Decompress(byte[] compressedArray)
{ {
......
...@@ -5,7 +5,7 @@ using Titanium.Web.Proxy.Shared; ...@@ -5,7 +5,7 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Decompression namespace Titanium.Web.Proxy.Decompression
{ {
class GZipDecompression : IDecompression internal class GZipDecompression : IDecompression
{ {
public async Task<byte[]> Decompress(byte[] compressedArray) public async Task<byte[]> Decompress(byte[] compressedArray)
{ {
......
...@@ -3,8 +3,8 @@ using System.Threading.Tasks; ...@@ -3,8 +3,8 @@ using System.Threading.Tasks;
namespace Titanium.Web.Proxy.Decompression namespace Titanium.Web.Proxy.Decompression
{ {
interface IDecompression internal interface IDecompression
{ {
Task<byte[]> Decompress(byte[] compressedArray); Task<byte[]> Decompress(byte[] compressedArray);
} }
} }
...@@ -5,7 +5,7 @@ using Titanium.Web.Proxy.Shared; ...@@ -5,7 +5,7 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Decompression namespace Titanium.Web.Proxy.Decompression
{ {
class ZlibDecompression : IDecompression internal class ZlibDecompression : IDecompression
{ {
public async Task<byte[]> Decompress(byte[] compressedArray) public async Task<byte[]> Decompress(byte[] compressedArray)
{ {
......
...@@ -7,10 +7,10 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -7,10 +7,10 @@ namespace Titanium.Web.Proxy.Extensions
/// <summary> /// <summary>
/// Extensions on HttpWebSession object /// Extensions on HttpWebSession object
/// </summary> /// </summary>
public static class HttpWebRequestExtensions internal static class HttpWebRequestExtensions
{ {
//Get encoding of the HTTP request //Get encoding of the HTTP request
public static Encoding GetEncoding(this Request request) internal static Encoding GetEncoding(this Request request)
{ {
try try
{ {
......
...@@ -4,9 +4,9 @@ using Titanium.Web.Proxy.Shared; ...@@ -4,9 +4,9 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Extensions namespace Titanium.Web.Proxy.Extensions
{ {
public static class HttpWebResponseExtensions internal static class HttpWebResponseExtensions
{ {
public static Encoding GetResponseCharacterEncoding(this Response response) internal static Encoding GetResponseCharacterEncoding(this Response response)
{ {
try try
{ {
......
...@@ -8,9 +8,9 @@ using Titanium.Web.Proxy.Shared; ...@@ -8,9 +8,9 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Extensions namespace Titanium.Web.Proxy.Extensions
{ {
public static class StreamHelper internal static class StreamHelper
{ {
public static async Task CopyToAsync(this Stream input, string initialData, Stream output) internal static async Task CopyToAsync(this Stream input, string initialData, Stream output)
{ {
if (!string.IsNullOrEmpty(initialData)) if (!string.IsNullOrEmpty(initialData))
{ {
......
using System.Net.Sockets; using System.Net.Sockets;
namespace Titanium.Web.Proxy.Extensions namespace Titanium.Web.Proxy.Extensions
{ {
internal static class TcpExtensions internal static class TcpExtensions
{ {
public static bool IsConnected(this Socket client) internal static bool IsConnected(this Socket client)
{ {
// This is how you can determine whether a socket is still connected. // This is how you can determine whether a socket is still connected.
bool blockingState = client.Blocking; bool blockingState = client.Blocking;
......
...@@ -15,18 +15,16 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -15,18 +15,16 @@ namespace Titanium.Web.Proxy.Helpers
/// using the specified encoding /// using the specified encoding
/// as well as to read bytes as required /// as well as to read bytes as required
/// </summary> /// </summary>
public class CustomBinaryReader : IDisposable internal class CustomBinaryReader : IDisposable
{ {
private Stream stream; private Stream stream;
internal CustomBinaryReader(Stream stream) internal CustomBinaryReader(Stream stream)
{ {
this.stream = stream; this.stream = stream;
} }
internal Stream BaseStream => stream;
public Stream BaseStream => stream;
/// <summary> /// <summary>
/// Read a line from the byte stream /// Read a line from the byte stream
...@@ -41,25 +39,25 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -41,25 +39,25 @@ namespace Titanium.Web.Proxy.Helpers
var lastChar = default(char); var lastChar = default(char);
var buffer = new byte[1]; var buffer = new byte[1];
while (await this.stream.ReadAsync(buffer, 0, 1).ConfigureAwait(false) > 0) while (this.stream.Read(buffer, 0, 1) > 0)
{ {
if (lastChar == '\r' && buffer[0] == '\n') if (lastChar == '\r' && buffer[0] == '\n')
{ {
return readBuffer.Remove(readBuffer.Length - 1, 1).ToString(); return await Task.FromResult(readBuffer.Remove(readBuffer.Length - 1, 1).ToString());
} }
if (buffer[0] == '\0') if (buffer[0] == '\0')
{ {
return readBuffer.ToString(); return await Task.FromResult(readBuffer.ToString());
} }
readBuffer.Append((char)buffer[0]); readBuffer.Append((char)buffer[0]);
lastChar = (char)buffer[0]; lastChar = (char)buffer[0];
} }
return readBuffer.ToString(); return await Task.FromResult(readBuffer.ToString());
} }
catch (IOException) catch (IOException)
{ {
return readBuffer.ToString(); return await Task.FromResult(readBuffer.ToString());
} }
} }
......
...@@ -16,9 +16,9 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -16,9 +16,9 @@ namespace Titanium.Web.Proxy.Helpers
internal class HttpSystemProxyValue internal class HttpSystemProxyValue
{ {
public string HostName { get; set; } internal string HostName { get; set; }
public int Port { get; set; } internal int Port { get; set; }
public bool IsHttps { get; set; } internal bool IsHttps { get; set; }
public override string ToString() public override string ToString()
{ {
...@@ -29,12 +29,12 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -29,12 +29,12 @@ namespace Titanium.Web.Proxy.Helpers
} }
} }
public static class SystemProxyHelper internal static class SystemProxyHelper
{ {
public const int InternetOptionSettingsChanged = 39; internal const int InternetOptionSettingsChanged = 39;
public const int InternetOptionRefresh = 37; internal const int InternetOptionRefresh = 37;
public static void SetHttpProxy(string hostname, int port) internal static void SetHttpProxy(string hostname, int port)
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
...@@ -60,7 +60,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -60,7 +60,7 @@ namespace Titanium.Web.Proxy.Helpers
} }
public static void RemoveHttpProxy() internal static void RemoveHttpProxy()
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
...@@ -89,7 +89,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -89,7 +89,7 @@ namespace Titanium.Web.Proxy.Helpers
Refresh(); Refresh();
} }
public static void SetHttpsProxy(string hostname, int port) internal static void SetHttpsProxy(string hostname, int port)
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
...@@ -116,7 +116,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -116,7 +116,7 @@ namespace Titanium.Web.Proxy.Helpers
Refresh(); Refresh();
} }
public static void RemoveHttpsProxy() internal static void RemoveHttpsProxy()
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
...@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy.Helpers
Refresh(); Refresh();
} }
public static void DisableAllProxy() internal static void DisableAllProxy()
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
......
...@@ -12,9 +12,9 @@ using Titanium.Web.Proxy.Shared; ...@@ -12,9 +12,9 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
public class TcpHelper internal class TcpHelper
{ {
public async static Task SendRaw(Stream clientStream, string httpCmd, List<HttpHeader> requestHeaders, string hostName, internal async static Task SendRaw(Stream clientStream, string httpCmd, List<HttpHeader> requestHeaders, string hostName,
int tunnelPort, bool isHttps) int tunnelPort, bool isHttps)
{ {
StringBuilder sb = null; StringBuilder sb = null;
......
...@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.Network
/// </summary> /// </summary>
internal SessionEventArgs Session { get; set; } internal SessionEventArgs Session { get; set; }
public CustomSslStream(Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback) internal CustomSslStream(Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback userCertificateValidationCallback)
:base(innerStream, leaveInnerStreamOpen, userCertificateValidationCallback) :base(innerStream, leaveInnerStreamOpen, userCertificateValidationCallback)
{ {
......
...@@ -28,7 +28,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -28,7 +28,6 @@ namespace Titanium.Web.Proxy.Network
internal DateTime LastAccess { get; set; } internal DateTime LastAccess { get; set; }
internal TcpConnection() internal TcpConnection()
{ {
LastAccess = DateTime.Now; LastAccess = DateTime.Now;
...@@ -192,9 +191,17 @@ namespace Titanium.Web.Proxy.Network ...@@ -192,9 +191,17 @@ namespace Titanium.Web.Proxy.Network
finally { connectionAccessLock.Release(); } finally { connectionAccessLock.Release(); }
} }
private static bool clearConenctions { get; set; }
internal static void StopClearIdleConnections()
{
clearConenctions = false;
}
internal async static void ClearIdleConnections() internal async static void ClearIdleConnections()
{ {
while (true) clearConenctions = true;
while (clearConenctions)
{ {
await connectionAccessLock.WaitAsync(); await connectionAccessLock.WaitAsync();
try try
......
...@@ -21,8 +21,7 @@ namespace Titanium.Web.Proxy ...@@ -21,8 +21,7 @@ namespace Titanium.Web.Proxy
static ProxyServer() static ProxyServer()
{ {
ProxyEndPoints = new List<ProxyEndPoint>(); ProxyEndPoints = new List<ProxyEndPoint>();
Initialize();
} }
private static CertificateManager CertManager { get; set; } private static CertificateManager CertManager { get; set; }
...@@ -58,6 +57,13 @@ namespace Titanium.Web.Proxy ...@@ -58,6 +57,13 @@ namespace Titanium.Web.Proxy
public static void Initialize() public static void Initialize()
{ {
TcpConnectionManager.ClearIdleConnections(); TcpConnectionManager.ClearIdleConnections();
CertManager.ClearIdleCertificates();
}
public static void Quit()
{
TcpConnectionManager.StopClearIdleConnections();
CertManager.StopClearIdleCertificates();
} }
public static void AddEndPoint(ProxyEndPoint endPoint) public static void AddEndPoint(ProxyEndPoint endPoint)
...@@ -153,7 +159,7 @@ namespace Titanium.Web.Proxy ...@@ -153,7 +159,7 @@ namespace Titanium.Web.Proxy
CertManager = new CertificateManager(RootCertificateIssuerName, CertManager = new CertificateManager(RootCertificateIssuerName,
RootCertificateName); RootCertificateName);
certTrusted = CertManager.CreateTrustedRootCertificate().Result; certTrusted = CertManager.CreateTrustedRootCertificate().Result;
foreach (var endPoint in ProxyEndPoints) foreach (var endPoint in ProxyEndPoints)
...@@ -161,6 +167,8 @@ namespace Titanium.Web.Proxy ...@@ -161,6 +167,8 @@ namespace Titanium.Web.Proxy
Listen(endPoint); Listen(endPoint);
} }
Initialize();
proxyRunning = true; proxyRunning = true;
} }
...@@ -186,6 +194,8 @@ namespace Titanium.Web.Proxy ...@@ -186,6 +194,8 @@ namespace Titanium.Web.Proxy
CertManager.Dispose(); CertManager.Dispose();
Quit();
proxyRunning = false; proxyRunning = false;
} }
......
...@@ -74,7 +74,7 @@ namespace Titanium.Web.Proxy ...@@ -74,7 +74,7 @@ namespace Titanium.Web.Proxy
await WriteConnectResponse(clientStreamWriter, version).ConfigureAwait(false); await WriteConnectResponse(clientStreamWriter, version).ConfigureAwait(false);
var certificate = await CertManager.CreateCertificate(httpRemoteUri.Host); var certificate = await CertManager.CreateCertificate(httpRemoteUri.Host, false);
SslStream sslStream = null; SslStream sslStream = null;
...@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy ...@@ -146,7 +146,7 @@ namespace Titanium.Web.Proxy
// certificate = CertManager.CreateCertificate(hostName); // certificate = CertManager.CreateCertificate(hostName);
//} //}
//else //else
certificate = await CertManager.CreateCertificate(endPoint.GenericCertificateName); certificate = await CertManager.CreateCertificate(endPoint.GenericCertificateName, false);
try try
{ {
......
...@@ -10,7 +10,7 @@ namespace Titanium.Web.Proxy.Shared ...@@ -10,7 +10,7 @@ namespace Titanium.Web.Proxy.Shared
/// </summary> /// </summary>
internal class Constants internal class Constants
{ {
public static readonly int BUFFER_SIZE = 8192; internal static readonly int BUFFER_SIZE = 8192;
internal static readonly char[] SpaceSplit = { ' ' }; internal static readonly char[] SpaceSplit = { ' ' };
internal static readonly char[] ColonSplit = { ':' }; internal static readonly char[] ColonSplit = { ':' };
......
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