Lots of structural app changes

This commit is contained in:
Akkadius 2018-08-14 03:45:48 -05:00
parent 3d30b00e96
commit 43b0b61393
30 changed files with 859 additions and 799 deletions

View File

@ -6,7 +6,7 @@ var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
var lease_parser = require('../lib/lease_parser.js');
var lease_parser = require('../core/lease-parser.js');
lease_parser.clean();
var dhcp_lease_data_return_buffer = {};

View File

@ -4,7 +4,7 @@
var express = require('express');
var router = express.Router();
var authorize = require('../lib/authorize.js');
var authorize = require('../core/authorize.js');
router.get('/', authorize.auth, function (req, res, next) {
var json_file = require('jsonfile');

1065
app.js

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
"admin_user": "glassadmin",
"admin_password": "glassadmin",
"leases_file": "/var/lib/dhcp/dhcpd.leases",
"log_file": "/var/log/dhcp.log",
"log_file": "/var/log/syslog",
"config_file": "/etc/dhcp/dhcpd.conf",
"shared_network_critical_threshold": "95",
"shared_network_warning_threshold": "0",

69
core/app-timers.js Normal file
View File

@ -0,0 +1,69 @@
var fs = require('fs');
const execSync = require('child_process').execSync;
module.exports = {
startLeasesPerMinuteCalculator: function () {
return setInterval(function () {
leases_per_minute_data[leases_per_minute_counter] = current_leases_per_second;
leases_per_minute_counter++;
/**
* Count how many actual data sets we walked that have values
*/
leases_per_minute = 0;
for (i = 0; i < 59; i++) {
if (leases_per_minute_data[i] > 0) {
leases_per_minute += leases_per_minute_data[i];
}
}
if (leases_per_minute_counter === 60)
leases_per_minute_counter = 0;
/**
* Websockets statistics subscription broadcast
*/
if (are_clients_subscribed_to_ws_event('dhcp_statistics')) {
return_data = {
"cpu_utilization": cpu_utilization,
"leases_per_second": current_leases_per_second,
"leases_per_minute": leases_per_minute
};
wss.broadcast_event(JSON.stringify(return_data), 'dhcp_statistics');
}
}, 1000);
},
startDashboardTimer: function () {
return setInterval(function () {
unix_time = Math.floor(new Date() / 1000);
if ((unix_time - 5) > leases_last_update_time) {
current_leases_per_second = 0;
}
}, 5000);
},
pollCpuUtilizationTimer: function () {
return setInterval(function () {
cpu_utilization = parseFloat(execSync("top -bn 1 | awk 'NR>7{s+=$9} END {print s/4}'").toString())
}, (15 * 1000));
},
clearStaleWebsocketConnectionsTimer: function () {
return setInterval(function () {
stale_connections_audit();
}, 30000);
},
purgeRequestDataTimer: function () {
return setInterval(function () {
for (var key in dhcp_requests) {
if (dhcp_requests[key].request_count <= 10)
delete dhcp_requests[key];
}
}, 600 * 1000);
},
purgeRequestDataCompleteTimer: function () {
return setInterval(function () {
dhcp_requests = {};
}, 3600 * 1000)
}
};

65
core/dhcp-leases.js Normal file
View File

@ -0,0 +1,65 @@
var lease_parser = require('./lease-parser.js');
var fs = require('fs');
var tail = require('always-tail2');
module.exports = {
parseLeasesFileOnce: function (glass_config) {
fs.readFile(glass_config.leases_file, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
else {
lease_parser.parse(data);
console.log("[Glass Server] Leases file loaded");
}
});
},
startLeaseListener: function (glass_config) {
var lease_read_buffer = "";
var options = {};
options.interval = 1000;
let file_tail = new tail(
glass_config.leases_file,
"\n",
options
);
console.log("[Glass Server] Watching leases file '" + glass_config.leases_file + "'");
file_tail.on("line", function (data) {
unix_time = Math.floor(new Date() / 1000);
/**
* Buffering lines until we get full lease data
*/
lease_read_buffer = lease_read_buffer + data + "\n";
/**
* End of lease - cut off and parse the buffer
*/
if (/}/i.test(data)) {
lease_parser.parse(lease_read_buffer);
lease_read_buffer = "";
}
/**
* Count leases per second
*/
if (/lease/.test(data)) {
leases_per_second++;
}
if (current_time !== unix_time) {
current_time = unix_time;
current_leases_per_second = leases_per_second;
leases_last_update_time = unix_time;
leases_per_second = 0;
}
});
},
setLeasesCleanTimer: function () {
return setInterval(function () {
lease_parser.clean();
}, (60 * 1000));
}
};

