Last active
December 17, 2022 09:33
-
-
Save hideki0403/3ac528a39508104bc1cd06d66b39c7a0 to your computer and use it in GitHub Desktop.
Wake on LAN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const path = require('path') | |
const app = express() | |
const port = 3000 | |
const password = '123456' | |
const default_mac = '00:AA:11:BB:22:CC' | |
const wake_on_lan = { | |
ready: false, | |
mac: null | |
} | |
app.use(express.json()) | |
app.use(express.urlencoded({ extended: true })) | |
app.get('/', (req, res) => { | |
if (req.query.password !== password) { | |
return res.status(400).send('Bad Request') | |
} | |
wake_on_lan.ready = true | |
wake_on_lan.mac = req.query.mac || null | |
return res.send('ready') | |
}) | |
app.get('/state', (req, res) => { | |
res.json({ | |
wol: wake_on_lan.ready, | |
mac: wake_on_lan.mac || default_mac | |
}) | |
}) | |
app.post('/state', (req, res) => { | |
if (req.body.password !== password) { | |
return res.status(400).send('Bad Request') | |
} | |
console.log('result: ' + req.body.result) | |
wake_on_lan.ready = false | |
wake_on_lan.mac = null | |
return res.send('ok') | |
}) | |
app.listen(port, () => console.log(`Listening on port ${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
password=123456 | |
endpoint=https://api.example.com | |
response=$(curl -s -X GET $endpoint/state) | |
wol=$(echo "$response" | jq '.wol') | |
mac=$(echo "$response" | jq '.mac' | sed -e 's/"//g') | |
if [ "$wol" == "true" ]; then | |
result=$(/usr/bin/etherwake -D -i br-lan $mac) | |
curl -s -X POST -H "Content-Type: application/json" -d "{\"result\": \"$result\", \"password\": \"$password\"}" $endpoint/state | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment