From ea0c3d3e9a44ba738c681b616c643afa6caa9a8a Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Wed, 28 Jun 2017 10:17:36 +0100 Subject: [PATCH] Check system time against NTP on startup --- EDMarketConnector.py | 5 +++++ L10n/en.template | 9 +++++++++ README.md | 6 +++--- ntp.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 ntp.py diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 6b7b818a..9f7750e0 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -57,6 +57,7 @@ import coriolis import eddb import edshipyard import loadout +from ntp import NTPCheck import stats import prefs import plug @@ -309,6 +310,10 @@ class AppWindow: except: if __debug__: print_exc() + if not NTPCheck(self.w): # Check system time + self.w.destroy() + return + self.postprefs(False) # Companion login happens in callback from monitor if keyring.get_keyring().priority < 1: diff --git a/L10n/en.template b/L10n/en.template index 42e6f967..1df03c72 100644 --- a/L10n/en.template +++ b/L10n/en.template @@ -52,6 +52,12 @@ /* Menu item. [EDMarketConnector.py] */ "Check for Updates..." = "Check for Updates..."; +/* Error message shown if system time is wrong. [ntp.py] */ +"Check your system's Date and Time settings." = "Check your system's Date and Time settings."; + +/* Error message shown if system time is wrong. [ntp.py] */ +"Check your system's Time Zone setting." = "Check your system's Time Zone setting."; + /* Federation rank. [stats.py] */ "Chief Petty Officer" = "Chief Petty Officer"; @@ -475,6 +481,9 @@ /* Appearance setting. [prefs.py] */ "Theme" = "Theme"; +/* Error message shown if system time is wrong. [ntp.py] */ +"This app requires accurate timestamps." = "This app requires accurate timestamps."; + /* Help text in settings. [prefs.py] */ "Tip: You can disable a plugin by{CR}adding '{EXT}' to it's folder name" = "Tip: You can disable a plugin by{CR}adding '{EXT}' to it's folder name"; diff --git a/README.md b/README.md index 08762db5..a505dcbd 100644 --- a/README.md +++ b/README.md @@ -178,17 +178,17 @@ Download and extract the source code of the [latest release](https://github.com/ Mac: -* Requires the Python “keyring”, “requests” and “watchdog” modules, plus an up-to-date “py2app” module if you also want to package the app - install these with `easy_install -U keyring requests watchdog py2app` . +* Requires the Python “keyring”, “ntplib”, “requests” and “watchdog” modules, plus an up-to-date “py2app” module if you also want to package the app - install these with `easy_install -U keyring ntplib requests watchdog py2app` . * Run with `./EDMarketConnector.py` . Windows: -* Requires Python2.7 and the Python “keyring”, “requests” and “watchdog” modules, plus “py2exe” 0.6 if you also want to package the app. +* Requires Python2.7 and the Python “keyring”, “ntplib”, “requests” and “watchdog” modules, plus “py2exe” 0.6 if you also want to package the app. * Run with `EDMarketConnector.py` . Linux: -* Requires the Python “imaging-tk”, “iniparse”, “keyring” and “requests” modules. On Debian-based systems install these with `sudo apt-get install python-imaging-tk python-iniparse python-keyring python-requests` . +* Requires the Python “imaging-tk”, “iniparse”, “keyring”, “ntplib” and “requests” modules. On Debian-based systems install these with `sudo apt-get install python-imaging-tk python-iniparse python-ntplib python-keyring python-requests` . * Run with `./EDMarketConnector.py` . Command-line diff --git a/ntp.py b/ntp.py new file mode 100644 index 00000000..5b8789d9 --- /dev/null +++ b/ntp.py @@ -0,0 +1,28 @@ +from ntplib import NTPClient +from tkMessageBox import showerror + +from config import applongname + +if __debug__: + from traceback import print_exc + +def NTPCheck(parent): + + DRIFT_THRESHOLD = 3 * 60 + TZ_THRESHOLD = 30 * 60 + CLOCK_THRESHOLD = 12 * 60 * 60 + DRIFT_THRESHOLD + + try: + response = NTPClient().request('pool.ntp.org') + if abs(response.offset) > DRIFT_THRESHOLD: + showerror(applongname, + _('This app requires accurate timestamps.') + '\n' + # Error message shown if system time is wrong + (TZ_THRESHOLD < abs(response.offset) < CLOCK_THRESHOLD and + _("Check your system's Time Zone setting.") or # Error message shown if system time is wrong + _("Check your system's Date and Time settings.")), # Error message shown if system time is wrong + parent = parent) + return False + except: + if __debug__: print_exc() + + return True