Make UpdateChecker use the shared Onion object instead of creating a new one

This commit is contained in:
Micah Lee 2017-05-14 19:54:12 -07:00
parent 224f2bb0ad
commit 68e02dab1f
5 changed files with 34 additions and 51 deletions

View file

@ -26,8 +26,9 @@ class Menu(QtWidgets.QMenuBar):
""" """
OnionShare's menu bar. OnionShare's menu bar.
""" """
def __init__(self, qtapp): def __init__(self, onion, qtapp):
super(Menu, self).__init__() super(Menu, self).__init__()
self.onion = onion
self.qtapp = qtapp self.qtapp = qtapp
file_menu = self.addMenu(strings._('gui_menu_file_menu', True)) file_menu = self.addMenu(strings._('gui_menu_file_menu', True))
@ -41,7 +42,7 @@ class Menu(QtWidgets.QMenuBar):
""" """
Settings action triggered. Settings action triggered.
""" """
SettingsDialog(self.qtapp) SettingsDialog(self.onion, self.qtapp)
def quit(self): def quit(self):
""" """

View file

@ -64,7 +64,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
tor_con.start() tor_con.start()
# Menu bar # Menu bar
self.setMenuBar(Menu(self.qtapp)) self.setMenuBar(Menu(self.onion, self.qtapp))
# Check for updates in a new thread, if enabled # Check for updates in a new thread, if enabled
system = platform.system() system = platform.system()
@ -73,7 +73,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
def update_available(update_url, installed_version, latest_version): def update_available(update_url, installed_version, latest_version):
Alert(strings._("update_available", True).format(update_url, installed_version, latest_version)) Alert(strings._("update_available", True).format(update_url, installed_version, latest_version))
t = UpdateThread() t = UpdateThread(self.onion)
t.update_available.connect(update_available) t.update_available.connect(update_available)
t.start() t.start()
@ -156,7 +156,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
a.exec_() a.exec_()
if a.clickedButton() == settings_button: if a.clickedButton() == settings_button:
SettingsDialog(self.qtapp) SettingsDialog(self.onion, self.qtapp)
else: else:
self.qtapp.quit() self.qtapp.quit()
@ -168,7 +168,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
The TorConnectionDialog wants to open the Settings dialog The TorConnectionDialog wants to open the Settings dialog
""" """
def open_settings(): def open_settings():
SettingsDialog(self.qtapp) SettingsDialog(self.onion, self.qtapp)
# Wait 1ms for the event loop to finish closing the TorConnectionDialog # Wait 1ms for the event loop to finish closing the TorConnectionDialog
QtCore.QTimer.singleShot(1, open_settings) QtCore.QTimer.singleShot(1, open_settings)

View file

