From 8490540384266282d67f5c3c2f8105845f817194 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Tue, 3 Jan 2023 23:58:35 +1100 Subject: [PATCH] version check --- Dockerfile | 7 +- back/pialert.py | 35 ++++++++ back/report_template.html | 4 +- back/report_template_new_version.html | 120 +++++++++++++++++++++++++ front/css/pialert.css | 15 +++- front/js/handle_version.js | 60 +++++++++++++ front/js/pialert_common.js | 65 +++++++++++++- front/maintenance.php | 7 ++ front/php/templates/footer.php | 1 + front/php/templates/header.php | 40 +++++---- front/php/templates/language/en_us.php | 4 + front/settings.php | 2 +- 12 files changed, 333 insertions(+), 27 deletions(-) create mode 100755 back/report_template_new_version.html create mode 100644 front/js/handle_version.js diff --git a/Dockerfile b/Dockerfile index 23128a9d..f903188e 100755 --- a/Dockerfile +++ b/Dockerfile @@ -8,8 +8,7 @@ ENV USER=pi USER_ID=1000 USER_GID=1000 TZ=Europe/London PORT=20211 RUN apt-get update \ && apt-get install --no-install-recommends tini ca-certificates curl libwww-perl arp-scan perl apt-utils cron sudo nginx-light php php-cgi php-fpm php-sqlite3 php-curl sqlite3 dnsutils net-tools python3 iproute2 nmap python3-pip zip -y \ - && pip3 install requests paho-mqtt scapy cron-converter pytz \ - # && pip3 install requests paho-mqtt ssdpy upnpclient \ + && pip3 install requests paho-mqtt scapy cron-converter pytz \ && update-alternatives --install /usr/bin/python python /usr/bin/python3 10 \ && apt-get clean autoclean \ && apt-get autoremove \ @@ -40,7 +39,9 @@ RUN rm /etc/nginx/sites-available/default \ # Create a backup of the pialert.conf to be used if the user didn't supply a configuration file && cp /home/pi/pialert/config/pialert.conf /home/pi/pialert/back/pialert.conf_bak \ # Create a backup of the pialert.db to be used if the user didn't supply a database - && cp /home/pi/pialert/db/pialert.db /home/pi/pialert/back/pialert.db_bak + && cp /home/pi/pialert/db/pialert.db /home/pi/pialert/back/pialert.db_bak \ + # Create a buildtimestamp.txt to later check if a new version was released + && date +%s > /home/pi/pialert/front/buildtimestamp.txt ENTRYPOINT ["tini", "--"] diff --git a/back/pialert.py b/back/pialert.py index 32cfa2d5..bd2b868e 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -641,6 +641,8 @@ def main (): # re-load user configuration importConfig() + isNewVersion() + # proceed if 1 minute passed if last_run + datetime.timedelta(minutes=1) < time_started : @@ -2224,6 +2226,9 @@ def send_notifications (): # Open html Template template_file = open(PIALERT_BACK_PATH + '/report_template.html', 'r') + if isNewVersion(): + template_file = open(PIALERT_BACK_PATH + '/report_template_new_version.html', 'r') + mail_html = template_file.read() template_file.close() @@ -2235,6 +2240,8 @@ def send_notifications (): mail_text = mail_text.replace ('', socket.gethostname() ) mail_html = mail_html.replace ('', socket.gethostname() ) + + if 'internet' in INCLUDED_SECTIONS: # Compose Internet Section mail_section_Internet = False @@ -3218,6 +3225,34 @@ def hide_email(email): return email +#------------------------------------------------------------------------------- +def isNewVersion(): + + f = open(pialertPath + '/front/buildtimestamp.txt', 'r') + buildTimestamp = int(f.read().strip()) + f.close() + + url = requests.get("https://api.github.com/repos/jokob-sk/Pi.Alert/releases") + text = url.text + + data = json.loads(text) + + # Debug + write_file (logPath + '/pialert_version_new.json', json.dumps(data)) + + # make sure we received a valid response and not an API rate limit exceeded message + if len(data) > 0: + + dateTimeStr = data[0]["published_at"] + + realeaseTimestamp = int(datetime.datetime.strptime(dateTimeStr, '%Y-%m-%dT%H:%M:%SZ').strftime('%s')) + + if realeaseTimestamp > buildTimestamp + 600: + file_print("here") + return True + + return False + #------------------------------------------------------------------------------- # Cron-like Scheduling #------------------------------------------------------------------------------- diff --git a/back/report_template.html b/back/report_template.html index 283d47a2..778bab3a 100755 --- a/back/report_template.html +++ b/back/report_template.html @@ -20,7 +20,7 @@ Pi.Alert Report - + @@ -36,7 +36,7 @@

