From dda440eb53fc2301cb5797620c7047b64f4559f9 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Fri, 27 Dec 2024 14:05:49 +1100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8CLUCIRPC=20-=20OpenWRT=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- Dockerfile.debian | 2 +- front/css/app.css | 20 +- front/devices.php | 2 +- front/php/templates/language/fr_fr.json | 0 front/plugins/README.md | 4 +- front/plugins/custom_props/README.md | 2 +- front/plugins/luci_import/README.md | 9 + front/plugins/luci_import/config.json | 545 ++++++++++++++++++++++++ front/plugins/luci_import/script.py | 74 ++++ install/install_dependencies.debian.sh | 2 +- 11 files changed, 655 insertions(+), 7 deletions(-) mode change 100644 => 100755 front/php/templates/language/fr_fr.json create mode 100755 front/plugins/luci_import/README.md create mode 100755 front/plugins/luci_import/config.json create mode 100755 front/plugins/luci_import/script.py diff --git a/Dockerfile b/Dockerfile index 13a330b4..13c53e24 100755 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ ENV PATH="/opt/venv/bin:$PATH" COPY . ${INSTALL_DIR}/ -RUN pip install graphene flask netifaces tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros git+https://github.com/foreign-sub/aiofreepybox.git \ +RUN pip install OpenWrtRpc graphene flask netifaces tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros git+https://github.com/foreign-sub/aiofreepybox.git \ && bash -c "find ${INSTALL_DIR} -type d -exec chmod 750 {} \;" \ && bash -c "find ${INSTALL_DIR} -type f -exec chmod 640 {} \;" \ && bash -c "find ${INSTALL_DIR} -type f \( -name '*.sh' -o -name '*.py' -o -name 'speedtest-cli' \) -exec chmod 750 {} \;" diff --git a/Dockerfile.debian b/Dockerfile.debian index 789fa351..af997196 100755 --- a/Dockerfile.debian +++ b/Dockerfile.debian @@ -43,7 +43,7 @@ RUN phpenmod -v 8.2 sqlite3 RUN apt-get install -y python3-venv RUN python3 -m venv myenv -RUN /bin/bash -c "source myenv/bin/activate && update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && pip3 install tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros " +RUN /bin/bash -c "source myenv/bin/activate && update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && pip3 install OpenWrtRpc graphene flask netifaces tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros " # Create a buildtimestamp.txt to later check if a new version was released RUN date +%s > ${INSTALL_DIR}/front/buildtimestamp.txt diff --git a/front/css/app.css b/front/css/app.css index b8a604f4..7088a33e 100755 --- a/front/css/app.css +++ b/front/css/app.css @@ -1409,6 +1409,23 @@ input[readonly] { transition: box-shadow 0.1s ease-in-out, background-color 0.1s ease-in-out; } +.devicePropAction +{ + width: 1.2em; + height: 1.2em; + display: inline-block; + padding: 0.1em; + margin: 0.1em; +} + +.devicePropAction:hover +{ + font-size: larger; + padding: 0em; + margin: 0em; + +} + /* ----------------------------------------------------------------- */ /* MODAL popups */ /* ----------------------------------------------------------------- */ @@ -1746,7 +1763,8 @@ input[readonly] { table.dataTable tbody > tr.selected { - color:red; + /* color:red; */ + color: #353c42; } /* ----------------------------------------------------------------------------- diff --git a/front/devices.php b/front/devices.php index 60861254..2dd99ee2 100755 --- a/front/devices.php +++ b/front/devices.php @@ -925,7 +925,7 @@ function renderCustomProps(custProps, mac) { break; } - html += ` ${atob(propMap["CUSTPROP_icon"])} `; + html += `
${atob(propMap["CUSTPROP_icon"])}
`; } }); diff --git a/front/php/templates/language/fr_fr.json b/front/php/templates/language/fr_fr.json old mode 100644 new mode 100755 diff --git a/front/plugins/README.md b/front/plugins/README.md index 4aa648ce..f40d0b05 100755 --- a/front/plugins/README.md +++ b/front/plugins/README.md @@ -29,15 +29,17 @@ Device-detecting plugins insert values into the `CurrentScan` database table. T | `ARPSCAN` | 🔍 | ARP-scan on current network | | | Script | [arp_scan](/front/plugins/arp_scan/) | | `AVAHISCAN` | 🆎 | Avahi (mDNS-based) name resolution | | | Script | [avahi_scan](/front/plugins/avahi_scan/) | | `CSVBCKP` | ⚙ | CSV devices backup | | | Script | [csv_backup](/front/plugins/csv_backup/) | +| `CUSTPROP` | ⚙ | Managing custom device properties values | | Yes | Template | [custom_props](/front/plugins/custom_props/) | | `DBCLNP` | ⚙ | Database cleanup | | Yes* | Script | [db_cleanup](/front/plugins/db_cleanup/) | | `DDNS` | ⚙ | DDNS update | | | Script | [ddns_update](/front/plugins/ddns_update/) | | `DHCPLSS` | 🔍/📥/🆎| Import devices from DHCP leases | | | Script | [dhcp_leases](/front/plugins/dhcp_leases/) | | `DHCPSRVS` | ♻ | DHCP servers | | | Script | [dhcp_servers](/front/plugins/dhcp_servers/) | -| `FREEBOX` | 🔍/♻/🆎 | Pull data and names from Freebox/Iliadbox | | | Script | [freebox](/front/plugins/freebox/) | +| `FREEBOX` | 🔍/♻/🆎| Pull data and names from Freebox/Iliadbox | | | Script | [freebox](/front/plugins/freebox/) | | `ICMP` | 🔍 | ICMP (ping) status checker | | | Script | [icmp_scan](/front/plugins/icmp_scan/) | | `INTRNT` | 🔍 | Internet IP scanner | | | Script | [internet_ip](/front/plugins/internet_ip/) | | `INTRSPD` | ♻ | Internet speed test | | | Script | [internet_speedtest](/front/plugins/internet_speedtest/) | | `IPNEIGH` | 🔍 | Scan ARP (IPv4) and NDP (IPv6) tables | | | Script | [ipneigh](/front/plugins/ipneigh/) | +| `LUCIRPC` | 🔍 | Import connected devices from OpenWRT | | | Script | [luci_import](/front/plugins/luci_import/) | | `MAINT` | ⚙ | Maintenance of logs, etc. | | | Script | [maintenance](/front/plugins/maintenance/) | | `MQTT` | ▶️ | MQTT for synching to Home Assistant | | | Script | [_publisher_mqtt](/front/plugins/_publisher_mqtt/) | | `NBTSCAN` | 🆎 | Nbtscan (NetBIOS-based) name resolution | | | Script | [nbtscan_scan](/front/plugins/nbtscan_scan/) | diff --git a/front/plugins/custom_props/README.md b/front/plugins/custom_props/README.md index 068ab5d1..946709d1 100755 --- a/front/plugins/custom_props/README.md +++ b/front/plugins/custom_props/README.md @@ -1,6 +1,6 @@ ## Overview -Settings related to the custom properties functionality on a device. +Settings related to the custom properties functionality on a device. A user guide can be found here: [Custom Properties](/docs/CUSTOM_PROPERTIES.md) ### Usage diff --git a/front/plugins/luci_import/README.md b/front/plugins/luci_import/README.md new file mode 100755 index 00000000..e8e3decf --- /dev/null +++ b/front/plugins/luci_import/README.md @@ -0,0 +1,9 @@ +## Overview + +The plugin is used to import connected devices from OpenWRT + +### Other info + +- Author : [vaga9938](https://github.com/vaga9938) +- Date : 28-Dec-2024 - version 1.0 + diff --git a/front/plugins/luci_import/config.json b/front/plugins/luci_import/config.json new file mode 100755 index 00000000..257d94cf --- /dev/null +++ b/front/plugins/luci_import/config.json @@ -0,0 +1,545 @@ +{ + "code_name": "luci_import", + "show_ui": true, + "unique_prefix": "LUCIRPC", + "plugin_type": "device_scanner", + "execution_order" : "Layer_1", + "enabled": true, + "data_source": "script", + "mapped_to_table": "CurrentScan", + "data_filters": [ + { + "compare_column": "Object_PrimaryID", + "compare_operator": "==", + "compare_field_id": "txtMacFilter", + "compare_js_template": "'{value}'.toString()", + "compare_use_quotes": true + } + ], + "localized": ["display_name", "description", "icon"], + "display_name": [ + { + "language_code": "en_us", + "string": "Luci rpc import (Device import)" + }, + { + "language_code": "ru_ru", + "string": "Luci rpc импорт (Device import)" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "The plugin is used to import connected devices from OpenWRT" + }, + { + "language_code": "ru_ru", + "string": "Плагин используется для импорта подключённых устройств из OpenWRT" + } + ], + "icon": [ + { + "language_code": "en_us", + "string": "" + }, + { + "language_code": "ru_ru", + "string": "" + } + ], + "params": [ + ], + "settings": [ + { + "function": "RUN", + "events": ["run"], + "type": { + "dataType": "string", + "elements": [ + { "elementType": "select", "elementOptions": [], "transformers": [] } + ] + }, + "default_value": "disabled", + "options": [ + "disabled", + "once", + "schedule" + ], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "When to run" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "When the plugin should run. Good options are schedule" + }, + { + "language_code": "ru_ru", + "string": "Когда должен запуститься плагин. Хорошими вариантами являются schedule" + } + ] + }, + { + "function": "RUN_SCHD", + "type": { + "dataType": "string", + "elements": [ + { "elementType": "input", "elementOptions": [], "transformers": [] } + ] + }, + "default_value": "*/1 * * * *", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Schedule" + }, + { + "language_code": "ru_ru", + "string": "Schedule" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Only enabled if you select schedule in the SYNC_RUN setting. Make sure you enter the schedule in the correct cron-like format (e.g. validate at crontab.guru). For example entering 0 4 * * * will run the scan after 4 am in the TIMEZONE you set above. Will be run NEXT time the time passes." + }, + { + "language_code": "ru_ru", + "string": "Доступно только в том случае, если вы выбрали schedule в SYNC_RUN setting. Убедитесь, что вы вводите расписание в правильном формате, похожем на cron (например, проверьте на crontab.guru). Например, если вы введете 0 4 * * *, то сканирование будет запускаться после 4 часов утра в TIMEZONE который вы установили выше. Сканирование будет запускаться СЛЕДУЮЩИЙ РАЗ, когда пройдет это время." + } + ] + }, + { + "function": "CMD", + "type": { + "dataType": "string", + "elements": [ + { + "elementType": "input", + "elementOptions": [{ "readonly": "true" }], + "transformers": [] + } + ] + }, + "default_value": "python3 /app/front/plugins/luci_import/script.py", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Command" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Command to run. This can not be changed" + } + , + { + "language_code": "ru_ru", + "string": "Команда для запуска. Это невозможно изменить" + } + ] + }, + { + "function": "RUN_TIMEOUT", + "type": { + "dataType": "integer", + "elements": [ + { + "elementType": "input", + "elementOptions": [{ "type": "number" }], + "transformers": [] + } + ] + }, + "default_value": 30, + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Run timeout" + }, + { + "language_code": "ru_ru", + "string": "Время ожидания выполнения" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Maximum time in seconds to wait for the script to finish. If this time is exceeded the script is aborted." + }, + { + "language_code": "ru_ru", + "string": "Максимальное время в секундах, в течение которого нужно ждать завершения работы скрипта. Если это время превышено, скрипт прерывается." + } + ] + }, + { + "function": "host", + "type": { + "dataType": "string", + "elements": [ + { "elementType": "input", "elementOptions": [], "transformers": [] } + ] + }, + "default_value": "192.168.1.1", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Router ip" + }, + { + "language_code": "ru_ru", + "string": "Ip адрес роутера" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Router ip(do not include http:// or https://)." + }, + { + "language_code": "ru_ru", + "string": "Ip адрес роутера(без http:// и https://)." + } + ] + }, + { + "function": "user", + "type": { + "dataType": "string", + "elements": [ + { "elementType": "input", "elementOptions": [], "transformers": [] } + ] + }, + "default_value": "root", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Router user" + }, + { + "language_code": "ru_ru", + "string": "Имя пользователя для подлючения к роутеру" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "User name used to login into your router." + }, + { + "language_code": "ru_ru", + "string": "Имя пользователя для подлючения к роутеру." + } + ] + }, + { + "function": "password", + "type": { + "dataType": "string", + "elements": [ + { + "elementType": "input", + "elementOptions": [{ "type": "password" }], + "transformers": [] + } + ] + }, + "default_value": "", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Router password" + }, + { + "language_code": "ru_ru", + "string": "Пароль роутера" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Password used to login into your router." + }, + { + "language_code": "ru_ru", + "string": "Пароль для подлючения к роутеру." + } + ] + }, + { + "function": "ssl", + "type": { + "dataType": "boolean", + "elements": [ + { + "elementType": "input", + "elementOptions": [{ "type": "checkbox" }], + "transformers": [] + } + ] + }, + "default_value": false, + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Router SSL" + }, + { + "language_code": "ru_ru", + "string": "Роутер SSL." + } + ], + "description": [ + { + "language_code": "en_us", + "string": "If your router enforces SSL connections." + }, + { + "language_code": "ru_ru", + "string": "Если роутер поддерживает SSL подключение." + } + ] + }, + { + "function": "verify_ssl", + "type": { + "dataType": "boolean", + "elements": [ + { + "elementType": "input", + "elementOptions": [{ "type": "checkbox" }], + "transformers": [] + } + ] + }, + "default_value": true, + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "Verify SSL" + }, + { + "language_code": "ru_ru", + "string": "Проверка SSL" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "If SSL/TLS verification for HTTPS resources needs to be turned off (for self-signed certs, etc.)" + }, + { + "language_code": "ru_ru", + "string": "Если необходимо отключить проверку SSL/TLS для ресурсов HTTPS (для самоподписанных сертификатов и т. д.)" + } + ] + } + ], + "database_column_definitions": [ + { + "column": "Index", + "css_classes": "col-sm-2", + "show": true, + "type": "none", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Index" + }, + { + "language_code": "ru_ru", + "string": "Индекс" + } + ] + }, + { + "column": "Object_PrimaryID", + "mapped_to_column": "cur_MAC", + "css_classes": "col-sm-2", + "show": true, + "type": "device_name_mac", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "MAC" + }, + { + "language_code": "ru_ru", + "string": "MAC" + } + ] + }, + { + "column": "Object_SecondaryID", + "mapped_to_column": "cur_IP", + "css_classes": "col-sm-2", + "show": true, + "type": "label", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "IP" + }, + { + "language_code": "ru_ru", + "string": "IP" + } + ] + }, + { + "column": "Watched_Value1", + "css_classes": "col-sm-2", + "show": true, + "type": "label", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Ip host" + }, + { + "language_code": "ru_ru", + "string": "Ip хоста" + } + ] + }, + { + "column": "Watched_Value2", + "mapped_to_column": "cur_NAME", + "css_classes": "col-sm-2", + "show": true, + "type": "label", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Name" + }, + { + "language_code": "ru_ru", + "string": "Имя" + } + ] + }, + { + "column": "Dummy", + "mapped_to_column": "cur_ScanMethod", + "mapped_to_column_data": { + "value": "LUCIRPC" + }, + "css_classes": "col-sm-2", + "show": true, + "type": "label", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Scan method" + }, + { + "language_code": "ru_ru", + "string": "Метод сканирования" + } + ] + }, + { + "column": "DateTimeCreated", + "css_classes": "col-sm-2", + "show": true, + "type": "label", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Created" + }, + { + "language_code": "ru_ru", + "string": "Создано" + } + ] + }, + { + "column": "DateTimeChanged", + "css_classes": "col-sm-2", + "show": true, + "type": "label", + "default_value": "", + "options": [], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Changed" + }, + { + "language_code": "ru_ru", + "string": "Изменено" + } + ] + }, + { + "column": "Status", + "css_classes": "col-sm-1", + "show": true, + "type": "replace", + "default_value": "", + "options": [ + { + "equals": "watched-not-changed", + "replacement": "
" + } + ], + "localized": ["name"], + "name": [ + { + "language_code": "en_us", + "string": "Status" + }, + { + "language_code": "ru_ru", + "string": "Статус" + } + ] + } + ] + } \ No newline at end of file diff --git a/front/plugins/luci_import/script.py b/front/plugins/luci_import/script.py new file mode 100755 index 00000000..74dbe62b --- /dev/null +++ b/front/plugins/luci_import/script.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +import os +import sys + + +INSTALL_PATH="/app" +sys.path.extend([f"{INSTALL_PATH}/front/plugins", f"{INSTALL_PATH}/server"]) +pluginName = 'LUCIRPC' + +from plugin_helper import Plugin_Object, Plugin_Objects, decodeBase64 +from logger import mylog, Logger, append_line_to_file +from helper import timeNowTZ, get_setting_value +from const import logPath, applicationPath +import conf +from pytz import timezone + +try: + from openwrt_luci_rpc import OpenWrtRpc +except: + mylog('error', [f'[{pluginName}] Failed import openwrt_luci_rpc']) + exit() + +conf.tz = timezone(get_setting_value('TIMEZONE')) + +Logger(get_setting_value('LOG_LEVEL')) + +LOG_PATH = logPath + '/plugins' +LOG_FILE = os.path.join(LOG_PATH, f'script.{pluginName}.log') +RESULT_FILE = os.path.join(LOG_PATH, f'last_result.{pluginName}.log') + +plugin_objects = Plugin_Objects(RESULT_FILE) + +def main(): + mylog('verbose', [f'[{pluginName}] start script.']) + + device_data = get_device_data() + + for entry in device_data: + mylog('verbose', [f'[{pluginName}] found: ', str(entry.mac).lower()]) + plugin_objects.add_object( + primaryId = str(entry.mac).lower(), + secondaryId = entry.ip, + watched1 = entry.host, + watched2 = str(entry.hostname), + watched3 = "", + watched4 = "", + extra = pluginName, + foreignKey = str(entry.mac).lower()) + + plugin_objects.write_result_file() + + mylog('verbose', [f'[{pluginName}] Script finished']) + + return 0 + +def get_device_data(): + router = OpenWrtRpc( + get_setting_value("LUCIRPC_host"), + get_setting_value("LUCIRPC_user"), + get_setting_value("LUCIRPC_password"), + get_setting_value("LUCIRPC_ssl"), + get_setting_value("LUCIRPC_verify_ssl") + ) + + if router.is_logged_in(): + mylog('verbose', [f'[{pluginName}] login successfully.']) + else: + mylog('error', [f'[{pluginName}] login fail.']) + + device_data = router.get_all_connected_devices(only_reachable=True) + return device_data + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/install/install_dependencies.debian.sh b/install/install_dependencies.debian.sh index d91cbdcd..d8f5ef90 100755 --- a/install/install_dependencies.debian.sh +++ b/install/install_dependencies.debian.sh @@ -30,5 +30,5 @@ source myenv/bin/activate update-alternatives --install /usr/bin/python python /usr/bin/python3 10 # install packages thru pip3 -pip3 install graphene flask netifaces tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros git+https://github.com/foreign-sub/aiofreepybox.git +pip3 install OpenWrtRpc graphene flask netifaces tplink-omada-client wakeonlan pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython librouteros git+https://github.com/foreign-sub/aiofreepybox.git