Commit c46293d5 authored by Honfika's avatar Honfika

better websocker unmask

parent 7cb42ed1
...@@ -152,9 +152,21 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -152,9 +152,21 @@ namespace Titanium.Web.Proxy.Examples.Basic
} }
} }
private void WebSocket_DataSent(object sender, DataEventArgs e)
{
var args = (SessionEventArgs)sender;
WebSocketDataSentReceived(args, e, true);
}
private void WebSocket_DataReceived(object sender, DataEventArgs e) private void WebSocket_DataReceived(object sender, DataEventArgs e)
{ {
var args = (SessionEventArgs)sender; var args = (SessionEventArgs)sender;
WebSocketDataSentReceived(args, e, false);
}
private void WebSocketDataSentReceived(SessionEventArgs args, DataEventArgs e, bool sent)
{
var color = sent ? ConsoleColor.Green : ConsoleColor.Blue;
foreach (var frame in args.WebSocketDecoder.Decode(e.Buffer, e.Offset, e.Count)) foreach (var frame in args.WebSocketDecoder.Decode(e.Buffer, e.Offset, e.Count))
{ {
...@@ -162,12 +174,12 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -162,12 +174,12 @@ namespace Titanium.Web.Proxy.Examples.Basic
{ {
var data = frame.Data.ToArray(); var data = frame.Data.ToArray();
string str = string.Join(",", data.ToArray().Select(x => x.ToString("X2"))); string str = string.Join(",", data.ToArray().Select(x => x.ToString("X2")));
writeToConsole(str, ConsoleColor.Blue).Wait(); writeToConsole(str, color).Wait();
} }
if (frame.OpCode == WebsocketOpCode.Text) if (frame.OpCode == WebsocketOpCode.Text)
{ {
writeToConsole(frame.GetText(), ConsoleColor.Blue).Wait(); writeToConsole(frame.GetText(), color).Wait();
} }
} }
} }
...@@ -233,6 +245,7 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -233,6 +245,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
{ {
if (e.HttpClient.ConnectRequest?.TunnelType == TunnelType.Websocket) if (e.HttpClient.ConnectRequest?.TunnelType == TunnelType.Websocket)
{ {
e.DataSent += WebSocket_DataSent;
e.DataReceived += WebSocket_DataReceived; e.DataReceived += WebSocket_DataReceived;
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using Titanium.Web.Proxy.StreamExtended.BufferPool; using Titanium.Web.Proxy.StreamExtended.BufferPool;
namespace Titanium.Web.Proxy namespace Titanium.Web.Proxy
...@@ -60,25 +61,51 @@ namespace Titanium.Web.Proxy ...@@ -60,25 +61,51 @@ namespace Titanium.Web.Proxy
} }
} }
uint mask = 0; if (size < 0)
if (masked)
{ {
//mask = (uint)(((long)data1[idx++] << 24) + (data1[idx++] << 16) + (data1[idx++] << 8) + data1[idx++]); ;
mask = (uint)(data1[idx++] + (data1[idx++] << 8) + (data1[idx++] << 16) + ((long)data1[idx++] << 24)); }
if (data1.Length < idx + size)
{
break;
} }
if (masked) if (masked)
{ {
uint m = mask; //mask = (uint)(((long)data1[idx++] << 24) + (data1[idx++] << 16) + (data1[idx++] << 8) + data1[idx++]);
for (int i = 0; i < size; i++) //mask = (uint)(data1[idx++] + (data1[idx++] << 8) + (data1[idx++] << 16) + ((long)data1[idx++] << 24));
{ var uData = MemoryMarshal.Cast<byte, uint>(data1.Slice(idx, (int)size + 4));
data[i + idx] = (byte)(data1[i + idx] ^ (byte)mask); idx += 4;
m >>= 8; uint mask = uData[0];
long size1 = size;
if (size > 4)
{
uData = uData.Slice(1);
for (int i = 0; i < uData.Length; i++)
{
uData[i] = uData[i] ^ mask;
}
if (m == 0) size1 -= uData.Length * 4;
m = mask;
} }
if (size1 > 0)
{
int pos = (int)(idx + size - size1);
data1[pos] ^= (byte)mask;
if (size1 > 1)
{
data1[pos + 1] ^= (byte)(mask >> 8);
}
if (size1 > 2)
{
data1[pos + 2] ^= (byte)(mask >> 16);
}
; }
} }
var frameData = buffer.Slice(idx, (int)size); var frameData = buffer.Slice(idx, (int)size);
......
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