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

Commit 33bb3f0

Browse files
Merge pull request #472 from justcoding121/beta
Stable
2 parents 35cd23b + 30782ea commit 33bb3f0

File tree

100 files changed

+5087
-1002
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+5087
-1002
lines changed

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

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Net;
55
using System.Net.Security;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Titanium.Web.Proxy.EventArguments;
89
using Titanium.Web.Proxy.Exceptions;
@@ -14,7 +15,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
1415
{
1516
public class ProxyTestController
1617
{
17-
private readonly object lockObj = new object();
18+
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1);
1819

1920
private readonly ProxyServer proxyServer;
2021

@@ -23,34 +24,41 @@ public class ProxyTestController
2324
public ProxyTestController()
2425
{
2526
proxyServer = new ProxyServer();
26-
27+
proxyServer.EnableConnectionPool = true;
2728
// generate root certificate without storing it in file system
2829
//proxyServer.CertificateManager.CreateRootCertificate(false);
2930

3031
//proxyServer.CertificateManager.TrustRootCertificate();
3132
//proxyServer.CertificateManager.TrustRootCertificateAsAdmin();
3233

33-
proxyServer.ExceptionFunc = exception =>
34+
proxyServer.ExceptionFunc = async exception =>
3435
{
35-
lock (lockObj)
36+
await @lock.WaitAsync();
37+
38+
try
3639
{
37-
var color = Console.ForegroundColor;
38-
Console.ForegroundColor = ConsoleColor.Red;
39-
if (exception is ProxyHttpException phex)
40-
{
41-
Console.WriteLine(exception.Message + ": " + phex.InnerException?.Message);
42-
}
43-
else
44-
{
45-
Console.WriteLine(exception.Message);
46-
}
47-
48-
Console.ForegroundColor = color;
40+
var color = Console.ForegroundColor;
41+
Console.ForegroundColor = ConsoleColor.Red;
42+
if (exception is ProxyHttpException phex)
43+
{
44+
Console.WriteLine(exception.Message + ": " + phex.InnerException?.Message);
45+
}
46+
else
47+
{
48+
Console.WriteLine(exception.Message);
49+
}
50+
51+
Console.ForegroundColor = color;
52+
53+
}
54+
finally
55+
{
56+
@lock.Release();
4957
}
5058
};
5159
proxyServer.ForwardToUpstreamGateway = true;
5260
proxyServer.CertificateManager.SaveFakeCertificates = true;
53-
61+
5462
// optionally set the Certificate Engine
5563
// Under Mono or Non-Windows runtimes only BouncyCastle will be supported
5664
//proxyServer.CertificateManager.CertificateEngine = Network.CertificateEngine.BouncyCastle;
@@ -122,15 +130,15 @@ public void Stop()
122130
proxyServer.ClientCertificateSelectionCallback -= OnCertificateSelection;
123131

124132
proxyServer.Stop();
125-
133+
126134
// remove the generated certificates
127135
//proxyServer.CertificateManager.RemoveTrustedRootCertificates();
128136
}
129137

130138
private async Task OnBeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e)
131139
{
132140
string hostname = e.WebSession.Request.RequestUri.Host;
133-
WriteToConsole("Tunnel to: " + hostname);
141+
await WriteToConsole("Tunnel to: " + hostname);
134142

135143
if (hostname.Contains("dropbox.com"))
136144
{
@@ -141,15 +149,16 @@ private async Task OnBeforeTunnelConnectRequest(object sender, TunnelConnectSess
141149
}
142150
}
143151

144-
private async Task OnBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEventArgs e)
152+
private Task OnBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEventArgs e)
145153
{
154+
return Task.FromResult(false);
146155
}
147156

148157
// intecept & cancel redirect or update requests
149158
private async Task OnRequest(object sender, SessionEventArgs e)
150159
{
151-
WriteToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
152-
WriteToConsole(e.WebSession.Request.Url);
160+
await WriteToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
161+
await WriteToConsole(e.WebSession.Request.Url);
153162

154163
// store it in the UserData property
155164
// It can be a simple integer, Guid, or any type
@@ -187,19 +196,19 @@ private async Task OnRequest(object sender, SessionEventArgs e)
187196
}
188197

189198
// Modify response
190-
private void MultipartRequestPartSent(object sender, MultipartRequestPartSentEventArgs e)
199+
private async Task MultipartRequestPartSent(object sender, MultipartRequestPartSentEventArgs e)
191200
{
192201
var session = (SessionEventArgs)sender;
193-
WriteToConsole("Multipart form data headers:");
202+
await WriteToConsole("Multipart form data headers:");
194203
foreach (var header in e.Headers)
195204
{
196-
WriteToConsole(header.ToString());
205+
await WriteToConsole(header.ToString());
197206
}
198207
}
199208

200209
private async Task OnResponse(object sender, SessionEventArgs e)
201210
{
202-
WriteToConsole("Active Server Connections:" + ((ProxyServer)sender).ServerConnectionCount);
211+
await WriteToConsole("Active Server Connections:" + ((ProxyServer)sender).ServerConnectionCount);
203212

204213
string ext = System.IO.Path.GetExtension(e.WebSession.Request.RequestUri.AbsolutePath);
205214

@@ -271,12 +280,18 @@ public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs
271280
return Task.FromResult(0);
272281
}
273282

