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

Commit d0a7738

Browse files
authored
Merge pull request #639 from justcoding121/master
beta
2 parents c83769b + 299d4ba commit d0a7738

20 files changed

+105
-103
lines changed

.build/build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if($Branch -eq "beta" ) { $Version = "$Version-beta" }
2929

3030
$NuGet = Join-Path $RepoRoot ".nuget\nuget.exe"
3131

32-
$MSBuild = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe"
32+
$MSBuild = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe"
3333
$MSBuild -replace ' ', '` '
3434

3535
FormatTaskName (("-"*25) + "[{0}]" + ("-"*25))

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# version format
1010
version: 3.1.{build}
11-
image: Visual Studio 2017
11+
image: Visual Studio 2019
1212

1313
shallow_clone: false
1414

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

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<UseWPF>true</UseWPF>
77
</PropertyGroup>
88

9-
<!--<ItemGroup>
9+
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
1010
<Reference Include="PresentationCore" />
1111
<Reference Include="PresentationFramework" />
1212
<Reference Include="WindowsBase" />
13-
</ItemGroup>-->
13+
</ItemGroup>
1414

1515
<ItemGroup>
1616
<Compile Remove="Properties\AssemblyInfo.cs" />
@@ -20,23 +20,6 @@
2020
<ProjectReference Include="..\..\src\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj" />
2121
</ItemGroup>
2222

23-
<!--<ItemGroup>
24-
<ApplicationDefinition Include="App.xaml">
25-
<Generator>MSBuild:Compile</Generator>
26-
<SubType>Designer</SubType>
27-
</ApplicationDefinition>
28-
<Page Include="MainWindow.xaml">
29-
<Generator>MSBuild:Compile</Generator>
30-
<SubType>Designer</SubType>
31-
</Page>
32-
<Compile Update="App.xaml.cs">
33-
<DependentUpon>App.xaml</DependentUpon>
34-
</Compile>
35-
<Compile Update="MainWindow.xaml.cs">
36-
<DependentUpon>MainWindow.xaml</DependentUpon>
37-
</Compile>
38-
</ItemGroup>
39-
4023
<ItemGroup>
4124
<Compile Update="Properties\Resources.Designer.cs">
4225
<AutoGen>True</AutoGen>
@@ -52,16 +35,6 @@
5235
<Generator>ResXFileCodeGenerator</Generator>
5336
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
5437
</EmbeddedResource>
55-
<None Update="App.xaml">
56-
<Generator>MSBuild:Compile</Generator>
57-
</None>
58-
<None Update="MainWindow.xaml">
59-
<Generator>MSBuild:Compile</Generator>
60-
</None>
61-
<None Update="Properties\Settings.settings">
62-
<Generator>SettingsSingleFileGenerator</Generator>
63-
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
64-
</None>
65-
</ItemGroup>-->
38+
</ItemGroup>
6639

6740
</Project>

