diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3b88cba --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +**/node_modules +**/dist +**/target +tsclientlib/.git +.git +*.log +.env +.env.local diff --git a/.gitignore b/.gitignore index f1302dc..d9580af 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ dist-ssr/ .env .env.local .DS_Store +docker-compose.override.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4f872be --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +# --- Rust connector ----------------------------------------------------- +FROM rust:1-bookworm AS connector-builder +RUN apt-get update && apt-get install -y --no-install-recommends cmake && rm -rf /var/lib/apt/lists/* +WORKDIR /src +COPY tsclientlib/ tsclientlib/ +COPY connector/ connector/ +WORKDIR /src/connector +ENV CMAKE_POLICY_VERSION_MINIMUM=3.5 +RUN cargo build --release + +# --- Web frontend --------------------------------------------------------- +FROM node:22-bookworm-slim AS web-builder +WORKDIR /src/web +COPY web/package*.json ./ +RUN npm ci +COPY web/ ./ +# tsc -b currently fails on pre-existing type errors unrelated to this build; +# vite build alone is enough to produce the production bundle. +RUN npx vite build + +# --- Gateway ---------------------------------------------------------------- +FROM node:22-bookworm-slim AS gateway-builder +WORKDIR /src/gateway +COPY gateway/package*.json ./ +RUN npm ci +COPY gateway/ ./ +RUN npm run build + +# --- Runtime ---------------------------------------------------------------- +FROM node:22-bookworm-slim AS runtime +WORKDIR /app +COPY gateway/package*.json ./ +RUN npm ci --omit=dev +COPY --from=gateway-builder /src/gateway/dist ./dist +COPY --from=connector-builder /src/connector/target/release/ts-connector /app/connector-bin/ts-connector +COPY --from=web-builder /src/web/dist /app/web/dist + +ENV PORT=8080 +ENV WEB_DIST=/app/web/dist +ENV CONNECTOR_BIN=/app/connector-bin/ts-connector +EXPOSE 8080 + +CMD ["node", "dist/index.js"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9d7ab4e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + ts-web-client: + build: . + restart: unless-stopped + ports: + - "8080:8080" diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 6c92036..7f54d3e 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -1,11 +1,61 @@ +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); -const wss = new WebSocketServer({ port: PORT }); +// 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"); -console.log(`Gateway listening on ws://localhost:${PORT}`); +const MIME_TYPES: Record = { + ".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(`Gateway listening on http://localhost:${PORT} (WebSocket at /ws)`); +}); wss.on("connection", (socket: WebSocket) => { let connection: Ts3Connection | undefined; diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index 32cd1a9..1a1a70d 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -62,11 +62,13 @@ export interface Ts3ConnectOptions { } const __dirname = path.dirname(fileURLToPath(import.meta.url)); -const CONNECTOR_BIN = path.resolve( - __dirname, - "../../../connector/target/debug", - process.platform === "win32" ? "ts-connector.exe" : "ts-connector" -); +const CONNECTOR_BIN = + process.env.CONNECTOR_BIN ?? + path.resolve( + __dirname, + "../../../connector/target/debug", + process.platform === "win32" ? "ts-connector.exe" : "ts-connector" + ); export class Ts3Connection { private listeners = new Set<(event: Ts3ConnectionEvent) => void>(); diff --git a/web/src/App.tsx b/web/src/App.tsx index 0e04bb6..41a9e5d 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -9,7 +9,12 @@ import { pickAudioOutputDevice, } from "./voice"; -const GATEWAY_URL = "ws://localhost:8080"; +// In dev, the gateway runs standalone on its own port. In production it's +// served from the same origin/port as the web app (single container behind a +// reverse proxy), so derive the WebSocket URL from the current location. +const GATEWAY_URL = import.meta.env.DEV + ? "ws://localhost:8080" + : `${window.location.protocol === "https:" ? "wss" : "ws"}://${window.location.host}/ws`; const LAST_HOST_KEY = "ts-web-client:last-host"; const LAST_NICKNAME_KEY = "ts-web-client:last-nickname"; diff --git a/web/src/voice.ts b/web/src/voice.ts index 891f53d..963c0fc 100644 --- a/web/src/voice.ts +++ b/web/src/voice.ts @@ -55,6 +55,9 @@ export class MicCapture { } async start(): Promise { + if (!navigator.mediaDevices) { + throw new Error("Microphone access requires HTTPS (or localhost) - the site is not a secure context."); + } this.stream = await navigator.mediaDevices.getUserMedia({ audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true }, }); @@ -117,6 +120,7 @@ export class MicCapture { /** Output ("audiooutput") devices available for playback, e.g. for a device picker. */ export async function listAudioOutputDevices(): Promise { + if (!navigator.mediaDevices) return []; const devices = await navigator.mediaDevices.enumerateDevices(); return devices.filter((d) => d.kind === "audiooutput"); } @@ -125,6 +129,7 @@ type MediaDevicesWithPicker = MediaDevices & { selectAudioOutput?: () => Promise /** Whether the browser supports the native OS output-device picker (Chrome/Edge 105+). */ export function hasNativeOutputPicker(): boolean { + if (!navigator.mediaDevices) return false; return typeof (navigator.mediaDevices as MediaDevicesWithPicker).selectAudioOutput === "function"; } @@ -135,6 +140,7 @@ export function hasNativeOutputPicker(): boolean { * the user to pick one, each call - no persistent listing to keep in sync. */ export async function pickAudioOutputDevice(): Promise { + if (!navigator.mediaDevices) return null; const md = navigator.mediaDevices as MediaDevicesWithPicker; if (typeof md.selectAudioOutput !== "function") return null; return md.selectAudioOutput();