-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-server.js
More file actions
218 lines (183 loc) · 7.1 KB
/
cloud-server.js
File metadata and controls
218 lines (183 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import express from "express";
import { WebSocketServer } from "ws";
import http from "http";
import path from "path";
import { fileURLToPath } from "url";
import crypto from "crypto";
const PORT = process.env.PORT || 10000;
const DEFAULT_ROOM = process.env.DEFAULT_ROOM || "baden";
// optional: Logs schützen (empfohlen!). Wenn gesetzt, brauchst du ?token=...
const LOG_TOKEN = process.env.LOG_TOKEN || "";
const MAX_LOGS = Number(process.env.MAX_LOGS || 2000);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(express.static(path.join(__dirname, "public"), { extensions: ["html"] }));
/* =========================
SIMPLE IN-MEMORY LOG
========================= */
const logs = [];
function addLog(evt) {
const row = {
ts: new Date().toISOString(),
...evt,
};
logs.push(row);
if (logs.length > MAX_LOGS) logs.splice(0, logs.length - MAX_LOGS);
}
function requireToken(req, res, next) {
if (!LOG_TOKEN) return next(); // unprotected if not configured
const token = req.query.token || req.headers["x-log-token"];
if (token === LOG_TOKEN) return next();
res.status(401).send("Unauthorized");
}
function htmlEscape(s) {
return String(s)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
/* =========================
ROUTES
========================= */
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "controller.html"));
});
app.get("/controller", (req, res) => {
res.sendFile(path.join(__dirname, "public", "controller.html"));
});
// optional multi-room controller page
app.get("/r/:room", (req, res) => {
res.sendFile(path.join(__dirname, "public", "controller.html"));
});
// logs pages
app.get("/logs", requireToken, (req, res) => {
const rows = logs.slice().reverse().slice(0, 500); // show last 500
const html = `<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>Pong Logs</title>
<style>
body{font-family:system-ui;margin:20px;background:#0b1020;color:#eaf0ff}
a{color:#5eead4}
.muted{opacity:.7}
table{width:100%;border-collapse:collapse;margin-top:12px}
th,td{border-bottom:1px solid rgba(255,255,255,.12);padding:10px 8px;text-align:left;vertical-align:top}
th{font-size:12px;letter-spacing:.12em;text-transform:uppercase;opacity:.75}
.pill{display:inline-block;border:1px solid rgba(255,255,255,.16);padding:2px 8px;border-radius:999px;font-size:12px;opacity:.9}
.wrap{white-space:pre-wrap;word-break:break-word}
</style>
</head>
<body>
<h1>Pong Logs</h1>
<div class="muted">Zeigt die letzten ${rows.length} Einträge (max 500 im UI). JSON: <a href="/logs.json${LOG_TOKEN ? `?token=${encodeURIComponent(req.query.token || "")}` : ""}">/logs.json</a></div>
<table>
<thead>
<tr><th>Zeit (UTC)</th><th>Room</th><th>Event</th><th>Details</th></tr>
</thead>
<tbody>
${rows.map(r => `
<tr>
<td class="muted">${htmlEscape(r.ts)}</td>
<td><span class="pill">${htmlEscape(r.room || "-")}</span></td>
<td><b>${htmlEscape(r.type || "-")}</b></td>
<td class="wrap">${htmlEscape(JSON.stringify(r.data || {}, null, 2))}</td>
</tr>
`).join("")}
</tbody>
</table>
</body>
</html>`;
res.setHeader("content-type", "text/html; charset=utf-8");
res.send(html);
});
app.get("/logs.json", requireToken, (req, res) => {
res.json({ count: logs.length, logs: logs.slice().reverse() });
});
/* =========================
WS RELAY
========================= */
const server = http.createServer(app);
const wss = new WebSocketServer({ server, path: "/ws" });
const rooms = new Map();
function getRoom(room) {
if (!rooms.has(room)) rooms.set(room, { renderer: null, controllers: new Map() });
return rooms.get(room);
}
function safeSend(ws, obj) {
if (ws?.readyState === 1) ws.send(JSON.stringify(obj));
}
function newCid() {
return crypto.randomBytes(8).toString("hex");
}
function parseRoom(reqUrl) {
try {
const u = new URL(reqUrl, "http://x");
return (u.searchParams.get("room") || DEFAULT_ROOM).trim() || DEFAULT_ROOM;
} catch {
return DEFAULT_ROOM;
}
}
function getClientIp(req) {
const xf = req.headers["x-forwarded-for"];
if (typeof xf === "string" && xf.length) return xf.split(",")[0].trim();
return req.socket?.remoteAddress || "";
}
wss.on("connection", (ws, req) => {
const ip = getClientIp(req);
let role = null;
try {
const u = new URL(req.url, `http://${req.headers.host}`);
role = u.searchParams.get("role");
} catch { }
const room = parseRoom(req.url);
if (!role) return ws.close();
const R = getRoom(room);
if (role === "renderer") {
if (R.renderer && R.renderer !== ws) { try { R.renderer.close(); } catch { } }
R.renderer = ws;
addLog({ type: "renderer_connected", room, data: { ip } });
safeSend(ws, { type: "hello", role: "renderer", room });
for (const c of R.controllers.values()) safeSend(c, { type: "renderer", online: true });
ws.on("message", (buf) => {
let msg; try { msg = JSON.parse(buf.toString()); } catch { return; }
// log interesting renderer events (optional)
if (msg?.type === "game_event") {
addLog({ type: "game_event", room, data: msg.data || {} });
}
// renderer -> all controllers
for (const c of R.controllers.values()) safeSend(c, msg);
});
ws.on("close", () => {
if (R.renderer === ws) R.renderer = null;
addLog({ type: "renderer_disconnected", room, data: { ip } });
for (const c of R.controllers.values()) safeSend(c, { type: "renderer", online: false });
});
return;
}
if (role !== "controller") return ws.close();
const cid = newCid();
R.controllers.set(cid, ws);
addLog({ type: "controller_connected", room, data: { cid, ip } });
safeSend(ws, { type: "hello", role: "controller", room, cid });
safeSend(ws, { type: "renderer", online: !!R.renderer });
if (R.renderer) safeSend(R.renderer, { type: "connect", cid });
ws.on("message", (buf) => {
let msg; try { msg = JSON.parse(buf.toString()); } catch { return; }
// log some controller actions (privacy: keep it minimal)
if (msg?.type === "mode" || msg?.type === "start" || msg?.type === "ready" || msg?.type === "claim") {
addLog({ type: `controller_${msg.type}`, room, data: { cid, ...msg } });
}
if (R.renderer) safeSend(R.renderer, { type: "input", cid, msg });
});
ws.on("close", () => {
R.controllers.delete(cid);
addLog({ type: "controller_disconnected", room, data: { cid, ip } });
if (R.renderer) safeSend(R.renderer, { type: "disconnect", cid });
});
});
server.listen(PORT, () => console.log("Cloud listening on", PORT, "default room =", DEFAULT_ROOM));