Display desktop notification to the user when the receiver closes the server, and finish up closing the server

This commit is contained in:
Micah Lee 2018-04-29 15:49:18 -07:00
parent 996df24646
commit 6cfb7026da
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
5 changed files with 78 additions and 21 deletions

View File

@ -50,6 +50,7 @@ class Web(object):
REQUEST_OTHER = 3 REQUEST_OTHER = 3
REQUEST_CANCELED = 4 REQUEST_CANCELED = 4
REQUEST_RATE_LIMIT = 5 REQUEST_RATE_LIMIT = 5
REQUEST_CLOSE_SERVER = 6
def __init__(self, common, gui_mode, receive_mode=False): def __init__(self, common, gui_mode, receive_mode=False):
self.common = common self.common = common
@ -118,6 +119,9 @@ class Web(object):
# shutting down the server only works within the context of flask, so the easiest way to do it is over http # shutting down the server only works within the context of flask, so the easiest way to do it is over http
self.shutdown_slug = self.common.random_string(16) self.shutdown_slug = self.common.random_string(16)
# Keep track if the server is running
self.running = False
# Define the ewb app routes # Define the ewb app routes
self.common_routes() self.common_routes()
if self.receive_mode: if self.receive_mode:
@ -331,6 +335,7 @@ class Web(object):
if self.common.settings.get('receive_allow_receiver_shutdown'): if self.common.settings.get('receive_allow_receiver_shutdown'):
self.force_shutdown() self.force_shutdown()
r = make_response(render_template('closed.html')) r = make_response(render_template('closed.html'))
self.add_request(self.REQUEST_CLOSE_SERVER, request.path)
return self.add_security_headers(r) return self.add_security_headers(r)
else: else:
return redirect('/{}'.format(slug_candidate)) return redirect('/{}'.format(slug_candidate))
@ -451,11 +456,12 @@ class Web(object):
""" """
Stop the flask web server, from the context of the flask app. Stop the flask web server, from the context of the flask app.
""" """
# shutdown the flask service # Shutdown the flask service
func = request.environ.get('werkzeug.server.shutdown') func = request.environ.get('werkzeug.server.shutdown')
if func is None: if func is None:
raise RuntimeError('Not running with the Werkzeug Server') raise RuntimeError('Not running with the Werkzeug Server')
func() func()
self.running = False
def start(self, port, stay_open=False, persistent_slug=None): def start(self, port, stay_open=False, persistent_slug=None):
""" """
@ -472,6 +478,7 @@ class Web(object):
else: else:
host = '127.0.0.1' host = '127.0.0.1'
self.running = True
self.app.run(host=host, port=port, threaded=True) self.app.run(host=host, port=port, threaded=True)
def stop(self, port): def stop(self, port):
@ -483,7 +490,8 @@ class Web(object):
# serving the file # serving the file
self.client_cancel = True self.client_cancel = True
# to stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown # To stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown
if self.running:
try: try:
s = socket.socket() s = socket.socket()
s.connect(('127.0.0.1', port)) s.connect(('127.0.0.1', port))

View File

@ -284,3 +284,41 @@ class Mode(QtWidgets.QWidget):
Add custom initialization here. Add custom initialization here.
""" """
pass pass
# Handle web server events
def handle_request_load(self, event):
"""
Handle REQUEST_LOAD event.
"""
pass
def handle_request_download(self, event):
"""
Handle REQUEST_DOWNLOAD event.
"""
pass
def handle_request_rate_limit(self, event):
"""
Handle REQUEST_RATE_LIMIT event.
"""
pass
def handle_request_progress(self, event):
"""
Handle REQUEST_PROGRESS event.
"""
pass
def handle_request_canceled(self, event):
"""
Handle REQUEST_CANCELED event.
"""
pass
def handle_request_close_server(self, event):
"""
Handle REQUEST_CLOSE_SERVER event.
"""
pass

View File

@ -382,41 +382,43 @@ class OnionShareGui(QtWidgets.QMainWindow):
# Process events from the web object # Process events from the web object
if self.mode == self.MODE_SHARE: if self.mode == self.MODE_SHARE:
web = self.share_mode.web mode = self.share_mode
else: else:
web = self.receive_mode.web mode = self.receive_mode
events = [] events = []
done = False done = False
while not done: while not done:
try: try:
r = web.q.get(False) r = mode.web.q.get(False)
events.append(r) events.append(r)
except queue.Empty: except queue.Empty:
done = True done = True
for event in events: for event in events:
if event["type"] == Web.REQUEST_LOAD: if event["type"] == Web.REQUEST_LOAD:
self.share_mode.handle_request_load(event) mode.handle_request_load(event)
elif event["type"] == Web.REQUEST_DOWNLOAD: elif event["type"] == Web.REQUEST_DOWNLOAD:
self.share_mode.handle_request_download(event) mode.handle_request_download(event)
elif event["type"] == Web.REQUEST_RATE_LIMIT: elif event["type"] == Web.REQUEST_RATE_LIMIT:
self.share_mode.handle_request_rate_limit(event) mode.handle_request_rate_limit(event)
elif event["type"] == Web.REQUEST_PROGRESS: elif event["type"] == Web.REQUEST_PROGRESS:
self.share_mode.handle_request_progress(event) mode.handle_request_progress(event)
elif event["type"] == Web.REQUEST_CANCELED: elif event["type"] == Web.REQUEST_CANCELED:
self.share_mode.handle_request_canceled(event) mode.handle_request_canceled(event)
elif event["type"] == Web.REQUEST_CLOSE_SERVER:
mode.handle_request_close_server(event)
elif event["path"] != '/favicon.ico': elif event["path"] != '/favicon.ico':
self.status_bar.showMessage('[#{0:d}] {1:s}: {2:s}'.format(web.error404_count, strings._('other_page_loaded', True), event["path"])) self.status_bar.showMessage('[#{0:d}] {1:s}: {2:s}'.format(mode.web.error404_count, strings._('other_page_loaded', True), event["path"]))
self.share_mode.timer_callback() mode.timer_callback()
self.receive_mode.timer_callback()
def copy_url(self): def copy_url(self):
""" """

View File

@ -92,3 +92,10 @@ class ReceiveMode(Mode):
# Continue # Continue
self.starting_server_step3.emit() self.starting_server_step3.emit()
self.start_server_finished.emit() self.start_server_finished.emit()
def handle_request_close_server(self, event):
"""
Handle REQUEST_CLOSE_SERVER event.
"""
self.stop_server()
self.system_tray.showMessage(strings._('systray_close_server_title', True), strings._('systray_close_server_message', True))

View File

@ -179,5 +179,7 @@
"gui_settings_downloads_label": "Save files to", "gui_settings_downloads_label": "Save files to",
"gui_settings_downloads_button": "Browse", "gui_settings_downloads_button": "Browse",
"gui_settings_receive_allow_receiver_shutdown_checkbox": "Allow people who upload files to you to stop Receive Mode for you", "gui_settings_receive_allow_receiver_shutdown_checkbox": "Allow people who upload files to you to stop Receive Mode for you",
"gui_settings_receive_public_mode_checkbox": "Receive Mode is open to the public\n(don't try to prevent people from guessing the OnionShare address)" "gui_settings_receive_public_mode_checkbox": "Receive Mode is open to the public\n(don't try to prevent people from guessing the OnionShare address)",
"systray_close_server_title": "OnionShare Server Closed",
"systray_close_server_message": "The user closed the server"
} }