From ff99caa644ebe4c42f9d35ceecff1748efc0ffa5 Mon Sep 17 00:00:00 2001 From: rosa Date: Tue, 3 Feb 2026 00:28:19 +0100 Subject: [PATCH] feat!: new targets.json structure, add target status endpoint, move frontend to HTML file with table BREAKING CHANGE: convert older targets.json file to new structure --- api.js | 47 +++++++++++++++----------------- package.json | 2 +- public/index.html | 64 ++++++++++++++++++++++++++++++++++++++++++++ targets.example.json | 10 +++++-- 4 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 public/index.html diff --git a/api.js b/api.js index 02a6990..67692b6 100644 --- a/api.js +++ b/api.js @@ -1,34 +1,20 @@ import express from 'express'; import cors from 'cors'; -import { sendWolPacket } from './wol.js'; +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("/", (req, res) => { - let html = ` - - - -

Targets:

- - `; - res.send(html); -}); - app.get("/targets", (req, res) => { - res.json(Object.keys(targets)); + res.json(targets); }); app.post("/wake", (req, res) => { @@ -62,14 +48,12 @@ app.get("/wake", (req, res) => { }); app.get("/wake/:name", (req, res) => { - const name = req.params.name; - const mac = targets[name]; - - if (!mac) { + const target = targets[req.params.name]; + if (!target?.mac) { return res.status(404).json({ status: "error", message: "Unknown target" }); } - sendWolPacket(mac) + sendWolPacket(target.mac) .then(() => res.json({ status: "ok" })) .catch(err => { console.error(err); @@ -77,6 +61,17 @@ app.get("/wake/:name", (req, res) => { }); }); +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); }); \ No newline at end of file diff --git a/package.json b/package.json index ee500ab..29cc7db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wol-api", - "version": "1.2.0", + "version": "2.0.0", "description": "Wake-on-LAN API in Node.js", "keywords": [ "node", diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..3bfcc47 --- /dev/null +++ b/public/index.html @@ -0,0 +1,64 @@ + + + + + WOL + + + +

Targets

+ + + + + + + + + + + \ No newline at end of file diff --git a/targets.example.json b/targets.example.json index 79e576a..1025255 100644 --- a/targets.example.json +++ b/targets.example.json @@ -1,4 +1,10 @@ { - "pc": "00:AA:BB:CC:DD:EE", - "server": "01:23:45:67:89:00" + "pc": { + "mac": "00:AA:BB:CC:DD:EE", + "ip": "192.168.0.1" + }, + "server": { + "mac": "01:23:45:67:89:00", + "ip": "192.168.0.2" + } } \ No newline at end of file
NameMACStatus