forked from AidanNelson/threejs-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
349 lines (307 loc) · 11.7 KB
/
Copy pathserver.js
File metadata and controls
349 lines (307 loc) · 11.7 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
*
* This is the server which runs all Websocket and WebRTC communications for our application.
*
*/
const express = require("express");
const { Readable } = require("stream");
const app = express();
let db;
let userCounter = 1;
const worldObjects = [];
db = {
find: (query, callback) => callback(null, worldObjects.slice()),
insert: (doc, callback) => {
worldObjects.push(doc);
if (callback) callback(null, doc);
},
deleteAll: (callback) => {
worldObjects.length = 0;
if(callback) callback(null);
}
};
app.use(express.static("public"));
const port = process.env.PORT || 8080;
const server = app.listen(port, "0.0.0.0", () => {
console.log(`Webserver is running on http://0.0.0.0:${port}`);
});
const io = require("socket.io")().listen(server);
let peers = {};
const adminSockets = new Set();
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || "gazpacho";
const availableMaps = {
"Resort": {
url: "https://gustavochico.com/paseito/resort.glb",
startPosition: [-100, 75, 295],
skyColors: ['#1a94c4', '#2fc1fe', '#212324ff'],
ambientTrack: "https://gustavochico.com/paseito/ambient_resort.mp3"
},
"Wind Waker": {
url: "https://gustavochico.com/paseito/windWaker.glb",
startPosition: [0, 0, 0],
skyColors: ['#1a94c4', '#2fc1fe', '#a0d8ef']
},
"Dust2": {
url: "https://gustavochico.com/paseito/dedust2.glb",
startPosition: [80, 850, 0],
skyColors: ['#5c6e80', '#829ab1', '#d9e2ec']
},
"Rainbow Road": {
url: "https://gustavochico.com/paseito/rainbowRoad.glb",
startPosition: [1210, 615, 580],
skyColors: ['#000000ff', '#173b5cff', '#000000ff']
},
"Shinobi Earth": {
url: "https://gustavochico.com/paseito/shinobi.glb",
startPosition: [0, 0, 0],
skyColors: ['#1a94c4', '#2fc1fe', '#212324ff']
},
"The Catacombs": {
url: "https://gustavochico.com/paseito/catacombs.glb",
startPosition: [10, 0, 100],
skyColors: ['#2b2e2fff', '#212324ff', '#131414ff']
},
"Anor Londo PELIGRO NO ES BROMA": {
url: "https://gustavochico.com/paseito/anorLondo.glb",
startPosition: [0, 0, 0],
skyColors: ['#1a94c4', '#2fc1fe', '#212324ff']
}
};
for (const [name, map] of Object.entries(availableMaps)) {
const encodedName = encodeURIComponent(name);
map.sourceUrl = map.url;
map.url = `/api/maps/${encodedName}/model`;
if (map.ambientTrack) {
map.ambientSourceUrl = map.ambientTrack;
map.ambientTrack = `/api/maps/${encodedName}/ambient`;
}
}
const fallbackMap = availableMaps.Resort.url;
async function proxyMapAsset(req, res, assetKey, contentType) {
const map = availableMaps[req.params.name];
const sourceUrl = map?.[assetKey];
if (!sourceUrl) {
res.status(404).send("Map asset not found");
return;
}
try {
const upstream = await fetch(sourceUrl, {
headers: {
"Accept": "application/octet-stream,*/*",
"User-Agent": "undici"
}
});
if (!upstream.ok || !upstream.body) {
res.status(upstream.status || 502).send("Unable to fetch map asset");
return;
}
res.setHeader("Cache-Control", "public, max-age=86400");
res.setHeader("Content-Type", contentType);
Readable.fromWeb(upstream.body).pipe(res);
} catch (error) {
console.error(`Failed to proxy ${sourceUrl}:`, error);
res.status(502).send("Unable to fetch map asset");
}
}
app.get("/api/maps/:name/model", (req, res) => {
proxyMapAsset(req, res, "sourceUrl", "model/gltf-binary");
});
app.get("/api/maps/:name/ambient", (req, res) => {
proxyMapAsset(req, res, "ambientSourceUrl", "audio/mpeg");
});
let serverState = {
currentMapUrl: availableMaps["Resort"].url, // Store URL for simplicity
availableMaps: availableMaps,
fallbackMap: fallbackMap,
voiceDistanceMultiplier: 1.0,
playerScale: 1.0,
maxSpeed: 2000,
acceleration: 600,
fallbackAmbientTrack: "https://gustavochico.com/paseito/ambient_resort.mp3"
};
const defaultServerState = { ...serverState };
const numericSettings = {
voiceDistanceMultiplier: { min: 0.25, max: 8 },
playerScale: { min: 0.2, max: 3 },
maxSpeed: { min: 100, max: 2500 },
acceleration: { min: 100, max: 2500 }
};
function clampNumber(value, min, max) {
const number = Number(value);
if (!Number.isFinite(number)) return null;
return Math.min(max, Math.max(min, number));
}
function isVector3(value) {
return Array.isArray(value) && value.length === 3 && value.every((number) => Number.isFinite(number) && Math.abs(number) < 1_000_000);
}
function isQuaternion(value) {
return Array.isArray(value) && value.length === 4 && value.every(Number.isFinite);
}
function sanitizeWorldData(data) {
if (!data || data.type !== "sign" || !data.data) return null;
const text = typeof data.data.text === "string" ? data.data.text.trim().slice(0, 180) : "";
if (!text || !isVector3(data.data.position) || !isQuaternion(data.data.rotation)) return null;
return {
type: "sign",
data: {
position: data.data.position,
rotation: data.data.rotation,
text
}
};
}
function requireAdmin(socket, action) {
if (adminSockets.has(socket.id)) return true;
console.warn(`Rejected admin command "${action}" from unauthenticated socket ${socket.id}`);
socket.emit("serverMessage", "Admin command rejected. Unlock the admin panel first.");
return false;
}
function getPeerSnapshot() {
return {
peers: Object.entries(peers).map(([id, peer]) => ({
id,
name: peer.name,
position: peer.position,
isShouting: !!peer.isShouting
})),
state: serverState,
objectCount: null
};
}
function main() {
setupSocketServer();
setInterval(() => {
io.sockets.emit("positions", peers);
}, 100);
}
main();
function setupSocketServer() {
io.on("connection", (socket) => {
console.log(`Peer joined with ID ${socket.id}. There are ${io.engine.clientsCount} peer(s) connected.`);
// Assign a sequential name to the new user
const userName = "User " + userCounter++;
peers[socket.id] = {
position: [0, 0.5, 0],
rotation: [0, 0, 0, 1],
name: userName,
isShouting: false
};
// Send the entire peers object with names and current server state
socket.emit("introduction", { peers: peers, state: serverState });
db.find({}, (err, docs) => {
if (err) return console.error("DB find error:", err);
if (docs) docs.forEach(doc => socket.emit("data", doc));
});
// Send new peer's data (including name) to others
socket.broadcast.emit("peerConnection", socket.id, peers[socket.id]);
socket.on("move", (data) => {
// Add validation to prevent server state corruption
if (peers[socket.id] && data && isVector3(data.position) && isQuaternion(data.rotation)) {
peers[socket.id].position = data.position;
peers[socket.id].rotation = data.rotation;
peers[socket.id].isShouting = !!data.isShouting;
}
});
socket.on("data", (data) => {
const sanitized = sanitizeWorldData(data);
if (!sanitized) return;
db.insert(sanitized, (err) => {
if (err) return console.error("DB insert error:", err);
io.sockets.emit("data", sanitized);
});
});
socket.on("signal", (to, from, data) => {
if (to in peers) io.to(to).emit("signal", to, from, data);
});
socket.on("admin:auth", (password, callback) => {
const ok = password === ADMIN_PASSWORD;
if (ok) {
adminSockets.add(socket.id);
console.log(`Admin unlocked for ${socket.id}`);
} else {
console.warn(`Failed admin unlock attempt from ${socket.id}`);
}
if (callback) callback({ ok, snapshot: ok ? getPeerSnapshot() : null });
});
socket.on("admin:getSnapshot", (callback) => {
if (!requireAdmin(socket, "getSnapshot")) return;
db.find({}, (err, docs) => {
const snapshot = getPeerSnapshot();
snapshot.objectCount = err || !docs ? null : docs.length;
if (callback) callback(snapshot);
});
});
socket.on("admin:deleteAllObjects", () => {
if (!requireAdmin(socket, "deleteAllObjects")) return;
console.log(`Admin command: deleteAllObjects received from ${socket.id}`);
db.deleteAll((err) => {
if (err) return console.error("DB deleteAll error:", err);
io.sockets.emit("clearAllObjects");
});
});
socket.on("admin:changeMap", (mapUrl) => {
if (!requireAdmin(socket, "changeMap")) return;
const isValidMap = Object.values(availableMaps).some(map => map.url === mapUrl);
if (isValidMap) {
console.log(`Admin command: changeMap to ${mapUrl} from ${socket.id}`);
serverState.currentMapUrl = mapUrl;
io.sockets.emit("changeMap", mapUrl);
}
});
socket.on("admin:updateSetting", ({ key, value }) => {
if (!requireAdmin(socket, "updateSetting")) return;
console.log(`Admin command: updateSetting ${key} to ${value} from ${socket.id}`);
if (key in numericSettings) {
const range = numericSettings[key];
const parsed = clampNumber(value, range.min, range.max);
if (parsed === null) return;
serverState[key] = parsed;
io.sockets.emit("updateSetting", { key, value: serverState[key] });
}
});
socket.on("admin:broadcastMessage", (message) => {
if (!requireAdmin(socket, "broadcastMessage")) return;
if (typeof message !== "string" || !message.trim()) return;
console.log(`Admin command: broadcast "${message}" from ${socket.id}`);
io.sockets.emit("serverMessage", `ADMIN: ${message.trim().slice(0, 300)}`);
});
socket.on("admin:teleportAllToMe", () => {
if (!requireAdmin(socket, "teleportAllToMe")) return;
console.log(`Admin command: teleportAllToMe from ${socket.id}`);
if (peers[socket.id]) {
const adminPosition = peers[socket.id].position;
for (const id in peers) {
if (id !== socket.id) { // Don't teleport the admin
peers[id].position = adminPosition.slice(); // Use slice to create a copy
}
}
io.sockets.emit("positions", peers);
}
});
socket.on("admin:respawnAll", () => {
if (!requireAdmin(socket, "respawnAll")) return;
const currentMap = Object.values(availableMaps).find(map => map.url === serverState.currentMapUrl) || availableMaps.Resort;
for (const id in peers) {
peers[id].position = currentMap.startPosition.slice();
}
io.sockets.emit("positions", peers);
io.sockets.emit("serverMessage", "ADMIN: Everyone was sent back to spawn.");
});
socket.on("admin:resetSettings", () => {
if (!requireAdmin(socket, "resetSettings")) return;
serverState.voiceDistanceMultiplier = defaultServerState.voiceDistanceMultiplier;
serverState.playerScale = defaultServerState.playerScale;
serverState.maxSpeed = defaultServerState.maxSpeed;
serverState.acceleration = defaultServerState.acceleration;
for (const key of Object.keys(numericSettings)) {
io.sockets.emit("updateSetting", { key, value: serverState[key] });
}
});
socket.on("disconnect", () => {
delete peers[socket.id];
adminSockets.delete(socket.id);
io.sockets.emit("peerDisconnection", socket.id);
console.log(`Peer ${socket.id} disconnected, there are ${io.engine.clientsCount} peer(s) connected.`);
});
});
}