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

Commit e154330

Browse files
Merge pull request #275 from justcoding121/beta
Stable
2 parents acad670 + ac2877b commit e154330

File tree

19 files changed

+1241
-262
lines changed

19 files changed

+1241
-262
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,4 @@ FakesAssemblies/
202202
*.plg
203203

204204
# Visual Studio 6 workspace options file
205-
*.opt
205+
*.opt

Examples/Titanium.Web.Proxy.Examples.Basic/Program.cs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ public static void Main(string[] args)
1414
//fix console hang due to QuickEdit mode
1515
ConsoleHelper.DisableQuickEditMode();
1616

17-
//On Console exit make sure we also exit the proxy
18-
NativeMethods.Handler = ConsoleEventCallback;
19-
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
20-
2117
//Start proxy controller
2218
controller.StartProxy();
2319

@@ -27,34 +23,5 @@ public static void Main(string[] args)
2723

2824
controller.Stop();
2925
}
30-
31-
32-
private static bool ConsoleEventCallback(int eventType)
33-
{
34-
if (eventType != 2) return false;
35-
try
36-
{
37-
controller.Stop();
38-
}
39-
catch
40-
{
41-
// ignored
42-
}
43-
return false;
44-
}
45-
}
46-
47-
internal static class NativeMethods
48-
{
49-
// Keeps it from getting garbage collected
50-
internal static ConsoleEventDelegate Handler;
51-
52-
[DllImport("kernel32.dll", SetLastError = true)]
53-
internal static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
54-
55-
// Pinvoke
56-
internal delegate bool ConsoleEventDelegate(int eventType);
57-
5826
}
59-
6027
}

Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Net;
45
using System.Net.Security;
56
using System.Threading.Tasks;
67
using Titanium.Web.Proxy.EventArguments;
8+
using Titanium.Web.Proxy.Helpers;
79
using Titanium.Web.Proxy.Models;
810

911
namespace Titanium.Web.Proxy.Examples.Basic
@@ -15,8 +17,8 @@ public class ProxyTestController
1517
//share requestBody outside handlers
1618
//Using a dictionary is not a good idea since it can cause memory overflow
1719
//ideally the data should be moved out of memory
18-
//private readonly Dictionary<Guid, string> requestBodyHistory
19-
// = new Dictionary<Guid, string>();
20+
//private readonly IDictionary<Guid, string> requestBodyHistory
21+
// = new ConcurrentDictionary<Guid, string>();
2022

2123
public ProxyTestController()
2224
{
@@ -29,6 +31,7 @@ public ProxyTestController()
2931

3032
proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message);
3133
proxyServer.TrustRootCertificate = true;
34+
proxyServer.ForwardToUpstreamGateway = true;
3235

3336
//optionally set the Certificate Engine
3437
//Under Mono only BouncyCastle will be supported
@@ -92,8 +95,9 @@ public void StartProxy()
9295
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
9396

9497
//Only explicit proxies can be set as system proxy!
95-
proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
96-
proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
98+
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
99+
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
100+
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
97101
}
98102

