packet-parser/index.js
2025-02-06 17:32:31 +03:00

32 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function simpleParseHeaders (packet) {
return Object.fromEntries(packet.match(/[a-z\-]{1,}:\s{0,}.{1,}(\n|\r){1,}/gmiu)?.map(i => {
const [key, value] = i.split(/:\s{0,}/miu);
return [
key.trim(),
value.trim()
];
}) ?? []);
}
class PacketParseResult {
constructor (result, isEncrypted = false) {
this.result = result;
this.isEncrypted = isEncrypted;
}
}
module.exports = async (bytes) => {
const packet = bytes.toString("utf8");
const isHTTP = packet.match(/(GET|POST|PUT|DELETE|OPTIONS|HEAD|PATCH|TRACE|CONNECT)\s{1,}\/[a-zA-Z0-9_\-\.\/?=&%:]{0,}\s{1,}HTTP(S|)\/(1\.0|1\.1|2\.0)(\n|\r)/gmu)?.length > 0 ?? false;
let isWebSocket = false;
if (isHTTP) {
const headers = simpleParseHeaders(packet);
if (headers.Upgrade === "websocket")
isWebSocket = true;
}
return new PacketParseResult({
isHTTP, isWebSocket
}, false);
}