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

Commit 4389585

Browse files
Merge pull request #174 from justcoding121/develop
Merge Develop to Beta
2 parents cd7e374 + 9020e09 commit 4389585

File tree

12 files changed

+145
-12
lines changed

12 files changed

+145
-12
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ public void StartProxy()
2828
proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
2929
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
3030

31-
//Exclude Https addresses you don't want to proxy
32-
//Usefull for clients that use certificate pinning
33-
//for example dropbox.com
3431
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true)
3532
{
33+
//Exclude Https addresses you don't want to proxy
34+
//Usefull for clients that use certificate pinning
35+
//for example dropbox.com
3636
// ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }
37+
38+
//Use self-issued generic certificate on all https requests
39+
//Optimizes performance by not creating a certificate for each https-enabled domain
40+
//Usefull when certificate trust is not requiered by proxy clients
41+
// GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
3742
};
3843

3944
//An explicit endpoint is where the client knows about the existance of a proxy

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ Setup HTTP proxy:
4949
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
5050

5151

52-
//Exclude Https addresses you don't want to proxy
53-
//Usefull for clients that use certificate pinning
54-
//for example dropbox.com
5552
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true)
5653
{
54+
//Exclude Https addresses you don't want to proxy
55+
//Usefull for clients that use certificate pinning
56+
//for example dropbox.com
5757
// ExcludedHttpsHostNameRegex = new List<string>() { "google.com", "dropbox.com" }
58+
59+
//Use self-issued generic certificate on all https requests
60+
//Optimizes performance by not creating a certificate for each https-enabled domain
61+
//Usefull when certificate trust is not requiered by proxy clients
62+
// GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
5863
};
5964

6065
//An explicit endpoint is where the client knows about the existance of a proxy

Tests/Titanium.Web.Proxy.UnitTests/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Reflection;
2-
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43

54
// General Information about an assembly is controlled through the following
596 Bytes
Binary file not shown.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
<ErrorReport>prompt</ErrorReport>
3535
<WarningLevel>4</WarningLevel>
3636
</PropertyGroup>
37+
<PropertyGroup>
38+
<SignAssembly>true</SignAssembly>
39+
</PropertyGroup>
40+
<PropertyGroup>
41+
<AssemblyOriginatorKeyFile>StrongNameKey.snk</AssemblyOriginatorKeyFile>
42+
</PropertyGroup>
3743
<ItemGroup>
3844
<Reference Include="System" />
3945
</ItemGroup>
@@ -60,6 +66,9 @@
6066
<Name>Titanium.Web.Proxy</Name>
6167
</ProjectReference>
6268
</ItemGroup>
69+
<ItemGroup>
70+
<None Include="StrongNameKey.snk" />
71+
</ItemGroup>
6372
<Choose>
6473
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
6574
<ItemGroup>

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ public async Task Ok(byte[] result)
405405
/// <param name="headers"></param>
406406
public async Task Ok(byte[] result, Dictionary<string, HttpHeader> headers)
407407
{
408-
var response = new OkResponse();
408+
var response = new OkResponse();           
409+
409410
if (headers != null && headers.Count > 0)
410411
{
411412
response.ResponseHeaders = headers;
@@ -417,7 +418,73 @@ public async Task Ok(byte[] result, Dictionary<string, HttpHeader> headers)
417418

418419
WebSession.Request.CancelRequest = true;
419420
}
420-
421+
422+
/// <summary>
423+
/// Before request is made to server 
424+
/// Respond with the specified HTML string to client
425+
/// and ignore the request 
426+
/// </summary>
427+
/// <param name="html"></param>
428+
/// <param name="status"></param>
429+
        public async Task GenericResponse(string html, HttpStatusCode status)       
430+
{
431+
            await GenericResponse(html, null, status);
432+
}
433+
434+
/// <summary>
435+
/// Before request is made to server 
436+
/// Respond with the specified HTML string to client
437+
/// and the specified status
438+
/// and ignore the request 
439+
/// </summary>
440+
/// <param name="html"></param>
441+
/// <param name="headers"></param>
442+
/// <param name="status"></param>
443+
        public async Task GenericResponse(string html, Dictionary<string, HttpHeader> headers, HttpStatusCode status)
444+
{           
445+
if (WebSession.Request.RequestLocked)           
446+
{               
447+
throw new Exception("You cannot call this function after request is made to server.");           
448+
}
449+
450+
if (html == null)
451+
{               
452+
html = string.Empty;           
453+
}
454+
455+
var result = Encoding.Default.GetBytes(html);
456+
457+
await GenericResponse(result, headers, status);       
458+
}
459+
460+
/// <summary>
461+
/// Before request is made to server
462+
/// Respond with the specified byte[] to client
463+
/// and the specified status
464+
/// and ignore the request
465+
/// </summary>
466+
/// <param name="result"></param>
467+
/// <param name="headers"></param>
468+
/// <param name="status"></param>
469+
/// <returns></returns>
470+
public async Task GenericResponse(byte[] result, Dictionary<string, HttpHeader> headers, HttpStatusCode status)
471+
{
472+
var response = new GenericResponse(status);
473+
474+
if (headers != null && headers.Count > 0)
475+
{
476+
response.ResponseHeaders = headers;
477+
}
478+
479+
response.HttpVersion = WebSession.Request.HttpVersion;
480+
481+
response.ResponseBody = result;
482+
483+
await Respond(response);
484+
485+
WebSession.Request.CancelRequest = true;
486+
}
487+
421488
public async Task Redirect(string url)
422489
{
423490
var response = new RedirectResponse();
@@ -452,4 +519,4 @@ public void Dispose()
452519

453520
}
454521
}
455-
}
522+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Net;
2+
3+
namespace Titanium.Web.Proxy.Http.Responses
4+
{
5+
    /// <summary>
6+
    /// Anything other than a 200 or 302 response
7+
    /// </summary>
8+
    public class GenericResponse : Response
9+
    {
10+
        public GenericResponse(HttpStatusCode status)
11+
        {
12+
            ResponseStatusCode = ((int)status).ToString();
13+
            ResponseStatusDescription = status.ToString(); 
14+
        }
15+
16+
        public GenericResponse(string statusCode, string statusDescription)
17+
        {
18+
            ResponseStatusCode = statusCode;
19+
            ResponseStatusDescription = statusCode;
20+
        }
21+
    }
22+
}

