This commit is contained in:
dank074 2025-04-16 22:59:48 -05:00
parent 623f8fcebc
commit 5bae44faf0
4 changed files with 82 additions and 56 deletions

View File

@ -31,13 +31,18 @@ import { Config, initDatabase, Sentry } from "@spacebar/util";
const app = express();
const server = http.createServer();
const port = Number(process.env.PORT) || 3001;
const wrtcWsPort = Number(process.env.WRTC_WS_PORT) || 3004;
const production = process.env.NODE_ENV == "development" ? false : true;
server.on("request", app);
const api = new Api.SpacebarServer({ server, port, production, app });
const cdn = new CDNServer({ server, port, production, app });
const gateway = new Gateway.Server({ server, port, production });
const webrtc = new Webrtc.Server({ server: undefined, port: 3004, production });
const webrtc = new Webrtc.Server({
server: undefined,
port: wrtcWsPort,
production,
});
process.on("SIGTERM", async () => {
console.log("Shutting down due to SIGTERM");

View File

@ -14,15 +14,32 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
const body = data.d as StreamWatchSchema;
// TODO: apply perms: check if user is allowed to watch
try {
const { type, channelId, guildId, userId } = parseStreamKey(
body.stream_key,
);
const stream = await Stream.findOneOrFail({
let parsedKey: {
type: "guild" | "call";
channelId: string;
guildId?: string;
userId: string;
};
try {
parsedKey = parseStreamKey(body.stream_key);
} catch (e) {
return this.close(4000, "Invalid stream key");
}
const { type, channelId, guildId, userId } = parsedKey;
const stream = await Stream.findOne({
where: { channel_id: channelId, owner_id: userId },
relations: ["channel"],
});
if (!stream) return this.close(4000, "Invalid stream key");
if (type === "guild" && stream.channel.guild_id != guildId)
return this.close(4000, "Invalid stream key");
const streamSession = StreamSession.create({
stream_id: stream.id,
user_id: this.user_id,
@ -66,7 +83,4 @@ export async function onStreamWatch(this: WebSocket, data: Payload) {
},
user_id: this.user_id,
});
} catch (e) {
return this.close(4000, "Invalid stream key");
}
}

View File

@ -22,6 +22,7 @@ import http from "http";
import ws from "ws";
import { Connection } from "./events/Connection";
import {
loadWebRtcLibrary,
mediaServer,
WRTC_PORT_MAX,
WRTC_PORT_MIN,
@ -77,11 +78,14 @@ export class Server {
await Config.init();
await initEvent();
// if we failed to load webrtc library
if (!mediaServer) {
// try to load webrtc library, if failed just don't start webrtc endpoint
try {
await loadWebRtcLibrary();
} catch (e) {
console.log(`[WebRTC] ${yellow("WEBRTC disabled")}`);
return Promise.resolve();
}
await mediaServer.start(WRTC_PUBLIC_IP, WRTC_PORT_MIN, WRTC_PORT_MAX);
if (!this.server.listening) {
this.server.listen(this.port);

View File

@ -44,7 +44,7 @@ class NoConfiguredLibraryError implements Error {
}
}
(async () => {
export const loadWebRtcLibrary = async () => {
try {
//mediaServer = require('medooze-spacebar-wrtc');
if (!selectedWrtcLibrary)
@ -56,9 +56,12 @@ class NoConfiguredLibraryError implements Error {
console.log(
`[WebRTC] ${green(`Succesfully loaded ${selectedWrtcLibrary}`)}`,
);
return Promise.resolve();
} catch (error) {
console.log(
`[WebRTC] ${red(`Failed to import ${selectedWrtcLibrary}: ${error instanceof NoConfiguredLibraryError ? error.message : ""}`)}`,
);
return Promise.reject();
}
})();
};