Internet:

- + diff --git a/back/report_template_new_version.html b/back/report_template_new_version.html new file mode 100755 index 00000000..06dabe14 --- /dev/null +++ b/back/report_template_new_version.html @@ -0,0 +1,120 @@ + + + + + + + + + +
Event Type Datetime IP
+ + + + + + + + + + + + + + + + + +
+ Pi.Alert Report +
🆕 New version available 🆕
+ + + +
Report Date:
+
+ +

Internet:

+ + + + + + + + + + +
Event Type Datetime IP Additional Info
+ +
+
+ + +

New Devices:

+ + + + + + + + + + + +
MAC Datetime IP Device Name Vendor
+ +
+
+ + +

Devices Down:

+ + + + + + + + + + +
MAC Datetime IP Device Name
+ +
+
+ + +

Events:

+ + + + + + + + + + + + +
MAC Datetime IP Event Type Device Name Additional Info
+
+
+ + + + +
Pi.Alert -
+
+ + + \ No newline at end of file diff --git a/front/css/pialert.css b/front/css/pialert.css index 953c079c..c0ea945b 100755 --- a/front/css/pialert.css +++ b/front/css/pialert.css @@ -666,10 +666,10 @@ height: 50px; width:46%; } -/* .setting_input +.myhidden { - width:35%; -} */ + display:none; +} .center { @@ -702,4 +702,13 @@ height: 50px; .drp-edit { cursor: pointer; +} + +.new-version +{ + top: -6px; + position: absolute; + z-index: 1; + left: 0px; + font-size: large; } \ No newline at end of file diff --git a/front/js/handle_version.js b/front/js/handle_version.js new file mode 100644 index 00000000..5cf44897 --- /dev/null +++ b/front/js/handle_version.js @@ -0,0 +1,60 @@ +function handleVersion(){ + + release_timestamp = getCookie("release_timestamp") + + if(release_timestamp != "") + { + + build_timestamp = parseInt($('#version').attr("data-build-time").match( /\d+/g ).join('')) + + // if the release_timestamp is older by 10 min or more as the build timestamp then there is a new release available + if(release_timestamp > build_timestamp + 600 ) + { + console.log("New release!") + $('#version').attr("class", $('#version').attr("class").replace("myhidden", "")) + $('#new-version-text').attr("class", $('#new-version-text').attr("class").replace("myhidden", "")) + + } + else{ + console.log("All up-to-date!") + $('#current-version-text').attr("class", $('#current-version-text').attr("class").replace("myhidden", "")) + } + } + + } + + //-------------------------------------------------------------- + + function getVersion() + { + release_timestamp = getCookie("release_timestamp") + + // no cached value available + if(release_timestamp == "") + { + // get parameter value + $.get('https://api.github.com/repos/jokob-sk/Pi.Alert/releases', function(data) { + + var releases = data; + + if(releases.length > 0) + { + release_datetime = releases[0].published_at; + release_timestamp = new Date(release_datetime).getTime() / 1000; + + // cache value + setCookie("release_timestamp", release_timestamp, 5); + + handleVersion(); + } + }); + } else + { + // cache is available, just call the handler + handleVersion() + } + } + + +// handle the dispaly of the NEW icon +getVersion() \ No newline at end of file diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 57267b02..c2d6a807 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -34,11 +34,70 @@ function setCache(key, data) // ----------------------------------------------------------------------------- -function setCookie (cookie, value, expirationHours='') { + +function handleVersion(){ + + release_timestamp = getCookie("release_timestamp") + + if(release_timestamp != "") + { + + build_timestamp = parseInt($('#version').attr("data-build-time").match( /\d+/g ).join('')) + + // if the release_timestamp is older by 10 min or more as the build timestamp then there is a new release available + if(release_timestamp > build_timestamp + 600 ) + { + console.log("New release!") + $('#version').attr("class", $('#version').attr("class").replace("myhidden", "")) + + } + else{ + console.log("All up-to-date!") + $('#current-version-text').attr("class", $('#current-version-text').attr("class").replace("myhidden", "")) + // new-version-text current-version-text + } + } + +} + +//-------------------------------------------------------------- + +function getVersion() +{ + release_timestamp = getCookie("release_timestamp") + + // no cached value available + if(release_timestamp == "") + { + // get parameter value + $.get('https://api.github.com/repos/jokob-sk/Pi.Alert/releases', function(data) { + + var releases = data; + + if(releases.length > 0) + { + release_datetime = releases[0].published_at; + release_timestamp = new Date(release_datetime).getTime() / 1000; + + // cache value + setCookie("release_timestamp", release_timestamp ,5); + + handleVersion(); + } + }); + } else + { + // cache is available, just call the handler + handleVersion() + } +} + +// ----------------------------------------------------------------------------- +function setCookie (cookie, value, expirationMinutes='') { // Calc expiration date var expires = ''; - if (typeof expirationHours === 'number') { - expires = ';expires=' + new Date(Date.now() + expirationHours *60*60*1000).toUTCString(); + if (typeof expirationMinutes === 'number') { + expires = ';expires=' + new Date(Date.now() + expirationMinutes *60*1000).toUTCString(); } // Save Cookie diff --git a/front/maintenance.php b/front/maintenance.php index ea1f5632..dc3faaa4 100755 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -154,6 +154,12 @@ if (isset($_POST['submit']) && submit && isset($_POST['skinselector_set'])) {
+
+
+
+
">' .lang('Maintenance_new_version').''.'' .lang('Maintenance_current_version').'';?>
+
+
@@ -811,3 +817,4 @@ window.onload = function asyncFooter() + diff --git a/front/php/templates/footer.php b/front/php/templates/footer.php index ca2b9f15..bc9f6c14 100755 --- a/front/php/templates/footer.php +++ b/front/php/templates/footer.php @@ -51,6 +51,7 @@ + diff --git a/front/php/templates/header.php b/front/php/templates/header.php index f56ffdeb..c54fc57e 100755 --- a/front/php/templates/header.php +++ b/front/php/templates/header.php @@ -223,6 +223,7 @@ if ($ENABLED_DARKMODE === True) {
  • +
    ">🆕
  • @@ -239,31 +240,40 @@ if ($ENABLED_DARKMODE === True) { + diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index 9fc0931e..fc71b90b 100755 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -216,6 +216,9 @@ $lang['en_us'] = array( ////////////////////////////////////////////////////////////////// 'Maintenance_Title' => 'Maintenance tools', +'Maintenance_version' => 'Application updates', +'Maintenance_new_version' => '🆕 A new version is available. Check out the release notes.', +'Maintenance_current_version' => 'You are up-to-date. Check out what I\'m working on.', 'Maintenance_database_path' => 'Database-Path', 'Maintenance_database_size' => 'Database-Size', 'Maintenance_database_lastmod' => 'Last Modification', @@ -432,6 +435,7 @@ $lang['en_us'] = array( '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\'t save your settings without specifying all setting keys. Refresh the page. This is probably caused by a high load on the database.', 'settings_old' => 'The settings in the DB (shown on this page) are outdated. This is probably caused by a running scan. The settings were saved in the pialert.conf file, but the background process didn\'t have time to import it yet to the DB. You can wait until the settings get refreshed so you don\'t overwrite your old values. Feel free to save your settings either way if you don\'t mind losing the settings between the last save and now. There are also backup files created if you need to compare your settings later.', +'settings_imported' => 'Last time settings were imported from the pialert.conf file:', //General 'SCAN_SUBNETS_name' => 'Subnets to scan', diff --git a/front/settings.php b/front/settings.php index 41c4cd14..197e9789 100644 --- a/front/settings.php +++ b/front/settings.php @@ -51,7 +51,7 @@ CommitDB();

    - Last time imported from the pialert.conf file: +