Files
webspeak3/gateway/src/index.ts
T
Moepchi 7901aafbb1 Add file transfer support (#83)
Channel-scoped file browser reachable via right-click on a channel:
navigate directories, create folders, rename/delete, and upload/download
files. Backed by tsclientlib's built-in download_file/upload_file (which
already handles the ftinitdownload/ftinitupload -> TCP data-port dance),
plus raw ftgetfilelist/ftcreatedir/ftdeletefile/ftrenamefile commands for
everything else. Downloads/uploads are buffered whole in memory and
base64-relayed over the existing stdin/WebSocket text protocol - fine for
a channel filebase's typical file sizes, not meant for huge transfers.

Verified against the real server: upload/list/download round-trip reaches
the server correctly (list succeeds, upload correctly surfaces the
guest account's missing permission via the new FiletransferFailed
handling instead of hanging or crashing).

Demo mode gets a small in-memory per-channel filesystem so the file
browser is fully interactive in the GitHub Pages demo too.
2026-08-05 22:39:35 +02:00

264 lines
7.8 KiB
TypeScript

import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { WebSocketServer, type WebSocket } from "ws";
import { Ts3Connection, type Ts3ConnectOptions } from "./ts3/connection.js";
const PORT = Number(process.env.PORT ?? 8080);
// In production (Docker), the built web app lives alongside the gateway and
// is served from the same port as the WebSocket endpoint, so a single
// reverse-proxied origin (e.g. a Zoraxy subdomain) is enough for everything.
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const WEB_DIST = process.env.WEB_DIST ?? path.resolve(__dirname, "../../web/dist");
const MIME_TYPES: Record<string, string> = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".svg": "image/svg+xml",
".png": "image/png",
".ico": "image/x-icon",
".json": "application/json",
".woff2": "font/woff2",
};
const server = createServer((req, res) => {
void (async () => {
try {
const url = new URL(req.url ?? "/", "http://localhost");
let filePath = path.join(WEB_DIST, decodeURIComponent(url.pathname));
if (!filePath.startsWith(WEB_DIST)) {
res.writeHead(403);
res.end();
return;
}
let body: Buffer;
try {
body = await readFile(filePath);
} catch {
// SPA fallback: unknown paths (client-side routes, or "/") serve index.html.
filePath = path.join(WEB_DIST, "index.html");
body = await readFile(filePath);
}
res.writeHead(200, { "Content-Type": MIME_TYPES[path.extname(filePath)] ?? "application/octet-stream" });
res.end(body);
} catch {
res.writeHead(404);
res.end("Not found");
}
})();
});
const wss = new WebSocketServer({ server, path: "/ws" });
server.listen(PORT, () => {
console.log(`WebSpeak3 gateway listening on http://localhost:${PORT} (WebSocket at /ws)`);
});
wss.on("connection", (socket: WebSocket) => {
let connection: Ts3Connection | undefined;
socket.on("message", async (raw) => {
const msg = JSON.parse(raw.toString());
switch (msg.type) {
case "connect": {
const options: Ts3ConnectOptions = {
host: msg.host,
nickname: msg.nickname,
serverPassword: msg.serverPassword,
channelPassword: msg.channelPassword,
defaultChannel: msg.defaultChannel,
identity: msg.identity,
};
connection = new Ts3Connection(options);
connection.onEvent((event) => socket.send(JSON.stringify(event)));
await connection.connect();
break;
}
case "switchChannel": {
await connection?.switchChannel(msg.channelId);
break;
}
case "sendChatMessage": {
await connection?.sendChatMessage(msg.message);
break;
}
case "sendServerMessage": {
await connection?.sendServerMessage(msg.message);
break;
}
case "sendPrivateMessage": {
await connection?.sendPrivateMessage(msg.clientId, msg.message);
break;
}
case "sendPoke": {
await connection?.sendPoke(msg.clientId, msg.message ?? "");
break;
}
case "sendAudio": {
await connection?.sendAudio(msg.pcm);
break;
}
case "setAway": {
await connection?.setAway(msg.away, msg.message ?? "");
break;
}
case "setInputMuted": {
await connection?.setInputMuted(msg.muted);
break;
}
case "setOutputMuted": {
await connection?.setOutputMuted(msg.muted);
break;
}
case "setNickname": {
await connection?.setNickname(msg.nickname);
break;
}
case "setWhisperTargets": {
await connection?.setWhisperTargets(msg.channelIds ?? [], msg.clientIds ?? []);
break;
}
case "getClientConnectionInfo": {
await connection?.getClientConnectionInfo(msg.clientId);
break;
}
case "getServerConnectionInfo": {
await connection?.getServerConnectionInfo();
break;
}
case "kickFromChannel": {
await connection?.kickFromChannel(msg.clientId, msg.reason ?? "");
break;
}
case "kickFromServer": {
await connection?.kickFromServer(msg.clientId, msg.reason ?? "");
break;
}
case "banClient": {
await connection?.banClient(msg.clientId, msg.seconds ?? 0, msg.reason ?? "");
break;
}
case "editServer": {
await connection?.editServer(msg.payload ?? {});
break;
}
case "getServerLog": {
await connection?.getServerLog();
break;
}
case "getBanList": {
await connection?.getBanList();
break;
}
case "deleteBan": {
await connection?.deleteBan(msg.banId);
break;
}
case "deleteAllBans": {
await connection?.deleteAllBans();
break;
}
case "getComplainList": {
await connection?.getComplainList();
break;
}
case "deleteComplaint": {
await connection?.deleteComplaint(msg.targetClientDbId, msg.fromClientDbId);
break;
}
case "deleteAllComplaintsFor": {
await connection?.deleteAllComplaintsFor(msg.targetClientDbId);
break;
}
case "getOfflineMessageList": {
await connection?.getOfflineMessageList();
break;
}
case "getOfflineMessage": {
await connection?.getOfflineMessage(msg.messageId);
break;
}
case "sendOfflineMessage": {
await connection?.sendOfflineMessage(msg.clientUid, msg.subject, msg.message);
break;
}
case "deleteOfflineMessage": {
await connection?.deleteOfflineMessage(msg.messageId);
break;
}
case "markOfflineMessageRead": {
await connection?.markOfflineMessageRead(msg.messageId);
break;
}
case "getChannelGroupList": {
await connection?.getChannelGroupList();
break;
}
case "getServerGroupList": {
await connection?.getServerGroupList();
break;
}
case "setChannelGroup": {
await connection?.setChannelGroup(msg.channelGroupId, msg.channelId, msg.clientDbId);
break;
}
case "addServerGroup": {
await connection?.addServerGroup(msg.serverGroupId, msg.clientDbId);
break;
}
case "removeServerGroup": {
await connection?.removeServerGroup(msg.serverGroupId, msg.clientDbId);
break;
}
case "serverQueryLogin": {
await connection?.serverQueryLogin(msg.username, msg.password);
break;
}
case "getPermissionOverview": {
await connection?.getPermissionOverview();
break;
}
case "getFileList": {
await connection?.getFileList(msg.channelId, msg.path);
break;
}
case "createDirectory": {
await connection?.createDirectory(msg.channelId, msg.dirname);
break;
}
case "deleteFile": {
await connection?.deleteFile(msg.channelId, msg.name);
break;
}
case "renameFile": {
await connection?.renameFile(msg.channelId, msg.oldName, msg.newName);
break;
}
case "downloadFile": {
await connection?.downloadFile(msg.channelId, msg.path);
break;
}
case "uploadFile": {
await connection?.uploadFile(msg.channelId, msg.path, msg.dataBase64);
break;
}
case "disconnect": {
await connection?.disconnect(msg.message ?? "");
break;
}
default:
socket.send(
JSON.stringify({ type: "error", message: `Unknown message type: ${msg.type}` })
);
}
});
socket.on("close", () => {
connection?.disconnect();
});
});