From 9dac7ddf9eb43c4fac263d00b0208e6d337506fd Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Thu, 2 Sep 2021 19:33:04 +0200 Subject: [PATCH 1/3] VOICE_STATE_UPDATE implementation, fix #210 --- gateway/src/events/Close.ts | 6 +- gateway/src/events/Connection.ts | 3 + gateway/src/opcodes/Identify.ts | 26 ++++++- gateway/src/opcodes/VoiceStateUpdate.ts | 67 ++++++++++++++----- ...Update.ts.ts => VoiceStateUpdateSchema.ts} | 4 +- gateway/src/util/SessionUtils.ts | 11 +++ gateway/src/util/WebSocket.ts | 1 + util/src/entities/Config.ts | 2 +- util/src/entities/Session.ts | 33 +++++++++ util/src/entities/VoiceState.ts | 12 ++++ util/src/entities/index.ts | 1 + 11 files changed, 142 insertions(+), 24 deletions(-) rename gateway/src/schema/{VoiceStateUpdate.ts.ts => VoiceStateUpdateSchema.ts} (85%) create mode 100644 gateway/src/util/SessionUtils.ts create mode 100644 util/src/entities/Session.ts diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts index f1a8fa9b..26f4e7df 100644 --- a/gateway/src/events/Close.ts +++ b/gateway/src/events/Close.ts @@ -1,7 +1,9 @@ -import WebSocket from "ws"; +import WebSocket from "../util/WebSocket"; import { Message } from "./Message"; +import {Session} from "@fosscord/util"; -export function Close(this: WebSocket, code: number, reason: string) { +export async function Close(this: WebSocket, code: number, reason: string) { + await Session.delete({session_id: this.session_id}) // @ts-ignore this.off("message", Message); } diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts index fed3c611..1af484eb 100644 --- a/gateway/src/events/Connection.ts +++ b/gateway/src/events/Connection.ts @@ -7,6 +7,7 @@ import { Send } from "../util/Send"; import { CLOSECODES, OPCODES } from "../util/Constants"; import { createDeflate } from "zlib"; import { URL } from "url"; +import {Session} from "@fosscord/util"; var erlpack: any; try { erlpack = require("erlpack"); @@ -56,10 +57,12 @@ export async function Connection(this: Server, socket: WebSocket, request: Incom }); socket.readyTimeout = setTimeout(() => { + Session.delete({session_id: socket.session_id}) //should we await? return socket.close(CLOSECODES.Session_timed_out); }, 1000 * 30); } catch (error) { console.error(error); + Session.delete({session_id: socket.session_id}) //should we await? return socket.close(CLOSECODES.Unknown_error); } } diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index 958f1b73..3984be73 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -8,6 +8,7 @@ import { Member, ReadyEventData, User, + Session, EVENTEnum, Config, dbConnection, @@ -18,8 +19,8 @@ import { Send } from "../util/Send"; // import experiments from "./experiments.json"; const experiments: any = []; import { check } from "./instanceOf"; -import { Like } from "../../../util/node_modules/typeorm"; import { Recipient } from "../../../util/dist/entities/Recipient"; +import { genSessionId } from "../util/SessionUtils"; // TODO: bot sharding // TODO: check priviliged intents @@ -73,6 +74,22 @@ export async function onIdentify(this: WebSocket, data: Payload) { const user = await User.findOneOrFail({ id: this.user_id }); if (!user) return this.close(CLOSECODES.Authentication_failed); + const session_id = genSessionId(); + this.session_id = session_id; //Set the session of the WebSocket object + const session = new Session({ + user_id: this.user_id, + session_id: session_id, + status: "online", //does the session always start as online? + client_info: { //TODO read from identity + client: "desktop", + os: "linux", + version: 0 + } + }); + + //We save the session and we delete it when the websocket is closed + await session.save(); + const public_user = { username: user.username, discriminator: user.discriminator, @@ -135,7 +152,7 @@ export async function onIdentify(this: WebSocket, data: Payload) { version: 642, }, private_channels: channels, - session_id: "", // TODO + session_id: session_id, analytics_token: "", // TODO connected_accounts: [], // TODO consents: { @@ -164,5 +181,10 @@ export async function onIdentify(this: WebSocket, data: Payload) { d, }); + //TODO send READY_SUPPLEMENTAL + //TODO send GUILD_MEMBER_LIST_UPDATE + //TODO send SESSIONS_REPLACE + //TODO send VOICE_STATE_UPDATE to let the client know if another device is already connected to a voice channel + await setupListener.call(this); } diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts index 0d51513d..4719949c 100644 --- a/gateway/src/opcodes/VoiceStateUpdate.ts +++ b/gateway/src/opcodes/VoiceStateUpdate.ts @@ -1,26 +1,59 @@ -import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdate.ts"; -import { CLOSECODES, Payload } from "../util/Constants"; -import { Send } from "../util/Send"; - +import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema"; +import { Payload } from "../util/Constants"; import WebSocket from "../util/WebSocket"; import { check } from "./instanceOf"; -// TODO: implementation +import { Config, emitEvent, VoiceServerUpdateEvent, VoiceState, VoiceStateUpdateEvent } from "@fosscord/util"; +import { genVoiceToken } from "../util/SessionUtils"; // TODO: check if a voice server is setup -// TODO: save voice servers in database and retrieve them // Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not. export async function onVoiceStateUpdate(this: WebSocket, data: Payload) { check.call(this, VoiceStateUpdateSchema, data.d); const body = data.d as VoiceStateUpdateSchema; - await Send(this, { - op: 0, - s: this.sequence++, - t: "VOICE_SERVER_UPDATE", - d: { - token: ``, - guild_id: body.guild_id, - endpoint: `localhost:3004`, - }, - }); -} + let voiceState + try { + voiceState = await VoiceState.findOneOrFail({where:{ user_id: this.user_id },relations: ["member", "member.user", "member.roles"]}); + if(voiceState.session_id !== this.session_id && body.channel_id === null) { //Should we also check guild_id === null? + //changing deaf or mute on a client that's not the one with the same session of the voicestate in the database should be ignored + return + } + + //The event send by Discord's client on channel leave has both guild_id and channel_id as null + if(body.guild_id === null) body.guild_id = voiceState.guild_id; + voiceState.assign(body); + } catch (error) { + voiceState = new VoiceState({ + ...body, + user_id: this.user_id, + deaf: false, + mute: false, + suppress: false + }) + } + + //If the session changed we generate a new token + if(voiceState.session_id !== this.session_id) + voiceState.token = genVoiceToken() + voiceState.session_id = this.session_id + + //TODO the member should only have these properties: hoisted_role, deaf, joined_at, mute, roles, user + //TODO the member.user should only have these properties: avatar, discriminator, id, username + const {id, ...newObj} = voiceState; + + await Promise.all([ + voiceState.save(), + emitEvent({ event: "VOICE_STATE_UPDATE", data: newObj, guild_id: voiceState.guild_id} as VoiceStateUpdateEvent), + ]); + + //If it's null it means that we are leaving the channel and this event is not needed + if(voiceState.channel_id !== null) { + const regions = Config.get().regions; + + await emitEvent({ event: "VOICE_SERVER_UPDATE", data: { + token: voiceState.token, + guild_id: voiceState.guild_id, + endpoint: regions.available[0].endpoint, //TODO return best endpoint or default + }, guild_id: voiceState.guild_id } as VoiceServerUpdateEvent) + } +} \ No newline at end of file diff --git a/gateway/src/schema/VoiceStateUpdate.ts.ts b/gateway/src/schema/VoiceStateUpdateSchema.ts similarity index 85% rename from gateway/src/schema/VoiceStateUpdate.ts.ts rename to gateway/src/schema/VoiceStateUpdateSchema.ts index 4345c2f6..9efa191e 100644 --- a/gateway/src/schema/VoiceStateUpdate.ts.ts +++ b/gateway/src/schema/VoiceStateUpdateSchema.ts @@ -1,6 +1,6 @@ export const VoiceStateUpdateSchema = { $guild_id: String, - channel_id: String, + $channel_id: String, self_mute: Boolean, self_deaf: Boolean, self_video: Boolean, @@ -8,7 +8,7 @@ export const VoiceStateUpdateSchema = { export interface VoiceStateUpdateSchema { guild_id?: string; - channel_id: string; + channel_id?: string; self_mute: boolean; self_deaf: boolean; self_video: boolean; diff --git a/gateway/src/util/SessionUtils.ts b/gateway/src/util/SessionUtils.ts new file mode 100644 index 00000000..1ca23316 --- /dev/null +++ b/gateway/src/util/SessionUtils.ts @@ -0,0 +1,11 @@ +export function genSessionId() { + return genRanHex(32) +} + +export function genVoiceToken() { + return genRanHex(16) +} + +function genRanHex(size: number) { + return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); +} \ No newline at end of file diff --git a/gateway/src/util/WebSocket.ts b/gateway/src/util/WebSocket.ts index d1e13555..2c763743 100644 --- a/gateway/src/util/WebSocket.ts +++ b/gateway/src/util/WebSocket.ts @@ -6,6 +6,7 @@ import { Channel } from "amqplib"; interface WebSocket extends WS { version: number; user_id: string; + session_id: string; encoding: "etf" | "json"; compress?: "zlib-stream"; shard_count?: bigint; diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index 03ff823b..ebc1c61d 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -271,7 +271,7 @@ export const DefaultConfigOptions: ConfigValue = { regions: { default: "fosscord", useDefaultAsOptimal: true, - available: [{ id: "fosscord", name: "Fosscord", endpoint: "127.0.0.1", vip: false, custom: false, deprecated: false }], + available: [{ id: "fosscord", name: "Fosscord", endpoint: "127.0.0.1:3004", vip: false, custom: false, deprecated: false }], }, rabbitmq: { host: null, diff --git a/util/src/entities/Session.ts b/util/src/entities/Session.ts new file mode 100644 index 00000000..fb288522 --- /dev/null +++ b/util/src/entities/Session.ts @@ -0,0 +1,33 @@ +import { User } from "./User"; +import { BaseClass } from "./BaseClass"; +import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; + +//TODO we need to remove all sessions on server start because if the server crashes without closing websockets it won't delete them + + +@Entity("sessions") +export class Session extends BaseClass { + @Column({ nullable: true }) + @RelationId((session: Session) => session.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; + + //TODO check, should be 32 char long hex string + @Column({ nullable: false }) + session_id: string; + + activities: []; //TODO + + @Column({ type: "simple-json", select: false }) + client_info: { + client: string, + os: string, + version: number + } + + @Column({ nullable: false }) + status: string; //TODO enum +} diff --git a/util/src/entities/VoiceState.ts b/util/src/entities/VoiceState.ts index c5040cf1..7975273b 100644 --- a/util/src/entities/VoiceState.ts +++ b/util/src/entities/VoiceState.ts @@ -3,7 +3,9 @@ import { BaseClass } from "./BaseClass"; import { Channel } from "./Channel"; import { Guild } from "./Guild"; import { User } from "./User"; +import {Member} from "./Member"; +//https://gist.github.com/vassjozsef/e482c65df6ee1facaace8b3c9ff66145#file-voice_state-ex @Entity("voice_states") export class VoiceState extends BaseClass { @Column({ nullable: true }) @@ -30,9 +32,16 @@ export class VoiceState extends BaseClass { @ManyToOne(() => User) user: User; + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => Member) + member: Member; + @Column() session_id: string; + @Column({ nullable: true }) + token: string; + @Column() deaf: boolean; @@ -53,4 +62,7 @@ export class VoiceState extends BaseClass { @Column() suppress: boolean; // whether this user is muted by the current user + + @Column({ nullable: true , default: null}) + request_to_speak_timestamp?: Date } diff --git a/util/src/entities/index.ts b/util/src/entities/index.ts index aa37ae2e..7b1c9750 100644 --- a/util/src/entities/index.ts +++ b/util/src/entities/index.ts @@ -16,6 +16,7 @@ export * from "./ReadState"; export * from "./Recipient"; export * from "./Relationship"; export * from "./Role"; +export * from "./Session"; export * from "./Sticker"; export * from "./Team"; export * from "./TeamMember"; From 2ba4c21f6b62c0ee8c2db06d36d489c6b6b20155 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 2 Sep 2021 20:10:25 +0200 Subject: [PATCH 2/3] :art: reformat files --- gateway/src/events/Close.ts | 4 +-- gateway/src/events/Connection.ts | 6 ++-- gateway/src/opcodes/Identify.ts | 7 ++-- gateway/src/opcodes/VoiceStateUpdate.ts | 43 ++++++++++++++++--------- gateway/src/util/SessionUtils.ts | 8 ++--- util/src/entities/Config.ts | 11 ++++++- util/src/entities/Session.ts | 37 +++++++++++---------- util/src/entities/VoiceState.ts | 6 ++-- 8 files changed, 71 insertions(+), 51 deletions(-) diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts index 26f4e7df..d68fc751 100644 --- a/gateway/src/events/Close.ts +++ b/gateway/src/events/Close.ts @@ -1,9 +1,9 @@ import WebSocket from "../util/WebSocket"; import { Message } from "./Message"; -import {Session} from "@fosscord/util"; +import { Session } from "@fosscord/util"; export async function Close(this: WebSocket, code: number, reason: string) { - await Session.delete({session_id: this.session_id}) + await Session.delete({ session_id: this.session_id }); // @ts-ignore this.off("message", Message); } diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts index 1af484eb..b3c94eaf 100644 --- a/gateway/src/events/Connection.ts +++ b/gateway/src/events/Connection.ts @@ -7,7 +7,7 @@ import { Send } from "../util/Send"; import { CLOSECODES, OPCODES } from "../util/Constants"; import { createDeflate } from "zlib"; import { URL } from "url"; -import {Session} from "@fosscord/util"; +import { Session } from "@fosscord/util"; var erlpack: any; try { erlpack = require("erlpack"); @@ -57,12 +57,12 @@ export async function Connection(this: Server, socket: WebSocket, request: Incom }); socket.readyTimeout = setTimeout(() => { - Session.delete({session_id: socket.session_id}) //should we await? + Session.delete({ session_id: socket.session_id }); //should we await? return socket.close(CLOSECODES.Session_timed_out); }, 1000 * 30); } catch (error) { console.error(error); - Session.delete({session_id: socket.session_id}) //should we await? + Session.delete({ session_id: socket.session_id }); //should we await? return socket.close(CLOSECODES.Unknown_error); } } diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts index 3984be73..f1d96959 100644 --- a/gateway/src/opcodes/Identify.ts +++ b/gateway/src/opcodes/Identify.ts @@ -80,11 +80,12 @@ export async function onIdentify(this: WebSocket, data: Payload) { user_id: this.user_id, session_id: session_id, status: "online", //does the session always start as online? - client_info: { //TODO read from identity + client_info: { + //TODO read from identity client: "desktop", os: "linux", - version: 0 - } + version: 0, + }, }); //We save the session and we delete it when the websocket is closed diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts index 4719949c..04392b62 100644 --- a/gateway/src/opcodes/VoiceStateUpdate.ts +++ b/gateway/src/opcodes/VoiceStateUpdate.ts @@ -11,16 +11,20 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) { check.call(this, VoiceStateUpdateSchema, data.d); const body = data.d as VoiceStateUpdateSchema; - let voiceState + let voiceState; try { - voiceState = await VoiceState.findOneOrFail({where:{ user_id: this.user_id },relations: ["member", "member.user", "member.roles"]}); - if(voiceState.session_id !== this.session_id && body.channel_id === null) { //Should we also check guild_id === null? + voiceState = await VoiceState.findOneOrFail({ + where: { user_id: this.user_id }, + relations: ["member", "member.user", "member.roles"], + }); + if (voiceState.session_id !== this.session_id && body.channel_id === null) { + //Should we also check guild_id === null? //changing deaf or mute on a client that's not the one with the same session of the voicestate in the database should be ignored - return + return; } //The event send by Discord's client on channel leave has both guild_id and channel_id as null - if(body.guild_id === null) body.guild_id = voiceState.guild_id; + if (body.guild_id === null) body.guild_id = voiceState.guild_id; voiceState.assign(body); } catch (error) { voiceState = new VoiceState({ @@ -28,32 +32,39 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) { user_id: this.user_id, deaf: false, mute: false, - suppress: false - }) + suppress: false, + }); } //If the session changed we generate a new token - if(voiceState.session_id !== this.session_id) - voiceState.token = genVoiceToken() - voiceState.session_id = this.session_id + if (voiceState.session_id !== this.session_id) voiceState.token = genVoiceToken(); + voiceState.session_id = this.session_id; //TODO the member should only have these properties: hoisted_role, deaf, joined_at, mute, roles, user //TODO the member.user should only have these properties: avatar, discriminator, id, username - const {id, ...newObj} = voiceState; + const { id, ...newObj } = voiceState; await Promise.all([ voiceState.save(), - emitEvent({ event: "VOICE_STATE_UPDATE", data: newObj, guild_id: voiceState.guild_id} as VoiceStateUpdateEvent), + emitEvent({ + event: "VOICE_STATE_UPDATE", + data: newObj, + guild_id: voiceState.guild_id, + } as VoiceStateUpdateEvent), ]); //If it's null it means that we are leaving the channel and this event is not needed - if(voiceState.channel_id !== null) { + if (voiceState.channel_id !== null) { const regions = Config.get().regions; - await emitEvent({ event: "VOICE_SERVER_UPDATE", data: { + await emitEvent({ + event: "VOICE_SERVER_UPDATE", + data: { token: voiceState.token, guild_id: voiceState.guild_id, endpoint: regions.available[0].endpoint, //TODO return best endpoint or default - }, guild_id: voiceState.guild_id } as VoiceServerUpdateEvent) + }, + guild_id: voiceState.guild_id, + } as VoiceServerUpdateEvent); } -} \ No newline at end of file +} diff --git a/gateway/src/util/SessionUtils.ts b/gateway/src/util/SessionUtils.ts index 1ca23316..c66c7e76 100644 --- a/gateway/src/util/SessionUtils.ts +++ b/gateway/src/util/SessionUtils.ts @@ -1,11 +1,11 @@ export function genSessionId() { - return genRanHex(32) + return genRanHex(32); } export function genVoiceToken() { - return genRanHex(16) + return genRanHex(16); } function genRanHex(size: number) { - return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); -} \ No newline at end of file + return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(""); +} diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index ebc1c61d..fd830db8 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -271,7 +271,16 @@ export const DefaultConfigOptions: ConfigValue = { regions: { default: "fosscord", useDefaultAsOptimal: true, - available: [{ id: "fosscord", name: "Fosscord", endpoint: "127.0.0.1:3004", vip: false, custom: false, deprecated: false }], + available: [ + { + id: "fosscord", + name: "Fosscord", + endpoint: "127.0.0.1:3004", + vip: false, + custom: false, + deprecated: false, + }, + ], }, rabbitmq: { host: null, diff --git a/util/src/entities/Session.ts b/util/src/entities/Session.ts index fb288522..d42a8f98 100644 --- a/util/src/entities/Session.ts +++ b/util/src/entities/Session.ts @@ -4,30 +4,29 @@ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; //TODO we need to remove all sessions on server start because if the server crashes without closing websockets it won't delete them - @Entity("sessions") export class Session extends BaseClass { - @Column({ nullable: true }) - @RelationId((session: Session) => session.user) - user_id: string; + @Column({ nullable: true }) + @RelationId((session: Session) => session.user) + user_id: string; - @JoinColumn({ name: "user_id" }) - @ManyToOne(() => User) - user: User; + @JoinColumn({ name: "user_id" }) + @ManyToOne(() => User) + user: User; - //TODO check, should be 32 char long hex string - @Column({ nullable: false }) - session_id: string; + //TODO check, should be 32 char long hex string + @Column({ nullable: false }) + session_id: string; - activities: []; //TODO + activities: []; //TODO - @Column({ type: "simple-json", select: false }) - client_info: { - client: string, - os: string, - version: number - } + @Column({ type: "simple-json", select: false }) + client_info: { + client: string; + os: string; + version: number; + }; - @Column({ nullable: false }) - status: string; //TODO enum + @Column({ nullable: false }) + status: string; //TODO enum } diff --git a/util/src/entities/VoiceState.ts b/util/src/entities/VoiceState.ts index 7975273b..d7a032c7 100644 --- a/util/src/entities/VoiceState.ts +++ b/util/src/entities/VoiceState.ts @@ -3,7 +3,7 @@ import { BaseClass } from "./BaseClass"; import { Channel } from "./Channel"; import { Guild } from "./Guild"; import { User } from "./User"; -import {Member} from "./Member"; +import { Member } from "./Member"; //https://gist.github.com/vassjozsef/e482c65df6ee1facaace8b3c9ff66145#file-voice_state-ex @Entity("voice_states") @@ -63,6 +63,6 @@ export class VoiceState extends BaseClass { @Column() suppress: boolean; // whether this user is muted by the current user - @Column({ nullable: true , default: null}) - request_to_speak_timestamp?: Date + @Column({ nullable: true, default: null }) + request_to_speak_timestamp?: Date; } From b90592f58c01b550c44b9db510e4f5c3d9fb7fda Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 2 Sep 2021 20:18:57 +0200 Subject: [PATCH 3/3] :sparkles: add editor config --- .editorconfig | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..ca46e26f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*.ts] +indent_style = tab +indent_size = 4