diff options
Diffstat (limited to 'webrtc/src/Server.ts')
-rw-r--r-- | webrtc/src/Server.ts | 64 |
1 files changed, 52 insertions, 12 deletions
diff --git a/webrtc/src/Server.ts b/webrtc/src/Server.ts index 5b76759a..67f60f9f 100644 --- a/webrtc/src/Server.ts +++ b/webrtc/src/Server.ts @@ -5,8 +5,8 @@ import OPCodeHandlers, { Payload } from "./opcodes"; import { setHeartbeat } from "./util"; import * as mediasoup from "mediasoup"; import { types as MediasoupTypes } from "mediasoup"; - import udp from "dgram"; +import sodium from "libsodium-wrappers"; var port = Number(process.env.PORT); if (isNaN(port)) port = 3004; @@ -47,19 +47,59 @@ export class Server { }); }); - // this.testUdp.bind(50001); - // this.testUdp.on("message", (msg, rinfo) => { - // if (msg[0] === 0 && msg[1] === 1 && msg[2] === 0) { //idk stun? - - // } - // }) + this.testUdp.bind(50001); + this.testUdp.on("message", (msg, rinfo) => { + //random key from like, the libsodium examples on npm lol + const decryptKey = sodium.from_hex("724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed"); + + //give me my remote port? + if (sodium.to_hex(msg) == "0001004600000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") { + this.testUdp.send(Buffer.from([rinfo.port, 0]), rinfo.port, rinfo.address); + console.log(`got magic packet to send remote port? ${rinfo.address}:${rinfo.port}`); + return; + } + + //Hello + if (sodium.to_hex(msg) == "0100000000000000") { + console.log(`[UDP] client helloed`); + return; + } + + const nonce = Buffer.concat([msg.slice(-4), Buffer.from("\x00".repeat(20))]); + console.log(`[UDP] nonce for this message: ${nonce}`); + + console.log(sodium.to_hex(msg)); + if (sodium.to_hex(msg).indexOf("80c8000600000001") == 0) { + //call status + const encrypted = msg.slice(8, -4); + const currentPacket = msg.slice(-4); + console.log(`[UDP] Current packet: ${currentPacket}`); + try { + console.log(`[UDP] Encrypted bytes: ${encrypted.toString("base64")}`); + const decrypted = sodium.crypto_secretbox_open_easy(encrypted, nonce, decryptKey); + console.log("[UDP] [ call status ]" + decrypted); + } + catch (e) { + console.error(`[UDP] decrypt failure\n${e}\n${encrypted.toString("base64")}`); + } + return; + } + + try { + const decrypted = sodium.crypto_secretbox_open_easy(msg, nonce, decryptKey); + console.log("[UDP] " + decrypted); + } + catch (e) { + console.error(`[UDP] decrypt failure\n${e}\n${msg.toString("base64")}`); + } + }); } async listen(): Promise<void> { // @ts-ignore await initDatabase(); await Config.init(); - await this.createWorkers(); + //await this.createWorkers(); console.log("[DB] connected"); console.log(`[WebRTC] online on 0.0.0.0:${port}`); } @@ -86,17 +126,17 @@ export class Server { transport.on('dtlsstatechange', (dtlsstate) => { console.log(dtlsstate); - }) + }); transport.on("sctpstatechange", (sctpstate) => { - console.log(sctpstate) - }) + console.log(sctpstate); + }); router.observer.on("newrtpobserver", (rtpObserver: MediasoupTypes.RtpObserver) => { console.log("new RTP observer created [id:%s]", rtpObserver.id); // rtpObserver.observer.on("") - }) + }); transport.on("connect", () => { console.log("transport connect"); |