diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-01-16 02:37:38 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-01-16 02:38:43 +1100 |
commit | 1569fec0cf23442b176b2a9a775208349aa719ed (patch) | |
tree | f36aff30a3d5e82f569fa7815c83a33a3066c7d3 /webrtc/src | |
parent | Fix typo in commit 277b28c (diff) | |
download | server-1569fec0cf23442b176b2a9a775208349aa719ed.tar.xz |
boilerplate stuff
Diffstat (limited to 'webrtc/src')
-rw-r--r-- | webrtc/src/Server.ts | 36 | ||||
-rw-r--r-- | webrtc/src/opcodes/Connect.ts | 5 | ||||
-rw-r--r-- | webrtc/src/opcodes/Heartbeat.ts | 7 | ||||
-rw-r--r-- | webrtc/src/opcodes/Identify.ts | 20 | ||||
-rw-r--r-- | webrtc/src/opcodes/Resume.ts | 5 | ||||
-rw-r--r-- | webrtc/src/opcodes/SelectProtocol.ts | 16 | ||||
-rw-r--r-- | webrtc/src/opcodes/Speaking.ts | 6 | ||||
-rw-r--r-- | webrtc/src/opcodes/index.ts | 35 | ||||
-rw-r--r-- | webrtc/src/start.ts | 1 | ||||
-rw-r--r-- | webrtc/src/util/Heartbeat.ts | 18 | ||||
-rw-r--r-- | webrtc/src/util/index.ts | 1 |
11 files changed, 129 insertions, 21 deletions
diff --git a/webrtc/src/Server.ts b/webrtc/src/Server.ts index 6591691c..06a36df9 100644 --- a/webrtc/src/Server.ts +++ b/webrtc/src/Server.ts @@ -1,5 +1,8 @@ import { Server as WebSocketServer } from "ws"; -import { Config, db } from "@fosscord/util"; +import { WebSocket, CLOSECODES, Payload, OPCODES } from "@fosscord/gateway"; +import { Config, initDatabase } from "@fosscord/util"; +import OPCodeHandlers from "./opcodes"; +import { setHeartbeat } from "./util" import mediasoup from "mediasoup"; var port = Number(process.env.PORT); @@ -7,38 +10,29 @@ if (isNaN(port)) port = 3004; export class Server { public ws: WebSocketServer; - public turn: any; constructor() { this.ws = new WebSocketServer({ port, maxPayload: 4096, }); - this.ws.on("connection", (socket) => { - socket.on("message", (message) => { - socket.emit( - JSON.stringify({ - op: 2, - d: { - ssrc: 1, - ip: "127.0.0.1", - port: 3004, - modes: [ - "xsalsa20_poly1305", - "xsalsa20_poly1305_suffix", - "xsalsa20_poly1305_lite", - ], - heartbeat_interval: 1, - }, - }) - ); + this.ws.on("connection", async (socket: WebSocket) => { + await setHeartbeat(socket); + + socket.on("message", async (message: string) => { + const payload: Payload = JSON.parse(message); + + if (OPCodeHandlers[payload.op]) + await OPCodeHandlers[payload.op](socket, payload); + else + console.error(`Unimplemented`, payload) }); }); } async listen(): Promise<void> { // @ts-ignore - await (db as Promise<Connection>); + await initDatabase(); await Config.init(); console.log("[DB] connected"); console.log(`[WebRTC] online on 0.0.0.0:${port}`); diff --git a/webrtc/src/opcodes/Connect.ts b/webrtc/src/opcodes/Connect.ts new file mode 100644 index 00000000..5cc66506 --- /dev/null +++ b/webrtc/src/opcodes/Connect.ts @@ -0,0 +1,5 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; + +export async function onConnect(socket: WebSocket, data: Payload) { +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Heartbeat.ts b/webrtc/src/opcodes/Heartbeat.ts new file mode 100644 index 00000000..04150e36 --- /dev/null +++ b/webrtc/src/opcodes/Heartbeat.ts @@ -0,0 +1,7 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { setHeartbeat } from "./../util"; + +export async function onHeartbeat(socket: WebSocket, data: Payload) { + await setHeartbeat(socket); +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Identify.ts b/webrtc/src/opcodes/Identify.ts new file mode 100644 index 00000000..2026d7c9 --- /dev/null +++ b/webrtc/src/opcodes/Identify.ts @@ -0,0 +1,20 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index" +import { VoiceOPCodes } from "@fosscord/util"; + +export async function onIdentify(socket: WebSocket, data: Payload) { + socket.send(JSON.stringify({ + op: VoiceOPCodes.READY, + d: { + ssrc: 1, + ip: "127.0.0.1", + port: 3005, + modes: [ + "xsalsa20_poly1305", + "xsalsa20_poly1305_suffix", + "xsalsa20_poly1305_lite", + ], + heartbeat_interval: 1, + }, + })); +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Resume.ts b/webrtc/src/opcodes/Resume.ts new file mode 100644 index 00000000..de21eba6 --- /dev/null +++ b/webrtc/src/opcodes/Resume.ts @@ -0,0 +1,5 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; + +export async function onResume(socket: WebSocket, data: Payload) { +} \ No newline at end of file diff --git a/webrtc/src/opcodes/SelectProtocol.ts b/webrtc/src/opcodes/SelectProtocol.ts new file mode 100644 index 00000000..f1732dd9 --- /dev/null +++ b/webrtc/src/opcodes/SelectProtocol.ts @@ -0,0 +1,16 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index"; +import { VoiceOPCodes } from "@fosscord/util"; + +export async function onSelectProtocol(socket: WebSocket, data: Payload) { + socket.send(JSON.stringify({ + op: VoiceOPCodes.SESSION_DESCRIPTION, + d: { + video_codec: "H264", + secret_key: new Array(32).fill(null).map(x => Math.random() * 256), + mode: "aead_aes256_gcm_rtpsize", + media_session_id: "d8eb5c84d987c6642ec4ce72ffa97f00", + audio_codec: "opus", + } + })); +} \ No newline at end of file diff --git a/webrtc/src/opcodes/Speaking.ts b/webrtc/src/opcodes/Speaking.ts new file mode 100644 index 00000000..14f86b3c --- /dev/null +++ b/webrtc/src/opcodes/Speaking.ts @@ -0,0 +1,6 @@ +import { WebSocket } from "@fosscord/gateway"; +import { Payload } from "./index" +import { VoiceOPCodes } from "@fosscord/util"; + +export async function onSpeaking(socket: WebSocket, data: Payload) { +} \ No newline at end of file diff --git a/webrtc/src/opcodes/index.ts b/webrtc/src/opcodes/index.ts new file mode 100644 index 00000000..2fe69c38 --- /dev/null +++ b/webrtc/src/opcodes/index.ts @@ -0,0 +1,35 @@ +import { WebSocket } from "@fosscord/gateway"; +import { VoiceOPCodes } from "@fosscord/util"; + +export interface Payload { + op: number; + d?: any; + s?: number; + t?: string; +} + +import { onIdentify } from "./Identify"; +import { onSelectProtocol } from "./SelectProtocol"; +import { onHeartbeat } from "./Heartbeat"; +import { onSpeaking } from "./Speaking"; +import { onResume } from "./Resume"; +import { onConnect } from "./Connect"; + +export type OPCodeHandler = (this: WebSocket, data: Payload) => any; + +export default { + [VoiceOPCodes.IDENTIFY]: onIdentify, //op 0 + [VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol, //op 1 + //op 2 voice_ready + [VoiceOPCodes.HEARTBEAT]: onHeartbeat, //op 3 + //op 4 session_description + [VoiceOPCodes.SPEAKING]: onSpeaking, //op 5 + //op 6 heartbeat_ack + [VoiceOPCodes.RESUME]: onResume, //op 7 + //op 8 hello + //op 9 resumed + //op 10? + //op 11? + [VoiceOPCodes.CLIENT_CONNECT]: onConnect, //op 12 + //op 13? +}; \ No newline at end of file diff --git a/webrtc/src/start.ts b/webrtc/src/start.ts index 68867a2c..5614982d 100644 --- a/webrtc/src/start.ts +++ b/webrtc/src/start.ts @@ -1,3 +1,4 @@ import { Server } from "./Server"; const server = new Server(); +server.listen(); \ No newline at end of file diff --git a/webrtc/src/util/Heartbeat.ts b/webrtc/src/util/Heartbeat.ts new file mode 100644 index 00000000..7b5ed9cd --- /dev/null +++ b/webrtc/src/util/Heartbeat.ts @@ -0,0 +1,18 @@ +import { WebSocket, CLOSECODES } from "@fosscord/gateway"; +import { VoiceOPCodes } from "@fosscord/util"; + +export async function setHeartbeat(socket: WebSocket) { + if (socket.heartbeatTimeout) clearTimeout(socket.heartbeatTimeout); + + socket.heartbeatTimeout = setTimeout(() => { + return socket.close(CLOSECODES.Session_timed_out); + }, 1000 * 45); + + socket.send(JSON.stringify({ + op: VoiceOPCodes.HEARTBEAT_ACK, + d: { + v: 6, + heartbeat_interval: 13750, + } + })); +} \ No newline at end of file diff --git a/webrtc/src/util/index.ts b/webrtc/src/util/index.ts new file mode 100644 index 00000000..e8557452 --- /dev/null +++ b/webrtc/src/util/index.ts @@ -0,0 +1 @@ +export * from "./Heartbeat" \ No newline at end of file |