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

Commit 87eb8c7

Browse files
committed
Merge pull request #64 from justcoding121/pr/62
Pr/62
2 parents cd254be + 82dac79 commit 87eb8c7

24 files changed

+396
-116
lines changed

Titanium.Web.Proxy.Test/App.config

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
1+
<?xml version="1.0" encoding="utf-8"?>
32
<configuration>
43
<startup>
5-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
65
</startup>
76
<runtime>
87
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
98
<dependentAssembly>
10-
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
11-
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
9+
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
10+
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0"/>
1211
</dependentAssembly>
1312
<dependentAssembly>
14-
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
15-
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
13+
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
14+
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0"/>
1615
</dependentAssembly>
1716
</assemblyBinding>
1817
</runtime>
1918

20-
</configuration>
19+
</configuration>

Titanium.Web.Proxy.Test/ProxyTestController.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ public void StartProxy()
4646
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
4747
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
4848

49-
//You can also add/remove end points after proxy has been started
50-
ProxyServer.RemoveEndPoint(transparentEndPoint);
51-
5249
//Only explicit proxies can be set as system proxy!
5350
ProxyServer.SetAsSystemHttpProxy(explicitEndPoint);
5451
ProxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
@@ -79,16 +76,26 @@ public void OnRequest(object sender, SessionEventArgs e)
7976

8077
//Get/Set request body as string
8178
string bodyString = e.GetRequestBodyAsString();
82-
e.SetRequestBodyString(bodyString);
79+
e.SetRequestBodyString(bodyString);
8380

8481
}
85-
82+
8683
//To cancel a request with a custom HTML content
8784
//Filter URL
88-
8985
if (e.ProxySession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
9086
{
91-
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
87+
e.Ok("<!DOCTYPE html>" +
88+
"<html><body><h1>" +
89+
"Website Blocked" +
90+
"</h1>" +
91+
"<p>Blocked by titanium web proxy.</p>" +
92+
"</body>" +
93+
"</html>");
94+
}
95+
//Redirect example
96+
if (e.ProxySession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
97+
{
98+
e.Redirect("https://www.paypal.com");
9299
}
93100
}
94101

@@ -107,7 +114,11 @@ public void OnResponse(object sender, SessionEventArgs e)
107114
{
108115
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
109116
{
117+
byte[] bodyBytes = e.GetResponseBody();
118+
e.SetResponseBody(bodyBytes);
119+
110120
string body = e.GetResponseBodyAsString();
121+
e.SetResponseBodyString(body);
111122
}
112123
}
113124
}

