1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

Merge pull request #306 from AlTech98/master

Gateway VOICE_STATE_UPDATE and sessions implementation, fix #210
This commit is contained in:
Flam3rboy 2021-09-02 20:20:15 +02:00 committed by GitHub
commit 6cd7d7230e
12 changed files with 166 additions and 23 deletions

5
.editorconfig Normal file
View File

@ -0,0 +1,5 @@
root = true
[*.ts]
indent_style = tab
indent_size = 4

View File

@ -1,7 +1,9 @@
import WebSocket from "ws"; import WebSocket from "../util/WebSocket";
import { Message } from "./Message"; 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 // @ts-ignore
this.off("message", Message); this.off("message", Message);
} }

View File

@ -7,6 +7,7 @@ import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants"; import { CLOSECODES, OPCODES } from "../util/Constants";
import { createDeflate } from "zlib"; import { createDeflate } from "zlib";
import { URL } from "url"; import { URL } from "url";
import { Session } from "@fosscord/util";
var erlpack: any; var erlpack: any;
try { try {
erlpack = require("erlpack"); erlpack = require("erlpack");
@ -56,10 +57,12 @@ export async function Connection(this: Server, socket: WebSocket, request: Incom
}); });
socket.readyTimeout = setTimeout(() => { socket.readyTimeout = setTimeout(() => {
Session.delete({ session_id: socket.session_id }); //should we await?
return socket.close(CLOSECODES.Session_timed_out); return socket.close(CLOSECODES.Session_timed_out);
}, 1000 * 30); }, 1000 * 30);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
Session.delete({ session_id: socket.session_id }); //should we await?
return socket.close(CLOSECODES.Unknown_error); return socket.close(CLOSECODES.Unknown_error);
} }
} }

View File

@ -8,6 +8,7 @@ import {
Member, Member,
ReadyEventData, ReadyEventData,
User, User,
Session,
EVENTEnum, EVENTEnum,
Config, Config,
dbConnection, dbConnection,
@ -18,8 +19,8 @@ import { Send } from "../util/Send";
// import experiments from "./experiments.json"; // import experiments from "./experiments.json";
const experiments: any = []; const experiments: any = [];
import { check } from "./instanceOf"; import { check } from "./instanceOf";
import { Like } from "../../../util/node_modules/typeorm";
import { Recipient } from "../../../util/dist/entities/Recipient"; import { Recipient } from "../../../util/dist/entities/Recipient";
import { genSessionId } from "../util/SessionUtils";
// TODO: bot sharding // TODO: bot sharding
// TODO: check priviliged intents // TODO: check priviliged intents
@ -73,6 +74,23 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const user = await User.findOneOrFail({ id: this.user_id }); const user = await User.findOneOrFail({ id: this.user_id });
if (!user) return this.close(CLOSECODES.Authentication_failed); 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 = { const public_user = {
username: user.username, username: user.username,
discriminator: user.discriminator, discriminator: user.discriminator,
@ -135,7 +153,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
version: 642, version: 642,
}, },
private_channels: channels, private_channels: channels,
session_id: "", // TODO session_id: session_id,
analytics_token: "", // TODO analytics_token: "", // TODO
connected_accounts: [], // TODO connected_accounts: [], // TODO
consents: { consents: {
@ -164,5 +182,10 @@ export async function onIdentify(this: WebSocket, data: Payload) {
d, 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); await setupListener.call(this);
} }

View File

@ -1,26 +1,70 @@
import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdate.ts"; import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
import { CLOSECODES, Payload } from "../util/Constants"; import { Payload } from "../util/Constants";
import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket"; import WebSocket from "../util/WebSocket";
import { check } from "./instanceOf"; 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: 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. // 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) { export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d); check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema; const body = data.d as VoiceStateUpdateSchema;
await Send(this, { let voiceState;
op: 0, try {
s: this.sequence++, voiceState = await VoiceState.findOneOrFail({
t: "VOICE_SERVER_UPDATE", where: { user_id: this.user_id },
d: { relations: ["member", "member.user", "member.roles"],
token: ``, });
guild_id: body.guild_id, if (voiceState.session_id !== this.session_id && body.channel_id === null) {
endpoint: `localhost:3004`, //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);
}
} }

View File

@ -1,6 +1,6 @@
export const VoiceStateUpdateSchema = { export const VoiceStateUpdateSchema = {
$guild_id: String, $guild_id: String,
channel_id: String, $channel_id: String,
self_mute: Boolean, self_mute: Boolean,
self_deaf: Boolean, self_deaf: Boolean,
self_video: Boolean, self_video: Boolean,
@ -8,7 +8,7 @@ export const VoiceStateUpdateSchema = {
export interface VoiceStateUpdateSchema { export interface VoiceStateUpdateSchema {
guild_id?: string; guild_id?: string;
channel_id: string; channel_id?: string;
self_mute: boolean; self_mute: boolean;
self_deaf: boolean; self_deaf: boolean;
self_video: boolean; self_video: boolean;

View File

@ -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("");
}

View File

@ -6,6 +6,7 @@ import { Channel } from "amqplib";
interface WebSocket extends WS { interface WebSocket extends WS {
version: number; version: number;
user_id: string; user_id: string;
session_id: string;
encoding: "etf" | "json"; encoding: "etf" | "json";
compress?: "zlib-stream"; compress?: "zlib-stream";
shard_count?: bigint; shard_count?: bigint;

View File

@ -271,7 +271,16 @@ export const DefaultConfigOptions: ConfigValue = {
regions: { regions: {
default: "fosscord", default: "fosscord",
useDefaultAsOptimal: true, 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: { rabbitmq: {
host: null, host: null,

View File

@ -0,0 +1,32 @@
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
}

View File

@ -3,7 +3,9 @@ import { BaseClass } from "./BaseClass";
import { Channel } from "./Channel"; import { Channel } from "./Channel";
import { Guild } from "./Guild"; import { Guild } from "./Guild";
import { User } from "./User"; import { User } from "./User";
import { Member } from "./Member";
//https://gist.github.com/vassjozsef/e482c65df6ee1facaace8b3c9ff66145#file-voice_state-ex
@Entity("voice_states") @Entity("voice_states")
export class VoiceState extends BaseClass { export class VoiceState extends BaseClass {
@Column({ nullable: true }) @Column({ nullable: true })
@ -30,9 +32,16 @@ export class VoiceState extends BaseClass {
@ManyToOne(() => User) @ManyToOne(() => User)
user: User; user: User;
@JoinColumn({ name: "user_id" })
@ManyToOne(() => Member)
member: Member;
@Column() @Column()
session_id: string; session_id: string;
@Column({ nullable: true })
token: string;
@Column() @Column()
deaf: boolean; deaf: boolean;
@ -53,4 +62,7 @@ export class VoiceState extends BaseClass {
@Column() @Column()
suppress: boolean; // whether this user is muted by the current user suppress: boolean; // whether this user is muted by the current user
@Column({ nullable: true, default: null })
request_to_speak_timestamp?: Date;
} }

View File

@ -16,6 +16,7 @@ export * from "./ReadState";
export * from "./Recipient"; export * from "./Recipient";
export * from "./Relationship"; export * from "./Relationship";
export * from "./Role"; export * from "./Role";
export * from "./Session";
export * from "./Sticker"; export * from "./Sticker";
export * from "./Team"; export * from "./Team";
export * from "./TeamMember"; export * from "./TeamMember";