Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket #1910

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"CBOR Decode",
"Caret/M-decode",
"Rison Encode",
"Rison Decode"
"Rison Decode",
"WebSocket Decode"
]
},
{
Expand Down
9 changes: 9 additions & 0 deletions src/core/lib/WebSocket.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* WebSocket operation resources.
*
* @author cod [[email protected]]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

export const WEBSOCKET_OPCODE = ["continuation frame", "text frame", "binary frame", "rsv3", "rsv4", "rsv5", "rsv6", "rsv7", "ping", "pong", "rsvb", "rsvc", "rsvd", "rsve", "rsvf"];
116 changes: 116 additions & 0 deletions src/core/operations/WebSocketDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @author cod [[email protected]]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import { WEBSOCKET_OPCODE } from "../lib/WebSocket.mjs";

/** WebSocket */
class WebSocketDecode extends Operation {

/**
* Default constructor
*/
constructor() {
super();

this.name = "WebSocket Decode";
this.module = "Code";
this.description = "Converts a WebSocket encoded to JSON. WebSocket is computer communitions protocol, providing simultaneous two-way communication channels";
this.infoURL = "https://wikipedia.org/wiki/WebSocket";
this.inputType = "ArrayBuffer";
this.outputType = "JSON";
this.args = [];
}

/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
try {
let pos = 0;
const buf = Buffer.from(new Uint8Array(input));

if (buf.length < 2) {
throw new OperationError("Expected size: 2 bytes");
}

const b0 = buf[0];
const b1 = buf[1];

const fin = (b0 & 0x80) === 0x80 ? true : false;
const opcode = WEBSOCKET_OPCODE[b0 & 0x7f];
let payloadLength = (b1 & 0x7f);

pos = 2;
const mask = (b1 & 0x80) === 0x80 ? true : false;
const maskSize = (mask) ? 4 : 0;

if (payloadLength < 126) {
/** payload is less of 126 bytes */
if (buf.length < (pos + payloadLength + maskSize))
throw new OperationError(`Expected size: ${(pos + payloadLength + maskSize)}`);
} else if (payloadLength === 126) {
/** payload is 2 bytes */
pos += 2;
payloadLength = buf.readUInt16BE(2);

if (buf.length < (pos + payloadLength + maskSize))
throw new OperationError(`Expected size: ${(pos + payloadLength + maskSize)}`);

} else if (payloadLength === 127) {
pos += 8;
payloadLength = buf.readUInt64BE(2);

if (buf.length < (pos + payloadLength + maskSize))
throw new OperationError(`Expected size: ${(pos + payloadLength + maskSize)}`);
}

const maskBytes = Buffer.alloc(4);

if (mask) {
maskBytes[0] = buf[pos];
maskBytes[1] = buf[pos+1];
maskBytes[2] = buf[pos+2];
maskBytes[3] = buf[pos+3];
pos += 4;
}

const payload = buf.slice(pos, buf.length);

if (mask) {
for (let i = 0; i < payload.length; i++) {
payload[i] = payload[i] ^ maskBytes[i % 4];
}
}

let content;

if ((b0 & 1) === 1) {
content = payload.toString();
} else {
content = payload.toString("hex");
}

const ws = {
fin: fin,
opcode: opcode,
maskPresent: mask,
mask: maskBytes.toString("hex"),
payloadLength: payloadLength,
payload: content
};

return ws;
} catch (err) {
throw new OperationError(`Could not decode WebSocket to JSON: ${err}`);
}
}
}

export default WebSocketDecode;
Loading