-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (42 loc) · 1.16 KB
/
index.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
var express = require('express');
var router = express.Router();
var wol = require('node-wol');
const fs = require('fs')
const path = require('path')
const {NodeSSH} = require('node-ssh')
const ssh = new NodeSSH()
/* GET home page. */
router.get('/turnon', function(req, res, next) {
var check = req.query.token === 'your_very_long_random_token'
if(check) {
wol.wake('YOUR_MAC_ADDRESS_HERE', function(error) {
if(error) {
res.send('There was an error sending the WOL packet');
return;
} else {
res.send('WOL Packet Sent ! Turn ON Successful !')
}
});
} else {
res.send('There was an error');
}
});
router.get('/turnoff', function(req, res, next) {
var check = req.query.token === 'your_very_long_random_token'
if(check) {
ssh.connect({
host: 'yourhost',
username: 'yourusername',
port: 22,
privateKey: '/home/your-user/.ssh/id_rsa'
})
.then(function() {
ssh.execCommand('sudo poweroff').then(function(result) {
res.send('Server is going to sleep...')
})
})
} else {
res.send('Wrong Token!')
}
});
module.exports = router;