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

Commit 7280015

Browse files
committed
import stream extended
1 parent 4e1f393 commit 7280015

27 files changed

+3059
-14
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@
7272
<Prefer32Bit>true</Prefer32Bit>
7373
</PropertyGroup>
7474
<ItemGroup>
75-
<Reference Include="StreamExtended, Version=1.0.209.0, Culture=neutral, PublicKeyToken=bbfa0f1d54f50043, processorArchitecture=MSIL">
76-
<HintPath>..\..\src\packages\StreamExtended.1.0.209-beta\lib\net45\StreamExtended.dll</HintPath>
77-
</Reference>
7875
<Reference Include="System" />
7976
<Reference Include="System.Data" />
8077
<Reference Include="System.Xml" />
@@ -128,7 +125,6 @@
128125
<Generator>ResXFileCodeGenerator</Generator>
129126
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
130127
</EmbeddedResource>
131-
<None Include="packages.config" />
132128
<None Include="Properties\Settings.settings">
133129
<Generator>SettingsSingleFileGenerator</Generator>
134130
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -145,6 +141,10 @@
145141
</BootstrapperPackage>
146142
</ItemGroup>
147143
<ItemGroup>
144+
<ProjectReference Include="..\..\src\StreamExtended\StreamExtended.csproj">
145+
<Project>{61539dc1-ce80-41e6-a696-8f455ace8d15}</Project>
146+
<Name>StreamExtended</Name>
147+
</ProjectReference>
148148
<ProjectReference Include="..\..\src\Titanium.Web.Proxy\Titanium.Web.Proxy.csproj">
149149
<Project>{91018b6d-a7a9-45be-9cb3-79cbb8b169a6}</Project>
150150
<Name>Titanium.Web.Proxy</Name>