73
core/dhcp-log-watcher.js Normal file
View File

@ -0,0 +1,73 @@
var tail = require('always-tail2');
module.exports = {
init: function (glass_config) {
console.log("[Glass Server] DHCP log watcher initialized");
var options = {};
options.interval = 300;
var tail_dhcp_log = new tail(
glass_config.log_file,
"\n",
options
);
tail_dhcp_log.on("line", function (data) {
/**
* Broadcast log data
*/
if (listening_to_log_file) {
wss.broadcast_event(data, 'dhcp_log_subscription');
}
/**
* Collect Excessive DHCP Request Data
*/
if (/DHCPREQUEST/i.test(data)) {
var request_from = "";
var request_for = "";
var request_via = "";
var request_data = data.split(" ");
var length = request_data.length;
for (var i = 0; i < length; i++) {
if (request_data[i] === "from") {
request_from = request_data[i + 1];
}
if (request_data[i] === "for") {
request_for = request_data[i + 1];
}
if (request_data[i] === "via") {
request_via = request_data[i + 1];
}
}
if (typeof dhcp_requests[request_from] === "undefined")
dhcp_requests[request_from] = {};
if (typeof dhcp_requests[request_from].request_for === "undefined")
dhcp_requests[request_from].request_for = request_for;
if (typeof dhcp_requests[request_from].request_via === "undefined")
dhcp_requests[request_from].request_via = request_via;
if (typeof dhcp_requests[request_from].request_count === "undefined")
dhcp_requests[request_from].request_count = 0;
if (typeof request_from !== "undefined") {
if (request_from.length === 17 && /:/.test(request_from)) {
var mac_oui = request_from.split(":").join("").toUpperCase().slice(0, 6);
if (typeof dhcp_requests[request_from].request_vendor === "undefined")
dhcp_requests[request_from].request_vendor = oui_data[mac_oui];
}
}
dhcp_requests[request_from].request_count++;
}
});
},
};

View File

@ -0,0 +1,17 @@
var fs = require('fs');
var json_file = require('jsonfile');
module.exports = {
init: function () {
console.log("[Glass Server] Config watcher initialized");
fs.watch('config/glass_config.json', function (event, filename) {
if (filename) {
fsTimeout = setTimeout(function () {
glass_config = json_file.readFileSync('config/glass_config.json');
console.log("[Glass Server] Config Loaded");
}, 1000);
}
});
},
};

145
core/lease-parser.js Normal file
View File

