|
|
@@ -0,0 +1,75 @@
|
|
|
+var ws = require("nodejs-websocket");
|
|
|
+var querystring = require('querystring');
|
|
|
+var url= require("url");
|
|
|
+var exec = require('child_process').exec;
|
|
|
+
|
|
|
+var WSserver = ws.createServer(function (conn) {
|
|
|
+ console.log("New connection");
|
|
|
+ console.log(conn);
|
|
|
+ conn.on("text", function (str) {
|
|
|
+ console.log("Received "+str);
|
|
|
+ conn.sendText(str.toUpperCase()+"!!!");
|
|
|
+ broadcast(WSserver, str);
|
|
|
+ })
|
|
|
+ conn.on("close", function (code, reason) {
|
|
|
+ console.log("Connection closed")
|
|
|
+ })
|
|
|
+}).listen(8080);
|
|
|
+
|
|
|
+function broadcast(server, msg) {
|
|
|
+ server.connections.forEach(function (conn) {
|
|
|
+ conn.sendText(msg)
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+var http = require('http');
|
|
|
+var HTMLserver = http.createServer(function(request, response) {
|
|
|
+ console.log((new Date()) + ' Received request for ' + request.url);
|
|
|
+ var myQuery= url.parse(request.url, true);
|
|
|
+ console.log(myQuery);
|
|
|
+ if(myQuery.query.command=="webReboot"){
|
|
|
+ console.log("will try to reboot lighttpd");
|
|
|
+ exec('systemctl stop lighttpd && systemctl start lighttpd', function(error, stdout, stderr) {
|
|
|
+ if(error){
|
|
|
+ console.error(`exec error: ${error}`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if(request.method == 'POST') {
|
|
|
+ processPost(request, response, function() {
|
|
|
+ response.writeHead(200, "OK", {'Content-Type': 'text/plain'});
|
|
|
+ response.end();
|
|
|
+ broadcast(WSserver, JSON.stringify(request.post));
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ response.writeHead(404);
|
|
|
+ response.end();
|
|
|
+ }
|
|
|
+});
|
|
|
+HTMLserver.listen(8081, function() {
|
|
|
+ console.log((new Date()) + ' HTMLServer is listening on port 8081');
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+function processPost(request, response, callback) {
|
|
|
+ var queryData = "";
|
|
|
+ if(typeof callback !== 'function') return null;
|
|
|
+ if(request.method == 'POST') {
|
|
|
+ request.on('data', function(data) {
|
|
|
+ queryData += data;
|
|
|
+ if(queryData.length > 1e6) {
|
|
|
+ queryData = "";
|
|
|
+ response.writeHead(413, {'Content-Type': 'text/plain'}).end();
|
|
|
+ request.connection.destroy();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ request.on('end', function() {
|
|
|
+ request.post = querystring.parse(queryData);
|
|
|
+ callback();
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ response.writeHead(405, {'Content-Type': 'text/plain'});
|
|
|
+ response.end();
|
|
|
+ }
|
|
|
+}
|