summary refs log tree commit diff
path: root/src/webrtc/opcodes/Identify.ts
blob: 45ad6c0a6de97efa226dc3f03bf852e24ca5acad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { CLOSECODES, Payload, Send, WebSocket } from "@fosscord/gateway";
import {
	validateSchema,
	VoiceIdentifySchema,
	VoiceState,
} from "@fosscord/util";
import { endpoint, getClients, VoiceOPCodes, PublicIP } from "@fosscord/webrtc";
import SemanticSDP from "semantic-sdp";
const defaultSDP = require("./sdp.json");

export async function onIdentify(this: WebSocket, data: Payload) {
	clearTimeout(this.readyTimeout);
	const { server_id, user_id, session_id, token, streams, video } =
		validateSchema("VoiceIdentifySchema", data.d) as VoiceIdentifySchema;

	const voiceState = await VoiceState.findOne({
		where: { guild_id: server_id, user_id, token, session_id },
	});
	if (!voiceState) return this.close(CLOSECODES.Authentication_failed);

	this.user_id = user_id;
	this.session_id = session_id;
	const sdp = SemanticSDP.SDPInfo.expand(defaultSDP);
	sdp.setDTLS(
		SemanticSDP.DTLSInfo.expand({
			setup: "actpass",
			hash: "sha-256",
			fingerprint: endpoint.getDTLSFingerprint(),
		}),
	);

	this.client = {
		websocket: this,
		out: {
			tracks: new Map(),
		},
		in: {
			audio_ssrc: 0,
			video_ssrc: 0,
			rtx_ssrc: 0,
		},
		sdp,
		channel_id: voiceState.channel_id,
	};

	const clients = getClients(voiceState.channel_id)!;
	clients.add(this.client);

	this.on("close", () => {
		clients.delete(this.client!);
	});

	await Send(this, {
		op: VoiceOPCodes.READY,
		d: {
			streams: [
				// { type: "video", ssrc: this.ssrc + 1, rtx_ssrc: this.ssrc + 2, rid: "100", quality: 100, active: false }
			],
			ssrc: -1,
			port: endpoint.getLocalPort(),
			modes: [
				"aead_aes256_gcm_rtpsize",
				"aead_aes256_gcm",
				"xsalsa20_poly1305_lite_rtpsize",
				"xsalsa20_poly1305_lite",
				"xsalsa20_poly1305_suffix",
				"xsalsa20_poly1305",
			],
			ip: PublicIP,
			experiments: [],
		},
	});
}