From fac7501855aa913a213e9323d7e73832ba530588 Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Wed, 28 Jun 2017 13:36:41 +0100 Subject: [PATCH] Use EDDN gateway's idea of time-of-day rather than NTP's Reverts ea0c3d3 --- EDMarketConnector.py | 19 ++++++++++++++++--- L10n/en.template | 6 +++--- README.md | 6 +++--- eddn.py | 18 ++++++++++++++++-- ntp.py | 28 ---------------------------- 5 files changed, 38 insertions(+), 39 deletions(-) delete mode 100644 ntp.py diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 9f7750e0..9453d6c3 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -11,7 +11,7 @@ from os import chdir, mkdir, environ from os.path import dirname, expanduser, isdir, join import re import requests -from time import time, localtime, strftime, strptime +from time import gmtime, time, localtime, strftime, strptime from calendar import timegm import webbrowser @@ -35,6 +35,7 @@ import Tkinter as tk import ttk import tkFileDialog import tkFont +import tkMessageBox from ttkHyperlinkLabel import HyperlinkLabel if __debug__: @@ -57,7 +58,6 @@ import coriolis import eddb import edshipyard import loadout -from ntp import NTPCheck import stats import prefs import plug @@ -71,6 +71,11 @@ EDDB = eddb.EDDB() SERVER_RETRY = 5 # retry pause for Companion servers [s] EDSM_POLL = 0.1 +# Limits on local clock drift from EDDN gateway +DRIFT_THRESHOLD = 3 * 60 +TZ_THRESHOLD = 30 * 60 +CLOCK_THRESHOLD = 11 * 60 * 60 + TZ_THRESHOLD + class AppWindow: @@ -310,7 +315,15 @@ class AppWindow: except: if __debug__: print_exc() - if not NTPCheck(self.w): # Check system time + # Check system time + drift = abs(time() - self.eddn.time()) + if drift > DRIFT_THRESHOLD: + tkMessageBox.showerror(applongname, + _('This app requires accurate timestamps.') + '\n' + # Error message shown if system time is wrong + (TZ_THRESHOLD < drift < 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 = self.w) self.w.destroy() return diff --git a/L10n/en.template b/L10n/en.template index 1df03c72..2b4f2366 100644 --- a/L10n/en.template +++ b/L10n/en.template @@ -52,10 +52,10 @@ /* Menu item. [EDMarketConnector.py] */ "Check for Updates..." = "Check for Updates..."; -/* Error message shown if system time is wrong. [ntp.py] */ +/* Error message shown if system time is wrong. [EDMarketConnector.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] */ +/* Error message shown if system time is wrong. [EDMarketConnector.py] */ "Check your system's Time Zone setting." = "Check your system's Time Zone setting."; /* Federation rank. [stats.py] */ @@ -481,7 +481,7 @@ /* Appearance setting. [prefs.py] */ "Theme" = "Theme"; -/* Error message shown if system time is wrong. [ntp.py] */ +/* Error message shown if system time is wrong. [EDMarketConnector.py] */ "This app requires accurate timestamps." = "This app requires accurate timestamps."; /* Help text in settings. [prefs.py] */ diff --git a/README.md b/README.md index a505dcbd..08762db5 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”, “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` . +* 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` . * Run with `./EDMarketConnector.py` . Windows: -* Requires Python2.7 and the Python “keyring”, “ntplib”, “requests” and “watchdog” modules, plus “py2exe” 0.6 if you also want to package the app. +* Requires Python2.7 and the Python “keyring”, “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”, “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` . +* 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` . * Run with `./EDMarketConnector.py` . Command-line diff --git a/eddn.py b/eddn.py index 68cdcd34..3b4cfd3b 100644 --- a/eddn.py +++ b/eddn.py @@ -10,6 +10,7 @@ import re import requests from sys import platform import time +from calendar import timegm import uuid if platform != 'win32': @@ -29,8 +30,10 @@ replayfile = None # For delayed messages class EDDN: - ### UPLOAD = 'http://localhost:8081/upload/' # testing - UPLOAD = 'http://eddn-gateway.elite-markets.net:8080/upload/' + ### SERVER = 'http://localhost:8081/' # testing + SERVER = 'http://eddn-gateway.elite-markets.net:8080/' + UPLOAD = '%s/upload/' % SERVER + HEALTH = '%s/health_check/' % SERVER REPLAYPERIOD = 400 # Roughly two messages per second, accounting for send delays [ms] REPLAYFLUSH = 20 # Update log on disk roughly every 10 seconds @@ -76,6 +79,17 @@ class EDDN: replayfile.close() replayfile = None + def time(self): + # Returns the EDDN gateway's idea of time-of-day. + # Assumes that the gateway returns a strictly compliant Date - https://tools.ietf.org/html/rfc7231#section-7.1.1.1 + try: + r = self.session.get(self.HEALTH, timeout=timeout) + return timegm(time.strptime(r.headers['Date'], "%a, %d %b %Y %H:%M:%S GMT")) + except: + # On any error assume that we're good + if __debug__: print_exc() + return time.time() + def send(self, cmdr, msg): if config.getint('anonymous'): uploaderID = config.get('uploaderID') diff --git a/ntp.py b/ntp.py deleted file mode 100644 index 5b8789d9..00000000 --- a/ntp.py +++ /dev/null @@ -1,28 +0,0 @@ -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