Raise a Waitress exception into the UI with a modal dialog and reset the share if it occurs

This commit is contained in:
Miguel Jacq 2023-05-31 15:38:52 +10:00
parent dd55e78d63
commit 4439838ee7
No known key found for this signature in database
GPG key ID: 59B3F0C24135C6A9
5 changed files with 40 additions and 16 deletions

View file

@ -57,6 +57,12 @@ except Exception:
pass pass
class WaitressException(Exception):
"""
There was a problem starting the waitress web server.
"""
class Web: class Web:
""" """
The Web object is the OnionShare web server, powered by flask The Web object is the OnionShare web server, powered by flask
@ -349,14 +355,17 @@ class Web:
if self.mode == "chat": if self.mode == "chat":
self.socketio.run(self.app, host=host, port=port) self.socketio.run(self.app, host=host, port=port)
else: else:
self.waitress = create_server( try:
self.app, self.waitress = create_server(
host=host, self.app,
port=port, host=host,
clear_untrusted_proxy_headers=True, port=port,
ident="OnionShare", clear_untrusted_proxy_headers=True,
) ident="OnionShare",
self.waitress.run() )
self.waitress.run()
except Exception as e:
raise WaitressException(f"Error starting Waitress: {e}")
def stop(self, port): def stop(self, port):
""" """
@ -389,7 +398,6 @@ class Web:
def waitress_custom_shutdown(self): def waitress_custom_shutdown(self):
"""Shutdown the Waitress server immediately""" """Shutdown the Waitress server immediately"""
# Code borrowed from https://github.com/Pylons/webtest/blob/4b8a3ebf984185ff4fefb31b4d0cf82682e1fcf7/webtest/http.py#L93-L104 # Code borrowed from https://github.com/Pylons/webtest/blob/4b8a3ebf984185ff4fefb31b4d0cf82682e1fcf7/webtest/http.py#L93-L104
self.waitress.was_shutdown = True
while self.waitress._map: while self.waitress._map:
triggers = list(self.waitress._map.values()) triggers = list(self.waitress._map.values())
for trigger in triggers: for trigger in triggers:

View file

@ -41,7 +41,7 @@ from onionshare_cli.onion import (
PortNotAvailable, PortNotAvailable,
) )
from onionshare_cli.meek import Meek from onionshare_cli.meek import Meek
from onionshare_cli.web.web import WaitressException
class GuiCommon: class GuiCommon:
""" """
@ -581,6 +581,13 @@ class GuiCommon:
return strings._("error_port_not_available") return strings._("error_port_not_available")
return None return None
@staticmethod
def get_translated_web_error(e):
"""
Takes an exception defined in web.py and returns a translated error message
"""
if type(e) is WaitressException:
return strings._("waitress_web_server_error")
class ToggleCheckbox(QtWidgets.QCheckBox): class ToggleCheckbox(QtWidgets.QCheckBox):
def __init__(self, text): def __init__(self, text):

View file

@ -252,5 +252,6 @@
"moat_bridgedb_error": "Could not contact BridgeDB.", "moat_bridgedb_error": "Could not contact BridgeDB.",
"moat_captcha_error": "Incorrect solution. Please try again.", "moat_captcha_error": "Incorrect solution. Please try again.",
"moat_solution_empty_error": "Enter the characters from the image", "moat_solution_empty_error": "Enter the characters from the image",
"mode_tor_not_connected_label": "OnionShare is not connected to the Tor network" "mode_tor_not_connected_label": "OnionShare is not connected to the Tor network",
"waitress_web_server_error": "There was a problem starting the web server"
} }

View file

@ -27,7 +27,7 @@ from .mode_settings_widget import ModeSettingsWidget
from ..server_status import ServerStatus from ..server_status import ServerStatus
from ... import strings from ... import strings
from ...threads import OnionThread, AutoStartTimer from ...threads import OnionThread, WebThread, AutoStartTimer
from ...widgets import Alert, MinimumSizeWidget from ...widgets import Alert, MinimumSizeWidget
@ -294,6 +294,8 @@ class Mode(QtWidgets.QWidget):
self.onion_thread.success.connect(self.starting_server_step2.emit) self.onion_thread.success.connect(self.starting_server_step2.emit)
self.onion_thread.success_early.connect(self.starting_server_early.emit) self.onion_thread.success_early.connect(self.starting_server_early.emit)
self.onion_thread.error.connect(self.starting_server_error.emit) self.onion_thread.error.connect(self.starting_server_error.emit)
self.web_thread = WebThread(self)
self.web_thread.error.connect(self.starting_server_error.emit)
self.onion_thread.start() self.onion_thread.start()
def start_scheduled_service(self, obtain_onion_early=False): def start_scheduled_service(self, obtain_onion_early=False):

View file

@ -39,6 +39,8 @@ from onionshare_cli.onion import (
PortNotAvailable, PortNotAvailable,
) )
from onionshare_cli.web.web import WaitressException
from . import strings from . import strings
@ -83,7 +85,6 @@ class OnionThread(QtCore.QThread):
# wait for modules in thread to load, preventing a thread-related cx_Freeze crash # wait for modules in thread to load, preventing a thread-related cx_Freeze crash
time.sleep(0.2) time.sleep(0.2)
# start onionshare http service in new thread # start onionshare http service in new thread
self.mode.web_thread = WebThread(self.mode)
self.mode.web_thread.start() self.mode.web_thread.start()
self.success.emit() self.success.emit()
@ -122,9 +123,14 @@ class WebThread(QtCore.QThread):
def run(self): def run(self):
self.mode.common.log("WebThread", "run") self.mode.common.log("WebThread", "run")
self.mode.web.start(self.mode.app.port) try:
self.success.emit() self.mode.web.start(self.mode.app.port)
self.success.emit()
except WaitressException as e:
message = self.mode.common.gui.get_translated_web_error(e)
self.mode.common.log("WebThread", "run", message)
self.error.emit(message)
return
class AutoStartTimer(QtCore.QThread): class AutoStartTimer(QtCore.QThread):
""" """