Commit 5074d5dd authored by Honfika's avatar Honfika

Exclude (for example) Google cloud messaging streams. It is a https tunnel,...

Exclude (for example) Google cloud messaging streams. It is a https tunnel, but the content is not http
parent 8c882108
......@@ -51,8 +51,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="StreamExtended, Version=1.0.127.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.127-beta\lib\net45\StreamExtended.dll</HintPath>
<Reference Include="StreamExtended, Version=1.0.132.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.132-beta\lib\net45\StreamExtended.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="StreamExtended" version="1.0.127-beta" targetFramework="net45" />
<package id="StreamExtended" version="1.0.132-beta" targetFramework="net45" />
</packages>
\ No newline at end of file
#if DEBUG
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StreamExtended.Network;
namespace Titanium.Web.Proxy.Network
{
class DebugCustomBufferedStream : CustomBufferedStream
{
private const string basePath = @"c:\\11\\";
private static int counter;
public int Counter { get; }
private FileStream fileStreamSent;
private FileStream fileStreamReceived;
public DebugCustomBufferedStream(Stream baseStream, int bufferSize) : base(baseStream, bufferSize)
{
Counter = Interlocked.Increment(ref counter);
fileStreamSent = new FileStream(Path.Combine(basePath, $"{Counter}_sent.dat"), FileMode.Create);
fileStreamReceived = new FileStream(Path.Combine(basePath, $"{Counter}_received.dat"), FileMode.Create);
}
protected override void OnDataSent(byte[] buffer, int offset, int count)
{
fileStreamSent.Write(buffer, offset, count);
}
protected override void OnDataReceived(byte[] buffer, int offset, int count)
{
fileStreamReceived.Write(buffer, offset, count);
}
public void Flush()
{
fileStreamSent.Flush(true);
fileStreamReceived.Flush(true);
}
}
}
#endif
\ No newline at end of file
......@@ -49,7 +49,6 @@ namespace Titanium.Web.Proxy
{
//read the first line HTTP command
string httpCmd = await clientStreamReader.ReadLineAsync();
if (string.IsNullOrEmpty(httpCmd))
{
return;
......@@ -153,11 +152,20 @@ namespace Titanium.Web.Proxy
return;
}
if (await CanBeHttpMethod(clientStream))
{
//Now read the actual HTTPS request line
httpCmd = await clientStreamReader.ReadLineAsync();
}
//Hostname is excluded or it is not an HTTPS connect
else
{
// It can be for example some Google (Cloude Messaging for Chrome) magic
excluded = true;
}
}
//Hostname is excluded or it is not an HTTPS connect
if (excluded || !isClientHello)
{
//create new connection
using (var connection = await GetServerConnection(connectArgs, true))
......@@ -221,6 +229,34 @@ namespace Titanium.Web.Proxy
}
}
private async Task<bool> CanBeHttpMethod(CustomBufferedStream clientStream)
{
int legthToCheck = 10;
for (int i = 0; i < legthToCheck; i++)
{
int b = await clientStream.PeekByteAsync(i);
if (b == -1)
{
return false;
}
if (b == ' ' && i > 2)
{
// at least 3 letters and a space
return true;
}
if (!char.IsLetter((char)b))
{
// non letter or too short
return false;
}
}
// only letters
return true;
}
/// <summary>
/// This is called when this proxy acts as a reverse proxy (like a real http server)
/// So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client
......
......@@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
<PackageReference Include="StreamExtended" Version="1.0.127-beta" />
<PackageReference Include="StreamExtended" Version="1.0.132-beta" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment