Commit 50129826 authored by Honfika's avatar Honfika

small improvement in ssl tools

parent 347c310c

using Titanium.Web.Proxy.StreamExtended.BufferPool;
using Titanium.Web.Proxy.StreamExtended.Network;
#if DEBUG
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace Titanium.Web.Proxy.Network
{
internal class DebugCustomBufferedStream : CustomBufferedStream
{
private const string basePath = @".";
private static int counter;
private readonly FileStream fileStreamReceived;
private readonly FileStream fileStreamSent;
public DebugCustomBufferedStream(Guid connectionId, string type, Stream baseStream, IBufferPool bufferPool, bool leaveOpen = false)
: base(baseStream, bufferPool, leaveOpen)
{
Counter = Interlocked.Increment(ref counter);
fileStreamSent = new FileStream(Path.Combine(basePath, $"{connectionId}_{type}_{Counter}_sent.dat"), FileMode.Create);
fileStreamReceived = new FileStream(Path.Combine(basePath, $"{connectionId}_{type}_{Counter}_received.dat"), FileMode.Create);
}
public int Counter { get; }
protected override void OnDataWrite(byte[] buffer, int offset, int count)
{
fileStreamSent.Write(buffer, offset, count);
Flush();
}
protected override void OnDataRead(byte[] buffer, int offset, int count)
{
fileStreamReceived.Write(buffer, offset, count);
Flush();
}
public void LogException(Exception ex)
{
var data = Encoding.UTF8.GetBytes("EXCEPTION: " + ex);
fileStreamReceived.Write(data, 0, data.Length);
fileStreamReceived.Flush();
}
public override void Flush()
{
fileStreamSent.Flush(true);
fileStreamReceived.Flush(true);
if (CanWrite)
{
base.Flush();
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Flush();
fileStreamSent.Dispose();
fileStreamReceived.Dispose();
}
base.Dispose(disposing);
}
}
}
#endif
...@@ -805,13 +805,6 @@ namespace Titanium.Web.Proxy ...@@ -805,13 +805,6 @@ namespace Titanium.Web.Proxy
/// <param name="exception">The exception.</param> /// <param name="exception">The exception.</param>
private void onException(CustomBufferedStream clientStream, Exception exception) private void onException(CustomBufferedStream clientStream, Exception exception)
{ {
#if DEBUG
if (clientStream is DebugCustomBufferedStream debugStream)
{
debugStream.LogException(exception);
}
#endif
ExceptionFunc(exception); ExceptionFunc(exception);
} }
......
...@@ -53,5 +53,5 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -53,5 +53,5 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
return buffer; return buffer;
} }
} }
} }
...@@ -127,11 +127,10 @@ namespace Titanium.Web.Proxy.StreamExtended ...@@ -127,11 +127,10 @@ namespace Titanium.Web.Proxy.StreamExtended
return null; return null;
} }
byte[] ciphersData = peekStream.ReadBytes(length); int[] ciphers = new int[length / 2];
int[] ciphers = new int[ciphersData.Length / 2];
for (int i = 0; i < ciphers.Length; i++) for (int i = 0; i < ciphers.Length; i++)
{ {
ciphers[i] = (ciphersData[2 * i] << 8) + ciphersData[2 * i + 1]; ciphers[i] = peekStream.ReadInt16();
} }
length = peekStream.ReadByte(); length = peekStream.ReadByte();
...@@ -286,11 +285,11 @@ namespace Titanium.Web.Proxy.StreamExtended ...@@ -286,11 +285,11 @@ namespace Titanium.Web.Proxy.StreamExtended
int cipherSuite = peekStream.ReadInt16(); int cipherSuite = peekStream.ReadInt16();
byte compressionMethod = peekStream.ReadByte(); byte compressionMethod = peekStream.ReadByte();
int extenstionsStartPosition = peekStream.Position; int extensionsStartPosition = peekStream.Position;
Dictionary<string, SslExtension>? extensions = null; Dictionary<string, SslExtension>? extensions = null;
if (extenstionsStartPosition < recordLength + 5) if (extensionsStartPosition < recordLength + 5)
{ {
extensions = await ReadExtensions(majorVersion, minorVersion, peekStream, bufferPool, cancellationToken); extensions = await ReadExtensions(majorVersion, minorVersion, peekStream, bufferPool, cancellationToken);
} }
...@@ -298,7 +297,7 @@ namespace Titanium.Web.Proxy.StreamExtended ...@@ -298,7 +297,7 @@ namespace Titanium.Web.Proxy.StreamExtended
var serverHelloInfo = new ServerHelloInfo(3, majorVersion, minorVersion, random, sessionId, cipherSuite, peekStream.Position) var serverHelloInfo = new ServerHelloInfo(3, majorVersion, minorVersion, random, sessionId, cipherSuite, peekStream.Position)
{ {
CompressionMethod = compressionMethod, CompressionMethod = compressionMethod,
EntensionsStartPosition = extenstionsStartPosition, EntensionsStartPosition = extensionsStartPosition,
Extensions = extensions, Extensions = extensions,
}; };
......
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