Titanium.Web.Proxy.Test/Titanium.Web.Proxy.Test.csproj

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,53 @@
1111
<RootNamespace>Titanium.Web.Proxy.Test</RootNamespace>
1212
<AssemblyName>Titanium.Web.Proxy.Test</AssemblyName>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<DebugSymbols>true</DebugSymbols>
1718
<DebugType>full</DebugType>
1819
<Optimize>false</Optimize>
1920
<OutputPath>bin\Debug\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<DefineConstants>TRACE;DEBUG;NET40</DefineConstants>
2122
<ErrorReport>prompt</ErrorReport>
2223
<WarningLevel>4</WarningLevel>
23-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
24+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
25+
<Prefer32Bit>false</Prefer32Bit>
2426
</PropertyGroup>
2527
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2628
<DebugType>pdbonly</DebugType>
2729
<Optimize>true</Optimize>
2830
<OutputPath>bin\Release\</OutputPath>
29-
<DefineConstants>TRACE</DefineConstants>
31+
<DefineConstants>TRACE;NET40</DefineConstants>
3032
<ErrorReport>prompt</ErrorReport>
3133
<WarningLevel>4</WarningLevel>
32-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
34+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
35+
<Prefer32Bit>false</Prefer32Bit>
3336
</PropertyGroup>
3437
<PropertyGroup>
3538
<StartupObject />
3639
</PropertyGroup>
3740
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug-Net45|AnyCPU'">
3841
<DebugSymbols>true</DebugSymbols>
3942
<OutputPath>bin\Debug-Net45\</OutputPath>
40-
<DefineConstants>DEBUG;TRACE</DefineConstants>
43+
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
4144
<DebugType>full</DebugType>
4245
<PlatformTarget>AnyCPU</PlatformTarget>
4346
<ErrorReport>prompt</ErrorReport>
4447
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
45-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
48+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
49+
<Prefer32Bit>true</Prefer32Bit>
4650
</PropertyGroup>
4751
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-Net45|AnyCPU'">
4852
<OutputPath>bin\Release-Net45\</OutputPath>
49-
<DefineConstants>TRACE</DefineConstants>
53+
<DefineConstants>TRACE;NET45</DefineConstants>
5054
<Optimize>true</Optimize>
5155
<DebugType>pdbonly</DebugType>
5256
<PlatformTarget>AnyCPU</PlatformTarget>
5357
<ErrorReport>prompt</ErrorReport>
5458
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
55-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
59+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
60+
<Prefer32Bit>true</Prefer32Bit>
5661
</PropertyGroup>
5762
<ItemGroup>
5863
<Reference Include="System" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Titanium.Web.Proxy.Compression
2+
{
3+
class CompressionFactory
4+
{
5+
public ICompression Create(string type)
6+
{
7+
switch (type)
8+
{
9+
case "gzip":
10+
return new GZipCompression();
11+
case "deflate":
12+
return new DeflateCompression();
13+
case "zlib":
14+
return new ZlibCompression();
15+
default:
16+
return null;
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.IO;
2+
using System.IO.Compression;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
class DeflateCompression : ICompression
7+
{
8+
public byte[] Compress(byte[] responseBody)
9+
{
10+
using (var ms = new MemoryStream())
11+
{
12+
using (var zip = new DeflateStream(ms, CompressionMode.Compress, true))
13+
{
14+
zip.Write(responseBody, 0, responseBody.Length);
15+
}
16+
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
class GZipCompression : ICompression
7+
{
8+
public byte[] Compress(byte[] responseBody)
9+
{
10+
using (var ms = new MemoryStream())
11+
{
12+
using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
13+
{
14+
zip.Write(responseBody, 0, responseBody.Length);
15+
}
16+
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Titanium.Web.Proxy.Compression
2+
{
3+
interface ICompression
4+
{
5+
byte[] Compress(byte[] responseBody);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Compression
5+
{
6+
class ZlibCompression : ICompression
7+
{
8+
public byte[] Compress(byte[] responseBody)
9+
{
10+
using (var ms = new MemoryStream())
11+
{
12+
using (var zip = new ZlibStream(ms, CompressionMode.Compress, true))
13+
{
14+
zip.Write(responseBody, 0, responseBody.Length);
15+
}
16+
17+
return ms.ToArray();
18+
}
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Titanium.Web.Proxy.Decompression
2+
{
3+
class DecompressionFactory
4+
{
5+
public IDecompression Create(string type)
6+
{
7+
switch(type)
8+
{
9+
case "gzip":
10+
return new GZipDecompression();
11+
case "deflate":
12+
return new DeflateDecompression();
13+
case "zlib":
14+
return new ZlibDecompression();
15+
default:
16+
return new DefaultDecompression();
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Titanium.Web.Proxy.Decompression
2+
{
3+
class DefaultDecompression : IDecompression
4+
{
5+
public byte[] Decompress(byte[] compressedArray)
6+
{
7+
return compressedArray;
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Decompression
5+
{
6+
class DeflateDecompression : IDecompression
7+
{
8+
public byte[] Decompress(byte[] compressedArray)
9+
{
10+
var stream = new MemoryStream(compressedArray);
11+
12+
using (var decompressor = new DeflateStream(stream, CompressionMode.Decompress))
13+
{
14+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
15+
16+
using (var output = new MemoryStream())
17+
{
18+
int read;
19+
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
20+
{
21+
output.Write(buffer, 0, read);
22+
}
23+
24+
return output.ToArray();
25+
}
26+
}
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.IO;
2+
using System.IO.Compression;
3+
4+
namespace Titanium.Web.Proxy.Decompression
5+
{
6+
class GZipDecompression : IDecompression
7+
{
8+
public byte[] Decompress(byte[] compressedArray)
9+
{
10+
using (var decompressor = new GZipStream(new MemoryStream(compressedArray), CompressionMode.Decompress))
11+
{
12+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
13+
using (var output = new MemoryStream())
14+
{
15+
int read;
16+
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
17+
{
18+
output.Write(buffer, 0, read);
19+
}
20+
return output.ToArray();
21+
}
22+
}
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.IO;
2+
3+
namespace Titanium.Web.Proxy.Decompression
4+
{
5+
interface IDecompression
6+
{
7+
byte[] Decompress(byte[] compressedArray);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Ionic.Zlib;
2+
using System.IO;
3+
4+
namespace Titanium.Web.Proxy.Decompression
5+
{
6+
class ZlibDecompression : IDecompression
7+
{
8+
public byte[] Decompress(byte[] compressedArray)
9+
{
10+
var memoryStream = new MemoryStream(compressedArray);
11+
using (var decompressor = new ZlibStream(memoryStream, CompressionMode.Decompress))
12+
{
13+
var buffer = new byte[ProxyServer.BUFFER_SIZE];
14+
15+
using (var output = new MemoryStream())
16+
{
17+
int read;
18+
while ((read = decompressor.Read(buffer, 0, buffer.Length)) > 0)
19+
{
20+
output.Write(buffer, 0, read);
21+
}
22+
return output.ToArray();
23+
}
24+
}
25+
}
26+
}
27+
}
28+

0 commit comments

Comments
 (0)