-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrunnerd.js
110 lines (93 loc) · 2.88 KB
/
runnerd.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
#!/usr/bin/env fibjs
var child_process = require('child_process');
if (process.argv[2] !== '--daemon' && process.argv[2] !== '--console') {
child_process.fork(__filename, ['--daemon'], {
stdio: "inherit",
detached: true
});
console.notice(`runnerd.js is running in the background.
use runnerctrl.js to control the service process.`);
process.exit();
}
var fs = require('fs');
var os = require('os');
var path = require('path');
var http = require('http');
var ws = require('ws');
var ssl = require('ssl');
var tls = require('tls');
var load_config = require('./lib/config');
var Runner = require('.');
var cfg = load_config();
var runner = new Runner();
function reload(r) {
try {
cfg = load_config();
runner.reload(cfg);
if (r)
r.response.json({});
} catch (e) {
if (r)
r.response.json({ error: e.message });
else
rethrow(e);
}
}
function json_call(r, func) {
try {
r.response.json(runner[func].apply(runner, r.params));
} catch (e) {
r.response.json({ error: e.message });
}
}
var handler = new http.Handler({
'/reload': reload,
'/list': r => json_call(r, 'list'),
'/stat/:name/:stat/:interval': r => json_call(r, 'stat'),
'/log/:name/:length': r => json_call(r, 'log'),
'/attach/:name/:length': ws.upgrade((sock, r) => runner.attach(r.params[0], sock, r.params[1])),
'/stop/:name': r => json_call(r, 'stop'),
'/start/:name': r => json_call(r, 'start'),
'/restart/:name': r => json_call(r, 'restart'),
'*': r => { }
});
if (ssl.setClientCert) {
var svr = new ssl.Server(cfg.cert.cert, cfg.cert.key, cfg.listen.address, cfg.listen.port, s => {
if (!s.peerCert)
return;
var ip = s.stream.remoteAddress;
var pub = s.peerCert.publicKey.json({ compress: true }).x;
if (ip == '127.0.0.1' ? pub != cfg.key.pub : cfg.key.admin.indexOf(pub) < 0)
s.write(`HTTP/1.1 403 Forbidden\r\n\r\n`);
else
return handler;
});
svr.verification = ssl.VERIFY_OPTIONAL;
svr.start();
} else {
var svr = new tls.Server({
cert: cfg.cert.cert,
key: cfg.cert.key,
rejectUnverified: false,
address: cfg.listen.address,
port: cfg.listen.port
}, s => {
const peerCert = s.getPeerX509Certificate();
if (!peerCert)
return;
var ip = s.remoteAddress;
var pub = peerCert.publicKey.export({
format: 'raw',
type: 'compressed'
}).toString('base64url');
if (ip == '127.0.0.1' ? pub != cfg.key.pub : cfg.key.admin.indexOf(pub) < 0)
s.write(`HTTP/1.1 403 Forbidden\r\n\r\n`);
else
return handler;
});
svr.start();
}
reload();
if (process.argv[2] == '--console')
while (true)
run(path.join(__dirname, "runnerctl.js"));