From 854ef20826fe135da3ee7669ef23d203165e36e9 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 2 Sep 2023 09:25:22 +1000 Subject: [PATCH] NMAP + docs work --- README.md | 2 +- front/php/templates/language/en_us.json | 1 + front/plugins/README.md | 34 ++++++++++- front/plugins/nmap_scan/config.json | 17 ++++-- front/pluginsCore.php | 79 +++++++++++++++++++++---- 5 files changed, 110 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index e1303b02..b51ac1ff 100755 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ The system continuously scans the network for, **New devices**, **New connection - **Pi-hole - DHCP leases**. Import of devices from the PiHole dhcp.leases file - **Generic DHCP leases**. Import of devices from the generic dhcp.leases file - **UNIFI import**. Import of devices from the UNIFI controller - - **SNMP-enabled router import**. Import of devices from an SNMP enabled router + - **SNMP-enabled router import**. Import of devices from an SNMP-enabled router ## 🧩 Integrations - [Apprise](https://hub.docker.com/r/caronc/apprise), [Pushsafer](https://www.pushsafer.com/), [NTFY](https://ntfy.sh/) diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index 4b67ff97..df1b80cb 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -440,6 +440,7 @@ "Plugins_History" : "Events History", "Plugins_Filters_Mac" : "Mac Filter", "Plugins_Out_of" : "out of", + "Plugins_no_control" : "No form control was found to render this value.", "Settings_Metadata_Toggle" : "Show/hide metadata for the given setting.", "settings_missing" : "Not all settings loaded, refresh the page! This is probably caused by a high load on the database.", "settings_missing_block" : "You can not save your settings without specifying all setting keys. Refresh the page. This is probably caused by a high load on the database.", diff --git a/front/plugins/README.md b/front/plugins/README.md index 928670db..87c2df3f 100755 --- a/front/plugins/README.md +++ b/front/plugins/README.md @@ -371,7 +371,7 @@ This approach is used to implement the `DHCPLSS` plugin. The script parses all s 3) That's it. PiAlert takes care of the rest. It loops thru the objects discovered by the plugin, takes the results line, by line and inserts them into the database table specified in `"mapped_to_table"`. The columns are translated from the generic plugin columns to the target table via the `"mapped_to_column"` property in the column definitions. > [!NOTE] -> You can create a column mapping with a default value via the `mapped_to_column_data` property. +> You can create a column mapping with a default value via the `mapped_to_column_data` property. This means that the value of the given column will always be this value. Taht also menas that the `"column": "NameDoesntMatter"` is not important as there is no databse source column. ```json { @@ -590,7 +590,7 @@ You can have any `"function": "my_custom_name"` custom name, however, the ones l ##### UI settings in database_column_definitions -The UI will adjust how columns are displayed in the UI based on the definition of the `database_column_definitions` object. These are the supported form controls and related functionality: +The UI will adjust how columns are displayed in the UI based on the resolvers definition of the `database_column_definitions` object. These are the supported form controls and related functionality: - Only columns with `"show": true` and also with at least an English translation will be shown in the UI. @@ -599,9 +599,10 @@ The UI will adjust how columns are displayed in the UI based on the definition o | `label` | Displays a column only. | | `text` | Makes a column editable, and a save icon is displayed next to it. See below for information on `threshold`, `replace`. | | | | -| `options` Property | Used in conjunction with types like `threshold`, `replace`. | +| `options` Property | Used in conjunction with types like `threshold`, `replace`, `regex`. | | `threshold` | The `options` array contains objects ordered from the lowest `maximum` to the highest. The corresponding `hexColor` is used for the value background color if it's less than the specified `maximum` but more than the previous one in the `options` array. | | `replace` | The `options` array contains objects with an `equals` property, which is compared to the "value." If the values are the same, the string in `replacement` is displayed in the UI instead of the actual "value". | +| `regex` | Applies a regex to the value. The `options` array contains objects with an `type` (must be set to `regex`) and `param` (must contain the regex itself) property. | | | | | Type Definitions | | | `device_mac` | The value is considered to be a MAC address, and a link pointing to the device with the given MAC address is generated. | @@ -609,6 +610,11 @@ The UI will adjust how columns are displayed in the UI based on the definition o | `device_name_mac` | The value is considered to be a MAC address, and a link pointing to the device with the given IP is generated. The link label is resolved as the target device name. | | `url` | The value is considered to be a URL, so a link is generated. | | `textbox_save` | Generates an editable and saveable text box that saves values in the database. Primarily intended for the `UserData` database column in the `Plugins_Objects` table. | +| `url_http_https` | Generates towo links with the `https` and `http` prefix as lock icons. | + + +> [!NOTE] +> Supports chaining. You can chain multiple resolvers with `.`. For example `regex.url_http_https`. This will apply the `regex` resolver and then the `url_http_https` resolver @@ -671,6 +677,28 @@ The UI will adjust how columns are displayed in the UI based on the definition o "language_code":"en_us", "string" : "Status" }] + }, + { + "column": "Watched_Value3", + "css_classes": "col-sm-1", + "show": true, + "type": "regex.url_http_https", + "default_value":"", + "options": [ + { + "type": "regex", + "param": "([\\d.:]+)" + } + ], + "localized": ["name"], + "name":[{ + "language_code":"en_us", + "string" : "HTTP/s links" + }, + { + "language_code":"es_es", + "string" : "N/A" + }] } ``` diff --git a/front/plugins/nmap_scan/config.json b/front/plugins/nmap_scan/config.json index cf419ec2..ba52e787 100755 --- a/front/plugins/nmap_scan/config.json +++ b/front/plugins/nmap_scan/config.json @@ -111,7 +111,7 @@ "column": "Object_SecondaryID", "css_classes": "col-sm-2", "show": true, - "type": "url", + "type": "label", "default_value":"", "options": [], "localized": ["name"], @@ -193,15 +193,20 @@ }, { "column": "Watched_Value3", - "css_classes": "col-sm-2", - "show": false, - "type": "label", + "css_classes": "col-sm-1", + "show": true, + "type": "regex.url_http_https", "default_value":"", - "options": [], + "options": [ + { + "type": "regex", + "param": "([\\d.:]+)" + } + ], "localized": ["name"], "name":[{ "language_code":"en_us", - "string" : "N/A" + "string" : "HTTP/s links" }, { "language_code":"es_es", diff --git a/front/pluginsCore.php b/front/pluginsCore.php index 764b41ed..ccef6819 100755 --- a/front/pluginsCore.php +++ b/front/pluginsCore.php @@ -60,10 +60,34 @@ function getFormControl(dbColumnDef, value, index) { result = '' - switch(dbColumnDef.type) + // Check if mapped_to_column_data exists and has a value to override the supplied value which is most likely `undefined` + if (dbColumnDef.mapped_to_column_data && dbColumnDef.mapped_to_column_data.value) { + value = dbColumnDef.mapped_to_column_data.value; + } + + + result = processColumnValue(dbColumnDef, value, index, dbColumnDef.type) + + return result; +} + +// ----------------------------------------------------------------------------- +// Process column value +function processColumnValue(dbColumnDef, value, index, type) { + if (type.includes('.')) { + const typeParts = type.split('.'); + + // recursion + for (const typePart of typeParts) { + value = processColumnValue(dbColumnDef, value, index, typePart) + } + + } else{ + // pick form control based on the supplied type + switch(type) { case 'label': - result = `${value}`; + value = `${value}`; break; case 'textbox_save': @@ -71,7 +95,7 @@ function getFormControl(dbColumnDef, value, index) { id = `${dbColumnDef.column}_${index}` - result = ` + value = `
@@ -79,22 +103,33 @@ function getFormControl(dbColumnDef, value, index) { `; break; case 'url': - result = `${value}`; + value = `${value}`; + break; + case 'url_http_https': + value = ` + + + + / + + + + `; break; case 'device_name_mac': - result = `${getNameByMacAddress(value)}`; + value = `${getNameByMacAddress(value)}`; break; case 'device_mac': - result = `${value}`; + value = `${value}`; break; case 'device_ip': - result = `${value}`; + value = `${value}`; break; case 'threshold': $.each(dbColumnDef.options, function(index, obj) { - if(Number(value) < obj.maximum && result == '') + if(Number(value) < obj.maximum && value == '') { - result = `
${value}
` + value = `
${value}
` // return; } }); @@ -103,17 +138,35 @@ function getFormControl(dbColumnDef, value, index) { $.each(dbColumnDef.options, function(index, obj) { if(value == obj.equals) { - result = `${obj.replacement}` + value = `${obj.replacement}` } }); break; + case 'regex': + for (const option of dbColumnDef.options) { + if (option.type === type) { + + const regexPattern = new RegExp(option.param); + const match = value.match(regexPattern); + if (match) { + // Return the first match + value = match[0]; + } + } + } + break; + default: - result = value; + value = value + `
` ; } + } - return result; + // Default behavior if no match is found + return value; } + + // ----------------------------------------------------------------------------- // Update the corresponding DB column and entry function saveData (id) { @@ -274,7 +327,7 @@ function generateTabs() for(var j=0;j' + clm += ''+ getFormControl(colDefinitions[j], pluginObjects[i][colDefinitions[j].column], pluginObjects[i]["Index"], colDefinitions, pluginObjects[i]) +'' } obRows += `${clm}` obCount++;