diff --git a/README.md b/README.md index 502c63c0..7e9521bc 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Pi.Alert -WIFI / LAN intruder detector. +💻🔍 WIFI / LAN intruder detector. Scan the devices connected to your WIFI / LAN and alert you the connection of unknown devices. It also warns if a "always connected" devices disconnects. @@ -9,14 +9,14 @@ unknown devices. It also warns if a "always connected" devices disconnects. ![Main screen][main] -# Docker image 🐳 +# 🐳 Docker image [![Docker](https://img.shields.io/github/workflow/status/jokob-sk/Pi.Alert/docker?label=Build&logo=GitHub)](https://github.com/jokob-sk/Pi.Alert/actions/workflows/docker.yml) [![GitHub Committed](https://img.shields.io/github/last-commit/jokob-sk/Pi.Alert?color=40ba12&label=Committed&logo=GitHub&logoColor=fff)](https://github.com/jokob-sk/Pi.Alert) [![Docker Size](https://img.shields.io/docker/image-size/jokobsk/pi.alert?label=Size&logo=Docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert) [![Docker Pulls](https://img.shields.io/docker/pulls/jokobsk/pi.alert?label=Pulls&logo=docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert) [![Docker Pushed](https://img.shields.io/badge/dynamic/json?color=0aa8d2&logoColor=fff&label=Pushed&query=last_updated&url=https%3A%2F%2Fhub.docker.com%2Fv2%2Frepositories%2Fjokobsk%2Fpi.alert%2F&logo=docker&link=http://left&link=https://hub.docker.com/repository/docker/jokobsk/pi.alert)](https://hub.docker.com/r/jokobsk/pi.alert) -🐳 Docker hub: [jokobsk/Pi.Alert](https://registry.hub.docker.com/r/jokobsk/pi.alert)
+🐳 [Docker hub](https://registry.hub.docker.com/r/jokobsk/pi.alert)
📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile)
📚 [Docker instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md) @@ -32,7 +32,7 @@ The system continuously scans the network for: - Devices IP changes - Internet IP address changes -## Scan Methods +## 🔍 Scan Methods Up to three scanning methods are used: - **Method 1: arp-scan**. The arp-scan system utility is used to search for devices on the network using arp frames. @@ -46,7 +46,7 @@ Up to three scanning methods are used: that were not discovered by the other methods. -### Notification Service Support +### 🧩 Integrations - [Apprise](https://hub.docker.com/r/caronc/apprise) - [Pushsafer](https://www.pushsafer.com/) - [NTFY](https://ntfy.sh/) @@ -56,13 +56,13 @@ Up to three scanning methods are used: | ![Report 1][report1] | ![Report 2][report2] | | -------------------- | -------------------- | -### Security +### 🔐 Security There is a configurable login to prevent unauthorized use. > * Set `PIALERT_WEB_PROTECTION = True` in `pialert.conf` to enable. The default password is `123456`. > To change password run `/home/pi/pialert/back/pialert-cli` -## Features +## 📑 Features - Display in a visual way all the information collected by the back - Sessions, Connected devices, Favorites - Events, Presence, Concurrent devices @@ -85,12 +85,10 @@ There is a configurable login to prevent unauthorized use. | ![Screen 3][screen3] | ![Screen 4][screen4] | | ![Screen 5][screen5] | ![Screen 6][screen6] | -# Installation +# 📥 Installation -Initially designed to run on a Raspberry Pi, probably it can run on many other -Linux distributions. -> ⚠ Please note, this [fork (jokob-sk)](https://github.com/jokob-sk/Pi.Alert) is only tested via the [docker install method](dockerfiles/README.md). Check out [leiweibau's fork](https://github.com/leiweibau/Pi.Alert/) if you want to install Pi.Alert on the server directly. + ⚠ Please note, this [fork (jokob-sk)](https://github.com/jokob-sk/Pi.Alert) is only tested via the [docker install method](dockerfiles/README.md). Check out [leiweibau's fork](https://github.com/leiweibau/Pi.Alert/) if you want to install Pi.Alert on the server directly. Instructions for [pucherot's original code can be found here](https://github.com/pucherot/Pi.Alert/) @@ -115,7 +113,7 @@ Instructions for [pucherot's original code can be found here](https://github.com https://github.com/adobe-fonts/source-sans -### Special thanks 🥇 +### 🥇 Special thanks This code is a collaborative body of work, with special thanks to: diff --git a/back/pialert.py b/back/pialert.py index e419faf5..43c1640c 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -62,6 +62,11 @@ try: except NameError: # variable not defined, use a default strdaystokeepEV = str(90) +try: + network_scan_minutes = SCAN_CYCLE_MINUTES +except NameError: + network_scan_minutes = 5 + # Which sections to include in the reports. Include everything by default try: includedSections = INCLUDED_SECTIONS @@ -146,6 +151,11 @@ try: except NameError: mqttQoS = 0 +try: + mqttDelay = MQTT_DELAY_SEC +except NameError: + mqttDelay = 2 + #=============================================================================== @@ -153,7 +163,6 @@ except NameError: #=============================================================================== cycle = "" check_report = [1, "internet_IP", "update_vendors_silent"] -network_scan_minutes = 5 mqtt_thread_up = False # timestamps of last execution times @@ -166,8 +175,6 @@ last_run = now_minus_24h last_cleanup = now_minus_24h last_update_vendors = time_now - timedelta(days = 7) -debug_once = False - def main (): # Initialize global variables global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors, network_scan_minutes, mqtt_thread_up @@ -188,7 +195,7 @@ def main (): # Header print ('\nLoop start') - print ('---------------------------------------------------------') + print ('---------------------------') log_timestamp = time_now @@ -219,16 +226,16 @@ def main (): last_network_scan = time_now cycle = 1 # network scan scan_network() + + # Reporting + if cycle in check_report: + email_reporting() # clean up the DB once a day if last_cleanup + timedelta(hours = 24) < time_now: last_cleanup = time_now cycle = 'cleanup' - cleanup_database() - - # Reporting - if cycle in check_report: - email_reporting() + cleanup_database() # Close SQL closeDB() @@ -240,7 +247,7 @@ def main (): # Footer print ('\nLoop end') - print ('---------------------------------------------------------') + print ('---------------------------') else: # do something cycle = "" @@ -1625,10 +1632,8 @@ def email_reporting (): # write_file (LOG_PATH + '/report_output.txt', mail_text) # write_file (LOG_PATH + '/report_output.html', mail_html) - # Send Mail - if mail_section_Internet == True or mail_section_new_devices == True \ - or mail_section_devices_down == True or mail_section_events == True : + if json_internet != [] or json_new_devices != [] or json_down_devices != [] or json_events != []: print ('\nChanges detected, sending reports...') if REPORT_MAIL : @@ -1880,7 +1885,7 @@ def send_apprise (html): print(e.output) #------------------------------------------------------------------------------- -mqtt_connected_to_broker = 0 +mqtt_connected_to_broker = False mqtt_sensors = [] def publish_mqtt(client, topic, message): @@ -1930,8 +1935,7 @@ def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon): break # save if unique - if is_unique: - mqtt_sensors.append(new_sensor_config) + if is_unique: publish_sensor(client, new_sensor_config) @@ -1946,7 +1950,9 @@ class sensor_config: self.hash = str(hash(str(deviceId) + str(deviceName)+ str(sensorType)+ str(sensorName)+ str(icon))) #------------------------------------------------------------------------------- -def publish_sensor(client, sensorConf): +def publish_sensor(client, sensorConf): + + global mqtt_sensors message = '{ \ "name":"'+ sensorConf.deviceName +' '+sensorConf.sensorName+'", \ @@ -1964,24 +1970,32 @@ def publish_sensor(client, sensorConf): topic='homeassistant/'+sensorConf.sensorType+'/'+sensorConf.deviceId+'/'+sensorConf.sensorName+'/config' - publish_mqtt(client, topic, message) - + # add the sensor to the global list to keep track of succesfully added sensors + if publish_mqtt(client, topic, message): + # hack - delay adding to the queue in case the process is + time.sleep(mqttDelay) # restarted and previous publish processes aborted + # (it takes ~2s to update a sensor config on the broker) + mqtt_sensors.append(sensorConf) + # print(len(mqtt_sensors)) #------------------------------------------------------------------------------- -def mqtt_start(): +def mqtt_create_client(): def on_disconnect(client, userdata, rc): global mqtt_connected_to_broker - mqtt_connected_to_broker = 0 + mqtt_connected_to_broker = False + + # not sure is below line is correct / necessary + # client = mqtt_create_client() def on_connect(client, userdata, flags, rc): global mqtt_connected_to_broker if rc == 0: - # print("Connected to broker") - mqtt_connected_to_broker = True #Signal connection - + print("Connected to broker") + mqtt_connected_to_broker = True # Signal connection else: print("Connection failed") + mqtt_connected_to_broker = False client = mqtt_client.Client(mqttClientId) # Set Connecting Client ID @@ -1989,7 +2003,18 @@ def mqtt_start(): client.on_connect = on_connect client.on_disconnect = on_disconnect client.connect(mqttBroker, mqttPort) - client.loop_start() + client.loop_start() + + return client + +#------------------------------------------------------------------------------- +def mqtt_start(): + + global client, mqtt_connected_to_broker + + if mqtt_connected_to_broker == False: + mqtt_connected_to_broker = True + client = mqtt_create_client() # General stats diff --git a/dockerfiles/README.md b/dockerfiles/README.md index 5dfa7279..eedfa6a4 100755 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -6,7 +6,7 @@ # 🐳 A docker image for Pi.Alert -🐳 Docker hub: [jokobsk/Pi.Alert](https://registry.hub.docker.com/r/jokobsk/pi.alert)
+🐳 [Docker hub](https://registry.hub.docker.com/r/jokobsk/pi.alert)
📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile)
📚 [Docker instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md) diff --git a/dockerfiles/start.sh b/dockerfiles/start.sh index d5c656b1..0cc460d9 100755 --- a/dockerfiles/start.sh +++ b/dockerfiles/start.sh @@ -16,4 +16,6 @@ chown -R www-data:www-data /home/pi/pialert/db/pialert.db /etc/init.d/php7.4-fpm start /etc/init.d/nginx start +# cron -f + python /home/pi/pialert/back/pialert.py > /home/pi/pialert/log/pialert.log 2>&1