-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
342 lines (298 loc) · 10.3 KB
/
server.js
File metadata and controls
342 lines (298 loc) · 10.3 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const dgram = require('dgram');
const crypto = require('crypto');
const app = express();
const PORT = process.env.PORT || 3000;
const DEVICES_FILE = process.env.DEVICES_FILE || '/config/devices.json';
const CONFIG_FILE = process.env.CONFIG_FILE || '/config/config.json';
const SESSION_TTL = (parseInt(process.env.SESSION_TTL_HOURS) || 8) * 60 * 60 * 1000;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
function loadConfig() {
try {
if (!fs.existsSync(CONFIG_FILE)) {
const dir = path.dirname(CONFIG_FILE);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(CONFIG_FILE, JSON.stringify({ pinHash: null }, null, 2));
}
return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
} catch {
return { pinHash: null };
}
}
function saveConfig(config) {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}
function loadDevices() {
try {
if (!fs.existsSync(DEVICES_FILE)) {
const dir = path.dirname(DEVICES_FILE);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(DEVICES_FILE, '[]');
}
return JSON.parse(fs.readFileSync(DEVICES_FILE, 'utf8'));
} catch {
return [];
}
}
function saveDevices(devices) {
fs.writeFileSync(DEVICES_FILE, JSON.stringify(devices, null, 2));
}
function isValidIPv4(ip) {
return /^(\d{1,3}\.){3}\d{1,3}$/.test(ip) && ip.split('.').every((n) => parseInt(n) <= 255);
}
function normalizeMAC(mac) {
const clean = mac.replace(/[^0-9A-Fa-f]/g, '');
if (clean.length !== 12) throw new Error('Invalid MAC address');
return clean.toUpperCase().match(/.{2}/g).join(':');
}
function sendMagicPacket(mac, broadcastAddress = '255.255.255.255', port = 9) {
return new Promise((resolve, reject) => {
const macClean = mac.replace(/[:-]/g, '');
const buffer = Buffer.alloc(102);
for (let i = 0; i < 6; i++) buffer[i] = 0xff;
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 6; j++) {
buffer[6 + i * 6 + j] = parseInt(macClean.slice(j * 2, j * 2 + 2), 16);
}
}
const socket = dgram.createSocket('udp4');
socket.once('error', (err) => { socket.close(); reject(err); });
socket.bind(() => {
socket.setBroadcast(true);
socket.send(buffer, 0, buffer.length, port, broadcastAddress, (err) => {
socket.close();
if (err) reject(err);
else resolve();
});
});
});
}
const sessions = new Map();
function hashPin(pin) {
return crypto.createHash('sha256').update(String(pin)).digest('hex');
}
function createSession() {
const token = crypto.randomBytes(32).toString('hex');
sessions.set(token, Date.now() + SESSION_TTL);
if (sessions.size > 1000) {
const now = Date.now();
for (const [k, exp] of sessions) if (now > exp) sessions.delete(k);
}
return token;
}
function isValidSession(token) {
if (!token) return false;
const exp = sessions.get(token);
if (!exp) return false;
if (Date.now() > exp) { sessions.delete(token); return false; }
return true;
}
function deleteSession(token) {
sessions.delete(token);
}
const loginAttempts = new Map();
const MAX_ATTEMPTS = 5;
const LOCK_MS = 5 * 60 * 1000;
function getIP(req) {
return req.headers['x-forwarded-for']?.split(',')[0].trim() || req.socket.remoteAddress || 'unknown';
}
function checkRateLimit(ip) {
const e = loginAttempts.get(ip);
if (!e) return { allowed: true };
if (e.lockedUntil && Date.now() < e.lockedUntil) {
return { allowed: false, remaining: Math.ceil((e.lockedUntil - Date.now()) / 1000) };
}
return { allowed: true };
}
function recordFail(ip) {
const e = loginAttempts.get(ip) || { count: 0 };
e.count++;
if (e.count >= MAX_ATTEMPTS) {
e.lockedUntil = Date.now() + LOCK_MS;
e.count = 0;
}
loginAttempts.set(ip, e);
}
function clearFails(ip) {
loginAttempts.delete(ip);
}
function requireAuth(req, res, next) {
const config = loadConfig();
if (!config.pinHash) return next();
const token = req.headers['x-session-token'];
if (isValidSession(token)) return next();
res.status(401).json({ error: 'Unauthorized', requiresPin: true });
}
app.get('/api/auth/status', (req, res) => {
const config = loadConfig();
const token = req.headers['x-session-token'];
res.json({
pinEnabled: !!config.pinHash,
authenticated: !config.pinHash || isValidSession(token),
});
});
app.post('/api/auth/login', (req, res) => {
const ip = getIP(req);
const rl = checkRateLimit(ip);
if (!rl.allowed) {
return res.status(429).json({ error: `Too many attempts. Try again in ${rl.remaining}s.` });
}
const config = loadConfig();
if (!config.pinHash) {
return res.json({ token: createSession() });
}
const { pin } = req.body;
if (!pin || hashPin(pin) !== config.pinHash) {
recordFail(ip);
const after = checkRateLimit(ip);
return res.status(401).json({
error: 'Incorrect PIN',
attemptsLeft: after.allowed ? Math.max(0, MAX_ATTEMPTS - (loginAttempts.get(ip)?.count || 0)) : 0,
locked: !after.allowed,
});
}
clearFails(ip);
res.json({ token: createSession() });
});
app.post('/api/auth/logout', (req, res) => {
deleteSession(req.headers['x-session-token']);
res.json({ success: true });
});
app.post('/api/auth/pin', requireAuth, (req, res) => {
const { newPin, currentPin } = req.body;
const config = loadConfig();
if (config.pinHash) {
if (!currentPin || hashPin(String(currentPin)) !== config.pinHash) {
return res.status(401).json({ error: 'Current PIN is incorrect' });
}
}
if (!newPin) {
config.pinHash = null;
saveConfig(config);
return res.json({ success: true, pinEnabled: false });
}
const pinStr = String(newPin);
if (!/^\d{4,8}$/.test(pinStr)) {
return res.status(400).json({ error: 'PIN must be 4-8 digits' });
}
config.pinHash = hashPin(pinStr);
saveConfig(config);
res.json({ success: true, pinEnabled: true });
});
app.get('/api/devices', requireAuth, (req, res) => {
res.json(loadDevices());
});
app.post('/api/devices', requireAuth, (req, res) => {
const { name, mac, ip, broadcastAddress } = req.body;
if (!name?.trim() || !mac) {
return res.status(400).json({ error: 'Name and MAC address are required' });
}
if (name.trim().length > 64) {
return res.status(400).json({ error: 'Name must be 64 characters or fewer' });
}
let normalizedMAC;
try { normalizedMAC = normalizeMAC(mac); }
catch { return res.status(400).json({ error: 'Invalid MAC address format' }); }
const trimmedIP = ip?.trim() || '';
if (trimmedIP && !isValidIPv4(trimmedIP)) {
return res.status(400).json({ error: 'Invalid IP address' });
}
const trimmedBroadcast = broadcastAddress?.trim() || '255.255.255.255';
if (!isValidIPv4(trimmedBroadcast)) {
return res.status(400).json({ error: 'Invalid broadcast address' });
}
const devices = loadDevices();
const device = {
id: crypto.randomUUID(),
name: name.trim(),
mac: normalizedMAC,
ip: trimmedIP,
broadcastAddress: trimmedBroadcast,
createdAt: new Date().toISOString(),
};
devices.push(device);
saveDevices(devices);
res.status(201).json(device);
});
app.put('/api/devices/:id', requireAuth, (req, res) => {
const { name, mac, ip, broadcastAddress } = req.body;
const devices = loadDevices();
const idx = devices.findIndex((d) => d.id === req.params.id);
if (idx === -1) return res.status(404).json({ error: 'Device not found' });
if (!name?.trim() || !mac) {
return res.status(400).json({ error: 'Name and MAC address are required' });
}
if (name.trim().length > 64) {
return res.status(400).json({ error: 'Name must be 64 characters or fewer' });
}
let normalizedMAC;
try { normalizedMAC = normalizeMAC(mac); }
catch { return res.status(400).json({ error: 'Invalid MAC address format' }); }
const trimmedIP = ip?.trim() || '';
if (trimmedIP && !isValidIPv4(trimmedIP)) {
return res.status(400).json({ error: 'Invalid IP address' });
}
const trimmedBroadcast = broadcastAddress?.trim() || '255.255.255.255';
if (!isValidIPv4(trimmedBroadcast)) {
return res.status(400).json({ error: 'Invalid broadcast address' });
}
devices[idx] = {
...devices[idx],
name: name.trim(),
mac: normalizedMAC,
ip: trimmedIP,
broadcastAddress: trimmedBroadcast,
updatedAt: new Date().toISOString(),
};
saveDevices(devices);
res.json(devices[idx]);
});
app.delete('/api/devices/:id', requireAuth, (req, res) => {
const devices = loadDevices();
const filtered = devices.filter((d) => d.id !== req.params.id);
if (filtered.length === devices.length) {
return res.status(404).json({ error: 'Device not found' });
}
saveDevices(filtered);
res.json({ success: true });
});
app.post('/api/wake/:id', requireAuth, async (req, res) => {
const devices = loadDevices();
const device = devices.find((d) => d.id === req.params.id);
if (!device) return res.status(404).json({ error: 'Device not found' });
try {
await sendMagicPacket(device.mac, device.broadcastAddress || '255.255.255.255');
res.json({ success: true, message: `Magic packet sent to ${device.name}` });
} catch (err) {
res.status(500).json({ error: `Failed to send packet: ${err.message}` });
}
});
app.post('/api/wake/mac/:mac', requireAuth, async (req, res) => {
const { broadcastAddress = '255.255.255.255' } = req.body || {};
if (!isValidIPv4(broadcastAddress)) {
return res.status(400).json({ error: 'Invalid broadcast address' });
}
let normalizedMAC;
try { normalizedMAC = normalizeMAC(req.params.mac); }
catch { return res.status(400).json({ error: 'Invalid MAC address' }); }
try {
await sendMagicPacket(normalizedMAC, broadcastAddress);
res.json({ success: true, message: `Magic packet sent to ${normalizedMAC}` });
} catch (err) {
res.status(500).json({ error: `Failed to send packet: ${err.message}` });
}
});
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', devices: loadDevices().length });
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`Wake on LAN app running on http://0.0.0.0:${PORT}`);
console.log(`Devices file: ${DEVICES_FILE}`);
console.log(`Config file: ${CONFIG_FILE}`);
});