2017-04-24 21:04:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-09 12:22:04 +00:00
|
|
|
|
using Square;
|
2017-04-24 21:04:52 +00:00
|
|
|
|
|
2017-04-27 20:44:30 +00:00
|
|
|
|
namespace Kneesocks {
|
|
|
|
|
public class Frame {
|
2017-05-01 21:04:34 +00:00
|
|
|
|
public enum kClosingReason {
|
|
|
|
|
Normal = 1000,
|
|
|
|
|
GoingAway = 1001,
|
|
|
|
|
ProtocolError = 1002,
|
|
|
|
|
FrameTypeError = 1003,
|
|
|
|
|
DataError = 1007,
|
|
|
|
|
PolicyError = 1008,
|
|
|
|
|
FrameTooBig = 1009,
|
|
|
|
|
RequestError = 1011
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 21:01:41 +00:00
|
|
|
|
public enum kOpcode {
|
|
|
|
|
Continuation = 0x0,
|
|
|
|
|
TextFrame = 0x1,
|
|
|
|
|
BinaryFrame = 0x2,
|
|
|
|
|
Close = 0x8,
|
|
|
|
|
Ping = 0x9,
|
|
|
|
|
Pong = 0xA
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-01 21:04:34 +00:00
|
|
|
|
public kOpcode Opcode { get; set; } = kOpcode.BinaryFrame;
|
|
|
|
|
public bool IsFinal { get; set; } = true;
|
|
|
|
|
public bool IsMasked { get; set; } = false;
|
2017-04-29 13:06:20 +00:00
|
|
|
|
public byte[] Mask { get; set; } = new byte[] { 0, 0, 0, 0 };
|
2017-05-01 21:04:34 +00:00
|
|
|
|
public byte Reserved { get; set; } = 0;
|
|
|
|
|
|
2017-05-05 21:05:52 +00:00
|
|
|
|
private int _HeaderLength = 0;
|
|
|
|
|
public int HeaderLength {
|
|
|
|
|
get {
|
|
|
|
|
if(_HeaderLength != 0)
|
|
|
|
|
return _HeaderLength;
|
|
|
|
|
|
|
|
|
|
int length = 2
|
|
|
|
|
+ (BodyLength >= 0x7E && BodyLength <= 0xFFFF ? 2 : 0)
|
2017-05-10 21:10:42 +00:00
|
|
|
|
+ (BodyLength > 0xFFFF ? 8 : 0)
|
2017-05-05 21:05:52 +00:00
|
|
|
|
+ (IsMasked ? 4 : 0);
|
|
|
|
|
|
|
|
|
|
return (_HeaderLength = length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int _BodyLength = 0;
|
|
|
|
|
public int BodyLength {
|
2017-05-20 23:33:39 +00:00
|
|
|
|
get => Content == null ? _BodyLength : Content.Length;
|
2017-05-05 21:05:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] Content { get; set; } = null;
|
2017-04-28 21:01:58 +00:00
|
|
|
|
public byte[] MaskedContent {
|
|
|
|
|
get {
|
2017-05-01 21:04:34 +00:00
|
|
|
|
long counter = 0;
|
|
|
|
|
return Content.Select(x => (byte)(x ^ Mask[counter++ % 4])).ToArray();
|
2017-04-28 21:01:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-24 21:04:52 +00:00
|
|
|
|
|
2017-05-01 21:04:34 +00:00
|
|
|
|
public byte[] GetBytes() {
|
2017-05-05 21:05:52 +00:00
|
|
|
|
var headerSize = 2;
|
|
|
|
|
var bodySize = (UInt64)Content.LongLength;
|
2017-05-01 21:04:34 +00:00
|
|
|
|
var headerLengthFirstByte = (byte)Content.Length;
|
|
|
|
|
if(bodySize >= 0x7E && bodySize <= 0xFFFF) {
|
2017-05-01 12:18:27 +00:00
|
|
|
|
headerSize += 2;
|
2017-05-19 21:02:39 +00:00
|
|
|
|
headerLengthFirstByte = 0x7e;
|
2017-05-01 21:04:34 +00:00
|
|
|
|
} else if(bodySize > 0xFFFF) {
|
2017-05-01 12:18:27 +00:00
|
|
|
|
headerSize += 8;
|
2017-05-19 21:02:39 +00:00
|
|
|
|
headerLengthFirstByte = 0x7f;
|
2017-05-01 21:04:34 +00:00
|
|
|
|
}
|
2017-05-19 21:02:39 +00:00
|
|
|
|
var headerLengthSize = headerSize - 1;
|
2017-05-01 12:18:27 +00:00
|
|
|
|
if(IsMasked)
|
|
|
|
|
headerSize += 4;
|
|
|
|
|
|
2017-05-05 21:05:52 +00:00
|
|
|
|
var returnValue = new byte[(UInt64)headerSize + bodySize];
|
2017-05-01 21:04:34 +00:00
|
|
|
|
returnValue[0] = (byte)(((byte)Opcode % 0x10)
|
|
|
|
|
| ((Reserved % 8) << 4)
|
|
|
|
|
| (IsFinal ? 0x80 : 0x0));
|
|
|
|
|
|
|
|
|
|
returnValue[1] = (byte)(headerLengthFirstByte
|
|
|
|
|
| (IsMasked ? 0x80 : 0x0));
|
|
|
|
|
|
|
|
|
|
if(headerLengthFirstByte >= 0x7E) {
|
|
|
|
|
var upperBound = headerLengthFirstByte == 0x7E ? 2 : 8;
|
|
|
|
|
for(var i = 0; i < upperBound; ++i)
|
|
|
|
|
returnValue[2 + i] |= (byte)((bodySize >> (8*(upperBound - i))) & 0xFF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(IsMasked)
|
|
|
|
|
Array.Copy(Mask, 0, returnValue, headerSize - 4, 4);
|
|
|
|
|
|
|
|
|
|
Array.Copy(IsMasked ? MaskedContent : Content, 0L, returnValue, headerSize, Content.LongLength);
|
2017-05-01 12:18:27 +00:00
|
|
|
|
return returnValue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-10 21:10:42 +00:00
|
|
|
|
public static int HeaderLengthFromBytes(byte[] raw) {
|
2017-05-05 21:05:52 +00:00
|
|
|
|
if(raw.Length < 2)
|
|
|
|
|
throw new FormatException("Need first two bytes to analyze");
|
|
|
|
|
|
|
|
|
|
var lengthByte = raw[1] & 0x7F;
|
|
|
|
|
return 2
|
2017-05-19 21:02:39 +00:00
|
|
|
|
+ ((raw[1] & 0x80) != 0 ? 4 : 0)
|
|
|
|
|
+ (lengthByte == 0x7E ? 2 : 0)
|
|
|
|
|
+ (lengthByte == 0x7F ? 8 : 0);
|
2017-05-05 21:05:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Frame HeaderFromBytes(byte[] raw) {
|
2017-04-29 13:06:20 +00:00
|
|
|
|
if(raw.Length < 2)
|
|
|
|
|
throw new FormatException("Websocket frame cannot be less than two bytes long");
|
|
|
|
|
|
|
|
|
|
var rawOpcode = raw[0] & 0x0F;
|
|
|
|
|
if(!Enum.IsDefined(typeof(kOpcode), rawOpcode))
|
2017-05-05 21:05:52 +00:00
|
|
|
|
throw new ArgumentException("Opcode '0x" + rawOpcode.ToString("X") + "' not understood");
|
|
|
|
|
|
2017-04-29 13:06:20 +00:00
|
|
|
|
var returnFrame = new Frame {
|
2017-05-05 21:05:52 +00:00
|
|
|
|
IsFinal = (raw[0] & 0x80) != 0,
|
|
|
|
|
Opcode = (kOpcode)rawOpcode,
|
2017-04-29 13:06:20 +00:00
|
|
|
|
IsMasked = (raw[1] & 0x80) != 0,
|
|
|
|
|
Reserved = (byte)((raw[0] & 0x70) >> 4)
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-01 12:18:27 +00:00
|
|
|
|
ulong bodyLength = raw[1] & 0x7Ful;
|
2017-05-05 21:05:52 +00:00
|
|
|
|
int headerOffset =
|
|
|
|
|
bodyLength < 0x7E
|
|
|
|
|
? 1
|
2017-05-01 21:04:34 +00:00
|
|
|
|
: (bodyLength == 0x7E ? 3 : 9);
|
2017-05-05 21:05:52 +00:00
|
|
|
|
|
2017-05-10 21:10:42 +00:00
|
|
|
|
if(raw.Length < headerOffset + 1 + (returnFrame.IsMasked ? 4 : 0))
|
2017-05-01 21:04:34 +00:00
|
|
|
|
throw new FormatException("Websocket frame is smaller than expected header size");
|
2017-04-29 21:07:17 +00:00
|
|
|
|
|
2017-05-10 21:10:42 +00:00
|
|
|
|
if(bodyLength >= 0x7E)
|
|
|
|
|
bodyLength = bodyLength == 0x7E ? raw.Subset(2, 2).UnpackUInt16()
|
|
|
|
|
: raw.Subset(2, 8).UnpackUInt64();
|
2017-05-05 21:05:52 +00:00
|
|
|
|
|
|
|
|
|
if(bodyLength > Int32.MaxValue)
|
|
|
|
|
throw new FormatException("Frame is too large to interpret");
|
2017-04-29 21:07:17 +00:00
|
|
|
|
|
2017-05-05 21:05:52 +00:00
|
|
|
|
returnFrame._BodyLength = (int)bodyLength;
|
|
|
|
|
|
|
|
|
|
if(returnFrame.IsMasked)
|
2017-05-01 21:04:34 +00:00
|
|
|
|
Array.Copy(raw, headerOffset + 1, returnFrame.Mask, 0, 4);
|
2017-04-29 21:07:17 +00:00
|
|
|
|
|
2017-05-05 21:05:52 +00:00
|
|
|
|
return returnFrame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Frame FromBytes(byte[] raw) {
|
|
|
|
|
var returnFrame = HeaderFromBytes(raw);
|
|
|
|
|
|
|
|
|
|
uint expectedFrameLength = (uint)returnFrame.BodyLength + (uint)returnFrame.HeaderLength;
|
2017-05-10 21:10:42 +00:00
|
|
|
|
if((uint)raw.Length < expectedFrameLength)
|
2017-05-05 21:05:52 +00:00
|
|
|
|
throw new FormatException("Raw frame length ("+ (uint)raw.Length + ") is less than described size ("+ expectedFrameLength + ")");
|
|
|
|
|
|
|
|
|
|
returnFrame.Content = new byte[returnFrame.BodyLength];
|
|
|
|
|
Array.Copy(raw, returnFrame.HeaderLength, returnFrame.Content, 0L, (long)returnFrame.BodyLength);
|
2017-05-01 21:04:34 +00:00
|
|
|
|
if(returnFrame.IsMasked)
|
|
|
|
|
returnFrame.Content = returnFrame.MaskedContent;
|
2017-05-01 12:18:27 +00:00
|
|
|
|
|
2017-05-01 21:04:34 +00:00
|
|
|
|
return returnFrame;
|
|
|
|
|
}
|
2017-05-01 12:18:27 +00:00
|
|
|
|
|
2017-05-01 21:04:34 +00:00
|
|
|
|
public static Frame Closing(kClosingReason status = kClosingReason.Normal, string reason = "") {
|
|
|
|
|
var data = new byte[2 + reason.ByteLength()];
|
|
|
|
|
data[0] = (byte)(((short)status >> 8) & 0xFF);
|
|
|
|
|
data[1] = (byte)((short)status & 0xFF);
|
|
|
|
|
Array.Copy(reason.GetBytes(), 0, data, 2, reason.ByteLength());
|
2017-04-29 13:06:20 +00:00
|
|
|
|
|
2017-05-01 21:04:34 +00:00
|
|
|
|
return new Frame {
|
|
|
|
|
Opcode = kOpcode.Close,
|
|
|
|
|
Content = data
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Frame Ping(string data = "") {
|
|
|
|
|
return new Frame {
|
|
|
|
|
Opcode = kOpcode.Ping,
|
|
|
|
|
Content = data.GetBytes()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Frame Pong(string data = "") {
|
|
|
|
|
return new Frame {
|
|
|
|
|
Opcode = kOpcode.Pong,
|
|
|
|
|
Content = data.GetBytes()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Frame Pong(Frame pingFrame) {
|
|
|
|
|
pingFrame.Opcode = kOpcode.Pong;
|
|
|
|
|
return pingFrame;
|
2017-04-28 21:01:58 +00:00
|
|
|
|
}
|
2017-04-24 21:04:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|