diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-09-25 18:24:21 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-09-25 23:35:18 +1000 |
commit | f44f5d7ac2d24ff836c2e1d4b2fa58da04b13052 (patch) | |
tree | a6655c41bb3db79c30fd876b06ee60fe9cf70c9b /src/webrtc/events | |
parent | Allow edited_timestamp to passthrough in handleMessage (diff) | |
download | server-f44f5d7ac2d24ff836c2e1d4b2fa58da04b13052.tar.xz |
Refactor to mono-repo + upgrade packages
Diffstat (limited to 'src/webrtc/events')
-rw-r--r-- | src/webrtc/events/Close.ts | 9 | ||||
-rw-r--r-- | src/webrtc/events/Connection.ts | 60 | ||||
-rw-r--r-- | src/webrtc/events/Message.ts | 38 |
3 files changed, 107 insertions, 0 deletions
diff --git a/src/webrtc/events/Close.ts b/src/webrtc/events/Close.ts new file mode 100644 index 00000000..1c203653 --- /dev/null +++ b/src/webrtc/events/Close.ts @@ -0,0 +1,9 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Session } from "@fosscord/util"; + +export async function onClose(this: WebSocket, code: number, reason: string) { + console.log("[WebRTC] closed", code, reason.toString()); + + if (this.session_id) await Session.delete({ session_id: this.session_id }); + this.removeAllListeners(); +} \ No newline at end of file diff --git a/src/webrtc/events/Connection.ts b/src/webrtc/events/Connection.ts new file mode 100644 index 00000000..bf228d64 --- /dev/null +++ b/src/webrtc/events/Connection.ts @@ -0,0 +1,60 @@ +import { CLOSECODES, Send, setHeartbeat, WebSocket } from "@fosscord/gateway"; +import { IncomingMessage } from "http"; +import { URL } from "url"; +import WS from "ws"; +import { VoiceOPCodes } from "../util"; +import { onClose } from "./Close"; +import { onMessage } from "./Message"; +var erlpack: any; +try { + erlpack = require("@yukikaze-bot/erlpack"); +} catch (error) {} + +// TODO: check rate limit +// TODO: specify rate limit in config +// TODO: check msg max size + +export async function Connection(this: WS.Server, socket: WebSocket, request: IncomingMessage) { + try { + socket.on("close", onClose.bind(socket)); + socket.on("message", onMessage.bind(socket)); + console.log("[WebRTC] new connection", request.url); + + if (process.env.WS_LOGEVENTS) { + [ + "close", + "error", + "upgrade", + //"message", + "open", + "ping", + "pong", + "unexpected-response" + ].forEach((x) => { + socket.on(x, (y) => console.log("[WebRTC]", x, y)); + }); + } + + const { searchParams } = new URL(`http://localhost${request.url}`); + + socket.encoding = "json"; + socket.version = Number(searchParams.get("v")) || 5; + if (socket.version < 3) return socket.close(CLOSECODES.Unknown_error, "invalid version"); + + setHeartbeat(socket); + + socket.readyTimeout = setTimeout(() => { + return socket.close(CLOSECODES.Session_timed_out); + }, 1000 * 30); + + await Send(socket, { + op: VoiceOPCodes.HELLO, + d: { + heartbeat_interval: 1000 * 30 + } + }); + } catch (error) { + console.error("[WebRTC]", error); + return socket.close(CLOSECODES.Unknown_error); + } +} \ No newline at end of file diff --git a/src/webrtc/events/Message.ts b/src/webrtc/events/Message.ts new file mode 100644 index 00000000..8f75a815 --- /dev/null +++ b/src/webrtc/events/Message.ts @@ -0,0 +1,38 @@ +import { CLOSECODES, Payload, WebSocket } from "@fosscord/gateway"; +import { Tuple } from "lambert-server"; +import OPCodeHandlers from "../opcodes"; +import { VoiceOPCodes } from "../util"; + +const PayloadSchema = { + op: Number, + $d: new Tuple(Object, Number), // or number for heartbeat sequence + $s: Number, + $t: String +}; + +export async function onMessage(this: WebSocket, buffer: Buffer) { + try { + var data: Payload = JSON.parse(buffer.toString()); + if (data.op !== VoiceOPCodes.IDENTIFY && !this.user_id) return this.close(CLOSECODES.Not_authenticated); + + // @ts-ignore + const OPCodeHandler = OPCodeHandlers[data.op]; + if (!OPCodeHandler) { + // @ts-ignore + console.error("[WebRTC] Unkown opcode " + VoiceOPCodes[data.op]); + // TODO: if all opcodes are implemented comment this out: + // this.close(CloseCodes.Unknown_opcode); + return; + } + + if (![VoiceOPCodes.HEARTBEAT, VoiceOPCodes.SPEAKING].includes(data.op as VoiceOPCodes)) { + // @ts-ignore + console.log("[WebRTC] Opcode " + VoiceOPCodes[data.op]); + } + + return await OPCodeHandler.call(this, data); + } catch (error) { + console.error("[WebRTC] error", error); + // if (!this.CLOSED && this.CLOSING) return this.close(CloseCodes.Unknown_error); + } +} \ No newline at end of file |