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

Commit a07bc3d

Browse files
committed
Merge branch 'develop' into beta
2 parents 6e995d6 + e6837be commit a07bc3d

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,11 @@ public void DisableAllSystemProxies()
586586
/// <summary>
587587
/// Start this proxy server instance.
588588
/// </summary>
589-
public void Start()
589+
/// <param name="changeSystemProxySettings">
590+
/// Whether or not clear any system proxy settings which is pointing to our own endpoint (causing a cycle).
591+
/// E.g due to ungracious proxy shutdown before.
592+
/// </param>
593+
public void Start(bool changeSystemProxySettings = true)
590594
{
591595
if (ProxyRunning)
592596
{
@@ -600,9 +604,7 @@ public void Start()
600604
CertificateManager.EnsureRootCertificate();
601605
}
602606

603-
// clear any system proxy settings which is pointing to our own endpoint (causing a cycle)
604-
// due to ungracious proxy shutdown before or something else
605-
if (systemProxySettingsManager != null && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
607+
if (changeSystemProxySettings && systemProxySettingsManager != null && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
606608
{
607609
var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry();
608610
if (proxyInfo?.Proxies != null)

tests/Titanium.Web.Proxy.IntegrationTests/NestedProxyTests.cs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ public async Task Nested_Proxy_Farm_Without_Connection_Cache_Should_Not_Hang()
114114
proxy1.EnableConnectionPool = false;
115115
var proxy2 = proxies2[rnd.Next() % proxies2.Count];
116116

117-
var explicitEndpoint = proxy1.ProxyEndPoints.OfType<ExplicitProxyEndPoint>().First();
118-
explicitEndpoint.BeforeTunnelConnectRequest += (_, e) =>
117+
proxy1.GetCustomUpStreamProxyFunc += async (_) =>
119118
{
120-
e.CustomUpStreamProxy = new ExternalProxy()
119+
var proxy = new ExternalProxy()
121120
{
122121
HostName = "localhost",
123122
Port = proxy2.ProxyEndPoints[0].Port,
@@ -126,21 +125,7 @@ public async Task Nested_Proxy_Farm_Without_Connection_Cache_Should_Not_Hang()
126125
Password = "test_password"
127126
};
128127

129-
return Task.CompletedTask;
130-
};
131-
132-
proxy1.BeforeRequest += (_, e) =>
133-
{
134-
e.CustomUpStreamProxy = new ExternalProxy()
135-
{
136-
HostName = "localhost",
137-
Port = proxy2.ProxyEndPoints[0].Port,
138-
ProxyType = ExternalProxyType.Http,
139-
UserName = "test_user",
140-
Password = "test_password"
141-
};
142-
143-
return Task.CompletedTask;
128+
return await Task.FromResult(proxy);
144129
};
145130

146131
proxies1.Add(proxy1);
@@ -164,7 +149,7 @@ await client.PostAsync(new Uri(server.ListeningHttpsUrl),
164149
new StringContent("hello server. I am a client."));
165150

166151
}
167-
//if error is thrown because of server overloading its okay.
152+
//if error is thrown because of server getting overloaded its okay.
168153
//But client.PostAsync should'nt hang in all cases.
169154
catch { }
170155
});
@@ -212,24 +197,10 @@ public async Task Nested_Proxy_Farm_With_Connection_Cache_Should_Not_Hang()
212197
{
213198
var proxy1 = testSuite.GetProxy();
214199
var proxy2 = proxies2[rnd.Next() % proxies2.Count];
215-
var explicitEndpoint = proxy1.ProxyEndPoints.OfType<ExplicitProxyEndPoint>().First();
216-
explicitEndpoint.BeforeTunnelConnectRequest += (_, e) =>
217-
{
218-
e.CustomUpStreamProxy = new ExternalProxy()
219-
{
220-
HostName = "localhost",
221-
Port = proxy2.ProxyEndPoints[0].Port,
222-
ProxyType = ExternalProxyType.Http,
223-
UserName = "test_user",
224-
Password = "test_password"
225-
};
226-
227-
return Task.CompletedTask;
228-
};
229200

230-
proxy1.BeforeRequest += (_, e) =>
201+
proxy1.GetCustomUpStreamProxyFunc += async (_) =>
231202
{
232-
e.CustomUpStreamProxy = new ExternalProxy()
203+
var proxy = new ExternalProxy()
233204
{
234205
HostName = "localhost",
235206
Port = proxy2.ProxyEndPoints[0].Port,
@@ -238,7 +209,7 @@ public async Task Nested_Proxy_Farm_With_Connection_Cache_Should_Not_Hang()
238209
Password = "test_password"
239210
};
240211

241-
return Task.CompletedTask;
212+
return await Task.FromResult(proxy);
242213
};
243214

244215
proxies1.Add(proxy1);
@@ -261,7 +232,7 @@ public async Task Nested_Proxy_Farm_With_Connection_Cache_Should_Not_Hang()
261232
await client.PostAsync(new Uri(server.ListeningHttpsUrl),
262233
new StringContent("hello server. I am a client."));
263234
}
264-
//if error is thrown because of server overloading its okay.
235+
//if error is thrown because of server getting overloaded its okay.
265236
//But client.PostAsync should'nt hang in all cases.
266237
catch { }
267238
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
using Titanium.Web.Proxy.Models;
11+
12+
namespace Titanium.Web.Proxy.IntegrationTests
13+
{
14+
[TestClass]
15+
public class StressTests
16+
{
17+
[TestMethod]
18+
[Timeout(2 * 60 * 1000)]
19+
public async Task Stress_Test_With_One_Server_And_Many_Clients()
20+
{
21+
var rnd = new Random();
22+
23+
var testSuite = new TestSuite();
24+
25+
var server = testSuite.GetServer();
26+
server.HandleRequest((context) =>
27+
{
28+
return context.Response.WriteAsync("I am server. I received your greetings.");
29+
});
30+
31+
using var proxy = testSuite.GetProxy();
32+
33+
var tasks = new List<Task>();
34+
35+
//send 1000 requests to server
36+
for (int j = 0; j < 1000; j++)
37+
{
38+
var task = Task.Run(async () =>
39+
{
40+
using var client = testSuite.GetClient(proxy);
41+
42+
await client.PostAsync(new Uri(server.ListeningHttpsUrl),
43+
new StringContent("hello server. I am a client."));
44+
});
45+
46+
tasks.Add(task);
47+
}
48+
49+
await Task.WhenAll(tasks);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)