Implemented WebSocket frames support in TCP block.

This commit is contained in:
ruri
2019-04-24 01:24:18 +02:00
parent 514da41ede
commit cddca44d34
3 changed files with 66 additions and 4 deletions
+1 -1
View File
@@ -367,7 +367,7 @@ TCP COMMAND [ARGUMENTS]
Allowed Commands: Allowed Commands:
CONNECT "HOST" "PORT" [UseSSL?] [-> VAR/CAP "NAME"] CONNECT "HOST" "PORT" [UseSSL?] [-> VAR/CAP "NAME"]
SEND "MESSAGE" [-> VAR/CAP "NAME"] SEND "MESSAGE" [WebSocket?] [-> VAR/CAP "NAME"]
DISCONNECT DISCONNECT
Example: Example:
@@ -57,6 +57,7 @@
<TextBox Text="{Binding Message, UpdateSourceTrigger=PropertyChanged}"/> <TextBox Text="{Binding Message, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel> </DockPanel>
<Label Content="Remember to end your message with \r\n to let the server know it's over!" Foreground="{DynamicResource ForegroundCustom}"/> <Label Content="Remember to end your message with \r\n to let the server know it's over!" Foreground="{DynamicResource ForegroundCustom}"/>
<CheckBox Content="WebSocket" IsChecked="{Binding WebSocket}" VerticalContentAlignment="Center"/>
</StackPanel> </StackPanel>
</TabItem> </TabItem>
</TabControl> </TabControl>
+64 -3
View File
@@ -6,6 +6,7 @@ using System.Linq;
using System.Net.Security; using System.Net.Security;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media; using System.Windows.Media;
@@ -47,6 +48,10 @@ namespace RuriLib
/// <summary>Whether the client will communicate over the Secure Sockets Layer.</summary> /// <summary>Whether the client will communicate over the Secure Sockets Layer.</summary>
public bool UseSSL { get { return useSSL; } set { useSSL = value; OnPropertyChanged(); } } public bool UseSSL { get { return useSSL; } set { useSSL = value; OnPropertyChanged(); } }
private bool webSocket = false;
/// <summary>Whether to treat the message as a WebSocket payload (adds the frame overhead bytes).</summary>
public bool WebSocket { get { return webSocket; } set { webSocket = value; OnPropertyChanged(); } }
private string message = ""; private string message = "";
/// <summary>The message sent to the host.</summary> /// <summary>The message sent to the host.</summary>
public string Message { get { return message; } set { message = value; OnPropertyChanged(); } } public string Message { get { return message; } set { message = value; OnPropertyChanged(); } }
@@ -94,6 +99,9 @@ namespace RuriLib
case TCPCommand.Send: case TCPCommand.Send:
Message = LineParser.ParseLiteral(ref input, "Message"); Message = LineParser.ParseLiteral(ref input, "Message");
if (LineParser.Lookahead(ref input) == TokenType.Boolean)
LineParser.SetBool(ref input, this);
break; break;
default: default:
@@ -140,7 +148,8 @@ namespace RuriLib
case TCPCommand.Send: case TCPCommand.Send:
writer writer
.Literal(Message); .Literal(Message)
.Boolean(WebSocket, "WebSocket");
break; break;
} }
@@ -185,7 +194,7 @@ namespace RuriLib
} }
//Wait a bit and read the Stream if not Empty //Wait a bit and read the Stream if not Empty
System.Threading.Thread.Sleep(200); Thread.Sleep(200);
if (net.DataAvailable) if (net.DataAvailable)
{ {
if (UseSSL) bytes = ssl.Read(buffer, 0, buffer.Length); if (UseSSL) bytes = ssl.Read(buffer, 0, buffer.Length);
@@ -232,7 +241,59 @@ namespace RuriLib
} }
var msg = ReplaceValues(Message, data); var msg = ReplaceValues(Message, data);
var b = Encoding.ASCII.GetBytes(msg.Replace(@"\r\n", "\r\n")); byte[] b = { };
var payload = Encoding.ASCII.GetBytes(msg.Replace(@"\r\n", "\r\n"));
// Manual implementation of the WebSocket frame
if (WebSocket)
{
List<byte> bl = new List<byte>();
// (FIN=1) (RSV1=0) (RSV2=0) (RSV3=0) (OPCODE=0001) = 128 + 1 = 129
bl.Add(129);
ulong pllen = (ulong)payload.Length;
// We add 128 because the mask bit (MSB) is always 1. In this case the payload len is 7 bits long
if (pllen <= 125)
{
bl.Add((byte)(pllen + 128));
}
// Payload len set to 126 -> Next 2 bytes are payload len
else if (pllen <= ushort.MaxValue)
{
bl.Add(126 + 128);
bl.Add((byte)(pllen >> 8)); // Shift by 1 byte
bl.Add((byte)(pllen % 255)); // Take LSB
}
// Payload len set to 127 -> Next 4 bytes are payload len
else if (pllen <= ulong.MaxValue)
{
bl.Add(127 + 128);
bl.Add((byte)(pllen >> 24)); // Shift by 3 bytes
bl.Add((byte)((pllen >> 16) % 255)); // Shift by 2 bytes and take LSB
bl.Add((byte)((pllen >> 8) % 255)); // Shift by 1 byte and take LSB
bl.Add((byte)(pllen % 255)); // Take LSB
}
// Set the mask used for this message
byte[] mask = new byte[4] { 61, 84, 35, 6 };
bl.AddRange(mask);
// Finally we add the payload XORed with the mask
for (int i = 0; i < payload.Length; i++)
{
bl.Add((byte)(payload[i] ^ mask[i % 4]));
}
b = bl.ToArray();
}
else
{
b = payload;
}
data.Log(new LogEntry("> " + msg, Colors.White)); data.Log(new LogEntry("> " + msg, Colors.White));
if (data.TCPSSL) if (data.TCPSSL)