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

Commit 7d11594

Browse files
committed
Merge pull request #41 from titanium007/release
Release 2.1
2 parents 60c23d7 + 1b650f0 commit 7d11594

15 files changed

+573
-128
lines changed

.build/default.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if(!$Configuration) { $Configuration = $env:Configuration }
2121
if(!$Configuration) { $Configuration = "Release" }
2222

2323
if(!$Version) { $Version = $env:APPVEYOR_BUILD_VERSION }
24-
if(!$Version) { $Version = "1.0.$BuildNumber" }
24+
if(!$Version) { $Version = "2.0.$BuildNumber" }
2525

2626
if(!$Branch) { $Branch = $env:APPVEYOR_REPO_BRANCH }
2727
if(!$Branch) { $Branch = "local" }

README.md

+43-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Refer the HTTP Proxy Server library in your project, look up Test project to lea
2222

2323
Install by nuget:
2424

25-
Install-Package Titanium.Web.Proxy
25+
Install-Package Titanium.Web.Proxy -Pre
2626

2727
After installing nuget package mark following files to be copied to app directory
2828

@@ -34,13 +34,46 @@ Setup HTTP proxy:
3434

3535
```csharp
3636
// listen to client request & server response events
37-
ProxyServer.BeforeRequest += OnRequest;
38-
ProxyServer.BeforeResponse += OnResponse;
39-
40-
ProxyServer.EnableSSL = true;
41-
ProxyServer.SetAsSystemProxy = true;
37+
ProxyServer.BeforeRequest += OnRequest;
38+
ProxyServer.BeforeResponse += OnResponse;
39+
40+
//Exclude Https addresses you don't want to proxy
41+
//Usefull for clients that use certificate pinning
42+
//for example dropbox.com
43+
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true){
44+
ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
45+
};
46+
47+
//An explicit endpoint is where the client knows about the existance of a proxy
48+
//So client sends request in a proxy friendly manner
49+
ProxyServer.AddEndPoint(explicitEndPoint);
4250
ProxyServer.Start();
43-
51+
52+
53+
//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
54+
//A transparent endpoint usually requires a network router port forwarding HTTP(S) packets to this endpoint
55+
//Currently do not support Server Name Indication (It is not currently supported by SslStream class)
56+
//That means that the transparent endpoint will always provide the same Generic Certificate to all HTTPS requests
57+
//In this example only google.com will work for HTTPS requests
58+
//Other sites will receive a certificate mismatch warning on browser
59+
//Please read about it before asking questions!
60+
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Any, 8001, true) {
61+
GenericCertificateName = "google.com"
62+
};
63+
ProxyServer.AddEndPoint(transparentEndPoint);
64+
65+
66+
foreach (var endPoint in ProxyServer.ProxyEndPoints)
67+
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
68+
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
69+
70+
//You can also add/remove end points after proxy has been started
71+
ProxyServer.RemoveEndPoint(transparentEndPoint);
72+
73+
//Only explicit proxies can be set as system proxy!
74+
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
75+
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
76+
4477
//wait here (You can use something else as a wait function, I am using this as a demo)
4578
Console.Read();
4679

@@ -113,6 +146,8 @@ Sample request and response event handlers
113146
```
114147
Future updates
115148
============
149+
* Add callbacks for client/server certificate validation/selection
116150
* Support mutual authentication
151+
* Add Server Name Indication (SNI) for transparent endpoints
117152
* Support HTTP 2.0
118-
* Support modification of web socket requests
153+

Titanium.Web.Proxy.Test/Program.cs

-16
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ public static void Main(string[] args)
1414
NativeMethods.SetConsoleCtrlHandler(NativeMethods.Handler, true);
1515

1616

17-
Console.Write("Do you want to monitor HTTPS? (Y/N):");
18-
19-
var readLine = Console.ReadLine();
20-
if (readLine != null && readLine.Trim().ToLower() == "y")
21-
{
22-
Controller.EnableSsl = true;
23-
}
24-
25-
Console.Write("Do you want to set this as a System Proxy? (Y/N):");
26-
27-
var line = Console.ReadLine();
28-
if (line != null && line.Trim().ToLower() == "y")
29-
{
30-
Controller.SetAsSystemProxy = true;
31-
}
32-
3317
//Start proxy controller
3418
Controller.StartProxy();
3519

Titanium.Web.Proxy.Test/ProxyTestController.cs

+33-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,57 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
24
using System.Text.RegularExpressions;
35
using Titanium.Web.Proxy.EventArguments;
6+
using Titanium.Web.Proxy.Models;
47

