mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 09:52:50 +08:00
Add Docker deployment and fix crash in insecure (non-HTTPS) contexts
Adds a multi-stage Dockerfile (Rust connector + gateway + web build) and docker-compose.yml for running the whole stack as a single container behind a reverse proxy; the gateway now serves the built web app and the WebSocket endpoint (/ws) from one port, and the frontend derives its gateway URL from window.location in production instead of a hardcoded localhost address. Also guards all navigator.mediaDevices usage against being undefined, which happens when the page is loaded over plain HTTP on a non-localhost address (not a secure context) - previously this crashed the whole React app on mount instead of just disabling the affected voice features.
This commit is contained in:
1 parent
3715a52de6
commit
c48534dbc4
8 files changed
+129
-8
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
**/node_modules
|
||||||
|
**/dist
|
||||||
|
**/target
|
||||||
|
tsclientlib/.git
|
||||||
|
.git
|
||||||
|
*.log
|
||||||
|
.env
|
||||||
|
.env.local
|
||||||
@@ -6,3 +6,4 @@ dist-ssr/
|
|||||||
.env
|
.env
|
||||||
.env.local
|
.env.local
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
docker-compose.override.yml
|
||||||
+43
@@ -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"]
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
ts-web-client:
|
||||||
|
build: .
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
+52
-2
@@ -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 { WebSocketServer, type WebSocket } from "ws";
|
||||||
import { Ts3Connection, type Ts3ConnectOptions } from "./ts3/connection.js";
|
import { Ts3Connection, type Ts3ConnectOptions } from "./ts3/connection.js";
|
||||||
|
|
||||||
const PORT = Number(process.env.PORT ?? 8080);
|
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<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(`Gateway listening on http://localhost:${PORT} (WebSocket at /ws)`);
|
||||||
|
});
|
||||||
|
|
||||||
wss.on("connection", (socket: WebSocket) => {
|
wss.on("connection", (socket: WebSocket) => {
|
||||||
let connection: Ts3Connection | undefined;
|
let connection: Ts3Connection | undefined;
|
||||||
|
|||||||
@@ -62,11 +62,13 @@ export interface Ts3ConnectOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const CONNECTOR_BIN = path.resolve(
|
const CONNECTOR_BIN =
|
||||||
__dirname,
|
process.env.CONNECTOR_BIN ??
|
||||||
"../../../connector/target/debug",
|
path.resolve(
|
||||||
process.platform === "win32" ? "ts-connector.exe" : "ts-connector"
|
__dirname,
|
||||||
);
|
"../../../connector/target/debug",
|
||||||
|
process.platform === "win32" ? "ts-connector.exe" : "ts-connector"
|
||||||
|
);
|
||||||
|
|
||||||
export class Ts3Connection {
|
export class Ts3Connection {
|
||||||
private listeners = new Set<(event: Ts3ConnectionEvent) => void>();
|
private listeners = new Set<(event: Ts3ConnectionEvent) => void>();
|
||||||
|
|||||||
+6
-1
@@ -9,7 +9,12 @@ import {
|
|||||||
pickAudioOutputDevice,
|
pickAudioOutputDevice,
|
||||||
} from "./voice";
|
} 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_HOST_KEY = "ts-web-client:last-host";
|
||||||
const LAST_NICKNAME_KEY = "ts-web-client:last-nickname";
|
const LAST_NICKNAME_KEY = "ts-web-client:last-nickname";
|
||||||
|
|||||||
@@ -55,6 +55,9 @@ export class MicCapture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async start(): Promise<void> {
|
async start(): Promise<void> {
|
||||||
|
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({
|
this.stream = await navigator.mediaDevices.getUserMedia({
|
||||||
audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true },
|
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. */
|
/** Output ("audiooutput") devices available for playback, e.g. for a device picker. */
|
||||||
export async function listAudioOutputDevices(): Promise<MediaDeviceInfo[]> {
|
export async function listAudioOutputDevices(): Promise<MediaDeviceInfo[]> {
|
||||||
|
if (!navigator.mediaDevices) return [];
|
||||||
const devices = await navigator.mediaDevices.enumerateDevices();
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||||
return devices.filter((d) => d.kind === "audiooutput");
|
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+). */
|
/** Whether the browser supports the native OS output-device picker (Chrome/Edge 105+). */
|
||||||
export function hasNativeOutputPicker(): boolean {
|
export function hasNativeOutputPicker(): boolean {
|
||||||
|
if (!navigator.mediaDevices) return false;
|
||||||
return typeof (navigator.mediaDevices as MediaDevicesWithPicker).selectAudioOutput === "function";
|
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.
|
* the user to pick one, each call - no persistent listing to keep in sync.
|
||||||
*/
|
*/
|
||||||
export async function pickAudioOutputDevice(): Promise<MediaDeviceInfo | null> {
|
export async function pickAudioOutputDevice(): Promise<MediaDeviceInfo | null> {
|
||||||
|
if (!navigator.mediaDevices) return null;
|
||||||
const md = navigator.mediaDevices as MediaDevicesWithPicker;
|
const md = navigator.mediaDevices as MediaDevicesWithPicker;
|
||||||
if (typeof md.selectAudioOutput !== "function") return null;
|
if (typeof md.selectAudioOutput !== "function") return null;
|
||||||
return md.selectAudioOutput();
|
return md.selectAudioOutput();
|
||||||
|
|||||||
Reference in new issue
Block a user