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

Commit 4b86cde

Browse files
committed
asyncify more
1 parent ff0dddf commit 4b86cde

20 files changed

+340
-294
lines changed

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

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Net;
44
using System.Text.RegularExpressions;
5+
using System.Threading.Tasks;
56
using Titanium.Web.Proxy.EventArguments;
67
using Titanium.Web.Proxy.Models;
78

@@ -13,7 +14,7 @@ public void StartProxy()
1314
{
1415
ProxyServer.BeforeRequest += OnRequest;
1516
ProxyServer.BeforeResponse += OnResponse;
16-
ProxyServer.RemoteCertificateValidationCallback += OnCertificateValidation;
17+
ProxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
1718

1819
//Exclude Https addresses you don't want to proxy
1920
//Usefull for clients that use certificate pinning
@@ -61,9 +62,8 @@ public void Stop()
6162
ProxyServer.Stop();
6263
}
6364

64-
//Test On Request, intecept requests
65-
//Read browser URL send back to proxy by the injection script in OnResponse event
66-
public void OnRequest(object sender, SessionEventArgs e)
65+
//intecept & cancel, redirect or update requests
66+
public async Task OnRequest(object sender, SessionEventArgs e)
6767
{
6868
Console.WriteLine(e.WebSession.Request.Url);
6969

@@ -73,39 +73,37 @@ public void OnRequest(object sender, SessionEventArgs e)
7373
if ((e.WebSession.Request.Method.ToUpper() == "POST" || e.WebSession.Request.Method.ToUpper() == "PUT"))
7474
{
7575
//Get/Set request body bytes
76-
byte[] bodyBytes = e.GetRequestBody();
77-
e.SetRequestBody(bodyBytes);
76+
byte[] bodyBytes = await e.GetRequestBody();
77+
await e.SetRequestBody(bodyBytes);
7878

7979
//Get/Set request body as string
80-
string bodyString = e.GetRequestBodyAsString();
81-
e.SetRequestBodyString(bodyString);
80+
string bodyString = await e.GetRequestBodyAsString();
81+
await e.SetRequestBodyString(bodyString);
8282

8383
}
8484

8585
//To cancel a request with a custom HTML content
8686
//Filter URL
8787
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
8888
{
89-
e.Ok("<!DOCTYPE html>" +
90-
"<html><body><h1>" +
91-
"Website Blocked" +
92-
"</h1>" +
93-
"<p>Blocked by titanium web proxy.</p>" +
94-
"</body>" +
95-
"</html>");
89+
await e.Ok("<!DOCTYPE html>" +
90+
"<html><body><h1>" +
91+
"Website Blocked" +
92+
"</h1>" +
93+
"<p>Blocked by titanium web proxy.</p>" +
94+
"</body>" +
95+
"</html>");
9696
}
9797
//Redirect example
9898
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
9999
{
100-
e.Redirect("https://www.paypal.com");
100+
await e.Redirect("https://www.paypal.com");
101101
}
102102
}
103103

104-
//Test script injection
105-
//Insert script to read the Browser URL and send it back to proxy
106-
public void OnResponse(object sender, SessionEventArgs e)
104+
//Modify response
105+
public async Task OnResponse(object sender, SessionEventArgs e)
107106
{
108-
109107
//read response headers
110108
var responseHeaders = e.WebSession.Response.ResponseHeaders;
111109

@@ -116,11 +114,11 @@ public void OnResponse(object sender, SessionEventArgs e)
116114
{
117115
if (e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
118116
{
119-
byte[] bodyBytes = e.GetResponseBody();
120-
e.SetResponseBody(bodyBytes);
117+
byte[] bodyBytes = await e.GetResponseBody();
118+
await e.SetResponseBody(bodyBytes);
121119

122-
string body = e.GetResponseBodyAsString();
123-
e.SetResponseBodyString(body);
120+
string body = await e.GetResponseBodyAsString();
121+
await e.SetResponseBodyString(body);
124122
}
125123
}
126124
}
@@ -131,13 +129,13 @@ public void OnResponse(object sender, SessionEventArgs e)
131129
/// </summary>
132130
/// <param name="sender"></param>
133131
/// <param name="e"></param>
134-
public void OnCertificateValidation(object sender, CertificateValidationEventArgs e)
132+
public async Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
135133
{
136134
//set IsValid to true/false based on Certificate Errors
137135
if (e.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
138136
e.IsValid = true;
139137
else
140-
e.Session.Ok("Cannot validate server certificate! Not safe to proceed.");
138+
await e.Session.Ok("Cannot validate server certificate! Not safe to proceed.");
141139
}
142140
}
143141
}

