2026-05-15 04:25:06 +07:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
import http from "node:http";
|
|
|
|
|
import path from "node:path";
|
2026-05-16 18:18:39 +07:00
|
|
|
import { fileURLToPath } from "node:url";
|
2026-05-13 18:23:20 +07:00
|
|
|
import type { Client } from "discord.js-selfbot-v13";
|
2026-05-16 19:17:34 +07:00
|
|
|
import express, {
|
|
|
|
|
type NextFunction,
|
|
|
|
|
type Request,
|
|
|
|
|
type Response,
|
|
|
|
|
} from "express";
|
2026-05-13 16:57:07 +07:00
|
|
|
import helmet from "helmet";
|
2026-05-13 15:28:25 +07:00
|
|
|
import { WebSocketServer } from "ws";
|
2026-05-17 01:01:40 +07:00
|
|
|
import { config } from "./config";
|
2026-05-13 18:23:20 +07:00
|
|
|
import { AppError } from "./errors";
|
2026-05-13 16:57:07 +07:00
|
|
|
import { createChildLogger, logger } from "./logger";
|
2026-05-15 18:04:39 +07:00
|
|
|
import { MediaController } from "./media/mediaController";
|
2026-05-16 15:48:28 +07:00
|
|
|
import { createScreenShareController } from "./media/screenShareController";
|
2026-05-13 17:06:22 +07:00
|
|
|
import { getMetrics, uptimeGauge } from "./metrics";
|
2026-05-14 19:46:47 +07:00
|
|
|
import { createBroadcaster } from "./moderation/broadcaster";
|
2026-05-19 14:16:00 +07:00
|
|
|
import { createSharedUIStateStore } from "./state/uiState";
|
|
|
|
|
import { Streamer } from "./streaming";
|
|
|
|
|
import type { VoiceController } from "./voiceController";
|
|
|
|
|
import {
|
|
|
|
|
initializeMediaSettings,
|
|
|
|
|
persistMediaSettings,
|
|
|
|
|
} from "./state/mediaSettings";
|
2026-05-14 19:46:47 +07:00
|
|
|
import { createAnalysisRoutes } from "./routes/analysisRoutes";
|
2026-05-15 17:52:16 +07:00
|
|
|
import { createMediaRoutes } from "./routes/mediaRoutes";
|
2026-05-14 19:46:47 +07:00
|
|
|
import { createMessageRoutes } from "./routes/messageRoutes";
|
2026-05-19 02:49:55 +07:00
|
|
|
import { createRecordingsRoutes } from "./routes/recordingsRoutes";
|
2026-05-14 19:46:47 +07:00
|
|
|
import { createSyncRoutes } from "./routes/syncRoutes";
|
|
|
|
|
import { createUIStateRoutes } from "./routes/uiStateRoutes";
|
|
|
|
|
import { createVoiceRoutes } from "./routes/voiceRoutes";
|
2026-05-19 14:26:06 +07:00
|
|
|
import {
|
|
|
|
|
exposeActiveUserGlobal,
|
|
|
|
|
exposeModerationGlobals,
|
|
|
|
|
exposePcmBroadcastGlobal,
|
|
|
|
|
exposeVideoBroadcastGlobal,
|
|
|
|
|
} from "./ws/broadcastGlobals";
|
2026-05-19 14:34:24 +07:00
|
|
|
import { createVoiceAudioBridge } from "./ws/voiceAudioBridge";
|
2026-05-13 16:25:01 +07:00
|
|
|
|
2026-05-16 18:18:39 +07:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
|
2026-05-13 16:57:07 +07:00
|
|
|
const wsLogger = createChildLogger("webserver");
|
2026-05-13 02:30:09 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
const activeUsers = new Map<
|
|
|
|
|
string,
|
|
|
|
|
{ username: string; avatar: string; speaking: boolean }
|
|
|
|
|
>();
|
2026-05-13 02:30:09 +07:00
|
|
|
|
2026-05-14 14:55:21 +07:00
|
|
|
export async function startWebserver(
|
2026-05-13 18:23:20 +07:00
|
|
|
port: number = 3000,
|
|
|
|
|
_client: Client,
|
|
|
|
|
voiceController: VoiceController,
|
|
|
|
|
) {
|
2026-05-19 14:11:09 +07:00
|
|
|
const { getSharedUIState, patchSharedUIState } = await createSharedUIStateStore();
|
2026-05-17 04:52:20 +07:00
|
|
|
let mediaSettings = await initializeMediaSettings();
|
2026-05-14 14:55:21 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
const app = express();
|
|
|
|
|
const server = http.createServer(app);
|
2026-05-13 00:32:27 +07:00
|
|
|
|
2026-05-13 17:49:33 +07:00
|
|
|
const wsPath = "/ws";
|
|
|
|
|
const wss = new WebSocketServer({ server, path: wsPath });
|
|
|
|
|
wsLogger.info({ port, wsPath }, "WebSocket server listening");
|
2026-05-13 16:57:07 +07:00
|
|
|
|
2026-05-14 19:46:47 +07:00
|
|
|
// Create broadcaster instance
|
|
|
|
|
const broadcaster = createBroadcaster();
|
2026-05-19 14:26:06 +07:00
|
|
|
exposeModerationGlobals(broadcaster, config.ADMIN_PASSWORD);
|
2026-05-14 19:46:47 +07:00
|
|
|
|
2026-05-16 15:48:28 +07:00
|
|
|
const streamer = new Streamer(_client);
|
|
|
|
|
const screenController = createScreenShareController({
|
|
|
|
|
getVoiceStatus: () => voiceController.getStatus(),
|
|
|
|
|
streamer,
|
2026-05-17 17:35:37 +07:00
|
|
|
useTranscoder: true,
|
2026-05-17 19:39:46 +07:00
|
|
|
onBeforeStreamStart: async (guildId: string, channelId: string) => {
|
2026-05-17 17:35:37 +07:00
|
|
|
await voiceController.disconnect();
|
2026-05-17 19:39:46 +07:00
|
|
|
// Wait for Discord gateway to fully process the disconnect
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1500));
|
2026-05-17 17:35:37 +07:00
|
|
|
},
|
|
|
|
|
onAfterStreamEnd: async (guildId: string, channelId: string) => {
|
|
|
|
|
const current = voiceController.getStatus();
|
|
|
|
|
if (current.connected && current.activeGuildId === guildId) return;
|
|
|
|
|
await voiceController.connect(guildId, channelId);
|
|
|
|
|
},
|
2026-05-16 15:48:28 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-15 17:52:16 +07:00
|
|
|
const mediaController = new MediaController({
|
|
|
|
|
isVoiceConnected: () => voiceController.getStatus().connected,
|
2026-05-19 14:11:09 +07:00
|
|
|
isBrowserStreaming: () => getSharedUIState().isStreaming,
|
2026-05-16 15:48:28 +07:00
|
|
|
screenController,
|
2026-05-15 17:52:16 +07:00
|
|
|
onStateChange: (state) => broadcaster.mediaState(state),
|
2026-05-17 04:52:20 +07:00
|
|
|
initialMusicVolume: mediaSettings.musicVolume,
|
|
|
|
|
onMusicVolumeChange: async (volume) => {
|
|
|
|
|
mediaSettings = { ...mediaSettings, musicVolume: volume };
|
2026-05-19 14:16:00 +07:00
|
|
|
await persistMediaSettings(mediaSettings);
|
2026-05-17 04:52:20 +07:00
|
|
|
},
|
2026-05-15 17:52:16 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-13 18:56:44 +07:00
|
|
|
// Security headers. CSP disabled because the current static UI uses inline scripts/styles.
|
|
|
|
|
app.use(
|
|
|
|
|
helmet({
|
|
|
|
|
contentSecurityPolicy: false,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-05-13 16:57:07 +07:00
|
|
|
|
2026-05-16 19:17:34 +07:00
|
|
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
2026-05-14 03:17:07 +07:00
|
|
|
if (req.path.startsWith("/api/")) {
|
|
|
|
|
res.set("Cache-Control", "no-store");
|
|
|
|
|
}
|
|
|
|
|
res.on("finish", () => {
|
|
|
|
|
if (req.originalUrl.startsWith("/.well-known/appspecific/")) return;
|
2026-05-14 03:45:51 +07:00
|
|
|
if (req.originalUrl === "/favicon.ico") return;
|
2026-05-14 03:17:07 +07:00
|
|
|
if (res.statusCode >= 400) {
|
|
|
|
|
logger.error(
|
2026-05-14 15:02:23 +07:00
|
|
|
{
|
|
|
|
|
method: req.method,
|
|
|
|
|
url: req.originalUrl,
|
|
|
|
|
statusCode: res.statusCode,
|
|
|
|
|
},
|
2026-05-14 03:17:07 +07:00
|
|
|
"HTTP request failed",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
next();
|
|
|
|
|
});
|
2026-05-13 18:23:20 +07:00
|
|
|
app.use(express.json());
|
2026-05-13 00:32:27 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
app.use(express.static(path.join(__dirname, "../public")));
|
2026-05-16 21:08:39 +07:00
|
|
|
app.use(express.static(path.join(__dirname, "../public/app")));
|
2026-05-13 00:32:27 +07:00
|
|
|
|
2026-05-16 19:17:34 +07:00
|
|
|
app.get("/", (_req: Request, res: Response) => {
|
2026-05-14 21:16:03 +07:00
|
|
|
const reactIndex = path.join(__dirname, "../public/app/index.html");
|
|
|
|
|
if (fs.existsSync(reactIndex)) {
|
|
|
|
|
res.sendFile(reactIndex);
|
2026-05-16 19:17:34 +07:00
|
|
|
return;
|
2026-05-14 21:16:03 +07:00
|
|
|
}
|
2026-05-16 19:17:34 +07:00
|
|
|
res
|
|
|
|
|
.status(503)
|
|
|
|
|
.send("React dashboard is not built. Run pnpm run build:web.");
|
2026-05-14 00:14:25 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-13 16:57:07 +07:00
|
|
|
// Health check endpoint
|
2026-05-16 19:17:34 +07:00
|
|
|
app.get("/health", (_req: Request, res: Response) => {
|
2026-05-13 16:57:07 +07:00
|
|
|
res.json({
|
|
|
|
|
status: "ok",
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
uptime: process.uptime(),
|
|
|
|
|
activeUsers: activeUsers.size,
|
2026-05-14 19:46:47 +07:00
|
|
|
wsClients: broadcaster.clientCount(),
|
2026-05-13 16:57:07 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-13 17:06:22 +07:00
|
|
|
// Metrics endpoint
|
2026-05-16 19:17:34 +07:00
|
|
|
app.get("/metrics", async (_req: Request, res: Response) => {
|
2026-05-13 17:06:22 +07:00
|
|
|
res.set("Content-Type", "text/plain");
|
|
|
|
|
uptimeGauge.set(process.uptime());
|
|
|
|
|
res.send(await getMetrics());
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-17 00:20:29 +07:00
|
|
|
// Simple password-based auth
|
|
|
|
|
app.post("/api/auth/login", (req: Request, res: Response) => {
|
|
|
|
|
const { password } = req.body;
|
|
|
|
|
if (password === config.ADMIN_PASSWORD) {
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
} else {
|
|
|
|
|
res.status(401).json({ error: "Invalid password" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-14 19:46:47 +07:00
|
|
|
// Register route modules
|
|
|
|
|
app.use(
|
|
|
|
|
"/api",
|
|
|
|
|
createUIStateRoutes({ getSharedUIState, patchSharedUIState }),
|
|
|
|
|
);
|
2026-05-14 19:46:47 +07:00
|
|
|
app.use(
|
|
|
|
|
"/api",
|
|
|
|
|
createVoiceRoutes({
|
|
|
|
|
voiceController,
|
|
|
|
|
patchSharedUIState,
|
|
|
|
|
broadcaster,
|
2026-05-17 00:39:29 +07:00
|
|
|
adminPassword: config.ADMIN_PASSWORD,
|
2026-05-14 19:46:47 +07:00
|
|
|
}),
|
|
|
|
|
);
|
2026-05-14 19:46:47 +07:00
|
|
|
app.use("/api", createMessageRoutes());
|
|
|
|
|
app.use("/api", createAnalysisRoutes());
|
|
|
|
|
app.use("/api", createSyncRoutes(_client));
|
2026-05-18 20:15:38 +07:00
|
|
|
app.use("/api", createRecordingsRoutes());
|
2026-05-17 00:39:29 +07:00
|
|
|
app.use(
|
|
|
|
|
"/api",
|
|
|
|
|
createMediaRoutes(mediaController, {
|
|
|
|
|
adminPassword: config.ADMIN_PASSWORD,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-05-14 04:02:25 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
function broadcastUserState() {
|
2026-05-14 19:46:47 +07:00
|
|
|
const users = Array.from(activeUsers.entries()).map(([id, data]) => ({
|
|
|
|
|
id,
|
|
|
|
|
...data,
|
|
|
|
|
}));
|
|
|
|
|
broadcaster.userState(users);
|
2026-05-13 19:34:15 +07:00
|
|
|
}
|
|
|
|
|
|
2026-05-19 14:26:06 +07:00
|
|
|
exposePcmBroadcastGlobal(broadcaster);
|
|
|
|
|
exposeVideoBroadcastGlobal(() => broadcaster.getClients(), wsLogger);
|
|
|
|
|
exposeActiveUserGlobal(activeUsers, broadcastUserState);
|
|
|
|
|
|
2026-05-19 14:34:24 +07:00
|
|
|
const voiceAudioBridge = createVoiceAudioBridge(wsLogger);
|
2026-05-13 02:30:09 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
wss.on("connection", (ws) => {
|
2026-05-13 17:49:33 +07:00
|
|
|
wsLogger.info({ port, wsPath }, "New WebSocket connection");
|
2026-05-14 19:46:47 +07:00
|
|
|
broadcaster.addClient(ws);
|
2026-05-13 02:58:11 +07:00
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "user_state",
|
|
|
|
|
users: Array.from(activeUsers.entries()).map(([id, data]) => ({
|
|
|
|
|
id,
|
|
|
|
|
...data,
|
|
|
|
|
})),
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-05-14 01:45:27 +07:00
|
|
|
ws.send(JSON.stringify({ type: "ui_state", state: getSharedUIState() }));
|
2026-05-15 18:04:39 +07:00
|
|
|
ws.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "media_state",
|
|
|
|
|
state: mediaController.getState(),
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-05-13 02:30:09 +07:00
|
|
|
|
2026-05-15 07:13:37 +07:00
|
|
|
ws.on("message", (data: Buffer | ArrayBuffer | Buffer[]) => {
|
2026-05-13 15:28:25 +07:00
|
|
|
if (!Buffer.isBuffer(data)) return;
|
2026-05-19 14:34:24 +07:00
|
|
|
voiceAudioBridge.handleBrowserAudio(data);
|
2026-05-13 00:32:27 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
ws.on("close", () => {
|
2026-05-14 19:46:47 +07:00
|
|
|
broadcaster.removeClient(ws);
|
2026-05-13 00:32:27 +07:00
|
|
|
});
|
2026-05-13 15:28:25 +07:00
|
|
|
ws.on("error", () => {
|
2026-05-14 19:46:47 +07:00
|
|
|
broadcaster.removeClient(ws);
|
2026-05-13 15:28:25 +07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-13 18:23:20 +07:00
|
|
|
app.use(
|
|
|
|
|
(
|
|
|
|
|
error: Error,
|
|
|
|
|
_req: express.Request,
|
|
|
|
|
res: express.Response,
|
|
|
|
|
_next: express.NextFunction,
|
|
|
|
|
) => {
|
|
|
|
|
if (error instanceof AppError) {
|
|
|
|
|
res.status(error.statusCode).json({
|
|
|
|
|
error: error.code,
|
|
|
|
|
message: error.message,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wsLogger.error({ error }, "Unhandled webserver error");
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
error: "INTERNAL_SERVER_ERROR",
|
|
|
|
|
message: "Internal server error",
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-13 15:28:25 +07:00
|
|
|
server.listen(port, "0.0.0.0", () => {
|
2026-05-13 16:57:07 +07:00
|
|
|
wsLogger.info({ port }, "Web interface listening");
|
2026-05-13 15:28:25 +07:00
|
|
|
});
|
2026-05-13 00:32:27 +07:00
|
|
|
}
|