File size: 13,956 Bytes
0f1b8ae
0325642
 
 
 
e20cd96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0325642
e20cd96
0325642
 
 
 
 
e20cd96
0325642
 
 
 
 
 
 
 
 
 
e20cd96
9962ce3
0325642
 
 
 
 
e20cd96
 
 
 
 
 
 
 
0325642
 
 
 
e20cd96
0325642
 
 
 
 
e20cd96
0325642
 
 
 
e20cd96
0325642
 
 
 
 
 
e20cd96
0325642
e20cd96
0325642
 
 
9962ce3
0325642
 
 
 
 
 
 
 
9962ce3
0325642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20cd96
0325642
 
 
e20cd96
0325642
e20cd96
 
 
 
 
 
 
 
 
0325642
 
 
 
e20cd96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0325642
 
 
 
 
e20cd96
0325642
 
 
 
e20cd96
 
 
0325642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20cd96
 
0325642
 
 
e20cd96
0325642
 
 
 
 
 
 
 
 
 
 
 
 
e20cd96
 
0325642
e20cd96
 
0325642
 
 
 
 
 
e20cd96
 
0325642
e20cd96
0325642
 
 
 
 
 
 
 
 
e20cd96
 
0325642
 
 
 
 
 
 
 
 
e20cd96
 
0325642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20cd96
0325642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20cd96
0325642
 
 
 
 
9962ce3
0325642
 
9962ce3
0325642
 
 
 
e20cd96
 
0325642
 
 
 
 
 
 
9962ce3
0325642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9962ce3
0325642
 
 
 
 
9962ce3
0325642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e20cd96
0325642
 
9962ce3
 
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const http = require('http');
const fs = require('fs');
const path = require('path');
const { WebSocketServer, WebSocket } = require('ws');
const { v4: uuidv4 } = require('uuid');
const crypto = require('crypto');

// ─── Auth ─────────────────────────────────────────────────────────────────────
// Secret key from environment variable "Key"
const SECRET = process.env.Key || '';
if (!SECRET) console.warn('[Auth] WARNING: Environment variable "Key" is not set. Admin login will be disabled.');

// HMAC-based session tokens: token = hmac(SECRET, nonce+timestamp)
// Sessions are stored server-side; possession of token alone is not enough β€”
// the server must have issued it.
const activeSessions = new Set(); // Set<token>

function createSession() {
  const nonce = crypto.randomBytes(32).toString('hex');
  const token = crypto.createHmac('sha256', SECRET).update(nonce).digest('hex');
  activeSessions.add(token);
  return token;
}

function isValidSession(token) {
  return typeof token === 'string' && activeSessions.has(token);
}

// ─── In-Memory State ──────────────────────────────────────────────────────────
const sounds = {};
const notifications = {};
const devices = {};

const deviceClients = {};
const adminClients = new Set(); // only authenticated WS connections

const MIME = {
  '.html': 'text/html',
  '.js':   'application/javascript',
  '.css':  'text/css',
  '.json': 'application/json',
  '.png':  'image/png',
  '.ico':  'image/x-icon',
};

// ─── HTTP File Server ─────────────────────────────────────────────────────────
const httpServer = http.createServer((req, res) => {
  let urlPath = req.url.split('?')[0];
  if (urlPath === '/' || urlPath === '') urlPath = '/index.html';
  if (urlPath === '/admin' || urlPath === '/admin/') urlPath = '/admin/index.html';

  const filePath = path.join(__dirname, 'public', urlPath);

  // Prevent path traversal
  const publicDir = path.resolve(__dirname, 'public');
  const resolved = path.resolve(filePath);
  if (!resolved.startsWith(publicDir)) {
    res.writeHead(403); res.end('Forbidden'); return;
  }

  const ext = path.extname(filePath);
  const mime = MIME[ext] || 'application/octet-stream';

  fs.readFile(filePath, (err, data) => {
    if (err) { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not found'); return; }
    res.writeHead(200, { 'Content-Type': mime });
    res.end(data);
  });
});

// ─── WebSocket Server ─────────────────────────────────────────────────────────
const wss = new WebSocketServer({ server: httpServer });

wss.on('connection', (ws, req) => {
  const urlPath = req.url || '/';
  if (urlPath.startsWith('/admin-ws')) {
    handleAdminConnection(ws);
  } else {
    handleDeviceConnection(ws);
  }
});

// ─── Helpers ──────────────────────────────────────────────────────────────────
function send(ws, msg) {
  if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg));
}

function broadcastAdmin(msg) {
  for (const ws of adminClients) send(ws, msg);
}

