-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
executable file
·80 lines (69 loc) · 2.43 KB
/
server.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
var exec = require('child_process').exec;
var os = require('os');
var http = require('http');
var command;
switch(os.platform()) {
case "win32":
command = "rundll32.exe powrprof.dll,SetSuspendState 0,1,0";
break;
case "darwin":
command = "pmset sleepnow";
break;
default:
console.error("Operating system not supported. "+
"Please send a pull request to http://github.com/jpillora/sleep-server");
process.exit(1);
}
var pkg = require('./package.json');
var program = require('commander');
program
.version(pkg.version)
.option('-p, --port <port>', 'Port to run server on (default: 57339)', 57339)
.option('-H, --host <host>', 'Interface to run server on (default: 0.0.0.0)', '0.0.0.0')
.option('-u, --url <url>', 'URL to trigger sleep (default: sleep)', 'sleep')
.option('-w, --wait <ms>', 'Wait in milliseconds before sleeping (default: 3000)', 3000)
.option('-d, --daemonize', 'Daemonize the sleep server and send output to a file (default: log.txt)', 'log.txt')
.parse(process.argv);
//daemonize?
if(program.daemonize && program.args.indexOf('is-daemon') === -1) {
var path = program.daemonize;
var fs = require('fs');
var spawn = require('child_process').spawn;
var log = fs.openSync(path, 'a');
var prog = process.argv[1];
var args = process.argv.slice(2);
args.push('is-daemon');
var child = spawn(prog, args, {
detached: true,
stdio: [ 'ignore', log, log ]
});
child.unref();
process.exit(1);
return;
}
//add a slash
if(program.url.charAt(0) !== '/')
program.url = '/'+program.url;
var msgId = 1;
http.createServer(function (request,response) {
if(request.url.indexOf(program.url) !== 0)
return response.end('not-ok');
console.log("#%s:\n Sleeping in %sms\n Time: %s\n IP address: %s",
msgId++, program.wait, new Date(), request.connection.remoteAddress);
setTimeout(function() {
exec(command);
}, program.wait);
response.end('ok');
}).listen(program.port, function() {
console.log('Listening for sleep requests at http://%s:%s%s', program.host, program.port, program.url);
});
//if interval is late by more than 2secs, assume has just awoken
var checkInterval = 5000;
var threshold = 2000;
var lastTime = Date.now();
setInterval(function() {
var currentTime = Date.now();
if (currentTime > (lastTime + checkInterval + threshold))
console.log("#%s:\n Woke\n Time: %s", msgId++, new Date());
lastTime = currentTime;
}, checkInterval);