Initial commit
This commit is contained in:
99
index.js
Normal file
99
index.js
Normal file
@ -0,0 +1,99 @@
|
||||
const Express = require('express');
|
||||
const pug = require('pug');
|
||||
const fs = require("fs");
|
||||
const readline = require("readline");
|
||||
const ping = require('ping');
|
||||
|
||||
const app = Express();
|
||||
const port = 3333;
|
||||
const version = 1.1;
|
||||
|
||||
app.use( Express.json() );
|
||||
app.use( Express.urlencoded( {extended: true } ) );
|
||||
app.disable( 'x-powered-by' );
|
||||
|
||||
app.set( 'views', __dirname + '/views' );
|
||||
app.set( 'view engine', 'pug' );
|
||||
|
||||
app.get( '/', async ( req, res ) => {
|
||||
res.send( pug.renderFile( 'views/index.pug', { } ) );
|
||||
res.end();
|
||||
} );
|
||||
|
||||
|
||||
/** API **/
|
||||
const apiResponse = {
|
||||
"api-name": "network-devices",
|
||||
"api-version": version,
|
||||
"api-author": "Tobias Hopp",
|
||||
"status": 200,
|
||||
data: null
|
||||
};
|
||||
|
||||
app.get( '/api/getDeviceStatus/:ipAddress', async ( req, res ) => {
|
||||
console.log( 'Loading device status for ' + req.params.ipAddress + '...' );
|
||||
let ping_response = await ping.promise.probe(req.params.ipAddress, {
|
||||
timeout: 5,
|
||||
extra: ['-i', '1'],
|
||||
});
|
||||
console.log(ping_response);
|
||||
let response = apiResponse;
|
||||
response.status = 200;
|
||||
res.status(200);
|
||||
let data;
|
||||
|
||||
if( ping_response.alive )
|
||||
{
|
||||
data = { online: true, host: ping_response.host, time: ping_response.time, avg: ping_response.avg }
|
||||
}
|
||||
else
|
||||
{
|
||||
data = { online: false, host: ping_response.host, time: ping_response.time, avg: ping_response.avg }
|
||||
}
|
||||
|
||||
response.data = data;
|
||||
|
||||
res.json( response );
|
||||
|
||||
res.end();
|
||||
} );
|
||||
|
||||
|
||||
app.get( '/api/getDevices', async ( req, res ) => {
|
||||
// Read the dnsmasq file
|
||||
console.log( 'Loading all devices...' );
|
||||
|
||||
const fileStream = fs.createReadStream('/home/tobias/IdeaProjects/network-devices/dnsmasq.leases' );
|
||||
const rl = readline.createInterface( { input: fileStream, crlfDelay: Infinity } );
|
||||
|
||||
let data = [];
|
||||
for await ( const nline of rl )
|
||||
{
|
||||
let thisData = {};
|
||||
let line = nline.substr( nline.indexOf( ' ' ) +1 )
|
||||
thisData.macAddress = line.substr( 0, line.indexOf( ' ' ) );
|
||||
thisData.ipAddress = line.substr( thisData.macAddress.length +1, line.substr( thisData.macAddress.length+1 ).indexOf( ' ') );
|
||||
thisData.hostname = line.substr( thisData.macAddress.length + thisData.ipAddress.length +2, line.length-1 );
|
||||
//console.log( nline );
|
||||
data.push( thisData );
|
||||
}
|
||||
|
||||
|
||||
|
||||
let response = apiResponse;
|
||||
response.status = 200;
|
||||
res.status(200);
|
||||
|
||||
response.data = data;
|
||||
res.json( response );
|
||||
|
||||
res.end();
|
||||
} );
|
||||
|
||||
|
||||
app.get( '*', ( req, res ) => {
|
||||
res.status( 404 ).send( "not found" );
|
||||
res.end();
|
||||
} );
|
||||
|
||||
app.listen( port, () => { console.log( "Server started!" )} );
|
Reference in New Issue
Block a user