-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwol-server.js
342 lines (294 loc) · 9.78 KB
/
wol-server.js
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
// Lightweight standalone Wake on LAN server for Raspberry Pi or other lightweight Linux devices
const express = require('express');
const cors = require('cors');
const wol = require('wake_on_lan');
const dotenv = require('dotenv');
const jwt = require('jsonwebtoken');
const fs = require('fs').promises;
const path = require('path');
const { execSync } = require('child_process');
// Load environment variables
dotenv.config();
const app = express();
const PORT = process.env.PORT || 8080;
const JWT_SECRET = process.env.JWT_SECRET || 'your_jwt_secret_change_this_in_production';
const ALLOWED_ORIGINS = process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS.split(',') : ['*'];
const DEVICES_FILE = path.join(__dirname, 'devices.json');
// Basic configuration
app.use(express.json());
// Configure CORS with specific origins
app.use(cors({
origin: function(origin, callback) {
// Allow requests with no origin (like mobile apps, curl, etc)
if (!origin) return callback(null, true);
if (ALLOWED_ORIGINS.includes('*') || ALLOWED_ORIGINS.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true
}));
// Simple authentication middleware
const auth = async (req, res, next) => {
try {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) {
return res.status(401).send({ error: 'Authentication required' });
}
const decoded = jwt.verify(token, JWT_SECRET);
// We're not querying a database here, just checking if the token is valid
// In a production environment, you might want to implement a proper user system
req.user = { id: decoded.id };
req.token = token;
next();
} catch (error) {
res.status(401).send({ error: 'Authentication failed' });
}
};
// Utility function to read devices from file
const readDevices = async () => {
try {
const data = await fs.readFile(DEVICES_FILE, 'utf8');
return JSON.parse(data);
} catch (error) {
// If file doesn't exist, return empty array
if (error.code === 'ENOENT') {
return [];
}
throw error;
}
};
// Utility function to write devices to file
const writeDevices = async (devices) => {
await fs.writeFile(DEVICES_FILE, JSON.stringify(devices, null, 2), 'utf8');
};
// Status endpoint
app.get('/api/status', (req, res) => {
res.send({
status: 'online',
message: 'WOL Server is running',
version: '1.0.0'
});
});
// Network scanning endpoint
app.get('/api/network/scan', auth, async (req, res) => {
try {
let discoveredDevices = [];
try {
// Prefer arp-scan if available
try {
const arpScan = execSync('arp-scan -l').toString();
const lines = arpScan.split('\n');
for (const line of lines) {
// Parse arp-scan output
const match = line.match(/(\d+\.\d+\.\d+\.\d+)\s+([0-9a-f:]+)/i);
if (match) {
discoveredDevices.push({
ipAddress: match[1],
macAddress: match[2],
isOnline: true,
name: `Device (${match[1]})` // Optional: provide a default name
});
}
}
} catch (arpScanErr) {
// Fallback to arp command
const arpTable = execSync('arp -a').toString();
const lines = arpTable.split('\n');
for (const line of lines) {
// Parse arp -a output
const match = line.match(/(\d+\.\d+\.\d+\.\d+)\s+([0-9a-f-]+)/i);
if (match) {
const mac = match[2].replace(/-/g, ':');
discoveredDevices.push({
ipAddress: match[1],
macAddress: mac,
isOnline: true,
name: `Device (${match[1]})` // Optional: provide a default name
});
}
}
}
res.send(discoveredDevices);
} catch (err) {
console.error('Network scan error:', err);
res.status(500).send({
error: 'Failed to perform network scan',
details: err.message
});
}
} catch (error) {
console.error('Complete network scan error:', error);
res.status(500).send({ error: error.message });
}
});
// Devices endpoint - List all devices
app.get('/api/devices', auth, async (req, res) => {
try {
const devices = await readDevices();
res.send(devices);
} catch (error) {
console.error('Error reading devices:', error);
res.status(500).send({ error: 'Failed to retrieve devices' });
}
});
// Add a new device
app.post('/api/devices', auth, async (req, res) => {
try {
const { name, macAddress, ipAddress, tags = [] } = req.body;
// Validate required fields
if (!macAddress) {
return res.status(400).send({ error: 'MAC address is required' });
}
// Validate MAC address format
const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
if (!macRegex.test(macAddress)) {
return res.status(400).send({ error: 'Invalid MAC address format' });
}
// Read existing devices
const devices = await readDevices();
// Check if device already exists
const existingDeviceIndex = devices.findIndex(
d => d.macAddress.toLowerCase() === macAddress.toLowerCase()
);
if (existingDeviceIndex !== -1) {
// Update existing device
devices[existingDeviceIndex] = {
...devices[existingDeviceIndex],
name: name || devices[existingDeviceIndex].name,
ipAddress: ipAddress || devices[existingDeviceIndex].ipAddress,
tags: [...new Set([...devices[existingDeviceIndex].tags, ...tags])]
};
} else {
// Add new device
devices.push({
id: Date.now().toString(), // Simple unique ID
name: name || `Device (${macAddress})`,
macAddress,
ipAddress,
tags,
isOnline: false,
createdAt: new Date().toISOString()
});
}
// Write updated devices
await writeDevices(devices);
// Return the added/updated device
const savedDevice = devices.find(
d => d.macAddress.toLowerCase() === macAddress.toLowerCase()
);
res.status(201).send(savedDevice);
} catch (error) {
console.error('Error adding device:', error);
res.status(500).send({ error: 'Failed to add device' });
}
});
// Update a device
app.patch('/api/devices/:id', auth, async (req, res) => {
try {
const { id } = req.params;
const updateFields = req.body;
// Read existing devices
const devices = await readDevices();
// Find the device
const deviceIndex = devices.findIndex(d => d.id === id);
if (deviceIndex === -1) {
return res.status(404).send({ error: 'Device not found' });
}
// Update device
devices[deviceIndex] = {
...devices[deviceIndex],
...updateFields
};
// Write updated devices
await writeDevices(devices);
res.send(devices[deviceIndex]);
} catch (error) {
console.error('Error updating device:', error);
res.status(500).send({ error: 'Failed to update device' });
}
});
// Delete a device
app.delete('/api/devices/:id', auth, async (req, res) => {
try {
const { id } = req.params;
// Read existing devices
let devices = await readDevices();
// Filter out the device
const initialLength = devices.length;
devices = devices.filter(d => d.id !== id);
if (devices.length === initialLength) {
return res.status(404).send({ error: 'Device not found' });
}
// Write updated devices
await writeDevices(devices);
res.send({ message: 'Device deleted successfully' });
} catch (error) {
console.error('Error deleting device:', error);
res.status(500).send({ error: 'Failed to delete device' });
}
});
// Wake on LAN endpoint
app.post('/api/wake', auth, async (req, res) => {
try {
const { macAddress, broadcastAddress = '255.255.255.255', port = 9 } = req.body;
if (!macAddress) {
return res.status(400).send({ error: 'MAC address is required' });
}
// Validate MAC address format (basic validation)
const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
if (!macRegex.test(macAddress)) {
return res.status(400).send({ error: 'Invalid MAC address format' });
}
wol.wake(macAddress, {
address: broadcastAddress,
port: port
}, (error) => {
if (error) {
return res.status(500).send({ error: 'Failed to send WOL packet' });
}
res.send({
message: 'Wake packet sent successfully',
macAddress,
broadcastAddress,
port
});
});
} catch (error) {
console.error('Error sending wake packet:', error);
res.status(500).send({ error: error.message });
}
});
// Wake with device ID endpoint for compatibility with main app
app.post('/api/devices/:id/wake', auth, async (req, res) => {
try {
// Read devices to find the device by ID
const devices = await readDevices();
const device = devices.find(d => d.id === req.params.id);
if (!device) {
return res.status(404).send({ error: 'Device not found' });
}
wol.wake(device.macAddress, {
address: device.broadcastAddress || '255.255.255.255',
port: device.port || 9
}, (error) => {
if (error) {
return res.status(500).send({ error: 'Failed to send WOL packet' });
}
res.send({
message: 'Wake packet sent successfully',
macAddress: device.macAddress,
broadcastAddress: device.broadcastAddress || '255.255.255.255',
port: device.port || 9
});
});
} catch (error) {
console.error('Error sending wake packet:', error);
res.status(500).send({ error: error.message });
}
});
// Start server
app.listen(PORT, () => {
console.log(`WOL Server running on port ${PORT}`);
});