function broadcastDevice(uuid, msg) {
  const ws = deviceClients[uuid];
  if (ws) send(ws, msg);
}

function broadcastAllDevices(msg) {
  for (const uuid of Object.keys(deviceClients)) broadcastDevice(uuid, msg);
}

function getFullState() {
  return {
    sounds: Object.values(sounds),
    notifications: Object.values(notifications),
    devices: Object.values(devices).map(devicePublic),
  };
}

function devicePublic(d) {
  return {
    uuid: d.uuid,
    name: d.name,
    notifications: d.notifications,
    lastConnection: d.lastConnection,
    schedule: d.schedule || [],
    pendingChanges: d.pendingChanges || [],
    online: !!deviceClients[d.uuid],
    cachedSounds: d.cachedSounds || [],
  };
}

// ─── Admin Connection Handler ─────────────────────────────────────────────────
function handleAdminConnection(ws) {
  let authenticated = false;

  // Give them 10 seconds to authenticate, then close
  const authTimeout = setTimeout(() => {
    if (!authenticated) {
      send(ws, { type: 'auth_timeout' });
      ws.close();
    }
  }, 10000);

  ws.on('message', (raw) => {
    let msg;
    try { msg = JSON.parse(raw); } catch { return; }

    // ── Authentication handshake ──────────────────────────────────────────────
    if (!authenticated) {
      if (msg.type === 'auth_resume' && isValidSession(msg.token)) {
        // Client is resuming a valid session
        authenticated = true;
        clearTimeout(authTimeout);
        adminClients.add(ws);
        send(ws, { type: 'auth_ok', token: msg.token });
        send(ws, { type: 'full_state', ...getFullState() });
        console.log('[Admin] session resumed');
        return;
      }

      if (msg.type === 'auth_login') {
        if (!SECRET) {
          send(ws, { type: 'auth_error', reason: 'Server has no Key configured.' });
          return;
        }
        // Constant-time comparison to prevent timing attacks
        const provided = Buffer.from(String(msg.password || ''));
        const expected = Buffer.from(SECRET);
        const match = provided.length === expected.length &&
                      crypto.timingSafeEqual(provided, expected);
        if (match) {
          authenticated = true;
          clearTimeout(authTimeout);
          const token = createSession();
          adminClients.add(ws);
          send(ws, { type: 'auth_ok', token });
          send(ws, { type: 'full_state', ...getFullState() });
          console.log('[Admin] authenticated');
        } else {
          send(ws, { type: 'auth_error', reason: 'Invalid password.' });
          console.warn('[Admin] failed login attempt');
        }
        return;
      }

      // Any other message before auth: reject silently
      send(ws, { type: 'auth_required' });
      return;
    }

    // ── Authenticated messages ─────────────────────────────────────────────────
    handleAdminMessage(ws, msg);
  });

  ws.on('close', () => {
    adminClients.delete(ws);
    clearTimeout(authTimeout);
    console.log(`[Admin] disconnected, total=${adminClients.size}`);
  });

  ws.on('error', (e) => console.error('[Admin] WS error', e.message));

  // Prompt client to authenticate
  send(ws, { type: 'auth_required' });
}