274-
private void WriteToConsole(string message)
283+
private async Task WriteToConsole(string message)
275284
{
276-
lock (lockObj)
285+
await @lock.WaitAsync();
286+
287+
try
277288
{
278289
Console.WriteLine(message);
279290
}
291+
finally
292+
{
293+
@lock.Release();
294+
}
280295
}
281296

282297
///// <summary>

Examples/Titanium.Web.Proxy.Examples.Wpf/Titanium.Web.Proxy.Examples.Wpf.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
<WarningLevel>4</WarningLevel>
5252
</PropertyGroup>
5353
<ItemGroup>
54-
<Reference Include="StreamExtended, Version=1.0.164.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
55-
<HintPath>..\..\packages\StreamExtended.1.0.164\lib\net45\StreamExtended.dll</HintPath>
54+
<Reference Include="StreamExtended, Version=1.0.179.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
55+
<HintPath>..\..\packages\StreamExtended.1.0.179\lib\net45\StreamExtended.dll</HintPath>
5656
</Reference>
5757
<Reference Include="System" />
5858
<Reference Include="System.Data" />
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="StreamExtended" version="1.0.164" targetFramework="net45" />
3+
<package id="StreamExtended" version="1.0.179" targetFramework="net45" />
44
</packages>

PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Doneness:
22
- [ ] Build is okay - I made sure that this change is building successfully.
33
- [ ] No Bugs - I made sure that this change is working properly as expected. It doesn't have any bugs that you are aware of.
4-
- [ ] Branching - If this is not a hotfix, I am making this request against develop branch
4+
- [ ] Branching - If this is not a hotfix, I am making this request against master branch

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ Kindly report only issues/bugs here . For programming help or questions use [Sta
1919

2020
### Features
2121

22-
* Multithreaded & fully asynchronous proxy
23-
* Supports HTTP(S) and most features of HTTP 1.1
24-
* Supports redirect/block/update requests and modifying responses
25-
* Safely relays Web Socket requests over HTTP
26-
* Supports mutual SSL authentication
27-
* Supports proxy authentication & automatic proxy detection
22+
* Multithreaded & fully asynchronous proxy employing server connection pooling, certificate cache & buffer pooling
23+
* View/modify/redirect/block requests & responses
24+
* Supports mutual SSL authentication, proxy authentication & automatic upstream proxy detection
2825
* Kerberos/NTLM authentication over HTTP protocols for windows domain
2926

3027
### Usage
@@ -36,7 +33,7 @@ Install by [nuget](https://www.nuget.org/packages/Titanium.Web.Proxy)
3633

3734
For beta releases on [beta branch](https://github.com/justcoding121/Titanium-Web-Proxy/tree/beta)
3835

39-
Install-Package Titanium.Web.Proxy -Pre
36+
Install-Package Titanium.Web.Proxy
4037

4138
For stable releases on [stable branch](https://github.com/justcoding121/Titanium-Web-Proxy/tree/stable)
4239

Titanium.Web.Proxy.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
2424
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
2525
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
26+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a4ab2e69_002D4d9c_002D4345_002Dbcd1_002D5541dacf5d38/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Method (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="METHOD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
2627
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=dda2ffa1_002D435c_002D4111_002D88eb_002D1a7c93c382f0/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Property (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="PROPERTY" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
2728
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
2829
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>

Titanium.Web.Proxy/EventArguments/LimitedStream.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
using System.Globalization;
33
using System.IO;
44
using System.Threading.Tasks;
5-
using StreamExtended.Helpers;
5+
using StreamExtended;
66
using StreamExtended.Network;
77

88
namespace Titanium.Web.Proxy.EventArguments
99
{
1010
internal class LimitedStream : Stream
1111
{
12+
private readonly IBufferPool bufferPool;
1213
private readonly ICustomStreamReader baseStream;
1314
private readonly bool isChunked;
1415
private long bytesRemaining;
1516

1617
private bool readChunkTrail;
1718

18-
internal LimitedStream(ICustomStreamReader baseStream, bool isChunked,
19+
internal LimitedStream(ICustomStreamReader baseStream, IBufferPool bufferPool, bool isChunked,
1920
long contentLength)
20-
{
21+
{
2122
this.baseStream = baseStream;
23+
this.bufferPool = bufferPool;
2224
this.isChunked = isChunked;
2325
bytesRemaining = isChunked
2426
? 0
@@ -41,7 +43,7 @@ public override long Position
4143
set => throw new NotSupportedException();
4244
}
4345

44-
private void GetNextChunk()
46+
private void getNextChunk()
4547
{
4648
if (readChunkTrail)
4749
{
@@ -96,7 +98,7 @@ public override int Read(byte[] buffer, int offset, int count)
9698
{
9799
if (isChunked)
98100
{
99-
GetNextChunk();
101+
getNextChunk();
100102
}
101103
else
102104
{
@@ -125,7 +127,7 @@ public async Task Finish()
125127
{
126128
if (bytesRemaining != -1)
127129
{
128-
var buffer = BufferPool.GetBuffer(baseStream.BufferSize);
130+
var buffer = bufferPool.GetBuffer(baseStream.BufferSize);
129131
try
130132
{
131133
int res = await ReadAsync(buffer, 0, buffer.Length);
@@ -136,7 +138,7 @@ public async Task Finish()
136138
}
137139
finally
138140
{
139-
BufferPool.ReturnBuffer(buffer);
141+
bufferPool.ReturnBuffer(buffer);
140142
}
141143
}
142144
}

0 commit comments

Comments
 (0)