82 lines
2.2 KiB
JavaScript
82 lines
2.2 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import { sendWolPacket } from './wol.js';
|
|
import targets from "./targets.json" with { type: "json" };
|
|
|
|
const app = express();
|
|
const port = 9999;
|
|
|
|
app.use(cors()).use(express.json());
|
|
|
|
app.get("/", (req, res) => {
|
|
let html = `<!DOCTYPE html><head>
|
|
<style>
|
|
body { background: #fff; color: #000; }
|
|
@media (prefers-color-scheme: dark) {
|
|
body { background: #111; color: #eee; }
|
|
a { color: #7295f6; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h2 style="margin-bottom: 5px">Targets:</h2>
|
|
<ul style="margin: 0; padding-left: 0; list-style-position: inside">
|
|
${Object.keys(targets).map(key => `<li><a href="/wake/${key}">${key}</a></li>`).join("")}
|
|
</ul>
|
|
</body>`;
|
|
res.send(html);
|
|
});
|
|
|
|
app.get("/targets", (req, res) => {
|
|
res.json(Object.keys(targets));
|
|
});
|
|
|
|
app.post("/wake", (req, res) => {
|
|
const { mac } = req.body;
|
|
|
|
if(!mac) {
|
|
return res.status(400).json({ status: 'error', message: 'MAC address required' });
|
|
}
|
|
|
|
sendWolPacket(mac)
|
|
.then(() => res.json({ status: "ok" }))
|
|
.catch(err => {
|
|
console.error(err);
|
|
res.status(500).json({ status: "error", message: "Failed to send WOL packet" })
|
|
});
|
|
});
|
|
|
|
app.get("/wake", (req, res) => {
|
|
const mac = req.query.mac;
|
|
|
|
if(!mac) {
|
|
return res.status(400).json({ status: 'error', message: 'MAC address required' });
|
|
}
|
|
|
|
sendWolPacket(mac)
|
|
.then(() => res.json({ status: "ok" }))
|
|
.catch(err => {
|
|
console.error(err);
|
|
res.status(500).json({ status: "error", message: "Failed to send WOL packet" });
|
|
});
|
|
});
|
|
|
|
app.get("/wake/:name", (req, res) => {
|
|
const name = req.params.name;
|
|
const mac = targets[name];
|
|
|
|
if (!mac) {
|
|
return res.status(404).json({ status: "error", message: "Unknown target" });
|
|
}
|
|
|
|
sendWolPacket(mac)
|
|
.then(() => res.json({ status: "ok" }))
|
|
.catch(err => {
|
|
console.error(err);
|
|
res.status(500).json({ status: "error", message: "Failed to send WOL packet" });
|
|
});
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log('WOL server running on port ' + port);
|
|
}); |