const http = require('http') const url = require('url') const fs = require('fs') const ws = require('ws') const redis = require('redis') const mime = require('mime') const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest const host = 'http://127.0.0.1:8080/' const outpath = ['sym', '', 'manifest.json', 'admin', 'stats', 'analytics'] let led1_status = 'off'; //redis client const redis_cli = redis.createClient({ host: 'redis', port: 6379 }) redis_cli.on("error", function (error) { console.error(error) }) //HTTP server http.createServer(function (req, res) { const q = url.parse(req.url, true); let filename = "./public" + q.pathname; let path_split = q.pathname.split("/", 4); if (path_split[path_split.length - 1] === "") filename += "/index.html"; let file_type = mime.getType(filename) if (path_split[1] === "connect" && path_split[2] !== "") { //device registration res.writeHead(200, {'Content-Type': file_type}); redis_cli.hmset("device;" + path_split[3], "status", "online", "ip", path_split[2], "time", Date.now()) wss.clients.forEach(clients => { clients.send("device;"+path_split[3]+";online") }) res.write("200"); /*let check = setInterval(to => { let xhr = new XMLHttpRequest(); xhr.open('GET', `http://192.168.1.210/info`, true); xhr.timeout = 1000; xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')} xhr.send(); setTimeout(to => {if (xhr.readyState !== 4){ //ws.send('error;device "led1" took too long to reach') wss.clients.forEach(clients => { clients.send('device;led1;offline') }) xhr.abort() clearInterval(check) }}, 2000) }, 5000)*/ return res.end(); } fs.readFile(filename, function(err, data) { if (err) { res.writeHead(404, {'Content-Type': "text/html"}); return res.end("404 Not Found"); } res.writeHead(200, {'Content-Type': file_type}); res.write(data); return res.end(); }) }).listen(8080); //WS server const wss = new ws.Server({ port: 8081, perMessageDeflate: { zlibDeflateOptions: { // See zlib defaults. chunkSize: 1024, memLevel: 7, level: 3 }, zlibInflateOptions: { chunkSize: 10 * 1024 }, // Other options settable: clientNoContextTakeover: true, // Defaults to negotiated value. serverNoContextTakeover: true, // Defaults to negotiated value. serverMaxWindowBits: 10, // Defaults to negotiated value. // Below options specified as default values. concurrencyLimit: 10, // Limits zlib concurrency for perf. threshold: 1024 // Size (in bytes) below which messages // should not be compressed. } }); //check available devices let checkstat = setInterval(to => { redis_cli.keys("device;*", function(err, keys) { if(err) console.log(err) if(keys){ keys.forEach(key => { redis_cli.hgetall(key, function(err, device) { if(err) console.log(err) if (device){ let device_available = "online" let xhr = new XMLHttpRequest(); xhr.open('GET', `http://${device[3]}/info`, true); xhr.timeout = 1000; xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')} xhr.send(); setTimeout(to => {if (xhr.readyState !== 4){ device_available = "offline" xhr.abort() } if (device_available !== device[1]){ wss.clients.forEach(clients => clients.send(`${key};${device_available}`)) }}, 2000) } }) })} }) }, 5000) //WS handler wss.on('connection', ws => { ws.on('message', message => { console.log(`Received message => ${message}`) let msg = message.split(";", 2) if (msg[0] === 'led1') { if (msg[1] === '') ws.send('error;try again') else if (msg[1] === 'on' || msg[1] === "off"){ let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { wss.clients.forEach(clients => { clients.send(message) }) } } xhr.open('GET', `http://192.168.1.210/${msg[1]}`, true); xhr.timeout = 1000; xhr.ontimeout = function(e) {ws.send('error;device "led1" is offline')} xhr.send(); setTimeout(to => {if (xhr.readyState !== 4){ ws.send('error;device "led1" took too long to reach') ws.send('device;led1;offline') xhr.abort() }}, 1500) }else ws.send('error;try again') } }) ws.send('websocket connected') ws.send(`led1;${led1_status}`) }) //random key function get_key(length) { let forbidden = false; let output = '' let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' do{ for (let i = 0; i < length; i++ ) output += characters.charAt(Math.floor(Math.random() * characters.length)) for (let i = 0; i < output.length; i++) if (output === outpath[i]) forbidden = true redis_cli.hget("surl;"+output, "url", function(err, obj) { if(err) console.log(err) if(obj) forbidden = true }) } while (forbidden) return output }