summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-06-05 22:02:21 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-06-05 22:02:21 +1000
commit77b31d79a38447a4a68e5afa8c28345b38d73fef (patch)
tree3f34ad8db5a301e56733407ddf76af1ed5cc51e1
parentMerge branch 'master' into maddyrtc (diff)
downloadserver-77b31d79a38447a4a68e5afa8c28345b38d73fef.tar.xz
More random bullshit
-rw-r--r--webrtc/src/Server.ts61
-rw-r--r--webrtc/src/opcodes/Identify.ts16
-rw-r--r--webrtc/src/opcodes/SelectProtocol.ts231
3 files changed, 177 insertions, 131 deletions
diff --git a/webrtc/src/Server.ts b/webrtc/src/Server.ts
index a43768ac..7a1070b9 100644
--- a/webrtc/src/Server.ts
+++ b/webrtc/src/Server.ts
@@ -7,6 +7,7 @@ import * as mediasoup from "mediasoup";
 import { types as MediasoupTypes } from "mediasoup";
 import udp from "dgram";
 import sodium from "libsodium-wrappers";
+import { assert } from "console";
 
 var port = Number(process.env.PORT);
 if (isNaN(port)) port = 3004;
@@ -19,7 +20,7 @@ export class Server {
 	public mediasoupProducers: MediasoupTypes.Producer[] = [];
 	public mediasoupConsumers: MediasoupTypes.Consumer[] = [];
 
-	public decryptKey: number[] = [];
+	public decryptKey: Uint8Array;
 	public testUdp = udp.createSocket("udp6");
 
 	constructor() {
@@ -46,9 +47,20 @@ export class Server {
 					socket.close(CLOSECODES.Unknown_opcode);
 				}
 			});
+
+			socket.on("close", (code: number, reason: string) => {
+				console.log(`client closed ${code} ${reason}`);
+				for (var consumer of this.mediasoupConsumers)  consumer.close();
+				for (var producer of this.mediasoupProducers) producer.close();
+				for (var transport of this.mediasoupTransports) transport.close();
+
+				this.mediasoupConsumers = [];
+				this.mediasoupProducers = [];
+				this.mediasoupTransports = [];
+			})
 		});
 
