add route function
This commit is contained in:
parent
be7a895dc7
commit
26939a62cf
133
bin/telebitd.js
133
bin/telebitd.js
|
@ -599,63 +599,90 @@ function handleApi(req, res) {
|
|||
));
|
||||
}
|
||||
|
||||
if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) {
|
||||
getConfigOnly();
|
||||
return;
|
||||
function route() {
|
||||
if (/\b(config)\b/.test(opts.pathname) && /get/i.test(req.method)) {
|
||||
getConfigOnly();
|
||||
return;
|
||||
}
|
||||
if (/\b(init|config)\b/.test(opts.pathname)) {
|
||||
initOrConfig();
|
||||
return;
|
||||
}
|
||||
if (/restart/.test(opts.pathname)) {
|
||||
restart();
|
||||
return;
|
||||
}
|
||||
//
|
||||
// Check for proper config
|
||||
//
|
||||
if (!state.config.relay || !state.config.email || !state.config.agreeTos) {
|
||||
invalidConfig();
|
||||
return;
|
||||
}
|
||||
//
|
||||
// With proper config
|
||||
//
|
||||
if (/http/.test(opts.pathname)) {
|
||||
controllers.http(req, res, opts);
|
||||
return;
|
||||
}
|
||||
if (/tcp/.test(opts.pathname)) {
|
||||
controllers.tcp(req, res, opts);
|
||||
return;
|
||||
}
|
||||
if (/save|commit/.test(opts.pathname)) {
|
||||
saveAndCommit();
|
||||
return;
|
||||
}
|
||||
if (/ssh/.test(opts.pathname)) {
|
||||
controllers.ssh(req, res, opts);
|
||||
return;
|
||||
}
|
||||
if (/enable/.test(opts.pathname)) {
|
||||
enable();
|
||||
return;
|
||||
}
|
||||
if (/disable/.test(opts.pathname)) {
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
if (/status/.test(opts.pathname)) {
|
||||
getStatus();
|
||||
return;
|
||||
}
|
||||
if (/list/.test(opts.pathname)) {
|
||||
listSuccess();
|
||||
return;
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}}));
|
||||
}
|
||||
if (/\b(init|config)\b/.test(opts.pathname)) {
|
||||
initOrConfig();
|
||||
return;
|
||||
}
|
||||
if (/restart/.test(opts.pathname)) {
|
||||
restart();
|
||||
return;
|
||||
}
|
||||
//
|
||||
// Check for proper config
|
||||
//
|
||||
if (!state.config.relay || !state.config.email || !state.config.agreeTos) {
|
||||
invalidConfig();
|
||||
return;
|
||||
}
|
||||
//
|
||||
// With proper config
|
||||
//
|
||||
if (/http/.test(opts.pathname)) {
|
||||
controllers.http(req, res, opts);
|
||||
return;
|
||||
}
|
||||
if (/tcp/.test(opts.pathname)) {
|
||||
controllers.tcp(req, res, opts);
|
||||
return;
|
||||
}
|
||||
if (/save|commit/.test(opts.pathname)) {
|
||||
saveAndCommit();
|
||||
return;
|
||||
}
|
||||
if (/ssh/.test(opts.pathname)) {
|
||||
controllers.ssh(req, res, opts);
|
||||
return;
|
||||
}
|
||||
if (/enable/.test(opts.pathname)) {
|
||||
enable();
|
||||
return;
|
||||
}
|
||||
if (/disable/.test(opts.pathname)) {
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
if (/status/.test(opts.pathname)) {
|
||||
getStatus();
|
||||
return;
|
||||
}
|
||||
if (/list/.test(opts.pathname)) {
|
||||
listSuccess();
|
||||
|
||||
if (!req.headers['content-length'] && !req.headers['content-type']) {
|
||||
route();
|
||||
return;
|
||||
}
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"error":{"message":"unrecognized rpc"}}));
|
||||
var body = '';
|
||||
req.on('readable', function () {
|
||||
var data;
|
||||
while (true) {
|
||||
data = req.read();
|
||||
if (!data) { break; }
|
||||
body += data.toString();
|
||||
}
|
||||
});
|
||||
req.on('end', function () {
|
||||
try {
|
||||
opts.body = JSON.parse(body);
|
||||
} catch(e) {
|
||||
res.statusCode = 400;
|
||||
res.end('{"error":{"message":"POST body is not valid json"}}');
|
||||
return;
|
||||
}
|
||||
route();
|
||||
});
|
||||
}
|
||||
function serveControlsHelper() {
|
||||
controlServer = http.createServer(handleRemoteClient);
|
||||
|
|
Loading…
Reference in New Issue