src/Titanium.Web.Proxy.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Global
6868
{4406CE17-9A39-4F28-8363-6169A4F799C1}.Debug|x64.ActiveCfg = Debug|Any CPU
6969
{4406CE17-9A39-4F28-8363-6169A4F799C1}.Debug|x64.Build.0 = Debug|Any CPU
7070
{4406CE17-9A39-4F28-8363-6169A4F799C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{4406CE17-9A39-4F28-8363-6169A4F799C1}.Release|Any CPU.Build.0 = Release|Any CPU
7172
{4406CE17-9A39-4F28-8363-6169A4F799C1}.Release|x64.ActiveCfg = Release|Any CPU
7273
{4406CE17-9A39-4F28-8363-6169A4F799C1}.Release|x64.Build.0 = Release|Any CPU
7374
{1D053D72-DCB4-4517-ACDD-D35ADC186950}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -89,7 +90,7 @@ Global
8990
{1D053D72-DCB4-4517-ACDD-D35ADC186950} = {BC1E0789-D348-49CF-8B67-5E99D50EDF64}
9091
EndGlobalSection
9192
GlobalSection(ExtensibilityGlobals) = postSolution
92-
SolutionGuid = {625C1EB5-44CF-47DE-A85A-B4C8C40ED90A}
9393
EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35
94+
SolutionGuid = {625C1EB5-44CF-47DE-A85A-B4C8C40ED90A}
9495
EndGlobalSection
9596
EndGlobal

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
288288
if (!clientStream.IsClosed && !connection.Stream.IsClosed)
289289
{
290290
await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
291-
null, null,
292-
connectArgs.CancellationTokenSource, ExceptionFunc);
291+
null, null, connectArgs.CancellationTokenSource, ExceptionFunc);
293292
}
294293
}
295294
finally
@@ -338,17 +337,15 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
338337
await connection.StreamWriter.WriteLineAsync("SM", cancellationToken);
339338
await connection.StreamWriter.WriteLineAsync(cancellationToken);
340339
#if NETCOREAPP2_1
341-
await Http2Helper.SendHttp2(clientStream, connection.Stream, BufferPool.BufferSize,
342-
(buffer, offset, count) => { connectArgs.OnDecryptedDataSent(buffer, offset, count); },
343-
(buffer, offset, count) => { connectArgs.OnDecryptedDataReceived(buffer, offset, count); },
340+
await Http2Helper.SendHttp2(clientStream, connection.Stream,
344341
() => new SessionEventArgs(this, endPoint, cancellationTokenSource)
345342
{
346343
ProxyClient = { Connection = clientConnection },
347344
HttpClient = { ConnectRequest = connectArgs?.HttpClient.ConnectRequest },
348345
UserData = connectArgs?.UserData
349346
},
350-
async args => { await invokeBeforeRequest(args); },
351-
async args => { await invokeBeforeResponse(args); },
347+
async args => { await onBeforeRequest(args); },
348+
async args => { await onBeforeResponse(args); },
352349
connectArgs.CancellationTokenSource, clientConnection.Id, ExceptionFunc);
353350
#endif
354351
}

src/Titanium.Web.Proxy/Extensions/SslExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static string GetServerName(this ClientHelloInfo clientHelloInfo)
2828
return null;
2929
}
3030

31-
#if NETCOREAPP2_1
31+
#if NETCOREAPP2_1 || NETSTANDARD2_1
3232
internal static List<SslApplicationProtocol> GetAlpn(this ClientHelloInfo clientHelloInfo)
3333
{
3434
if (clientHelloInfo.Extensions != null && clientHelloInfo.Extensions.TryGetValue("ALPN", out var alpnExtension))
@@ -78,7 +78,7 @@ internal static Task AuthenticateAsServerAsync(this SslStream sslStream, SslServ
7878
#endif
7979
}
8080

81-
#if !NETCOREAPP2_1
81+
#if !NETCOREAPP2_1 && !NETSTANDARD2_1
8282
internal enum SslApplicationProtocol
8383
{
8484
Http11,

src/Titanium.Web.Proxy/Helpers/HttpHelper.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,28 @@ internal static Task<int> IsPriMethod(ICustomStreamReader clientStreamReader, IB
151151
/// </returns>
152152
private static async Task<int> startsWith(ICustomStreamReader clientStreamReader, IBufferPool bufferPool, string expectedStart, CancellationToken cancellationToken = default)
153153
{
154-
int iRet = -1;
155154
const int lengthToCheck = 10;
156155
byte[] buffer = null;
157156
try
158157
{
159-
buffer = bufferPool.GetBuffer(Math.Max(bufferPool.BufferSize, lengthToCheck));
158+
if (bufferPool.BufferSize < lengthToCheck)
159+
{
160+
throw new Exception($"Buffer is too small. Minimum size is {lengthToCheck} bytes");
161+
}
160162

161-
int peeked = await clientStreamReader.PeekBytesAsync(buffer, 0, 0, lengthToCheck, cancellationToken);
163+
buffer = bufferPool.GetBuffer(bufferPool.BufferSize);
162164

163-
if (peeked > 0)
165+
bool isExpected = true;
166+
int i = 0;
167+
while (i < lengthToCheck)
164168
{
165-
bool isExpected = true;
169+
int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken);
170+
if (peeked == 0)
171+
return - 1;
172+
173+
peeked += i;
166174

167-
for (int i = 0; i < lengthToCheck; i++)
175+
while (i < peeked)
168176
{
169177
int b = buffer[i];
170178

@@ -173,23 +181,23 @@ private static async Task<int> startsWith(ICustomStreamReader clientStreamReader
173181
else
174182
{
175183
char ch = (char)b;
176-
if (!char.IsLetter(ch))
184+
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
177185
return -1;
178186
else if (i >= expectedStart.Length || ch != expectedStart[i])
179-
isExpected = false;
187+
isExpected = false;
180188
}
181-
}
182189

183-
// only letters
184-
iRet = isExpected ? 1 : 0;
190+
i++;
191+
}
185192
}
193+
194+
// only letters
195+
return 0;
186196
}
187197
finally
188198
{
189199
bufferPool.ReturnBuffer(buffer);
190-
buffer = null;
191200
}
192-
return iRet;
193201
}
194202
}
195203
}