-		this.testUdp.bind(50001);
+		this.testUdp.bind(60000);
 		this.testUdp.on("message", (msg, rinfo) => {
 			//random key from like, the libsodium examples on npm lol
 
@@ -70,23 +82,28 @@ export class Server {
 
 			console.log(`[UDP] message: ${sodium.to_hex(msg)}`);
 
-			let encrypted;
-			if (msg.slice(0, 2).indexOf("\x81\xc9") == 0) {
-				encrypted = msg.slice(0x18, -4);
-			}
-			else if (msg.slice(0, 2).indexOf("\x90\x78") == 0) {
-				encrypted = msg.slice(0x1C, -4);
-			}
-			else {
-				encrypted = msg.slice(0x18, -4);
-				console.log(`wtf header received: ${encrypted.toString("hex")}`);
-			}
+			// let encrypted;
+			// if (Buffer.from(msg).indexOf("\x81\xc9") == 0) {
+			// 	encrypted = msg.slice(0x18, -4);
+			// }
+			// else if (Buffer.from(msg).indexOf("\x90\x78") == 0) {
+			// 	encrypted = msg.slice(0x1C, -4);
+			// }
+			// else {
+			// 	encrypted = msg.slice(0x18, -4);
+			// 	console.log(`wtf header received: ${encrypted.toString("hex")}`);
+			// }
+
+			let encrypted = msg;
 
 			if (sodium.to_hex(msg).indexOf("80c8000600000001") == 0) {
 				//call status
 
+				encrypted = encrypted.slice(8, -4);
+				assert(encrypted.length == 40);
+
 				try {
-					const decrypted = sodium.crypto_secretbox_open_easy(encrypted, nonce, Uint8Array.from(this.decryptKey));
+					const decrypted = sodium.crypto_secretbox_open_easy(encrypted, nonce, Buffer.from(this.decryptKey));
 					console.log("[UDP] [ call status ]" + decrypted);
 				}
 				catch (e) {
@@ -95,13 +112,13 @@ export class Server {
 				return;
 			}
 
-			try {
-				const decrypted = sodium.crypto_secretbox_open_easy(encrypted, nonce, Uint8Array.from(this.decryptKey));
-				console.log("[UDP] " + decrypted);
-			}
-			catch (e) {
-				console.error(`[UDP] decrypt failure\n${e}\n${msg.toString("base64")}`);
-			}
+			// try {
+			// 	const decrypted = sodium.crypto_secretbox_open_easy(encrypted, nonce, Buffer.from(this.decryptKey.map(x => String.fromCharCode(x)).join("")));
+			// 	console.log("[UDP] " + decrypted);
+			// }
+			// catch (e) {
+			// 	console.error(`[UDP] decrypt failure\n${e}\n${msg.toString("base64")}`);
+			// }
 		});
 	}
 
@@ -109,7 +126,7 @@ export class Server {
 		// @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}`);
 	}
diff --git a/webrtc/src/opcodes/Identify.ts b/webrtc/src/opcodes/Identify.ts
index 68452d4f..ef0386a7 100644
--- a/webrtc/src/opcodes/Identify.ts
+++ b/webrtc/src/opcodes/Identify.ts
@@ -33,18 +33,18 @@ export async function onIdentify(this: Server, socket: WebSocket, data: Identify
 	if (!guild.members.find(x => x.id === user.id))
 		return socket.close(CLOSECODES.Invalid_intent);
 
-	// var transport = this.mediasoupTransports[0] || await this.mediasoupRouters[0].createWebRtcTransport({
-	// 	listenIps: [{ ip: "10.22.64.56" }],
-	// 	enableUdp: true,
-	// });
-7
+	var transport = this.mediasoupTransports[0] || await this.mediasoupRouters[0].createWebRtcTransport({
+		listenIps: [{ ip: "10.22.64.146" }],
+		enableUdp: true,
+	});
+
 	socket.send(JSON.stringify({
 		op: VoiceOPCodes.READY,
 		d: {
-			streams: data.d.streams ? [...data.d.streams.map(x => ({ ...x, rtx_ssrc: Math.floor(Math.random() * 10000), ssrc: Math.floor(Math.random() * 10000), active: false, }))] : undefined,
+			streams: data.d.streams ? [...data.d.streams.map(x => ({ ...x, rtx_ssrc: Math.floor(Math.random() * 10000), ssrc: Math.floor(Math.random() * 10000), active: true, }))] : undefined,
 			ssrc: Math.floor(Math.random() * 10000),
-			ip: "127.0.0.1",//transport.iceCandidates[0].ip,
-			port: 50001,//transport.iceCandidates[0].port,
+			ip: transport.iceCandidates[0].ip,
+			port: transport.iceCandidates[0].port,
 			modes: [
 				"aead_aes256_gcm_rtpsize",
 				"aead_aes256_gcm",
diff --git a/webrtc/src/opcodes/SelectProtocol.ts b/webrtc/src/opcodes/SelectProtocol.ts
index 75ab4495..72fb9c79 100644
--- a/webrtc/src/opcodes/SelectProtocol.ts
+++ b/webrtc/src/opcodes/SelectProtocol.ts
@@ -7,6 +7,24 @@ import { RtpCodecCapability } from "mediasoup/node/lib/RtpParameters";
 import * as sdpTransform from 'sdp-transform';
 import sodium from "libsodium-wrappers";
 
+export interface CodecPayload {
+	name: string,
+	type: "audio" | "video",
+	priority: number,
+	payload_type: number,
+	rtx_payload_type: number | null,
+}
+
+export interface SelectProtocolPayload extends Payload {
+	d: {
+		codecs: Array<CodecPayload>,
+		data: string,	// SDP if webrtc
+		protocol: string,
+		rtc_connection_id: string,
+		sdp?: string,	// same as data
+	};
+}
+
 /*
 
 	Sent by client:
@@ -70,108 +88,119 @@ import sodium from "libsodium-wrappers";
 }
 */
 
-export async function onSelectProtocol(this: Server, socket: WebSocket, data: Payload) {
-	// const rtpCapabilities = this.mediasoupRouters[0].rtpCapabilities;
-	// const codecs = rtpCapabilities.codecs as RtpCodecCapability[];
-
+export async function onSelectProtocol(this: Server, socket: WebSocket, data: SelectProtocolPayload) {
 	if (data.d.sdp) {
-		// const transport = this.mediasoupTransports[0];	//whatever
-
-		// const res = sdpTransform.parse(data.d.sdp);
-
-		// const videoCodec = this.mediasoupRouters[0].rtpCapabilities.codecs!.find((x: any) => x.kind === "video");
-		// const audioCodec = this.mediasoupRouters[0].rtpCapabilities.codecs!.find((x: any) => x.kind === "audio");
-
-		// const producer = this.mediasoupProducers[0] || await transport.produce({
-		// 	kind: "audio",
-		// 	rtpParameters: {
-		// 		mid: "audio",
-		// 		codecs: [{
-		// 			clockRate: audioCodec!.clockRate,
-		// 			payloadType: audioCodec!.preferredPayloadType as number,
-		// 			mimeType: audioCodec!.mimeType,
-		// 			channels: audioCodec?.channels,
-		// 		}],
-		// 		headerExtensions: res.ext?.map(x => ({
-		// 			id: x.value,
-		// 			uri: x.uri,
-		// 		})),
-		// 	},
-		// 	paused: false,
-		// });
-
-		// console.log("can consume: " + this.mediasoupRouters[0].canConsume({ producerId: producer.id, rtpCapabilities: rtpCapabilities }));
-
-		// // const consumer = this.mediasoupConsumers[0] || await transport.consume({
-		// // 	producerId: producer.id,
-		// // 	paused: false,
-		// // 	rtpCapabilities,
-		// // });
-
-		// socket.send(JSON.stringify({
-		// 	op: VoiceOPCodes.SESSION_DESCRIPTION,
-		// 	d: {
-		// 		video_codec: videoCodec?.mimeType?.substring(6) || undefined,
-		// 		// mode: "xsalsa20_poly1305_lite",
-		// 		media_session_id: transport.id,
-		// 		audio_codec: audioCodec?.mimeType.substring(6),
-		// 		secret_key: sodium.from_hex("724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed").buffer,
-		// 		sdp: `m=audio ${50001} ICE/SDP\n`
-		// 			+ `a=fingerprint:sha-256 ${transport.dtlsParameters.fingerprints.find(x => x.algorithm === "sha-256")?.value}\n`
-		// 			+ `c=IN IP4 ${transport.iceCandidates[0].ip}\n`
-		// 			+ `t=0 0\n`
-		// 			+ `a=ice-lite\n`
-		// 			+ `a=rtcp-mux\n`
-		// 			+ `a=rtcp:${50001}\n`
-		// 			+ `a=ice-ufrag:${transport.iceParameters.usernameFragment}\n`
-		// 			+ `a=ice-pwd:${transport.iceParameters.password}\n`
-		// 			+ `a=fingerprint:sha-256 ${transport.dtlsParameters.fingerprints.find(x => x.algorithm === "sha-256")?.value}\n`
-		// 			+ `a=candidate:1 1 ${transport.iceCandidates[0].protocol.toUpperCase()} ${transport.iceCandidates[0].priority} ${transport.iceCandidates[0].ip} ${50001} typ ${transport.iceCandidates[0].type}`
-		// 	}
-		// }));
+		const rtpCapabilities = this.mediasoupRouters[0].rtpCapabilities;
+		const codecs = rtpCapabilities.codecs as RtpCodecCapability[];
+
+		const transport = this.mediasoupTransports[0];	//whatever
+
+		const res = sdpTransform.parse(data.d.sdp);
+
+		const videoCodec = this.mediasoupRouters[0].rtpCapabilities.codecs!.find((x: any) => x.kind === "video");
+		const audioCodec = this.mediasoupRouters[0].rtpCapabilities.codecs!.find((x: any) => x.kind === "audio");
+
+		const producer = this.mediasoupProducers[0] || await transport.produce({
+			kind: "audio",
+			rtpParameters: {
+				mid: "audio",
+				codecs: [{
+					clockRate: audioCodec!.clockRate,
+					payloadType: audioCodec!.preferredPayloadType as number,
+					mimeType: audioCodec!.mimeType,
+					channels: audioCodec?.channels,
+				}],
+				headerExtensions: res.ext?.map(x => ({
+					id: x.value,
+					uri: x.uri,
+				})),
+			},
+			paused: false,
+		});
+
+		console.log("can consume: " + this.mediasoupRouters[0].canConsume({ producerId: producer.id, rtpCapabilities: rtpCapabilities }));
+
+		const consumer = this.mediasoupConsumers[0] || await transport.consume({
+			producerId: producer.id,
+			paused: false,
+			rtpCapabilities,
+		});
+
+		transport.connect({
+			dtlsParameters: {
+				fingerprints: transport.dtlsParameters.fingerprints,
+				role: "server",
+			}
+		});
+
+		socket.send(JSON.stringify({
+			op: VoiceOPCodes.SESSION_DESCRIPTION,
+			d: {
+				video_codec: videoCodec?.mimeType?.substring(6) || undefined,
+				// mode: "xsalsa20_poly1305_lite",
+				media_session_id: transport.id,
+				audio_codec: audioCodec?.mimeType.substring(6),
+				secret_key: sodium.from_hex("724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed").buffer,
+				sdp: `m=audio ${50001} ICE/SDP\n`
+					+ `a=fingerprint:sha-256 ${transport.dtlsParameters.fingerprints.find(x => x.algorithm === "sha-256")?.value}\n`
+					+ `c=IN IP4 ${transport.iceCandidates[0].ip}\n`
+					+ `t=0 0\n`
+					+ `a=ice-lite\n`
+					+ `a=rtcp-mux\n`
+					+ `a=rtcp:${50001}\n`
+					+ `a=ice-ufrag:${transport.iceParameters.usernameFragment}\n`
+					+ `a=ice-pwd:${transport.iceParameters.password}\n`
+					+ `a=fingerprint:sha-256 ${transport.dtlsParameters.fingerprints.find(x => x.algorithm === "sha-256")?.value}\n`
+					+ `a=candidate:1 1 ${transport.iceCandidates[0].protocol.toUpperCase()} ${transport.iceCandidates[0].priority} ${transport.iceCandidates[0].ip} ${50001} typ ${transport.iceCandidates[0].type}`
+			}
+		}));
 		return;
 	}
-	/*
-		{
-			"video_codec":"H264",
-			"sdp":
-			"
-				m=audio 50010 ICE/SDP
-				a=fingerprint:sha-256 4A:79:94:16:44:3F:BD:05:41:5A:C7:20:F3:12:54:70:00:73:5D:33:00:2D:2C:80:9B:39:E1:9F:2D:A7:49:87
-				c=IN IP4 109.200.214.158
-				a=rtcp:50010
-				a=ice-ufrag:+npq
-				a=ice-pwd:+jf7jAesMeHHby43FRqWTy
-				a=fingerprint:sha-256 4A:79:94:16:44:3F:BD:05:41:5A:C7:20:F3:12:54:70:00:73:5D:33:00:2D:2C:80:9B:39:E1:9F:2D:A7:49:87
-				a=candidate:1 1 UDP 4261412862 109.200.214.158 50010 typ host",
-				"media_session_id":"59265c94fa13e313492c372c4c8da801
-			",
-			"audio_codec":"opus"
-		}
-	*/
-
-
-	/*
-		{
-			"video_codec": "H264",
-			"secret_key": [36, 80, 96, 53, 95, 149, 253, 16, 137, 186, 238, 222, 251, 180, 94, 150, 112, 137, 192, 109, 69, 79, 218, 111, 217, 197, 56, 74, 18, 41, 51, 140],
-			"mode": "aead_aes256_gcm_rtpsize",
-			"media_session_id": "797575a97a87b63e81e2399348b97ad1",
-			"audio_codec": "opus"
-		};
-	*/
-
-	this.decryptKey = [...sodium.randombytes_buf(sodium.crypto_secretbox_KEYBYTES)];
-	console.log(this.decryptKey.map(x => String.fromCharCode(x)).join(""));
-
-	socket.send(JSON.stringify({
-		op:VoiceOPCodes.SESSION_DESCRIPTION,
-		d: {
-			video_codec: "H264",
-			secret_key: this.decryptKey,
-			mode: "aead_aes256_gcm_rtpsize",
-			media_session_id: "blah blah blah",
-			audio_codec: "opus",
-		}
-	}));
+	else {
+		/*
+			{
+				"video_codec":"H264",
+				"sdp":
+				"
+					m=audio 50010 ICE/SDP
+					a=fingerprint:sha-256 4A:79:94:16:44:3F:BD:05:41:5A:C7:20:F3:12:54:70:00:73:5D:33:00:2D:2C:80:9B:39:E1:9F:2D:A7:49:87
+					c=IN IP4 109.200.214.158
+					a=rtcp:50010
+					a=ice-ufrag:+npq
+					a=ice-pwd:+jf7jAesMeHHby43FRqWTy
+					a=fingerprint:sha-256 4A:79:94:16:44:3F:BD:05:41:5A:C7:20:F3:12:54:70:00:73:5D:33:00:2D:2C:80:9B:39:E1:9F:2D:A7:49:87
+					a=candidate:1 1 UDP 4261412862 109.200.214.158 50010 typ host",
+					"media_session_id":"59265c94fa13e313492c372c4c8da801
+				",
+				"audio_codec":"opus"
+			}
+		*/
+
+
+		/*
+			{
+				"video_codec": "H264",
+				"secret_key": [36, 80, 96, 53, 95, 149, 253, 16, 137, 186, 238, 222, 251, 180, 94, 150, 112, 137, 192, 109, 69, 79, 218, 111, 217, 197, 56, 74, 18, 41, 51, 140],
+				"mode": "aead_aes256_gcm_rtpsize",
+				"media_session_id": "797575a97a87b63e81e2399348b97ad1",
+				"audio_codec": "opus"
+			};
+		*/
+
+		this.decryptKey = sodium.randombytes_buf(sodium.crypto_secretbox_KEYBYTES);
+
+		// this.decryptKey = new Array(sodium.crypto_secretbox_KEYBYTES).fill(null).map((x, i) => i + 1);
+		console.log(this.decryptKey);
+
+		socket.send(JSON.stringify({
+			op: VoiceOPCodes.SESSION_DESCRIPTION,
+			d: {
+				video_codec: "H264",
+				secret_key: [...this.decryptKey.values()],
+				mode: "aead_aes256_gcm_rtpsize",
+				media_session_id: "blah blah blah",
+				audio_codec: "opus",
+			}
+		}));
+	}
 }
\ No newline at end of file