@ -0,0 +1,145 @@
/**
* Created by cmiles on 8/9/2017.
*/
module.exports = {
parse: function (input) {
var lease_data = input.split("lease");
for (i = 0; i < lease_data.length; i++) {
ip_address = "";
lines = lease_data[i].split("\n");
for (l = 0; l < lines.length; l++) {
/**
* Trim whitespaces at each ends of the line
*/
lines[l] = lines[l].trim();
/**
* Break each newline into an array split into spaces
* Ex: [ 'starts', '3', '2017/08/09', '04:50:53;' ]
*
* @type {string[]}
*/
line_data_arg = lines[l].split(" ");
if (/{/i.test(lines[l]) && /\./i.test(lines[l]) && !/uid/i.test(lines[l])) {
ip_address = line_data_arg[0].trim();
if (typeof dhcp_lease_data[ip_address] === "undefined") {
dhcp_lease_data[ip_address] = {};
}
option_data = {};
}
if (ip_address !== "") {
if (/start/i.test(lines[l])) {
/*
Make sure we force format as UTC because that is what the leases are formatted in
*/
date = (line_data_arg[2] + ' ' + line_data_arg[3]).trim().replace(/\//gi, '-').replace(/;/i, '') + ' UTC';
start_unix_time = (Date.parse(date) / 1000);
dhcp_lease_data[ip_address].start = start_unix_time;
}
if (/ends/i.test(lines[l])) {
/*
Make sure we force format as UTC because that is what the leases are formatted in
*/
lease_end = (line_data_arg[2] + ' ' + line_data_arg[3]).trim().replace(/\//gi, '-').replace(/;/i, '') + ' UTC';
now_unix_time = parseInt((new Date().getTime() / 1000).toFixed(0));
end_unix_time = parseInt((new Date(lease_end).getTime() / 1000).toFixed(0).toLocaleString());
/*
console.log('now ' + now_unix_time);
console.log('end ' + end_unix_time);
console.log('now ' + new Date());
console.log('end_raw ' + lease_end);
console.log('end ' + new Date(lease_end));
*/
if (end_unix_time <= now_unix_time) {
delete dhcp_lease_data[ip_address];
break;
}
dhcp_lease_data[ip_address].end = end_unix_time;
}
if (/ethernet/i.test(lines[l])) {
if (typeof line_data_arg[2] !== "undefined") {
dhcp_lease_data[ip_address].mac = line_data_arg[2].replace(/;/gi, '').trim();
if (dhcp_lease_data[ip_address].mac.split(":").join("").trim() === "")
continue;
if (dhcp_lease_data[ip_address].mac.split(":").join("").toUpperCase().trim() === "")
continue;
/* Mac OUI Lookup */
var mac_oui = dhcp_lease_data[ip_address].mac.split(":").join("").toUpperCase().slice(0, 6);
dhcp_lease_data[ip_address].mac_oui_vendor = '';
if (typeof oui_data[mac_oui] !== "undefined") {
dhcp_lease_data[ip_address].mac_oui_vendor = oui_data[mac_oui];
}
}
}
if (/hostname/i.test(lines[l])) {
if (typeof line_data_arg[1] !== "undefined")
dhcp_lease_data[ip_address].host = line_data_arg[1].replace(/;/gi, '').replace(/"/gi, '').trim();
}
if (/set/i.test(lines[l])) {
set_data = lines[l].replace(/;/gi, '').replace(/"/gi, '').replace(/ = /gi, ' ').replace(/set/gi, '').trim();
set_data_split = set_data.split(" ");
option_key = set_data_split[0].trim();
option_value = set_data.replace(RegExp(option_key, "g"), '').trim();
option_data[option_key] = option_value;
if (typeof dhcp_lease_data[ip_address]['options'] === "undefined")
dhcp_lease_data[ip_address]['options'] = [];
}
if (/option/i.test(lines[l])) {
set_data = lines[l].replace(/;/gi, '').replace(/"/gi, '').replace(/ = /gi, ' ').replace(/option/gi, '').trim();
set_data_split = set_data.split(" ");
option_key = set_data_split[0].trim();
option_value = set_data.replace(RegExp(option_key, "g"), '').trim();
option_data[option_key] = option_value;
if (typeof dhcp_lease_data[ip_address]['options'] === "undefined")
dhcp_lease_data[ip_address]['options'] = [];
}
if (lines[l].charAt(0) === "}" && typeof dhcp_lease_data[ip_address]['options'] !== "undefined") {
if (typeof option_data !== 'undefined') {
dhcp_lease_data[ip_address]['options'] = option_data;
}
option_data = [];
}
/* End of Lease */
if (lines[l].charAt(0) === "}") {
if (debug_watch_lease_parse_stream) {
console.log("[Glass Server] Lease Parse");
console.log(JSON.stringify(dhcp_lease_data[ip_address], null, 2));
}
}
}
}
}
return;
},
clean: function () {
for (var key in dhcp_lease_data) {
now_unix_time = parseInt((new Date().getTime() / 1000).toFixed(0));
end_unix_time = dhcp_lease_data[key].end;
if ((now_unix_time >= end_unix_time)) {
console.log("[DHCP Lease Data] Lease " + key + " has expired - clearing");
delete dhcp_lease_data[key];
}
}
},
};

27
core/oui-reader.js Normal file
View File

@ -0,0 +1,27 @@
var fs = require('fs');
module.exports = {
initOuiDatabase: function () {
var oui_database_file = "bin/oui_table.txt";
if (fs.existsSync(oui_database_file)) {
fs.readFile(oui_database_file, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
else {
lines = data.split("\n");
for (l = 0; l < lines.length; l++) {
/* Trim whitespaces at each ends of the line */
lines[l] = lines[l].trim();
var oui_line_data = lines[l].split(":::");
if (typeof oui_line_data[1] !== "undefined")
oui_data[oui_line_data[0].trim()] = oui_line_data[1].trim();
}
console.log("[Glass Server] OUI Database Loaded");
}
});
}
},
};

View File

@ -29,5 +29,4 @@ module.exports = {
form_input: function (title, input) {
return '<label>' + title + '</label><div class="form-group"><div class="form-line">' + input + '</div></div>';
},
};
};

View File

@ -1,140 +0,0 @@
/**
* Created by cmiles on 8/9/2017.
*/
module.exports = {
parse: function (input) {
var lease_data = input.split("lease");
for (i = 0; i < lease_data.length; i++) {
ip_address = "";
lines = lease_data[i].split("\n");
for (l = 0; l < lines.length; l++) {
/* Trim whitespaces at each ends of the line */
lines[l] = lines[l].trim();
/* Break each newline into an array split into spaces
Ex: [ 'starts', '3', '2017/08/09', '04:50:53;' ]
*/
line_data_arg = lines[l].split(" ");
if (/{/i.test(lines[l]) && /\./i.test(lines[l]) && !/uid/i.test(lines[l])) {
ip_address = line_data_arg[0].trim();
if (typeof dhcp_lease_data[ip_address] === "undefined") {
dhcp_lease_data[ip_address] = {};
}
option_data = {};
}
if (ip_address != "") {
if (/start/i.test(lines[l])) {
/*
Make sure we force format as UTC because that is what the leases are formatted in
*/
date = (line_data_arg[2] + ' ' + line_data_arg[3]).trim().replace(/\//gi, '-').replace(/;/i, '') + ' UTC';
start_unix_time = (Date.parse(date) / 1000);
dhcp_lease_data[ip_address].start = start_unix_time;
}
if (/ends/i.test(lines[l])) {
/*
Make sure we force format as UTC because that is what the leases are formatted in
*/
lease_end = (line_data_arg[2] + ' ' + line_data_arg[3]).trim().replace(/\//gi, '-').replace(/;/i, '') + ' UTC';
now_unix_time = parseInt((new Date().getTime() / 1000).toFixed(0));
end_unix_time = parseInt((new Date(lease_end).getTime() / 1000).toFixed(0).toLocaleString());
/*
console.log('now ' + now_unix_time);
console.log('end ' + end_unix_time);
console.log('now ' + new Date());
console.log('end_raw ' + lease_end);
console.log('end ' + new Date(lease_end));
*/
if (end_unix_time <= now_unix_time) {
delete dhcp_lease_data[ip_address];
break;
}
dhcp_lease_data[ip_address].end = end_unix_time;
}
if (/ethernet/i.test(lines[l])) {
if (typeof line_data_arg[2] !== "undefined") {
dhcp_lease_data[ip_address].mac = line_data_arg[2].replace(/;/gi, '').trim();
if (dhcp_lease_data[ip_address].mac.split(":").join("").trim() == "")
continue;
if (dhcp_lease_data[ip_address].mac.split(":").join("").toUpperCase().trim() == "")
continue;
/* Mac OUI Lookup */
var mac_oui = dhcp_lease_data[ip_address].mac.split(":").join("").toUpperCase().slice(0, 6);
dhcp_lease_data[ip_address].mac_oui_vendor = '';
if (typeof oui_data[mac_oui] !== "undefined") {
dhcp_lease_data[ip_address].mac_oui_vendor = oui_data[mac_oui];
}
}
}
if (/hostname/i.test(lines[l])) {
if (typeof line_data_arg[1] !== "undefined")
dhcp_lease_data[ip_address].host = line_data_arg[1].replace(/;/gi, '').replace(/"/gi, '').trim();
}
if (/set/i.test(lines[l])) {
set_data = lines[l].replace(/;/gi, '').replace(/"/gi, '').replace(/ = /gi, ' ').replace(/set/gi, '').trim();
set_data_split = set_data.split(" ");
option_key = set_data_split[0].trim();
option_value = set_data.replace(RegExp(option_key, "g"), '').trim();
option_data[option_key] = option_value;
if (typeof dhcp_lease_data[ip_address]['options'] === "undefined")
dhcp_lease_data[ip_address]['options'] = [];
}
if (/option/i.test(lines[l])) {
set_data = lines[l].replace(/;/gi, '').replace(/"/gi, '').replace(/ = /gi, ' ').replace(/option/gi, '').trim();
set_data_split = set_data.split(" ");
option_key = set_data_split[0].trim();
option_value = set_data.replace(RegExp(option_key, "g"), '').trim();
option_data[option_key] = option_value;
if (typeof dhcp_lease_data[ip_address]['options'] === "undefined")
dhcp_lease_data[ip_address]['options'] = [];
}
if (lines[l].charAt(0) == "}" && typeof dhcp_lease_data[ip_address]['options'] !== "undefined") {
if (typeof option_data !== 'undefined') {
dhcp_lease_data[ip_address]['options'] = option_data;
}
option_data = [];
}
/* End of Lease */
if (lines[l].charAt(0) == "}") {
if (debug_watch_lease_parse_stream) {
console.log("[Glass Server] Lease Parse");
console.log(JSON.stringify(dhcp_lease_data[ip_address], null, 2));
}
}
}
}
}
return;
},
clean: function () {
for (var key in dhcp_lease_data) {
now_unix_time = parseInt((new Date().getTime() / 1000).toFixed(0));
end_unix_time = dhcp_lease_data[key].end;
if ((now_unix_time >= end_unix_time)) {
console.log("[DHCP Lease Data] Lease " + key + " has expired - clearing");
delete dhcp_lease_data[key];
}
}
},
};

View File

@ -2,11 +2,13 @@ function log_action (action) {
switch (action) {
case "stop":
websockets_unsubscribe_event("dhcp_log_subscription");
notification('Log watch stopped');
break;
case "start":
killed_connection = 0;
console.log('start readystate is ' + socket.readyState);
websockets_subscribe_event("dhcp_log_subscription");
notification('Log watch started');
break;
case "clear":
editor.setValue("");

View File

@ -157,8 +157,8 @@
<!-- Footer -->
<div class="legal">
<div class="copyright">
&copy; 2017 <a href="javascript:void(0);">Glass | Chris Miles</a><br>
&copy; 2016 - 2017 <a href="javascript:void(0);">AdminBSB - Material Design</a>
&copy; 2017 - 2018 <a href="https://github.com/Akkadius/glass-isc-dhcp" target="glass">Glass | Chris Miles</a><br>
&copy; 2016 - 2017 <a href="https://github.com/gurayyarar/AdminBSBMaterialDesign" target="bsb">AdminBSB - Material Design</a>
</div>
<div class="version">
<b>Version: </b> 1.0.0

View File

@ -4,7 +4,7 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var template_render = require('../core/render-template.js');
router.get('/', function(req, res, next) {

View File

@ -1,8 +1,8 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js');
var template_render = require('../core/render-template.js');
var authorize = require('../core/authorize.js');
router.get('/', authorize.auth, function(req, res, next) {

View File

@ -5,7 +5,7 @@
var express = require('express');
var router = express.Router();
var authorize = require('../lib/authorize.js');
var authorize = require('../core/authorize.js');
router.post('/', authorize.auth, function(req, res, next) {
var request = req.body;

View File

@ -1,8 +1,8 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js');
var template_render = require('../core/render-template.js');
var authorize = require('../core/authorize.js');
router.post('/', authorize.auth, function(req, res, next) {
var request = req.body;

View File

@ -1,8 +1,8 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js');
var template_render = require('../core/render-template.js');
var authorize = require('../core/authorize.js');
function human_time (time){
var time = new Date(time);

View File

@ -1,7 +1,7 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var template_render = require('../core/render-template.js');
function human_time (time){
var time = new Date(time);

View File

@ -1,7 +1,7 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var template_render = require('../core/render-template.js');
function human_time (time){
var time = new Date(time);

View File

@ -1,7 +1,7 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var template_render = require('../core/render-template.js');
function human_time (time){
var time = new Date(time);

View File

@ -1,7 +1,7 @@
var express = require('express');
var router = express.Router();
var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js');
var template_render = require('../core/render-template.js');
var authorize = require('../core/authorize.js');
router.get('/', authorize.auth, function (req, res, next) {

View File

@ -1,7 +1,7 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var template_render = require('../core/render-template.js');
router.get('/', function(req, res, next) {
dhcp_leases = template_render.get_template("dhcp_statistics_page");

View File

@ -4,7 +4,7 @@
var express = require('express');
var router = express.Router();
var authorize = require('../lib/authorize.js');
var authorize = require('../core/authorize.js');
router.post('/', authorize.auth, function(req, res, next) {
var request = req.body;

View File

@ -5,8 +5,8 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js');
var template_render = require('../core/render-template.js');
var authorize = require('../core/authorize.js');
router.get('/', authorize.auth, function(req, res, next) {

View File

@ -5,8 +5,8 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var authorize = require('../lib/authorize.js');
var template_render = require('../core/render-template.js');
var authorize = require('../core/authorize.js');
router.get('/', authorize.auth, function(req, res, next) {

View File

@ -4,7 +4,7 @@
var express = require('express');
var router = express.Router();
var authorize = require('../lib/authorize.js');
var authorize = require('../core/authorize.js');
router.post('/', authorize.auth, function(req, res, next) {
var request = req.body;

View File

@ -1,7 +1,7 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
var template_render = require('../core/render-template.js');
/* GET home page. */
router.get('/', function(req, res, next) {