examples/Titanium.Web.Proxy.Examples.Wpf/packages.config

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Concurrent;
2+
3+
namespace StreamExtended
4+
{
5+
6+
/// <summary>
7+
/// A concrete IBufferPool implementation using a thread-safe stack.
8+
/// Works well when all consumers ask for buffers with the same size.
9+
/// If your application would use variable size buffers consider implementing IBufferPool using System.Buffers library from Microsoft.
10+
/// </summary>
11+
public class DefaultBufferPool : IBufferPool
12+
{
13+
private readonly ConcurrentStack<byte[]> buffers = new ConcurrentStack<byte[]>();
14+
15+
/// <summary>
16+
/// Gets a buffer.
17+
/// </summary>
18+
/// <param name="bufferSize">Size of the buffer.</param>
19+
/// <returns></returns>
20+
public byte[] GetBuffer(int bufferSize)
21+
{
22+
if (!buffers.TryPop(out var buffer) || buffer.Length != bufferSize)
23+
{
24+
buffer = new byte[bufferSize];
25+
}
26+
27+
return buffer;
28+
}
29+
30+
/// <summary>
31+
/// Returns the buffer.
32+
/// </summary>
33+
/// <param name="buffer">The buffer.</param>
34+
public void ReturnBuffer(byte[] buffer)
35+
{
36+
if (buffer != null)
37+
{
38+
buffers.Push(buffer);
39+
}
40+
}
41+
42+
public void Dispose()
43+
{
44+
buffers.Clear();
45+
}
46+
}
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace StreamExtended
4+
{
5+
/// <summary>
6+
/// Use this interface to implement custom buffer pool.
7+
/// To use the default buffer pool implementation use DefaultBufferPool class.
8+
/// </summary>
9+
public interface IBufferPool : IDisposable
10+
{
11+
byte[] GetBuffer(int bufferSize);
12+
void ReturnBuffer(byte[] buffer);
13+
}
14+
}

src/StreamExtended/ClientHelloInfo.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using StreamExtended.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace StreamExtended
8+
{
9+
/// <summary>
10+
/// Wraps up the client SSL hello information.
11+
/// </summary>
12+
public class ClientHelloInfo
13+
{
14+
private static readonly string[] compressions = {
15+
"null",
16+
"DEFLATE"
17+
};
18+
19+
public int HandshakeVersion { get; set; }
20+
21+
public int MajorVersion { get; set; }
22+
23+
public int MinorVersion { get; set; }
24+
25+
public byte[] Random { get; set; }
26+
27+
public DateTime Time
28+
{
29+
get
30+
{
31+
DateTime time = DateTime.MinValue;
32+
if (Random.Length > 3)
33+
{
34+
time = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
35+
.AddSeconds(((uint)Random[3] << 24) + ((uint)Random[2] << 16) + ((uint)Random[1] << 8) + (uint)Random[0]).ToLocalTime();
36+
}
37+
38+
return time;
39+
}
40+
}
41+
42+
public byte[] SessionId { get; set; }
43+
44+
public int[] Ciphers { get; set; }
45+
46+
public byte[] CompressionData { get; set; }
47+
48+
internal int ClientHelloLength { get; set; }
49+
50+
internal int EntensionsStartPosition { get; set; }
51+
52+
public Dictionary<string, SslExtension> Extensions { get; set; }
53+
54+
private static string SslVersionToString(int major, int minor)
55+
{
56+
string str = "Unknown";
57+
if (major == 3 && minor == 3)
58+
str = "TLS/1.2";
59+
else if (major == 3 && minor == 2)
60+
str = "TLS/1.1";
61+
else if (major == 3 && minor == 1)
62+
str = "TLS/1.0";
63+
else if (major == 3 && minor == 0)
64+
str = "SSL/3.0";
65+
else if (major == 2 && minor == 0)
66+
str = "SSL/2.0";
67+
68+
return $"{major}.{minor} ({str})";
69+
}
70+
71+
/// <summary>
72+
/// Returns a <see cref="System.String" /> that represents this instance.
73+
/// </summary>
74+
/// <returns>
75+
/// A <see cref="System.String" /> that represents this instance.
76+
/// </returns>
77+
public override string ToString()
78+
{
79+
var sb = new StringBuilder();
80+
sb.AppendLine($"A SSLv{HandshakeVersion}-compatible ClientHello handshake was found. Titanium extracted the parameters below.");
81+
sb.AppendLine();
82+
sb.AppendLine($"Version: {SslVersionToString(MajorVersion, MinorVersion)}");
83+
sb.AppendLine($"Random: {string.Join(" ", Random.Select(x => x.ToString("X2")))}");
84+
sb.AppendLine($"\"Time\": {Time}");
85+
sb.AppendLine($"SessionID: {string.Join(" ", SessionId.Select(x => x.ToString("X2")))}");
86+
87+
if (Extensions != null)
88+
{
89+
sb.AppendLine("Extensions:");
90+
foreach (var extension in Extensions.Values.OrderBy(x => x.Position))
91+
{
92+
sb.AppendLine($"{extension.Name}: {extension.Data}");
93+
}
94+
}
95+
96+
if (CompressionData != null && CompressionData.Length > 0)
97+
{
98+
int compressionMethod = CompressionData[0];
99+
string compression = compressions.Length > compressionMethod
100+
? compressions[compressionMethod]
101+
: $"unknown [0x{compressionMethod:X2}]";
102+
sb.AppendLine($"Compression: {compression}");
103+
}
104+
105+
if (Ciphers.Length > 0)
106+
{
107+
sb.AppendLine("Ciphers:");
108+
foreach (int cipherSuite in Ciphers)
109+
{
110+
if (!SslCiphers.Ciphers.TryGetValue(cipherSuite, out string cipherStr))
111+
{
112+
cipherStr = "unknown";
113+
}
114+
115+
sb.AppendLine($"[0x{cipherSuite:X4}] {cipherStr}");
116+
}
117+
}
118+
119+
return sb.ToString();
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)