You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.8 KiB
JavaScript
114 lines
3.8 KiB
JavaScript
4 years ago
|
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: '127.0.0.1',
|
||
|
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("/", 3);
|
||
|
if (path_split[path_split.length - 1] === "") filename += "/index.html";
|
||
|
let file_type = mime.getType(filename)
|
||
|
if (path_split[1] === "connect") {
|
||
|
res.writeHead(200, {'Content-Type': file_type});
|
||
|
res.write("");
|
||
|
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.
|
||
|
}
|
||
|
});
|
||
|
|
||
|
//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)
|
||
|
})
|
||
|
led1_status = msg[1]
|
||
|
}
|
||
|
}
|
||
|
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');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
|
||
|
}
|