src/Titanium.Web.Proxy/Http2/Http2Helper.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if NETCOREAPP2_1
1+
#if NETCOREAPP2_1 || NETSTANDARD2_1
22
using System;
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
@@ -24,8 +24,7 @@ internal class Http2Helper
2424
/// Task-based Asynchronous Pattern
2525
/// </summary>
2626
/// <returns></returns>
27-
internal static async Task SendHttp2(Stream clientStream, Stream serverStream, int bufferSize,
28-
Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive,
27+
internal static async Task SendHttp2(Stream clientStream, Stream serverStream,
2928
Func<SessionEventArgs> sessionFactory,
3029
Func<SessionEventArgs, Task> onBeforeRequest, Func<SessionEventArgs, Task> onBeforeResponse,
3130
CancellationTokenSource cancellationTokenSource, Guid connectionId,
@@ -38,25 +37,25 @@ internal static async Task SendHttp2(Stream clientStream, Stream serverStream, i
3837

3938
// Now async relay all server=>client & client=>server data
4039
var sendRelay =
41-
copyHttp2FrameAsync(clientStream, serverStream, onDataSend, clientSettings, serverSettings,
40+
copyHttp2FrameAsync(clientStream, serverStream, clientSettings, serverSettings,
4241
sessionFactory, sessions, onBeforeRequest,
43-
bufferSize, connectionId, true, cancellationTokenSource.Token, exceptionFunc);
42+
connectionId, true, cancellationTokenSource.Token, exceptionFunc);
4443
var receiveRelay =
45-
copyHttp2FrameAsync(serverStream, clientStream, onDataReceive, serverSettings, clientSettings,
44+
copyHttp2FrameAsync(serverStream, clientStream, serverSettings, clientSettings,
4645
sessionFactory, sessions, onBeforeResponse,
47-
bufferSize, connectionId, false, cancellationTokenSource.Token, exceptionFunc);
46+
connectionId, false, cancellationTokenSource.Token, exceptionFunc);
4847

4948
await Task.WhenAny(sendRelay, receiveRelay);
5049
cancellationTokenSource.Cancel();
5150

5251
await Task.WhenAll(sendRelay, receiveRelay);
5352
}
5453

55-
private static async Task copyHttp2FrameAsync(Stream input, Stream output, Action<byte[], int, int> onCopy,
54+
private static async Task copyHttp2FrameAsync(Stream input, Stream output,
5655
Http2Settings localSettings, Http2Settings remoteSettings,
5756
Func<SessionEventArgs> sessionFactory, ConcurrentDictionary<int, SessionEventArgs> sessions,
5857
Func<SessionEventArgs, Task> onBeforeRequestResponse,
59-
int bufferSize, Guid connectionId, bool isClient, CancellationToken cancellationToken,
58+
Guid connectionId, bool isClient, CancellationToken cancellationToken,
6059
ExceptionHandler exceptionFunc)
6160
{
6261
int headerTableSize = 0;
@@ -69,7 +68,6 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
6968
{
7069
var frameHeaderBuffer = frameHeader.Buffer;
7170
int read = await forceRead(input, frameHeaderBuffer, 0, 9, cancellationToken);
72-
onCopy(frameHeaderBuffer, 0, read);
7371
if (read != 9)
7472
{
7573
return;
@@ -92,7 +90,6 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
9290
}
9391

9492
read = await forceRead(input, buffer, 0, length, cancellationToken);
95-
onCopy(buffer, 0, read);
9693
if (read != length)
9794
{
9895
return;
@@ -127,6 +124,11 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
127124
//System.Diagnostics.Debug.WriteLine("CONN: " + connectionId + ", CLIENT: " + isClient + ", STREAM: " + streamId + ", TYPE: " + type);
128125
if (type == Http2FrameType.Data && args != null)
129126
{
127+
if (isClient)
128+
args.OnDataSent(buffer, 0, read);
129+
else
130+
args.OnDataReceived(buffer, 0, read);
131+
130132
rr = isClient ? (RequestResponseBase)args.HttpClient.Request : args.HttpClient.Response;
131133

132134
bool padded = (flags & Http2FrameFlag.Padded) != 0;
@@ -182,8 +184,10 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
182184
{
183185
args = sessionFactory();
184186
args.IsPromise = true;
185-
sessions.TryAdd(streamId, args);
186-
sessions.TryAdd(promisedStreamId, args);
187+
if (!sessions.TryAdd(streamId, args))
188+
;
189+
if (!sessions.TryAdd(promisedStreamId, args))
190+
;
187191
}
188192

189193
System.Diagnostics.Debug.WriteLine("PROMISE STREAM: " + streamId + ", " + promisedStreamId +
@@ -201,7 +205,8 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output, Actio
201205
if (!sessions.TryGetValue(streamId, out args))
202206
{
203207
args = sessionFactory();
204-
sessions.TryAdd(streamId, args);
208+
if (!sessions.TryAdd(streamId, args))
209+
;
205210
}
206211

207212
rr = isClient ? (RequestResponseBase)args.HttpClient.Request : args.HttpClient.Response;

src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Net;
4-
#if NETCOREAPP2_1
4+
#if NETCOREAPP2_1 || NETSTANDARD2_1
55
using System.Net.Security;
66
#endif
77
using System.Net.Sockets;

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
391391
CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation
392392
};
393393
await sslStream.AuthenticateAsClientAsync(options, cancellationToken);
394-
#if NETCOREAPP2_1
394+
#if NETCOREAPP2_1 || NETSTANDARD2_1
395395
negotiatedApplicationProtocol = sslStream.NegotiatedApplicationProtocol;
396396
#endif
397397

src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Net;
3-
#if NETCOREAPP2_1
3+
#if NETCOREAPP2_1 || NETSTANDARD2_1
44
using System.Net.Security;
55
#endif
66
using System.Net.Sockets;

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
235235
/// The buffer pool used throughout this proxy instance.
236236
/// Set custom implementations by implementing this interface.
237237
/// By default this uses DefaultBufferPool implementation available in StreamExtended library package.
238+
/// Buffer size should be at least 10 bytes.
238239
/// </summary>
239240
public IBufferPool BufferPool { get; set; }
240241

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using System.Net;
55
using System.Net.Sockets;
6-
#if NETCOREAPP2_1
6+
#if NETCOREAPP2_1 || NETSTANDARD2_1
77
using System.Net.Security;
88
#endif
99
using System.Text.RegularExpressions;
@@ -143,7 +143,7 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
143143
// proxy authorization check
144144
if (httpsConnectHostname == null && await checkAuthorization(args) == false)
145145
{
146-
await invokeBeforeResponse(args);
146+
await onBeforeResponse(args);
147147

148148
// send the response
149149
await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
@@ -166,10 +166,8 @@ await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
166166
// we need this to syphon out data from connection if API user changes them.
167167
request.SetOriginalHeaders();
168168

169-
args.TimeLine["Request Received"] = DateTime.Now;
170-
171169
// If user requested interception do it
172-
await invokeBeforeRequest(args);
170+
await onBeforeRequest(args);
173171

174172
var response = args.HttpClient.Response;
175173

@@ -286,7 +284,7 @@ await clientStreamWriter.WriteResponseAsync(args.HttpClient.Response,
286284
}
287285
finally
288286
{
289-
await invokeAfterResponse(args);
287+
await onAfterResponse(args);
290288
args.Dispose();
291289
}
292290
}
@@ -410,8 +408,10 @@ private void prepareRequestHeaders(HeaderCollection requestHeaders)
410408
/// </summary>
411409
/// <param name="args">The session event arguments.</param>
412410
/// <returns></returns>
413-
private async Task invokeBeforeRequest(SessionEventArgs args)
411+
private async Task onBeforeRequest(SessionEventArgs args)
414412
{
413+
args.TimeLine["Request Received"] = DateTime.Now;
414+
415415
if (BeforeRequest != null)
416416
{
417417
await BeforeRequest.InvokeAsync(this, args, ExceptionFunc);

0 commit comments

Comments
 (0)