function handleAdminMessage(ws, msg) {
  switch (msg.type) {

    case 'create_sound': {
      const id = uuidv4();
      const sound = { id, name: msg.name, data: msg.data };
      sounds[id] = sound;
      broadcastAdmin({ type: 'sound_added', sound });
      break;
    }

    case 'create_notification': {
      const id = uuidv4();
      const notif = {
        id, name: msg.name, heading: msg.heading, body: msg.body,
        hyperlink: msg.hyperlink || '', displayed: false, soundId: msg.soundId || null,
      };
      notifications[id] = notif;
      for (const uuid of Object.keys(devices)) {
        if (!devices[uuid].notifications.find(n => n.id === id))
          devices[uuid].notifications.push({ ...notif });
      }
      broadcastAdmin({ type: 'notification_added', notification: notif });
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      broadcastAllDevices({ type: 'notification_added', notification: notif });
      break;
    }

    case 'schedule_notification': {
      const device = devices[msg.uuid];
      if (!device) return;
      device.schedule = device.schedule || [];
      const existing = device.schedule.find(s => s.notificationId === msg.notificationId);
      if (existing) { existing.scheduledAt = msg.scheduledAt; }
      else { device.schedule.push({ notificationId: msg.notificationId, scheduledAt: msg.scheduledAt }); }
      const scheduleMsg = { type: 'schedule_update', schedule: device.schedule };
      if (deviceClients[msg.uuid]) { broadcastDevice(msg.uuid, scheduleMsg); }
      else { (device.pendingChanges = device.pendingChanges || []).push(scheduleMsg); }
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      break;
    }

    case 'play_now': {
      const playMsg = { type: 'play_now', notificationId: msg.notificationId };
      if (deviceClients[msg.uuid]) { broadcastDevice(msg.uuid, playMsg); }
      else {
        const device = devices[msg.uuid];
        if (device) (device.pendingChanges = device.pendingChanges || []).push(playMsg);
      }
      break;
    }

    case 'update_device_name': {
      const device = devices[msg.uuid];
      if (!device) return;
      device.name = msg.name;
      const nameMsg = { type: 'name_update', name: msg.name };
      if (deviceClients[msg.uuid]) { broadcastDevice(msg.uuid, nameMsg); }
      else { (device.pendingChanges = device.pendingChanges || []).push(nameMsg); }
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      break;
    }

    case 'remove_schedule': {
      const device = devices[msg.uuid];
      if (!device) return;
      device.schedule = (device.schedule || []).filter(s => s.notificationId !== msg.notificationId);
      const rmMsg = { type: 'schedule_update', schedule: device.schedule };
      if (deviceClients[msg.uuid]) { broadcastDevice(msg.uuid, rmMsg); }
      else { (device.pendingChanges = device.pendingChanges || []).push(rmMsg); }
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      break;
    }

    case 'request_sound': {
      const sound = sounds[msg.id];
      if (sound) send(ws, { type: 'sound_data', sound });
      break;
    }

    default:
      console.warn('[Admin] unknown message type:', msg.type);
  }
}

// ─── Device Connection Handler ────────────────────────────────────────────────
function handleDeviceConnection(ws) {
  let deviceUUID = null;

  ws.on('message', (raw) => {
    let msg;
    try { msg = JSON.parse(raw); } catch { return; }

    if (msg.type === 'hello') {
      if (msg.uuid && devices[msg.uuid]) {
        deviceUUID = msg.uuid;
      } else {
        deviceUUID = msg.uuid || uuidv4();
        devices[deviceUUID] = {
          uuid: deviceUUID,
          name: `Device ${Object.keys(devices).length + 1}`,
          notifications: Object.values(notifications).map(n => ({ ...n })),
          lastConnection: null, schedule: [], pendingChanges: [],
        };
      }

      deviceClients[deviceUUID] = ws;
      const device = devices[deviceUUID];
      device.lastConnection = null;

      if (device.pendingChanges && device.pendingChanges.length > 0) {
        for (const change of device.pendingChanges) send(ws, change);
        device.pendingChanges = [];
      }

      send(ws, {
        type: 'device_init', uuid: deviceUUID,
        notifications: device.notifications, schedule: device.schedule, name: device.name,
      });

      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      console.log(`[Device] ${deviceUUID} connected (${device.name})`);
      return;
    }

    if (!deviceUUID) return;
    handleDeviceMessage(ws, deviceUUID, msg);
  });

  ws.on('close', () => {
    if (deviceUUID && devices[deviceUUID]) {
      devices[deviceUUID].lastConnection = Date.now();
      delete deviceClients[deviceUUID];
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      console.log(`[Device] ${deviceUUID} disconnected`);
    }
  });

  ws.on('error', (e) => console.error('[Device] WS error', e.message));
}

function handleDeviceMessage(ws, uuid, msg) {
  const device = devices[uuid];
  if (!device) return;

  switch (msg.type) {
    case 'mark_displayed': {
      const notif = device.notifications.find(n => n.id === msg.notificationId);
      if (notif) notif.displayed = true;
      if (notifications[msg.notificationId]) notifications[msg.notificationId].displayed = true;
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      break;
    }
    case 'request_sound': {
      const sound = sounds[msg.soundId];
      if (sound) send(ws, { type: 'sound_data', sound });
      break;
    }
    case 'sync_schedule': {
      device.schedule = msg.schedule || [];
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      break;
    }
    case 'cached_sounds': {
      device.cachedSounds = msg.soundIds || [];
      broadcastAdmin({ type: 'devices_updated', devices: Object.values(devices).map(devicePublic) });
      break;
    }
    default:
      console.warn('[Device] unknown message type:', msg.type);
  }
}

// ─── Start ────────────────────────────────────────────────────────────────────
const PORT = process.env.PORT || 7860;
httpServer.listen(PORT, () => {
  console.log(`Server running on http://0.0.0.0:${PORT}`);
});