From 869f78edad2d8929d6087f60f2de65c3255e11dc Mon Sep 17 00:00:00 2001 From: justcoding121 Date: Tue, 17 Aug 2021 17:39:05 -0600 Subject: [PATCH 1/3] cleanup tests --- .../NestedProxyTests.cs | 45 ++++--------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs b/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs index 5a95aa8fd..fc3d3c946 100644 --- a/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs +++ b/tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs @@ -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().First(); - explicitEndpoint.BeforeTunnelConnectRequest += (_, e) => + proxy1.GetCustomUpStreamProxyFunc += async (_) => { - e.CustomUpStreamProxy = new ExternalProxy() + var proxy = new ExternalProxy() { HostName = "localhost", Port = proxy2.ProxyEndPoints[0].Port, @@ -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); @@ -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 { } }); @@ -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().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, @@ -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); @@ -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 { } }); From e9e861f6fb2c412bc70b9a5fafbfebf8dcde114a Mon Sep 17 00:00:00 2001 From: justcoding121 Date: Wed, 18 Aug 2021 22:23:25 -0600 Subject: [PATCH 2/3] Add stress test --- .../StressTests.cs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/Titanium.Web.Proxy.IntegrationTests/StressTests.cs diff --git a/tests/Titanium.Web.Proxy.IntegrationTests/StressTests.cs b/tests/Titanium.Web.Proxy.IntegrationTests/StressTests.cs new file mode 100644 index 000000000..0a659e5d6 --- /dev/null +++ b/tests/Titanium.Web.Proxy.IntegrationTests/StressTests.cs @@ -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(); + + //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); + } + } +} From e6837beb0cfd569a0ee4741b0fff19fc71acfc3b Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Wed, 8 Sep 2021 16:14:47 +0200 Subject: [PATCH 3/3] Added parameter to Start controlling if system proxy settings should be cleared. (#876) Co-authored-by: Hubert Badocha --- src/Titanium.Web.Proxy/ProxyServer.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Titanium.Web.Proxy/ProxyServer.cs b/src/Titanium.Web.Proxy/ProxyServer.cs index a0bca42c0..bd0f0872b 100644 --- a/src/Titanium.Web.Proxy/ProxyServer.cs +++ b/src/Titanium.Web.Proxy/ProxyServer.cs @@ -586,7 +586,11 @@ public void DisableAllSystemProxies() /// /// Start this proxy server instance. /// - public void Start() + /// + /// 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. + /// + public void Start(bool changeSystemProxySettings = true) { if (ProxyRunning) { @@ -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)