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

Commit

Permalink
Merge branch 'develop' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Sep 8, 2021
2 parents 6e995d6 + e6837be commit a07bc3d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 41 deletions.
10 changes: 6 additions & 4 deletions src/Titanium.Web.Proxy/ProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,11 @@ public void DisableAllSystemProxies()
/// <summary>
/// Start this proxy server instance.
/// </summary>
public void Start()
/// <param name="changeSystemProxySettings">
/// Whether or not clear any system proxy settings which is pointing to our own endpoint (causing a cycle).
/// E.g due to ungracious proxy shutdown before.
/// </param>
public void Start(bool changeSystemProxySettings = true)
{
if (ProxyRunning)
{
Expand All @@ -600,9 +604,7 @@ public void Start()
CertificateManager.EnsureRootCertificate();
}

// clear any system proxy settings which is pointing to our own endpoint (causing a cycle)
// due to ungracious proxy shutdown before or something else
if (systemProxySettingsManager != null && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
if (changeSystemProxySettings && systemProxySettingsManager != null && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
{
var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry();
if (proxyInfo?.Proxies != null)
Expand Down
45 changes: 8 additions & 37 deletions tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ public async Task Nested_Proxy_Farm_Without_Connection_Cache_Should_Not_Hang()
proxy1.EnableConnectionPool = false;
var proxy2 = proxies2[rnd.Next() % proxies2.Count];

var explicitEndpoint = proxy1.ProxyEndPoints.OfType<ExplicitProxyEndPoint>().First();
explicitEndpoint.BeforeTunnelConnectRequest += (_, e) =>
proxy1.GetCustomUpStreamProxyFunc += async (_) =>
{
e.CustomUpStreamProxy = new ExternalProxy()
var proxy = new ExternalProxy()
{
HostName = "localhost",
Port = proxy2.ProxyEndPoints[0].Port,
Expand All @@ -126,21 +125,7 @@ public async Task Nested_Proxy_Farm_Without_Connection_Cache_Should_Not_Hang()
Password = "test_password"
};
return Task.CompletedTask;
};

proxy1.BeforeRequest += (_, e) =>
{
e.CustomUpStreamProxy = new ExternalProxy()
{
HostName = "localhost",
Port = proxy2.ProxyEndPoints[0].Port,
ProxyType = ExternalProxyType.Http,
UserName = "test_user",
Password = "test_password"
};
return Task.CompletedTask;
return await Task.FromResult(proxy);
};

proxies1.Add(proxy1);
Expand All @@ -164,7 +149,7 @@ await client.PostAsync(new Uri(server.ListeningHttpsUrl),
new StringContent("hello server. I am a client."));
}
//if error is thrown because of server overloading its okay.
//if error is thrown because of server getting overloaded its okay.
//But client.PostAsync should'nt hang in all cases.
catch { }
});
Expand Down Expand Up @@ -212,24 +197,10 @@ public async Task Nested_Proxy_Farm_With_Connection_Cache_Should_Not_Hang()
{
var proxy1 = testSuite.GetProxy();
var proxy2 = proxies2[rnd.Next() % proxies2.Count];
var explicitEndpoint = proxy1.ProxyEndPoints.OfType<ExplicitProxyEndPoint>().First();
explicitEndpoint.BeforeTunnelConnectRequest += (_, e) =>
{
e.CustomUpStreamProxy = new ExternalProxy()
{
HostName = "localhost",
Port = proxy2.ProxyEndPoints[0].Port,
ProxyType = ExternalProxyType.Http,
UserName = "test_user",
Password = "test_password"
};
return Task.CompletedTask;
};

proxy1.BeforeRequest += (_, e) =>
proxy1.GetCustomUpStreamProxyFunc += async (_) =>
{
e.CustomUpStreamProxy = new ExternalProxy()
var proxy = new ExternalProxy()
{
HostName = "localhost",
Port = proxy2.ProxyEndPoints[0].Port,
Expand All @@ -238,7 +209,7 @@ public async Task Nested_Proxy_Farm_With_Connection_Cache_Should_Not_Hang()
Password = "test_password"
};
return Task.CompletedTask;
return await Task.FromResult(proxy);
};

proxies1.Add(proxy1);
Expand All @@ -261,7 +232,7 @@ public async Task Nested_Proxy_Farm_With_Connection_Cache_Should_Not_Hang()
await client.PostAsync(new Uri(server.ListeningHttpsUrl),
new StringContent("hello server. I am a client."));
}
//if error is thrown because of server overloading its okay.
//if error is thrown because of server getting overloaded its okay.
//But client.PostAsync should'nt hang in all cases.
catch { }
});
Expand Down
52 changes: 52 additions & 0 deletions tests/Titanium.Web.Proxy.IntegrationTests/StressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Titanium.Web.Proxy.Models;

namespace Titanium.Web.Proxy.IntegrationTests
{
[TestClass]
public class StressTests
{
[TestMethod]
[Timeout(2 * 60 * 1000)]
public async Task Stress_Test_With_One_Server_And_Many_Clients()
{
var rnd = new Random();

var testSuite = new TestSuite();

var server = testSuite.GetServer();
server.HandleRequest((context) =>
{
return context.Response.WriteAsync("I am server. I received your greetings.");
});

using var proxy = testSuite.GetProxy();

var tasks = new List<Task>();

//send 1000 requests to server
for (int j = 0; j < 1000; j++)
{
var task = Task.Run(async () =>
{
using var client = testSuite.GetClient(proxy);
await client.PostAsync(new Uri(server.ListeningHttpsUrl),
new StringContent("hello server. I am a client."));
});

tasks.Add(task);
}

await Task.WhenAll(tasks);
}
}
}

0 comments on commit a07bc3d

Please sign in to comment.