-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
134 lines (129 loc) · 3.23 KB
/
App.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
// this is a simple code for a remote power option powered by express
require('dotenv').config()
var express = require('express'),
app = express(),
path = require('path'),
isLinux = require('is-linux'),
ip = require("ip"),
isOsx = require('is-osx'),
isWindows = require('is-windows'),
argv = process.argv[2],
pck = require('./package.json'),
cp = require('child_process'),
port = process.env.PORT || argv || 3000;
app.use(express.static(path.join(__dirname, 'public')))
if(argv== '-v' ||argv == '--version'){
console.log( `${pck.version}`)
process.exit(1);
}
else if (argv =='-h'|| argv == '--help') { // checking undifined args
console.log(`
Usage: web-power <Port>
`);
}
else if (argv =='-i'|| argv == '--issue') { // checking undifined args
console.log(`
Issues at ${pck.bugs.url}
`);
}
else if (argv =='-a'|| argv == '--author') { // checking undifined args
console.log(`
Author: ${pck.author}
`);
}
else if (argv =='-d'|| argv == '--docs') { // checking undifined args
console.log(`
Docs at ${pck.homepage}
`);}
else{
app.listen(port, () => console.log(`server running at ${port}`))
}
// credits : inspired by hemant both sleep-mode and power-off Licence MIT
function poweroff(cb) {
var cmd = '';
if(isLinux() || isOsx()) {
cmd = 'sudo shutdown -h now';
} else if(isWindows()) {
cmd = 'shutdown -s -t 0';
} else {
throw new Error('Unknown OS!');
}
cp.exec(cmd, (err, stdout, stderr) => {
cb(err, stdout, stderr);
});
}
function sleep(cb){
var cmd = '';
if (isOsx()) {
cmd = 'pmset sleepnow';
} else if (isLinux()) {
// should work on all OSs using systemd.
cmd = 'sudo systemctl suspend';
} else if (isWindows()) {
cmd = 'rundll32.exe powrprof.dll,SetSuspendState 0,1,0';
} else {
throw new Error('Unknown OS!');
}
cp.exec(cmd, (err, stderr, stdout) => {
cb(err, stderr, stdout);
});
}
function reboot(cb){
var cmd = '';
if (isOsx()) {
cmd = 'reboot';
} else if (isLinux()) {
// should work on all OSs using systemd.
cmd = 'sudo reboot';
} else if (isWindows()) {
cmd = 'shutdown /r';
} else {
throw new Error('Unknown OS!');
}
cp.exec(cmd, (err, stderr, stdout) => {
cb(err, stderr, stdout);
});
}
app.get('/address',(req, res) => {
res.json({ address: ip.address() })
console.log(req.ip + 'requested address')
})
app.delete('/', (req, res) => {
res.end()
console.log(`Stopped ${pck.name}`)
console.log(req.ip + 'Stopped server')
process.exit()
})
app.post('/off',(req, res) => {
poweroff((err, stderr, stdout) => {
if (err) {
console.log(err)
res.status(500).json({ error: 'Can\'t run power-off' })
} else {
res.end()
}
})
})
app.post('/sleep', (req, res) =>{
sleep( (err, stderr, stdout) => {
if (err) {
console.log(err)
res.status(500).json({ error: 'Can\'t run sleep' })
} else {
console.log(req.ip + 'Send a sleep command')
res.end()
}
})
})
app.post('/reboot', (req, res) =>{
reboot( (err, stderr, stdout) => {
if (err) {
console.log(err)
res.status(500).json({ error: 'Can\'t run reboot' })
} else {
console.log(req.ip + 'Send a reboot command')
res.end()
}
})
})