2020-06-15 20:24:56 +02:00
|
|
|
const express = require('express');
|
|
|
|
|
const compression = require('compression');
|
|
|
|
|
const util = require('util');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const os = require('os');
|
2020-06-16 01:25:22 +02:00
|
|
|
const http = require("http");
|
2020-10-24 02:39:14 +02:00
|
|
|
const ping = require('ping');
|
2020-06-15 20:24:56 +02:00
|
|
|
|
2020-06-16 00:34:38 +02:00
|
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
|
const readFileAsync = util.promisify(fs.readFile);
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
const app = express(); // app is an instance of express
|
|
|
|
|
|
|
|
|
|
app.use(compression()); // enables gzip compression
|
2020-06-16 00:34:38 +02:00
|
|
|
|
|
|
|
|
app.options('*', (req, res) => {
|
2020-10-24 02:39:14 +02:00
|
|
|
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();
|
2020-06-15 20:24:56 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.get('/dvbStatus', async (req, res) => {
|
2020-10-24 02:39:14 +02:00
|
|
|
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
|
|
|
|
|
});
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/dvbChannel', async (req, res) => {
|
2020-10-24 02:39:14 +02:00
|
|
|
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))
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.get('/internetCheck', async (req, res) => {
|
2020-10-24 02:39:14 +02:00
|
|
|
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": ""
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-16 01:25:22 +02:00
|
|
|
});
|
2020-10-24 02:39:14 +02:00
|
|
|
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",
|
2020-11-03 01:31:48 +01:00
|
|
|
"stderr": "VPN not connected"
|
2020-10-24 02:39:14 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-16 01:25:22 +02:00
|
|
|
app.get('/sysStatus', (req, res) => {
|
2020-10-24 02:39:14 +02:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-24 02:39:14 +02:00
|
|
|
app.get('/lancianoCheck', async (req, res) => {
|
|
|
|
|
res.set({
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
});
|
|
|
|
|
let hostRes='';
|
2020-11-03 00:48:43 +01:00
|
|
|
let hosts = ["10.8.0.45","10.8.0.46"];
|
2020-10-24 02:39:14 +02:00
|
|
|
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) {
|
2020-11-03 00:48:43 +01:00
|
|
|
hostRes+=item.host+' alive ';
|
2020-10-24 02:39:14 +02:00
|
|
|
}
|
|
|
|
|
else{
|
2020-11-03 00:48:43 +01:00
|
|
|
hostRes+=item.host+' dead ';
|
2020-10-24 02:39:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.finally(()=>{
|
|
|
|
|
res.send({
|
|
|
|
|
"id": "lanciano-check",
|
|
|
|
|
"data": hostRes.toString(),
|
|
|
|
|
"stderr": ""
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-06-15 20:24:56 +02:00
|
|
|
|
2020-10-24 02:39:14 +02:00
|
|
|
app.use(express.static('/var/www/forbidden-fruit-dashboard')); // serve static build site
|
2020-06-15 20:24:56 +02:00
|
|
|
|
|
|
|
|
// app.get('*', (req, res) => {
|
|
|
|
|
// res.sendFile(__dirname + '/public/index.html');
|
|
|
|
|
// });
|
|
|
|
|
|
2020-10-24 02:39:14 +02:00
|
|
|
//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}`));
|