Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http_createServer = require("http").createServer;
- crypto_randombytes = require("crypto").pseudoRandomBytes;
- parseUrl = require("url").parse;
- exec = require("child_process").exec;
- os_hostname = require("os").hostname;
- function onRequest(request, response) {
- var queryFragments,
- queryParams,
- auth,
- hostName;
- queryFragments = parseUrl(request.url, true);
- queryParams = queryFragments.query;
- auth = queryParams.auth
- hostName = queryFragments.hostname || "";
- if (auth && auth !== "secret") {
- returnErrorPage("408", response);
- }
- else {
- exec("rrd.sh graph '" + hostName + "'", { encoding: "binary", maxBuffer: 10000000, timeout: 20000 }, handleGraph.bind(null, response, hostName));
- }
- }
- function returnErrorPage(status, response) {
- response.writeHead(status);
- response.end();
- }
- function handleGraph(response, hostName, err, stdout, stderr) {
- if (err !== null) {
- response.writeHead (500);
- response.end(err + stderr);
- }
- else {
- response.setTimeout (10000);
- response.writeHead (200, { "content-type": "image/png", "content-length": stdout.length, "content-disposition": "inline; filename=\"" + hostName + ".png\"" } );
- response.end(stdout, "binary");
- }
- }
- var server = http_createServer(onRequest);
- server.listen(8000);
- console.info (new Date().toISOString() + "\thttpd started");
- console.log("Server running at http://127.0.0.1:8000/");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement