77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import { ping, sendWolPacket } from './wol.js';
|
|
import targets from "./targets.json" with { type: "json" };
|
|
import { fileURLToPath } from "node:url";
|
|
import path from 'node:path';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const app = express();
|
|
const port = 9999;
|
|
|
|
app.use(cors()).use(express.json());
|
|
|
|
app.get("/targets", (req, res) => {
|
|
res.json(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 target = targets[req.params.name];
|
|
if (!target?.mac) {
|
|
return res.status(404).json({ status: "error", message: "Unknown target" });
|
|
}
|
|
|
|
sendWolPacket(target.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("/status/:name", (req, res) => {
|
|
const target = targets[req.params.name];
|
|
if (!target?.ip) {
|
|
return res.json({ isUp: null });
|
|
}
|
|
|
|
ping(target.ip).then(isUp => res.json({ isUp }));
|
|
});
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
app.listen(port, () => {
|
|
console.log('WOL server running on port ' + port);
|
|
}); |