From f38c824330aa4ac144b3737920f968816b2ecfb7 Mon Sep 17 00:00:00 2001 From: sco Date: Sat, 24 Oct 2020 02:39:14 +0200 Subject: [PATCH] Update 'node.js' --- node.js | 201 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 127 insertions(+), 74 deletions(-) diff --git a/node.js b/node.js index 22a5a5c..6e1c498 100644 --- a/node.js +++ b/node.js @@ -5,6 +5,7 @@ const fs = require('fs'); const path = require('path'); const os = require('os'); const http = require("http"); +const ping = require('ping'); const exec = util.promisify(require('child_process').exec); const readFileAsync = util.promisify(fs.readFile); @@ -14,103 +15,155 @@ const app = express(); // app is an instance of express app.use(compression()); // enables gzip compression app.options('*', (req, res) => { - res.set({ - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'GET, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type', - 'Access-Control-Max-Age': 600 - }) - res.status(200).end(); + res.set({ + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Max-Age': 600 + }) + res.status(200).end(); }) app.get('/dvbStatus', async (req, res) => { - res.set({ - 'Access-Control-Allow-Origin': '*' - }); - const { stdout, stderr } = await exec('DTV'); - const i = parseInt(stdout) * 1e-6; // i is in Hz; divide by 1M to have MHz - res.send({ - "id": "dvb-t-status", - "data": `Frontend Frequency: ${i} Mhz`, - "stderr": stderr - }); + res.set({ + 'Access-Control-Allow-Origin': '*' + }); + let tuner = 0; + tuner = req.query.t; + const { stdout, stderr } = await exec(`DTV2 -t ${tuner}`); + const i = parseInt(stdout) * 1e-6; // i is in Hz; divide by 1M to have MHz + res.send({ + "id": "dvb-t-status", + "data": `Frontend Frequency: ${i} Mhz`, + "stderr": stderr + }); }); app.get('/dvbChannel', async (req, res) => { - res.set({ - 'Access-Control-Allow-Origin': '*' - }); - const { stdout, stderr } = await exec('DTV'); - const i = parseInt(stdout) * 1e-6; // i is in Hz; divide by 1M to have MHz - readFileAsync(path.join(__dirname,"frequencies.json")) - .then(res2 => { - const db = JSON.parse(res2); - res.send({ - "id": `dvb-t-channel`, - "name": db[i]["name"].toString(), - "data": db[i]["channels"].toString(), - "stderr": stderr - }); - }) - .catch(err => console.error(err)) + res.set({ + 'Access-Control-Allow-Origin': '*' + }); + let tuner = 0; + tuner = req.query.t; + const { stdout, stderr } = await exec(`DTV2 -t ${tuner}`); + const i = parseInt(stdout) * 1e-6; // i is in Hz; divide by 1M to have MHz + readFileAsync(path.join(__dirname,"frequencies.json")) + .then(res2 => { + const db = JSON.parse(res2); + res.send({ + "id": `dvb-t-channel`, + "name": db[i]["name"].toString(), + "data": db[i]["channels"].toString(), + "stderr": stderr + }); + }) + .catch(err => console.error(err)) }); app.get('/internetCheck', async (req, res) => { - res.set({ - 'Access-Control-Allow-Origin': '*' - }); - http.get('http://icanhazip.com', (httpRes) => { - httpRes.setEncoding('utf8'); - httpRes.on('data', (body) => { - res.send( - { - "id": "internet-check", - "data": body, - "stderr": "" - } - ) - }); + res.set({ + 'Access-Control-Allow-Origin': '*' + }); + http.get('http://icanhazip.com', (httpRes) => { + httpRes.setEncoding('utf8'); + httpRes.on('data', (body) => { + res.send( + { + "id": "internet-check", + "data": body, + "stderr": "" + } + ) + }); + }); + }); +app.get('/vpnCheck', async (req, res) => { + res.set({ + 'Access-Control-Allow-Origin': '*' + }); + let ifaces = os.networkInterfaces() + if(ifaces.hasOwnProperty('tun0')){ + res.send( + { + "id": "vpn-check", + "data": ifaces["tun0"][0]["cidr"], + "stderr": "" + }) + } else { + res.send( + { + "id": "vpn-check", + "data": "Not connected", + "stderr": "" + }) + + } }); app.get('/sysStatus', (req, res) => { - res.set({ - 'Access-Control-Allow-Origin': '*' - }); - readFileAsync('/sys/class/thermal/thermal_zone0/temp') - .then(res2=> - { - res.send( - { - "id": "sys-stats", - "data": `Temp: ${(parseFloat(res2)*1e-3).toFixed(2)} °C Load Avg: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`, - "stderr": "" - } - ) - }) - .catch(err=> { - console.error(err) - res.send( - { - "id": "sys-stats", - "data": `Load Avg: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`, - "stderr": err - } - ) - }) + res.set({ + 'Access-Control-Allow-Origin': '*' + }); + readFileAsync('/sys/class/thermal/thermal_zone0/temp') + .then(res2=> + { + res.send({ + "id": "sys-stats", + "data": `${(parseFloat(res2)*1e-3).toFixed(2)} °C Load: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`, + "stderr": "" + }) + }) + .catch(err=> { + console.error(err) + res.send({ + "id": "sys-stats", + "data": `Load Avg: ${os.loadavg().map(x=>x.toFixed(2)).toString()}`, + "stderr": err + }) + }) }); +app.get('/lancianoCheck', async (req, res) => { + res.set({ + 'Access-Control-Allow-Origin': '*' + }); + let hostRes=''; + let hosts = ["10.8.0.2","10.8.0.3"]; + let promArr=[]; + for(let host of hosts) { + promArr.push(ping.promise.probe(host)); + } + Promise.all(promArr) + .then(function (res) { + for (let item of res) { + if(item.alive) { + hostRes+=item.host+' is alive '; + } + else{ + hostRes+=item.host+' is dead '; + } + } + }) + .finally(()=>{ + res.send({ + "id": "lanciano-check", + "data": hostRes.toString(), + "stderr": "" + }) + }) +}) -app.use(express.static('/var/www/default')); // serve static build site +app.use(express.static('/var/www/forbidden-fruit-dashboard')); // serve static build site // app.get('*', (req, res) => { // res.sendFile(__dirname + '/public/index.html'); // }); -//app.listen(8000,"192.168.10.14", () => console.log('listen on 8000')); -app.listen(process.env.NODE_PORT,'127.0.0.80', () => console.log('listen on 3000')); +//app.listen(8000,"192.168.10.14", () => console.log('listen on 8000')); +app.listen(process.env.NODE_PORT,process.env.NODE_IP, () => console.log(`Bind on ${process.env.NODE_IP}:${process.env.NODE_PORT}`)); \ No newline at end of file