99103
public void Stop()

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ Sample request and response event handlers
112112
```csharp
113113

114114
//To access requestBody from OnResponse handler
115-
private Dictionary<Guid, string> requestBodyHistory = new Dictionary<Guid, string>();
115+
private IDictionary<Guid, string> requestBodyHistory
116+
= new ConcurrentDictionary<Guid, string>();
116117

117118
public async Task OnRequest(object sender, SessionEventArgs e)
118119
{
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using Titanium.Web.Proxy.Helpers;
9+
using Titanium.Web.Proxy.Helpers.WinHttp;
10+
11+
namespace Titanium.Web.Proxy.UnitTests
12+
{
13+
[TestClass]
14+
public class SystemProxyTest
15+
{
16+
[TestMethod]
17+
public void CompareProxyAdddressReturendByWebProxyAndWinHttpProxyResolver()
18+
{
19+
var proxyManager = new SystemProxyManager();
20+
21+
try
22+
{
23+
CompareUrls();
24+
25+
proxyManager.SetProxy("127.0.0.1", 8000, ProxyProtocolType.Http);
26+
CompareUrls();
27+
28+
proxyManager.SetProxy("127.0.0.1", 8000, ProxyProtocolType.Https);
29+
CompareUrls();
30+
31+
proxyManager.SetProxy("127.0.0.1", 8000, ProxyProtocolType.AllHttp);
32+
CompareUrls();
33+
34+
// for this test you need to add a proxy.pac file to a local webserver
35+
//function FindProxyForURL(url, host)
36+
//{
37+
// if (shExpMatch(host, "google.com"))
38+
// {
39+
// return "PROXY 127.0.0.1:8888";
40+
// }
41+
42+
// return "DIRECT";
43+
//}
44+
45+
//proxyManager.SetAutoProxyUrl("http://localhost/proxy.pac");
46+
//CompareUrls();
47+
48+
proxyManager.SetProxyOverride("<-loopback>");
49+
CompareUrls();
50+
51+
proxyManager.SetProxyOverride("<local>");
52+
CompareUrls();
53+
54+
proxyManager.SetProxyOverride("yahoo.com");
55+
CompareUrls();
56+
57+
proxyManager.SetProxyOverride("*.local");
58+
CompareUrls();
59+
60+
proxyManager.SetProxyOverride("http://*.local");
61+
CompareUrls();
62+
63+
proxyManager.SetProxyOverride("<-loopback>;*.local");
64+
CompareUrls();
65+
66+
proxyManager.SetProxyOverride("<-loopback>;*.local;<local>");
67+
CompareUrls();
68+
}
69+
finally
70+
{
71+
proxyManager.RestoreOriginalSettings();
72+
}
73+
}
74+
75+
private void CompareUrls()
76+
{
77+
var webProxy = WebRequest.GetSystemWebProxy();
78+
79+
var resolver = new WinHttpWebProxyFinder();
80+
resolver.LoadFromIE();
81+
82+
CompareProxy(webProxy, resolver, "http://127.0.0.1");
83+
CompareProxy(webProxy, resolver, "https://127.0.0.1");
84+
CompareProxy(webProxy, resolver, "http://localhost");
85+
CompareProxy(webProxy, resolver, "https://localhost");
86+
87+
string hostName = null;
88+
try
89+
{
90+
hostName = Dns.GetHostName();
91+
}
92+
catch{}
93+
94+
if (hostName != null)
95+
{
96+
CompareProxy(webProxy, resolver, "http://" + hostName);
97+
CompareProxy(webProxy, resolver, "https://" + hostName);
98+
}
99+
100+
CompareProxy(webProxy, resolver, "http://google.com");
101+
CompareProxy(webProxy, resolver, "https://google.com");
102+
CompareProxy(webProxy, resolver, "http://bing.com");
103+
CompareProxy(webProxy, resolver, "https://bing.com");
104+
CompareProxy(webProxy, resolver, "http://yahoo.com");
105+
CompareProxy(webProxy, resolver, "https://yahoo.com");
106+
CompareProxy(webProxy, resolver, "http://test.local");
107+
CompareProxy(webProxy, resolver, "https://test.local");
108+
}
109+
110+
private void CompareProxy(IWebProxy webProxy, WinHttpWebProxyFinder resolver, string url)
111+
{
112+
var uri = new Uri(url);
113+
114+
var expectedProxyUri = webProxy.GetProxy(uri);
115+
116+
var proxy = resolver.GetProxy(uri);
117+
118+
if (expectedProxyUri == uri)
119+
{
120+
// no proxy
121+
Assert.AreEqual(proxy, null);
122+
return;
123+
}
124+
125+
Assert.AreEqual(expectedProxyUri.ToString(), $"http://{proxy.HostName}:{proxy.Port}/");
126+
}
127+
}
128+
}

Tests/Titanium.Web.Proxy.UnitTests/Titanium.Web.Proxy.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="CertificateManagerTests.cs" />
6161
<Compile Include="Properties\AssemblyInfo.cs" />
6262
<Compile Include="ProxyServerTests.cs" />
63+
<Compile Include="SystemProxyTest.cs" />
6364
<Compile Include="WinAuthTests.cs" />
6465
</ItemGroup>
6566
<ItemGroup>

0 commit comments

Comments
 (0)