README.md

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Setup HTTP proxy:
3434
```csharp
3535
ProxyServer.BeforeRequest += OnRequest;
3636
ProxyServer.BeforeResponse += OnResponse;
37-
ProxyServer.RemoteCertificateValidationCallback += OnCertificateValidation;
37+
ProxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
3838

3939
//Exclude Https addresses you don't want to proxy
4040
//Usefull for clients that use certificate pinning
@@ -61,8 +61,7 @@ Setup HTTP proxy:
6161
GenericCertificateName = "google.com"
6262
};
6363
ProxyServer.AddEndPoint(transparentEndPoint);
64-
65-
64+
6665
//ProxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
6766
//ProxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
6867
@@ -87,7 +86,8 @@ Sample request and response event handlers
8786

8887
```csharp
8988

90-
public void OnRequest(object sender, SessionEventArgs e)
89+
//intecept & cancel, redirect or update requests
90+
public async Task OnRequest(object sender, SessionEventArgs e)
9191
{
9292
Console.WriteLine(e.WebSession.Request.Url);
9393

@@ -97,37 +97,37 @@ Sample request and response event handlers
9797
if ((e.WebSession.Request.Method.ToUpper() == "POST" || e.WebSession.Request.Method.ToUpper() == "PUT"))
9898
{
9999
//Get/Set request body bytes
100-
byte[] bodyBytes = e.GetRequestBody();
101-
e.SetRequestBody(bodyBytes);
100+
byte[] bodyBytes = await e.GetRequestBody();
101+
await e.SetRequestBody(bodyBytes);
102102

103103
//Get/Set request body as string
104-
string bodyString = e.GetRequestBodyAsString();
105-
e.SetRequestBodyString(bodyString);
104+
string bodyString = await e.GetRequestBodyAsString();
105+
await e.SetRequestBodyString(bodyString);
106106

107107
}
108108

109109
//To cancel a request with a custom HTML content
110110
//Filter URL
111111
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
112112
{
113-
e.Ok("<!DOCTYPE html>" +
114-
"<html><body><h1>" +
115-
"Website Blocked" +
116-
"</h1>" +
117-
"<p>Blocked by titanium web proxy.</p>" +
118-
"</body>" +
119-
"</html>");
113+
await e.Ok("<!DOCTYPE html>" +
114+
"<html><body><h1>" +
115+
"Website Blocked" +
116+
"</h1>" +
117+
"<p>Blocked by titanium web proxy.</p>" +
118+
"</body>" +
119+
"</html>");
120120
}
121121
//Redirect example
122122
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
123123
{
124-
e.Redirect("https://www.paypal.com");
124+
await e.Redirect("https://www.paypal.com");
125125
}
126126
}
127-
128-
public void OnResponse(object sender, SessionEventArgs e)
129-
{
130127

128+
//Modify response
129+
public async Task OnResponse(object sender, SessionEventArgs e)
130+
{
131131
//read response headers
132132
var responseHeaders = e.WebSession.Response.ResponseHeaders;
133133

@@ -138,24 +138,28 @@ Sample request and response event handlers
138138
{
139139
if (e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
140140
{
141-
byte[] bodyBytes = e.GetResponseBody();
142-
e.SetResponseBody(bodyBytes);
141+
byte[] bodyBytes = await e.GetResponseBody();
142+
await e.SetResponseBody(bodyBytes);
143143

144-
string body = e.GetResponseBodyAsString();
145-
e.SetResponseBodyString(body);
144+
string body = await e.GetResponseBodyAsString();
145+
await e.SetResponseBodyString(body);
146146
}
147147
}
148148
}
149149
}
150150

151-
// Allows overriding default certificate validation logic
152-
public void OnCertificateValidation(object sender, CertificateValidationEventArgs e)
151+
/// <summary>
152+
/// Allows overriding default certificate validation logic
153+
/// </summary>
154+
/// <param name="sender"></param>
155+
/// <param name="e"></param>
156+
public async Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
153157
{
154158
//set IsValid to true/false based on Certificate Errors
155159
if (e.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
156160
e.IsValid = true;
157161
else
158-
e.Session.Ok("Cannot validate server certificate! Not safe to proceed.");
162+
await e.Session.Ok("Cannot validate server certificate! Not safe to proceed.");
159163
}
160164
```
161165
Future roadmap

Titanium.Web.Proxy/Compression/DeflateCompression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using System.IO;
22
using System.IO.Compression;
3+
using System.Threading.Tasks;
34

45
namespace Titanium.Web.Proxy.Compression
56
{
67
class DeflateCompression : ICompression
78
{
8-
public byte[] Compress(byte[] responseBody)
9+
public async Task<byte[]> Compress(byte[] responseBody)
910
{
1011
using (var ms = new MemoryStream())
1112
{
1213
using (var zip = new DeflateStream(ms, CompressionMode.Compress, true))
1314
{
14-
zip.Write(responseBody, 0, responseBody.Length);
15+
await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
1516
}
1617

1718
return ms.ToArray();

Titanium.Web.Proxy/Compression/GZipCompression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using Ionic.Zlib;
22
using System.IO;
3+
using System.Threading.Tasks;
34

45
namespace Titanium.Web.Proxy.Compression
56
{
67
class GZipCompression : ICompression
78
{
8-
public byte[] Compress(byte[] responseBody)
9+
public async Task<byte[]> Compress(byte[] responseBody)
910
{
1011
using (var ms = new MemoryStream())
1112
{
1213
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
1314
{
14-
zip.Write(responseBody, 0, responseBody.Length);
15+
await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
1516
}
1617

1718
return ms.ToArray();
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace Titanium.Web.Proxy.Compression
1+
using System.Threading.Tasks;
2+
3+
namespace Titanium.Web.Proxy.Compression
24
{
35
interface ICompression
46
{
5-
byte[] Compress(byte[] responseBody);
7+
Task<byte[]> Compress(byte[] responseBody);
68
}
79
}

Titanium.Web.Proxy/Compression/ZlibCompression.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using Ionic.Zlib;
22
using System.IO;
3+
using System.Threading.Tasks;
34

45
namespace Titanium.Web.Proxy.Compression
56
{
67
class ZlibCompression : ICompression
78
{
8-
public byte[] Compress(byte[] responseBody)
9+
public async Task<byte[]> Compress(byte[] responseBody)
910
{
1011
using (var ms = new MemoryStream())
1112
{
1213
using (var zip = new ZlibStream(ms, CompressionMode.Compress, true))
1314
{
14-
zip.Write(responseBody, 0, responseBody.Length);
15+
await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
1516
}
1617

1718
return ms.ToArray();
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
namespace Titanium.Web.Proxy.Decompression
1+
using System.Threading.Tasks;
2+
3+
namespace Titanium.Web.Proxy.Decompression
24
{
35
class DefaultDecompression : IDecompression
46
{
5-
public byte[] Decompress(byte[] compressedArray)
7+
public Task<byte[]> Decompress(byte[] compressedArray)
68
{
7-
return compressedArray;
9+
return Task.FromResult(compressedArray);
810
}
911
}
1012
}

Titanium.Web.Proxy/Decompression/DeflateDecompression.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using Ionic.Zlib;
22
using System.IO;
3+
using System.Threading.Tasks;
34
using Titanium.Web.Proxy.Shared;
45

56
namespace Titanium.Web.Proxy.Decompression
67
{
78
class DeflateDecompression : IDecompression
89
{
9-
public byte[] Decompress(byte[] compressedArray)
10+
public async Task<byte[]> Decompress(byte[] compressedArray)
1011
{
1112
var stream = new MemoryStream(compressedArray);
1213

@@ -17,9 +18,9 @@ public byte[] Decompress(byte[] compressedArray)
1718
using (var output = new MemoryStream())
1819
{
1920
int read;
20-
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
21+
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
2122
{
22-
output.Write(buffer, 0, read);
23+
await output.WriteAsync(buffer, 0, read).ConfigureAwait(false);
2324
}
2425

2526
return output.ToArray();

Titanium.Web.Proxy/Decompression/GZipDecompression.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
using System.IO;
22
using System.IO.Compression;
3+
using System.Threading.Tasks;
34
using Titanium.Web.Proxy.Shared;
45

56
namespace Titanium.Web.Proxy.Decompression
67
{
78
class GZipDecompression : IDecompression
89
{
9-
public byte[] Decompress(byte[] compressedArray)
10+
public async Task<byte[]> Decompress(byte[] compressedArray)
1011
{
1112
using (var decompressor = new GZipStream(new MemoryStream(compressedArray), CompressionMode.Decompress))
1213
{
1314
var buffer = new byte[Constants.BUFFER_SIZE];
1415
using (var output = new MemoryStream())
1516
{
1617
int read;
17-
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
18+
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
1819
{
19-
output.Write(buffer, 0, read);
20+
await output.WriteAsync(buffer, 0, read).ConfigureAwait(false);
2021
}
2122
return output.ToArray();
2223
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.IO;
2+
using System.Threading.Tasks;
23

34
namespace Titanium.Web.Proxy.Decompression
45
{
56
interface IDecompression
67
{
7-
byte[] Decompress(byte[] compressedArray);
8+
Task<byte[]> Decompress(byte[] compressedArray);
89
}
910
}

0 commit comments

Comments
 (0)