Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void Stop()
private async Task onBeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e)
{
string hostname = e.HttpClient.Request.RequestUri.Host;
//await writeToConsole("Tunnel to: " + hostname);
await writeToConsole("Tunnel to: " + hostname);

if (hostname.Contains("dropbox.com"))
{
Expand All @@ -138,8 +138,8 @@ private Task onBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEv
// intecept & cancel redirect or update requests
private async Task onRequest(object sender, SessionEventArgs e)
{
//await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
//await writeToConsole(e.HttpClient.Request.Url);
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
await writeToConsole(e.HttpClient.Request.Url);

// store it in the UserData property
// It can be a simple integer, Guid, or any type
Expand Down Expand Up @@ -189,7 +189,7 @@ private async Task multipartRequestPartSent(object sender, MultipartRequestPartS

private async Task onResponse(object sender, SessionEventArgs e)
{
//await writeToConsole("Active Server Connections:" + ((ProxyServer)sender).ServerConnectionCount);
await writeToConsole("Active Server Connections:" + ((ProxyServer)sender).ServerConnectionCount);

string ext = System.IO.Path.GetExtension(e.HttpClient.Request.RequestUri.AbsolutePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ private X509Certificate2 makeCertificate(string sSubjectCN, bool isRoot,
// KeyLength
const int keyLength = 2048;

var graceTime = DateTime.Now.AddDays(graceDays);
var now = DateTime.Now;
var graceTime = now.AddDays(graceDays);
var certificate = makeCertificate(isRoot, sSubjectCN, fullSubject, keyLength, hashAlgo, graceTime,
now.AddDays(validDays), isRoot ? null : signingCert);
return certificate;
Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Network/CertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ internal async void ClearIdleCertificates()
var cancellationToken = clearCertificatesTokenSource.Token;
while (!cancellationToken.IsCancellationRequested)
{
var cutOff = DateTime.Now.AddMinutes(-1 * CertificateCacheTimeOutMinutes);
var cutOff = DateTime.Now.AddMinutes(-CertificateCacheTimeOutMinutes);

var outdated = cachedCertificates.Where(x => x.Value.LastAccess < cutOff).ToList();

Expand Down
14 changes: 6 additions & 8 deletions src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,12 @@ internal async Task<TcpServerConnection> GetServerConnection(string remoteHostNa
{
if (cache.TryGetValue(cacheKey, out var existingConnections))
{
// +3 seconds for potential delay after getting connection
var cutOff = DateTime.Now.AddSeconds(-proxyServer.ConnectionTimeOutSeconds + 3);
while (existingConnections.Count > 0)
{
if (existingConnections.TryDequeue(out var recentConnection))
{
//+3 seconds for potential delay after getting connection
var cutOff = DateTime.Now.AddSeconds(-1 * proxyServer.ConnectionTimeOutSeconds + 3);

if (recentConnection.LastAccess > cutOff
&& recentConnection.TcpClient.IsGoodConnection())
{
Expand Down Expand Up @@ -481,7 +480,7 @@ private async Task clearOutdatedConnections()
{
try
{
var cutOff = DateTime.Now.AddSeconds(-1 * Server.ConnectionTimeOutSeconds);
var cutOff = DateTime.Now.AddSeconds(-Server.ConnectionTimeOutSeconds);
foreach (var item in cache)
{
var queue = item.Value;
Expand All @@ -490,8 +489,7 @@ private async Task clearOutdatedConnections()
{
if (queue.TryDequeue(out var connection))
{
if (!Server.EnableConnectionPool
|| connection.LastAccess < cutOff)
if (!Server.EnableConnectionPool || connection.LastAccess < cutOff)
{
disposalBag.Add(connection);
}
Expand All @@ -508,8 +506,8 @@ private async Task clearOutdatedConnections()
{
await @lock.WaitAsync();

//clear empty queues
var emptyKeys = cache.Where(x => x.Value.Count == 0).Select(x => x.Key).ToList();
// clear empty queues
var emptyKeys = cache.ToArray().Where(x => x.Value.Count == 0).Select(x => x.Key);
foreach (string key in emptyKeys)
{
cache.TryRemove(key, out _);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static CustomBufferedStream()
try
{
var method = typeof(NetworkStream).GetMethod(nameof(Stream.ReadAsync),
new Type[] { typeof(byte[]), typeof(int), typeof(int), typeof(CancellationToken) });
new[] { typeof(byte[]), typeof(int), typeof(int), typeof(CancellationToken) });
if (method != null && method.DeclaringType != typeof(Stream))
{
networkStreamHack = false;
Expand Down Expand Up @@ -684,8 +684,7 @@ public override int EndRead(IAsyncResult asyncResult)

return ((TaskResult<int>)asyncResult).Result;
}



/// <summary>
/// Fix the .net bug with SslStream slow WriteAsync
/// https://github.com/justcoding121/Titanium-Web-Proxy/issues/495
Expand All @@ -709,6 +708,7 @@ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, As

return vAsyncResult;
}

public override void EndWrite(IAsyncResult asyncResult)
{
if (!networkStreamHack)
Expand Down