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 @@ ...@@ -51,8 +51,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="StreamExtended, Version=1.0.127.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL"> <Reference Include="StreamExtended, Version=1.0.132.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
<HintPath>..\..\packages\StreamExtended.1.0.127-beta\lib\net45\StreamExtended.dll</HintPath> <HintPath>..\..\packages\StreamExtended.1.0.132-beta\lib\net45\StreamExtended.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="StreamExtended" version="1.0.127-beta" targetFramework="net45" /> <package id="StreamExtended" version="1.0.132-beta" targetFramework="net45" />
</packages> </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 ...@@ -49,7 +49,6 @@ namespace Titanium.Web.Proxy
{ {
//read the first line HTTP command //read the first line HTTP command
string httpCmd = await clientStreamReader.ReadLineAsync(); string httpCmd = await clientStreamReader.ReadLineAsync();
if (string.IsNullOrEmpty(httpCmd)) if (string.IsNullOrEmpty(httpCmd))
{ {
return; return;
...@@ -153,11 +152,20 @@ namespace Titanium.Web.Proxy ...@@ -153,11 +152,20 @@ namespace Titanium.Web.Proxy
return; return;
} }
//Now read the actual HTTPS request line if (await CanBeHttpMethod(clientStream))
httpCmd = await clientStreamReader.ReadLineAsync(); {
//Now read the actual HTTPS request line
httpCmd = await clientStreamReader.ReadLineAsync();
}
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 //Hostname is excluded or it is not an HTTPS connect
else if (excluded || !isClientHello)
{ {
//create new connection //create new connection
using (var connection = await GetServerConnection(connectArgs, true)) using (var connection = await GetServerConnection(connectArgs, true))
...@@ -221,6 +229,34 @@ namespace Titanium.Web.Proxy ...@@ -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> /// <summary>
/// This is called when this proxy acts as a reverse proxy (like a real http server) /// 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 /// So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" /> <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>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <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