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

Commit 703f521

Browse files
Merge pull request #80 from justcoding121/cleanup
Clean up
2 parents a4e2e3e + 3042d21 commit 703f521

38 files changed

+1279
-666
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net.Security;
4+
using System.Security.Cryptography.X509Certificates;
5+
using System.Threading.Tasks;
6+
using Titanium.Web.Proxy.EventArguments;
7+
8+
namespace Titanium.Web.Proxy
9+
{
10+
public partial class ProxyServer
11+
{
12+
/// <summary>
13+
/// Call back to override server certificate validation
14+
/// </summary>
15+
/// <param name="sender"></param>
16+
/// <param name="certificate"></param>
17+
/// <param name="chain"></param>
18+
/// <param name="sslPolicyErrors"></param>
19+
/// <returns></returns>
20+
internal static bool ValidateServerCertificate(
21+
object sender,
22+
X509Certificate certificate,
23+
X509Chain chain,
24+
SslPolicyErrors sslPolicyErrors)
25+
{
26+
//if user callback is registered then do it
27+
if (ServerCertificateValidationCallback != null)
28+
{
29+
var args = new CertificateValidationEventArgs();
30+
31+
args.Certificate = certificate;
32+
args.Chain = chain;
33+
args.SslPolicyErrors = sslPolicyErrors;
34+
35+
36+
Delegate[] invocationList = ServerCertificateValidationCallback.GetInvocationList();
37+
Task[] handlerTasks = new Task[invocationList.Length];
38+
39+
for (int i = 0; i < invocationList.Length; i++)
40+
{
41+
handlerTasks[i] = ((Func<object, CertificateValidationEventArgs, Task>)invocationList[i])(null, args);
42+
}
43+
44+
Task.WhenAll(handlerTasks).Wait();
45+
46+
return args.IsValid;
47+
}
48+
49+
if (sslPolicyErrors == SslPolicyErrors.None)
50+
return true;
51+
52+
//By default
53+
//do not allow this client to communicate with unauthenticated servers.
54+
return false;
55+
}
56+
57+
/// <summary>
58+
/// Call back to select client certificate used for mutual authentication
59+
/// </summary>
60+
/// <param name="sender"></param>
61+
/// <param name="certificate"></param>
62+
/// <param name="chain"></param>
63+
/// <param name="sslPolicyErrors"></param>
64+
/// <returns></returns>
65+
internal static X509Certificate SelectClientCertificate(
66+
object sender,
67+
string targetHost,
68+
X509CertificateCollection localCertificates,
69+
X509Certificate remoteCertificate,
70+
string[] acceptableIssuers)
71+
{
72+
X509Certificate clientCertificate = null;
73+
var customSslStream = sender as SslStream;
74+
75+
if (acceptableIssuers != null &&
76+
acceptableIssuers.Length > 0 &&
77+
localCertificates != null &&
78+
localCertificates.Count > 0)
79+
{
80+
// Use the first certificate that is from an acceptable issuer.
81+
foreach (X509Certificate certificate in localCertificates)
82+
{
83+
string issuer = certificate.Issuer;
84+
if (Array.IndexOf(acceptableIssuers, issuer) != -1)
85+
clientCertificate = certificate;
86+
}
87+
}
88+
89+
if (localCertificates != null &&
90+
localCertificates.Count > 0)
91+
clientCertificate = localCertificates[0];
92+
93+
//If user call back is registered
94+
if (ClientCertificateSelectionCallback != null)
95+
{
96+
var args = new CertificateSelectionEventArgs();
97+
98+
args.targetHost = targetHost;
99+
args.localCertificates = localCertificates;
100+
args.remoteCertificate = remoteCertificate;
101+
args.acceptableIssuers = acceptableIssuers;
102+
args.clientCertificate = clientCertificate;
103+
104+
Delegate[] invocationList = ClientCertificateSelectionCallback.GetInvocationList();
105+
Task[] handlerTasks = new Task[invocationList.Length];
106+
107+
for (int i = 0; i < invocationList.Length; i++)
108+
{
109+
handlerTasks[i] = ((Func<object, CertificateSelectionEventArgs, Task>)invocationList[i])(null, args);
110+
}
111+
112+
Task.WhenAll(handlerTasks).Wait();
113+
114+
return args.clientCertificate;
115+
}
116+
117+
return clientCertificate;
118+
119+
}
120+
}
121+
}

Titanium.Web.Proxy/Compression/CompressionFactory.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
namespace Titanium.Web.Proxy.Compression
22
{
3-
class CompressionFactory
3+
/// <summary>
4+
/// A factory to generate the compression methods based on the type of compression
5+
/// </summary>
6+
internal class CompressionFactory
47
{
58
public ICompression Create(string type)
69
{

Titanium.Web.Proxy/Compression/DeflateCompression.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
namespace Titanium.Web.Proxy.Compression
66
{
7-
class DeflateCompression : ICompression
7+
/// <summary>
8+
/// Concrete implementation of deflate compression
9+
/// </summary>
10+
internal class DeflateCompression : ICompression
811
{
912
public async Task<byte[]> Compress(byte[] responseBody)
1013
{
1114
using (var ms = new MemoryStream())
1215
{
1316
using (var zip = new DeflateStream(ms, CompressionMode.Compress, true))
1417
{
15-
await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
18+
await zip.WriteAsync(responseBody, 0, responseBody.Length);
1619
}
1720

1821
return ms.ToArray();

Titanium.Web.Proxy/Compression/GZipCompression.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
namespace Titanium.Web.Proxy.Compression
66
{
7-
class GZipCompression : ICompression
7+
/// <summary>
8+
/// concreate implementation of gzip compression
9+
/// </summary>
10+
internal class GZipCompression : ICompression
811
{
912
public async Task<byte[]> Compress(byte[] responseBody)
1013
{
1114
using (var ms = new MemoryStream())
1215
{
1316
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
1417
{
15-
await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
18+
await zip.WriteAsync(responseBody, 0, responseBody.Length);
1619
}
1720

1821
return ms.ToArray();

Titanium.Web.Proxy/Compression/ICompression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Titanium.Web.Proxy.Compression
44
{
5+
/// <summary>
6+
/// An inteface for http compression
7+
/// </summary>
58
interface ICompression
69
{
710
Task<byte[]> Compress(byte[] responseBody);

Titanium.Web.Proxy/Compression/ZlibCompression.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
namespace Titanium.Web.Proxy.Compression
66
{
7-
class ZlibCompression : ICompression
7+
/// <summary>
8+
/// concrete implementation of zlib compression
9+
/// </summary>
10+
internal class ZlibCompression : ICompression
811
{
912
public async Task<byte[]> Compress(byte[] responseBody)
1013
{
1114
using (var ms = new MemoryStream())
1215
{
1316
using (var zip = new ZlibStream(ms, CompressionMode.Compress, true))
1417
{
15-
await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
18+
await zip.WriteAsync(responseBody, 0, responseBody.Length);
1619
}
1720

1821
return ms.ToArray();

Titanium.Web.Proxy/Decompression/DecompressionFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace Titanium.Web.Proxy.Decompression
22
{
3+
/// <summary>
4+
/// A factory to generate the de-compression methods based on the type of compression
5+
/// </summary>
36
internal class DecompressionFactory
47
{
58
internal IDecompression Create(string type)

Titanium.Web.Proxy/Decompression/DefaultDecompression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Titanium.Web.Proxy.Decompression
44
{
5+
6+
/// <summary>
7+
/// When no compression is specified just return the byte array
8+
/// </summary>
59
internal class DefaultDecompression : IDecompression
610
{
711
public Task<byte[]> Decompress(byte[] compressedArray)

Titanium.Web.Proxy/Decompression/DeflateDecompression.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Titanium.Web.Proxy.Decompression
77
{
8+
/// <summary>
9+
/// concrete implementation of deflate de-compression
10+
/// </summary>
811
internal class DeflateDecompression : IDecompression
912
{
1013
public async Task<byte[]> Decompress(byte[] compressedArray)
@@ -18,9 +21,9 @@ public async Task<byte[]> Decompress(byte[] compressedArray)
1821
using (var output = new MemoryStream())
1922
{
2023
int read;
21-
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
24+
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length)) > 0)
2225
{
23-
await output.WriteAsync(buffer, 0, read).ConfigureAwait(false);
26+
await output.WriteAsync(buffer, 0, read);
2427
}
2528

2629
return output.ToArray();

Titanium.Web.Proxy/Decompression/GZipDecompression.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Titanium.Web.Proxy.Decompression
77
{
8+
/// <summary>
9+
/// concrete implementation of gzip de-compression
10+
/// </summary>
811
internal class GZipDecompression : IDecompression
912
{
1013
public async Task<byte[]> Decompress(byte[] compressedArray)
@@ -15,9 +18,9 @@ public async Task<byte[]> Decompress(byte[] compressedArray)
1518
using (var output = new MemoryStream())
1619
{
1720
int read;
18-
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
21+
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length)) > 0)
1922
{
20-
await output.WriteAsync(buffer, 0, read).ConfigureAwait(false);
23+
await output.WriteAsync(buffer, 0, read);
2124
}
2225
return output.ToArray();
2326
}

Titanium.Web.Proxy/Decompression/IDecompression.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.IO;
2-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
32

43
namespace Titanium.Web.Proxy.Decompression
54
{
5+
/// <summary>
6+
/// An interface for decompression
7+
/// </summary>
68
internal interface IDecompression
79
{
810
Task<byte[]> Decompress(byte[] compressedArray);

Titanium.Web.Proxy/Decompression/ZlibDecompression.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Titanium.Web.Proxy.Decompression
77
{
8+
/// <summary>
9+
/// concrete implemetation of zlib de-compression
10+
/// </summary>
811
internal class ZlibDecompression : IDecompression
912
{
1013
public async Task<byte[]> Decompress(byte[] compressedArray)
@@ -17,9 +20,9 @@ public async Task<byte[]> Decompress(byte[] compressedArray)
1720
using (var output = new MemoryStream())
1821
{
1922
int read;
20-
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
23+
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length)) > 0)
2124
{
22-
await output.WriteAsync(buffer, 0, read).ConfigureAwait(false);
25+
await output.WriteAsync(buffer, 0, read);
2326
}
2427
return output.ToArray();
2528
}

Titanium.Web.Proxy/EventArguments/CertificateSelectionEventArgs.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Titanium.Web.Proxy.EventArguments
55
{
6+
/// <summary>
7+
/// An argument passed on to user for client certificate selection during mutual SSL authentication
8+
/// </summary>
69
public class CertificateSelectionEventArgs : EventArgs, IDisposable
710
{
811
public object sender { get; internal set; }

Titanium.Web.Proxy/EventArguments/CertificateValidationEventArgs.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Titanium.Web.Proxy.EventArguments
66
{
7+
/// <summary>
8+
/// An argument passed on to the user for validating the server certificate during SSL authentication
9+
/// </summary>
710
public class CertificateValidationEventArgs : EventArgs, IDisposable
811
{
912
public X509Certificate Certificate { get; internal set; }

0 commit comments

Comments
 (0)