mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
Plugins 0.3 - dbHelper & remove unnecessary API settings
This commit is contained in:
@@ -334,7 +334,7 @@ def importConfigs ():
|
||||
# Nmap
|
||||
global NMAP_ACTIVE, NMAP_TIMEOUT, NMAP_RUN, NMAP_RUN_SCHD, NMAP_ARGS
|
||||
# API
|
||||
global API_RUN, API_RUN_SCHD, API_RUN_INTERVAL, API_CUSTOM_SQL
|
||||
global API_CUSTOM_SQL
|
||||
|
||||
# get config file
|
||||
config_file = Path(fullConfPath)
|
||||
@@ -438,9 +438,6 @@ def importConfigs ():
|
||||
NMAP_ARGS = ccd('NMAP_ARGS', '-p -10000' , c_d, 'Nmap custom arguments', 'text', '', 'Nmap')
|
||||
|
||||
# API
|
||||
API_RUN = ccd('API_RUN', 'schedule' , c_d, 'API execution', 'selecttext', "['none', 'interval', 'schedule']", 'API')
|
||||
API_RUN_SCHD = ccd('API_RUN_SCHD', '*/3 * * * *' , c_d, 'API schedule', 'text', '', 'API')
|
||||
API_RUN_INTERVAL = ccd('API_RUN_INTERVAL', 10 , c_d, 'API update interval', 'integer', '', 'API')
|
||||
API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'API')
|
||||
|
||||
# Prepare scheduler
|
||||
@@ -460,10 +457,6 @@ def importConfigs ():
|
||||
nmapSchedule = Cron(NMAP_RUN_SCHD).schedule(start_date=datetime.datetime.now(tz))
|
||||
mySchedules.append(schedule_class("nmap", nmapSchedule, nmapSchedule.next(), False))
|
||||
|
||||
# init API schedule
|
||||
apiSchedule = Cron(API_RUN_SCHD).schedule(start_date=datetime.datetime.now(tz))
|
||||
mySchedules.append(schedule_class("api", apiSchedule, apiSchedule.next(), False))
|
||||
|
||||
# Format and prepare the list of subnets
|
||||
updateSubnets()
|
||||
|
||||
@@ -537,7 +530,6 @@ now_minus_24h = time_started - datetime.timedelta(hours = 24)
|
||||
|
||||
last_network_scan = now_minus_24h
|
||||
last_internet_IP_scan = now_minus_24h
|
||||
last_API_update = now_minus_24h
|
||||
last_run = now_minus_24h
|
||||
last_cleanup = now_minus_24h
|
||||
last_update_vendors = time_started - datetime.timedelta(days = 6) # update vendors 24h after first run and then once a week
|
||||
@@ -547,7 +539,7 @@ newVersionAvailable = False
|
||||
|
||||
def main ():
|
||||
# Initialize global variables
|
||||
global time_started, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors, last_API_update
|
||||
global time_started, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors
|
||||
# second set of global variables
|
||||
global startTime, log_timestamp, sql_connection, sql, plugins_once_run
|
||||
|
||||
@@ -580,11 +572,8 @@ def main ():
|
||||
# check if there is a front end initiated event which needs to be executed
|
||||
check_and_run_event()
|
||||
|
||||
# Execute API update if enabled via the interval schedule settings and if enough time passed
|
||||
if API_RUN == "interval" and last_API_update + datetime.timedelta(seconds = API_RUN_INTERVAL) < time_started:
|
||||
|
||||
last_API_update = time_started
|
||||
update_api()
|
||||
# Update API endpoints
|
||||
update_api()
|
||||
|
||||
# proceed if 1 minute passed
|
||||
if last_run + datetime.timedelta(minutes=1) < time_started :
|
||||
@@ -654,19 +643,6 @@ def main ():
|
||||
nmapSchedule.last_run = datetime.datetime.now(tz).replace(microsecond=0)
|
||||
performNmapScan(get_all_devices())
|
||||
|
||||
# Execute scheduled API update if enabled
|
||||
if API_RUN == "schedule":
|
||||
|
||||
apiSchedule = [sch for sch in mySchedules if sch.service == "api"][0]
|
||||
run = False
|
||||
|
||||
# run if overdue scheduled time
|
||||
run = apiSchedule.runScheduleCheck()
|
||||
|
||||
if run:
|
||||
apiSchedule.last_run = datetime.datetime.now(tz).replace(microsecond=0)
|
||||
update_api()
|
||||
|
||||
# Perform a network scan via arp-scan or pihole
|
||||
if last_network_scan + datetime.timedelta(minutes=SCAN_CYCLE_MINUTES) < time_started:
|
||||
last_network_scan = time_started
|
||||
|
||||
154
front/php/server/dbHelper.php
Executable file
154
front/php/server/dbHelper.php
Executable file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
//------------------------------------------------------------------------------
|
||||
// Pi.Alert
|
||||
// Open Source Network Guard / WIFI & LAN intrusion detector
|
||||
//
|
||||
// parameters.php - Front module. Server side. Manage Parameters
|
||||
//------------------------------------------------------------------------------
|
||||
# Puche 2022+ jokob jokob@duck.com GNU GPLv3
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// External files
|
||||
require dirname(__FILE__).'/init.php';
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Action selector
|
||||
//------------------------------------------------------------------------------
|
||||
// Set maximum execution time to 15 seconds
|
||||
ini_set ('max_execution_time','15');
|
||||
|
||||
$skipCache = FALSE;
|
||||
$expireMinutes = 5;
|
||||
$defaultValue = '';
|
||||
$dbtable = '';
|
||||
$columns = '';
|
||||
$values = '';
|
||||
|
||||
|
||||
if (isset ($_REQUEST['skipcache'])) {
|
||||
$skipCache = TRUE;
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['defaultValue'])) {
|
||||
$defaultValue = $_REQUEST['defaultValue'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['expireMinutes'])) {
|
||||
$expireMinutes = $_REQUEST['expireMinutes'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['key'])) {
|
||||
$key = $_REQUEST['key'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['id'])) {
|
||||
$id = $_REQUEST['id'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['values'])) {
|
||||
$values = $_REQUEST['values'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['columns'])) {
|
||||
$columns = $_REQUEST['columns'];
|
||||
}
|
||||
|
||||
if (isset ($_REQUEST['dbtable'])) {
|
||||
$dbtable = $_REQUEST['dbtable'];
|
||||
}
|
||||
// TODO: Security, read, delete, edge cases
|
||||
// Action functions
|
||||
if (isset ($_REQUEST['action']) && !empty ($_REQUEST['action'])) {
|
||||
$action = $_REQUEST['action'];
|
||||
switch ($action) {
|
||||
case 'create': create($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values ); break;
|
||||
// case 'read' : read($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break;
|
||||
case 'update': update($key, $id, $skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break;
|
||||
// case 'delete': delete($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break;
|
||||
default: logServerConsole ('Action: '. $action); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// update
|
||||
//------------------------------------------------------------------------------
|
||||
function update($key, $id, $skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values) {
|
||||
|
||||
global $db;
|
||||
|
||||
if(strpos($columns, ',') !== false)
|
||||
{
|
||||
$columnsArr = explode(",", $columns);
|
||||
}else
|
||||
{
|
||||
$columnsArr = array($columns);
|
||||
}
|
||||
|
||||
if(strpos($values, ',') !== false)
|
||||
{
|
||||
$valuesArr = explode(",", $values);
|
||||
} else
|
||||
{
|
||||
$valuesArr = array($values);
|
||||
}
|
||||
|
||||
$columnValues = '';
|
||||
|
||||
$index = 0;
|
||||
foreach($columnsArr as $column)
|
||||
{
|
||||
$columnValues = $columnValues .' "' .$column.'" = "'.$valuesArr[$index] . '",' ;
|
||||
$index = $index + 1;
|
||||
}
|
||||
|
||||
$columnValues = substr($columnValues, 0, -1);
|
||||
|
||||
// Update value
|
||||
$sql = 'UPDATE '.$dbtable.' SET '. $columnValues .'
|
||||
WHERE "'. $key .'"="'. $id.'"';
|
||||
$result = $db->query($sql);
|
||||
|
||||
if (! $result == TRUE) {
|
||||
echo "Error updating parameter\n\n$sql \n\n". $db->lastErrorMsg();
|
||||
return;
|
||||
}
|
||||
|
||||
$changes = $db->changes();
|
||||
if ($changes == 0) {
|
||||
// Insert new value
|
||||
create($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values);
|
||||
}
|
||||
|
||||
// update cache
|
||||
$uniqueHash = hash('ripemd160', $dbtable . $columns);
|
||||
setCache($uniqueHash, $values, $expireMinutes);
|
||||
|
||||
echo 'OK';
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// create
|
||||
//------------------------------------------------------------------------------
|
||||
function create($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values)
|
||||
{
|
||||
global $db;
|
||||
|
||||
// Insert new value
|
||||
$sql = 'INSERT INTO '.$dbtable.' ('.$columns.')
|
||||
VALUES ("'. quotes($parameter) .'",
|
||||
"'. $values .'")';
|
||||
$result = $db->query($sql);
|
||||
|
||||
if (! $result == TRUE) {
|
||||
echo "Error creating etry\n\n$sql \n\n". $db->lastErrorMsg();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
@@ -48,10 +48,15 @@ function getFormControl(dbColumnDef, value, index) {
|
||||
result = `<span>${value}<span>`;
|
||||
break;
|
||||
case 'textboxsave':
|
||||
|
||||
value = value == 'null' ? '' : value; // hide 'null' values
|
||||
|
||||
id = `${dbColumnDef.column}_${index}`
|
||||
|
||||
result = `<span class="form-group">
|
||||
<div class="input-group">
|
||||
<input class="form-control" type="text" value="${value}" id="${dbColumnDef.column}_${index}" name="${dbColumnDef.column}">
|
||||
<span class="input-group-addon"><i class="fa fa-save pointer" onclick="alert('${dbColumnDef.column}_${index}');"></i></span>
|
||||
<input class="form-control" type="text" value="${value}" id="${id}" data-my-column="${dbColumnDef.column}" data-my-index="${index}" name="${dbColumnDef.column}">
|
||||
<span class="input-group-addon"><i class="fa fa-save pointer" onclick="saveData('${id}');"></i></span>
|
||||
</div>
|
||||
<span>`;
|
||||
break;
|
||||
@@ -82,6 +87,26 @@ function getFormControl(dbColumnDef, value, index) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Update the coresponding DB column and entry
|
||||
function saveData (id) {
|
||||
columnName = $(`#${id}`).attr('data-my-column')
|
||||
index = $(`#${id}`).attr('data-my-index')
|
||||
columnValue = $(`#${id}`).val()
|
||||
|
||||
$.get(`php/server/dbHelper.php?action=update&dbtable=Plugins_Objects&key=Index&id=${index}&columns=UserData&values=${columnValue}`, function(data) {
|
||||
|
||||
// var result = JSON.parse(data);
|
||||
console.log(data)
|
||||
|
||||
// if (result) {
|
||||
// period = result;
|
||||
// $('#period').val(period);
|
||||
// }
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Get translated string
|
||||
function localize (obj, key) {
|
||||
|
||||
Reference in New Issue
Block a user