Titanium.Web.Proxy/Models/EndPoint.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Net;
33
using System.Net.Sockets;
4+
using System.Security.Cryptography.X509Certificates;
45

56
namespace Titanium.Web.Proxy.Models
67
{
@@ -38,6 +39,8 @@ public class ExplicitProxyEndPoint : ProxyEndPoint
3839

3940
public List<string> ExcludedHttpsHostNameRegex { get; set; }
4041

42+
public X509Certificate2 GenericCertificate { get; set; }
43+
4144
public ExplicitProxyEndPoint(IPAddress IpAddress, int Port, bool EnableSsl)
4245
: base(IpAddress, Port, EnableSsl)
4346
{

Titanium.Web.Proxy/Properties/AssemblyInfo.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
[assembly: AssemblyCopyright("Copyright © 2015")]
1515
[assembly: AssemblyTrademark("")]
1616
[assembly: AssemblyCulture("")]
17-
[assembly: InternalsVisibleTo("Titanium.Web.Proxy.UnitTests")]
17+
[assembly: InternalsVisibleTo("Titanium.Web.Proxy.UnitTests, PublicKey=" +
18+
"0024000004800000940000000602000000240000525341310004000001000100e7368e0ccc717e" +
19+
"eb4d57d35ad6a8305cbbed14faa222e13869405e92c83856266d400887d857005f1393ffca2b92" +
20+
"de7f3ba0bdad35ec2d6057ee1846091b34be2abc3f97dc7e72c16fd4958c15126b12923df76964" +
21+
"7d84922c3f4f3b80ee0ae8e4cb40bc1973b782afb90bb00519fd16adf960f217e23696e7c31654" +
22+
"01d0acd6")]
1823

1924
// Setting ComVisible to false makes the types in this assembly not visible
2025
// to COM components. If you need to access a type in this assembly from

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,17 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClient tcpCli
113113
{
114114

115115
sslStream = new SslStream(clientStream, true);
116-
var certificate = certificateCacheManager.CreateCertificate(httpRemoteUri.Host, false);
116+
117+
X509Certificate2 certificate;
118+
if (endPoint.GenericCertificate != null)
119+
{
120+
certificate = endPoint.GenericCertificate;
121+
}
122+
else
123+
{
124+
certificate = certificateCacheManager.CreateCertificate(httpRemoteUri.Host, false);
125+
}
126+
117127
//Successfully managed to authenticate the client using the fake certificate
118128
await sslStream.AuthenticateAsServerAsync(certificate, false,
119129
SupportedSslProtocols, false);

0 commit comments

Comments
 (0)