@ -31,8 +31,9 @@ class SettingsDialog(QtWidgets.QDialog):
""" """
Settings dialog. Settings dialog.
""" """
def __init__(self, qtapp): def __init__(self, onion, qtapp):
super(SettingsDialog, self).__init__() super(SettingsDialog, self).__init__()
self.onion = onion
self.qtapp = qtapp self.qtapp = qtapp
self.setModal(True) self.setModal(True)
@ -373,8 +374,9 @@ class SettingsDialog(QtWidgets.QDialog):
""" """
Check for Updates button clicked. Manually force an update check. Check for Updates button clicked. Manually force an update check.
""" """
settings = Settings() # Disable buttons
settings.load() self._disable_buttons()
self.qtapp.processEvents()
# Check for updates # Check for updates
def update_available(update_url, installed_version, latest_version): def update_available(update_url, installed_version, latest_version):
@ -382,31 +384,22 @@ class SettingsDialog(QtWidgets.QDialog):
def update_not_available(): def update_not_available():
Alert(strings._('update_not_available', True)) Alert(strings._('update_not_available', True))
u = UpdateChecker() u = UpdateChecker(self.onion)
u.update_available.connect(update_available) u.update_available.connect(update_available)
u.update_not_available.connect(update_not_available) u.update_not_available.connect(update_not_available)
# Show Tor connection status if connection type is bundled tor
if settings.get('connection_type') == 'bundled':
self.tor_status.show()
self._disable_buttons()
u.tor_status_update.connect(self._tor_status_update)
try: try:
u.check(force=True) u.check(force=True)
except UpdateCheckerTorError: except UpdateCheckerCheckError:
Alert(strings._('update_error_tor', True), QtWidgets.QMessageBox.Warning) Alert(strings._('update_error_check_error', True), QtWidgets.QMessageBox.Warning)
except UpdateCheckerSOCKSHTTPError:
Alert(strings._('update_error_sockshttp', True), QtWidgets.QMessageBox.Warning)
except UpdateCheckerInvalidLatestVersion as e: except UpdateCheckerInvalidLatestVersion as e:
Alert(strings._('update_error_invalid_latest_version', True).format(e.latest_version), QtWidgets.QMessageBox.Warning) Alert(strings._('update_error_invalid_latest_version', True).format(e.latest_version), QtWidgets.QMessageBox.Warning)
# Clean up afterwards # Enable buttons
if settings.get('connection_type') == 'bundled': self._enable_buttons()
self.tor_status.hide()
self._enable_buttons()
# Update the last checked label # Update the last checked label
settings = Settings()
settings.load() settings.load()
autoupdate_timestamp = settings.get('autoupdate_timestamp') autoupdate_timestamp = settings.get('autoupdate_timestamp')
self._update_autoupdate_timestamp(autoupdate_timestamp) self._update_autoupdate_timestamp(autoupdate_timestamp)

View file

@ -26,15 +26,10 @@ from onionshare.onion import Onion
from . import strings, helpers from . import strings, helpers
class UpdateCheckerTorError(Exception): class UpdateCheckerCheckError(Exception):
""" """
Error checking for updates because of some Tor connection issue. Error checking for updates because of some Tor connection issue, or because
""" the OnionShare website is down.
pass
class UpdateCheckerSOCKSHTTPError(Exception):
"""
Error checking for updates because of some SOCKS proxy or HTTP request issue.
""" """
pass pass
@ -58,8 +53,9 @@ class UpdateChecker(QtCore.QObject):
update_not_available = QtCore.pyqtSignal() update_not_available = QtCore.pyqtSignal()
tor_status_update = QtCore.pyqtSignal(str) tor_status_update = QtCore.pyqtSignal(str)
def __init__(self): def __init__(self, onion):
super(UpdateChecker, self).__init__() super(UpdateChecker, self).__init__()
self.onion = onion
def check(self, force=False): def check(self, force=False):
# Load the settings # Load the settings
@ -86,12 +82,6 @@ class UpdateChecker(QtCore.QObject):
# Check for updates # Check for updates
if check_for_updates: if check_for_updates:
# Create an Onion object, for checking for updates over tor
try:
onion = Onion(settings=settings, bundled_tor_func=self._bundled_tor_func)
except:
raise UpdateCheckerTorError
# Download the latest-version file over Tor # Download the latest-version file over Tor
try: try:
# User agent string includes OnionShare version and platform # User agent string includes OnionShare version and platform
@ -103,7 +93,7 @@ class UpdateChecker(QtCore.QObject):
if force: if force:
path += '?force=1' path += '?force=1'
(socks_address, socks_port) = onion.get_tor_socks_port() (socks_address, socks_port) = self.onion.get_tor_socks_port()
socks.set_default_proxy(socks.SOCKS5, socks_address, socks_port) socks.set_default_proxy(socks.SOCKS5, socks_address, socks_port)
s = socks.socksocket() s = socks.socksocket()
@ -118,11 +108,8 @@ class UpdateChecker(QtCore.QObject):
http_response = s.recv(1024) http_response = s.recv(1024)
latest_version = http_response[http_response.find(b'\r\n\r\n'):].strip().decode('utf-8') latest_version = http_response[http_response.find(b'\r\n\r\n'):].strip().decode('utf-8')
# Clean up from Onion
onion.cleanup()
except: except:
raise UpdateCheckerSOCKSHTTPError raise UpdateCheckerCheckError
# Validate that latest_version looks like a version string # Validate that latest_version looks like a version string
# This regex is: 1-3 dot-separated numeric components # This regex is: 1-3 dot-separated numeric components
@ -145,19 +132,19 @@ class UpdateChecker(QtCore.QObject):
# No updates are available # No updates are available
self.update_not_available.emit() self.update_not_available.emit()
def _bundled_tor_func(self, message):
self.tor_status_update.emit(message)
class UpdateThread(QtCore.QThread): class UpdateThread(QtCore.QThread):
update_available = QtCore.pyqtSignal(str, str, str) update_available = QtCore.pyqtSignal(str, str, str)
update_not_available = QtCore.pyqtSignal()
tor_status_update = QtCore.pyqtSignal(str) tor_status_update = QtCore.pyqtSignal(str)
def __init__(self): def __init__(self, onion):
super(UpdateThread, self).__init__() super(UpdateThread, self).__init__()
self.onion = onion
def run(self): def run(self):
u = UpdateChecker() u = UpdateChecker(self.onion)
u.update_available.connect(self._update_available) u.update_available.connect(self._update_available)
u.update_not_available.connect(self._update_not_available)
u.tor_status_update.connect(self._tor_status_update) u.tor_status_update.connect(self._tor_status_update)
try: try:
u.check() u.check()
@ -168,5 +155,8 @@ class UpdateThread(QtCore.QThread):
def _update_available(self, update_url, installed_version, latest_version): def _update_available(self, update_url, installed_version, latest_version):
self.update_available.emit(update_url, installed_version, latest_version) self.update_available.emit(update_url, installed_version, latest_version)
def _update_not_available(self):
self.update_not_available.emit()
def _tor_status_update(self, message): def _tor_status_update(self, message):
self.tor_status_update.emit(message) self.tor_status_update.emit(message)