58
namespace Titanium.Web.Proxy.Test
69
{
710
public class ProxyTestController
811
{
9-
public int ListeningPort { get; set; }
10-
public bool EnableSsl { get; set; }
11-
public bool SetAsSystemProxy { get; set; }
12+
1213

1314
public void StartProxy()
1415
{
1516
ProxyServer.BeforeRequest += OnRequest;
1617
ProxyServer.BeforeResponse += OnResponse;
1718

18-
ProxyServer.EnableSsl = EnableSsl;
19-
20-
ProxyServer.SetAsSystemProxy = SetAsSystemProxy;
21-
2219
//Exclude Https addresses you don't want to proxy
2320
//Usefull for clients that use certificate pinning
2421
//for example dropbox.com
25-
ProxyServer.ExcludedHttpsHostNameRegex.Add(".dropbox.com");
22+
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true){
23+
ExcludedHttpsHostNameRegex = new List<string>() { "dropbox.com" }
24+
};
2625

26+
//An explicit endpoint is where the client knows about the existance of a proxy
27+
//So client sends request in a proxy friendly manner
28+
ProxyServer.AddEndPoint(explicitEndPoint);
2729
ProxyServer.Start();
2830

29-
ProxyServer.ListeningPort = ProxyServer.ListeningPort;
31+
32+
//Transparent endpoint is usefull for reverse proxying (client is not aware of the existance of proxy)
33+
//A transparent endpoint usually requires a network router port forwarding HTTP(S) packets to this endpoint
34+
//Currently do not support Server Name Indication (It is not currently supported by SslStream class)
35+
//That means that the transparent endpoint will always provide the same Generic Certificate to all HTTPS requests
36+
//In this example only google.com will work for HTTPS requests
37+
//Other sites will receive a certificate mismatch warning on browser
38+
//Please read about it before asking questions!
39+
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Any, 8001, true) {
40+
GenericCertificateName = "google.com"
41+
};
42+
ProxyServer.AddEndPoint(transparentEndPoint);
43+
44+
45+
foreach (var endPoint in ProxyServer.ProxyEndPoints)
46+
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
47+
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
3048

31-
Console.WriteLine("Proxy listening on local machine port: {0} ", ProxyServer.ListeningPort);
49+
//You can also add/remove end points after proxy has been started
50+
ProxyServer.RemoveEndPoint(transparentEndPoint);
51+
52+
//Only explicit proxies can be set as system proxy!
53+
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
54+
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
3255
}
3356

3457
public void Stop()
@@ -39,7 +62,6 @@ public void Stop()
3962
ProxyServer.Stop();
4063
}
4164

42-
4365
//Test On Request, intecept requests
4466
//Read browser URL send back to proxy by the injection script in OnResponse event
4567
public void OnRequest(object sender, SessionEventArgs e)

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ public static class HttpWebResponseExtensions
88
{
99
public static Encoding GetResponseEncoding(this HttpWebSession response)
1010
{
11-
if (string.IsNullOrEmpty(response.Response.CharacterSet)) return Encoding.GetEncoding("ISO-8859-1");
12-
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
11+
if (string.IsNullOrEmpty(response.Response.CharacterSet))
12+
return Encoding.GetEncoding("ISO-8859-1");
13+
14+
try
15+
{
16+
return Encoding.GetEncoding(response.Response.CharacterSet.Replace(@"""", string.Empty));
17+
}
18+
catch { return Encoding.GetEncoding("ISO-8859-1"); }
1319
}
1420
}
1521
}

Titanium.Web.Proxy/Extensions/StreamExtensions.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ public static class StreamHelper
88
{
99
public static void CopyToAsync(this Stream input, string initialData, Stream output, int bufferSize)
1010
{
11-
var bytes = Encoding.ASCII.GetBytes(initialData);
12-
output.Write(bytes, 0, bytes.Length);
11+
if(!string.IsNullOrEmpty(initialData))
12+
{
13+
var bytes = Encoding.ASCII.GetBytes(initialData);
14+
output.Write(bytes, 0, bytes.Length);
15+
}
16+
1317
CopyToAsync(input, output, bufferSize);
1418
}
1519

1620
//http://stackoverflow.com/questions/1540658/net-asynchronous-stream-read-write
17-
public static void CopyToAsync(this Stream input, Stream output, int bufferSize)
21+
private static void CopyToAsync(this Stream input, Stream output, int bufferSize)
1822
{
1923
try
2024
{

0 commit comments

Comments
 (0)