View file

@ -97,8 +97,7 @@
"error_tor_protocol_error": "Error talking to the Tor controller.\nIf you're using Whonix, check out https://www.whonix.org/wiki/onionshare to make OnionShare work.", "error_tor_protocol_error": "Error talking to the Tor controller.\nIf you're using Whonix, check out https://www.whonix.org/wiki/onionshare to make OnionShare work.",
"connecting_to_tor": "Connecting to the Tor network", "connecting_to_tor": "Connecting to the Tor network",
"update_available": "There is an OnionShare update available. <a href='{}'>Click here</a> to download it.<br><br>Installed version: {}<br>Latest version: {}", "update_available": "There is an OnionShare update available. <a href='{}'>Click here</a> to download it.<br><br>Installed version: {}<br>Latest version: {}",
"update_error_tor": "Error checking for updates: Can't connect to Tor.\nCheck your Tor connection settings.", "update_error_check_error": "Error checking for updates: Maybe you're not connected to Tor, or maybe the OnionShare website is down.",
"update_error_sockshttp": "Error checking for updates: Connected to Tor, but can't load the update HTTP request.",
"update_error_invalid_latest_version": "Error checking for updates: The OnionShare website responded saying the latest version is '{}', but that doesn't appear to be a valid version string.", "update_error_invalid_latest_version": "Error checking for updates: The OnionShare website responded saying the latest version is '{}', but that doesn't appear to be a valid version string.",
"update_not_available": "You are running the latest version of OnionShare.", "update_not_available": "You are running the latest version of OnionShare.",
"gui_tor_connection_error_settings": "Try adjusting how OnionShare connects to the Tor network in Settings." "gui_tor_connection_error_settings": "Try adjusting how OnionShare connects to the Tor network in Settings."