Refactor OnionShareGui to use new Web class

This commit is contained in:
Micah Lee 2018-03-06 00:56:40 -08:00
parent 0cec696055
commit 4a0c6e8dcd
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 37 additions and 33 deletions

View File

@ -41,7 +41,7 @@ class Web(object):
""" """
The Web object is the OnionShare web server, powered by flask The Web object is the OnionShare web server, powered by flask
""" """
def __init__(self, debug, stay_open, gui_mode, receive_mode): def __init__(self, debug, stay_open, gui_mode, receive_mode=False):
# The flask app # The flask app
self.app = Flask(__name__) self.app = Flask(__name__)
@ -50,14 +50,13 @@ class Web(object):
self.debug_mode() self.debug_mode()
# Stay open after the first download? # Stay open after the first download?
self.stay_open = False self.stay_open = stay_open
# Are we running in GUI mode? # Are we running in GUI mode?
self.gui_mode = False self.gui_mode = gui_mode
# Are we using receive mode? # Are we using receive mode?
self.receive_mode = False self.receive_mode = receive_mode
# Starting in Flask 0.11, render_template_string autoescapes template variables # Starting in Flask 0.11, render_template_string autoescapes template variables
# by default. To prevent content injection through template variables in # by default. To prevent content injection through template variables in

View File

@ -23,7 +23,7 @@ from .alert import Alert
from PyQt5 import QtCore, QtWidgets from PyQt5 import QtCore, QtWidgets
from onionshare import strings, common from onionshare import strings, common
from .web import Web from onionshare.web import Web
from onionshare.onion import Onion from onionshare.onion import Onion
from onionshare.onionshare import OnionShare from onionshare.onionshare import OnionShare
from onionshare.settings import Settings from onionshare.settings import Settings
@ -86,7 +86,6 @@ def main():
# Debug mode? # Debug mode?
if debug: if debug:
common.set_debug(debug) common.set_debug(debug)
web.debug_mode()
# Validation # Validation
if filenames: if filenames:
@ -101,15 +100,18 @@ def main():
if not valid: if not valid:
sys.exit() sys.exit()
# Create the Web object
web = Web(debug, stay_open, True)
# Start the Onion # Start the Onion
onion = Onion() onion = Onion()
# Start the OnionShare app # Start the OnionShare app
web.set_stay_open(stay_open)
app = OnionShare(onion, local_only, stay_open, shutdown_timeout) app = OnionShare(onion, local_only, stay_open, shutdown_timeout)
# Launch the gui # Launch the gui
gui = OnionShareGui(onion, qtapp, app, filenames, config) web.stay_open = stay_open
gui = OnionShareGui(web, onion, qtapp, app, filenames, config)
# Clean up when app quits # Clean up when app quits
def shutdown(): def shutdown():

View File

@ -17,10 +17,13 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import os, threading, time import os
import threading
import time
import queue
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings, common, web from onionshare import strings, common
from onionshare.settings import Settings from onionshare.settings import Settings
from onionshare.onion import * from onionshare.onion import *
@ -43,13 +46,14 @@ class OnionShareGui(QtWidgets.QMainWindow):
starting_server_step3 = QtCore.pyqtSignal() starting_server_step3 = QtCore.pyqtSignal()
starting_server_error = QtCore.pyqtSignal(str) starting_server_error = QtCore.pyqtSignal(str)
def __init__(self, onion, qtapp, app, filenames, config=False): def __init__(self, web, onion, qtapp, app, filenames, config=False):
super(OnionShareGui, self).__init__() super(OnionShareGui, self).__init__()
self._initSystemTray() self._initSystemTray()
common.log('OnionShareGui', '__init__') common.log('OnionShareGui', '__init__')
self.web = web
self.onion = onion self.onion = onion
self.qtapp = qtapp self.qtapp = qtapp
self.app = app self.app = app
@ -70,7 +74,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.file_selection.file_list.add_file(filename) self.file_selection.file_list.add_file(filename)
# Server status # Server status
self.server_status = ServerStatus(self.qtapp, self.app, web, self.file_selection, self.settings) self.server_status = ServerStatus(self.qtapp, self.app, self.web, self.file_selection, self.settings)
self.server_status.server_started.connect(self.file_selection.server_started) self.server_status.server_started.connect(self.file_selection.server_started)
self.server_status.server_started.connect(self.start_server) self.server_status.server_started.connect(self.start_server)
self.server_status.server_started.connect(self.update_server_status_indicator) self.server_status.server_started.connect(self.update_server_status_indicator)
@ -377,9 +381,8 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.server_share_status_label.setText('') self.server_share_status_label.setText('')
# Reset web counters # Reset web counters
web.download_count = 0 self.web.download_count = 0
web.error404_count = 0 self.web.error404_count = 0
web.set_gui_mode()
# start the onion service in a new thread # start the onion service in a new thread
def start_onion_service(self): def start_onion_service(self):
@ -395,7 +398,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.app.stay_open = not self.settings.get('close_after_first_download') self.app.stay_open = not self.settings.get('close_after_first_download')
# start onionshare http service in new thread # start onionshare http service in new thread
t = threading.Thread(target=web.start, args=(self.app.port, self.app.stay_open, self.settings.get('slug'))) t = threading.Thread(target=self.web.start, args=(self.app.port, self.app.stay_open, self.settings.get('slug')))
t.daemon = True t.daemon = True
t.start() t.start()
# 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
@ -428,8 +431,8 @@ class OnionShareGui(QtWidgets.QMainWindow):
if self._zip_progress_bar != None: if self._zip_progress_bar != None:
self._zip_progress_bar.update_processed_size_signal.emit(x) self._zip_progress_bar.update_processed_size_signal.emit(x)
try: try:
web.set_file_info(self.filenames, processed_size_callback=_set_processed_size) self.web.set_file_info(self.filenames, processed_size_callback=_set_processed_size)
self.app.cleanup_filenames.append(web.zip_filename) self.app.cleanup_filenames.append(self.web.zip_filename)
self.starting_server_step3.emit() self.starting_server_step3.emit()
# done # done
@ -455,7 +458,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self._zip_progress_bar = None self._zip_progress_bar = None
# warn about sending large files over Tor # warn about sending large files over Tor
if web.zip_filesize >= 157286400: # 150mb if self.web.zip_filesize >= 157286400: # 150mb
self.filesize_warning.setText(strings._("large_filesize", True)) self.filesize_warning.setText(strings._("large_filesize", True))
self.filesize_warning.show() self.filesize_warning.show()
@ -503,7 +506,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
if self.server_status.status != self.server_status.STATUS_STOPPED: if self.server_status.status != self.server_status.STATUS_STOPPED:
try: try:
web.stop(self.app.port) self.web.stop(self.app.port)
except: except:
# Probably we had no port to begin with (Onion service didn't start) # Probably we had no port to begin with (Onion service didn't start)
pass pass
@ -570,33 +573,33 @@ class OnionShareGui(QtWidgets.QMainWindow):
done = False done = False
while not done: while not done:
try: try:
r = web.q.get(False) r = self.web.q.get(False)
events.append(r) events.append(r)
except web.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"] == self.web.REQUEST_LOAD:
self.status_bar.showMessage(strings._('download_page_loaded', True)) self.status_bar.showMessage(strings._('download_page_loaded', True))
elif event["type"] == web.REQUEST_DOWNLOAD: elif event["type"] == self.web.REQUEST_DOWNLOAD:
self.downloads_container.show() # show the downloads layout self.downloads_container.show() # show the downloads layout
self.downloads.add_download(event["data"]["id"], web.zip_filesize) self.downloads.add_download(event["data"]["id"], self.web.zip_filesize)
self.new_download = True self.new_download = True
self.downloads_in_progress += 1 self.downloads_in_progress += 1
self.update_downloads_in_progress(self.downloads_in_progress) self.update_downloads_in_progress(self.downloads_in_progress)
if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'): if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'):
self.systemTray.showMessage(strings._('systray_download_started_title', True), strings._('systray_download_started_message', True)) self.systemTray.showMessage(strings._('systray_download_started_title', True), strings._('systray_download_started_message', True))
elif event["type"] == web.REQUEST_RATE_LIMIT: elif event["type"] == self.web.REQUEST_RATE_LIMIT:
self.stop_server() self.stop_server()
Alert(strings._('error_rate_limit'), QtWidgets.QMessageBox.Critical) Alert(strings._('error_rate_limit'), QtWidgets.QMessageBox.Critical)
elif event["type"] == web.REQUEST_PROGRESS: elif event["type"] == self.web.REQUEST_PROGRESS:
self.downloads.update_download(event["data"]["id"], event["data"]["bytes"]) self.downloads.update_download(event["data"]["id"], event["data"]["bytes"])
# is the download complete? # is the download complete?
if event["data"]["bytes"] == web.zip_filesize: if event["data"]["bytes"] == self.web.zip_filesize:
if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'): if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'):
self.systemTray.showMessage(strings._('systray_download_completed_title', True), strings._('systray_download_completed_message', True)) self.systemTray.showMessage(strings._('systray_download_completed_title', True), strings._('systray_download_completed_message', True))
# Update the total 'completed downloads' info # Update the total 'completed downloads' info
@ -607,7 +610,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.update_downloads_in_progress(self.downloads_in_progress) self.update_downloads_in_progress(self.downloads_in_progress)
# close on finish? # close on finish?
if not web.get_stay_open(): if not self.web.stay_open:
self.server_status.stop_server() self.server_status.stop_server()
self.status_bar.clearMessage() self.status_bar.clearMessage()
self.server_share_status_label.setText(strings._('closing_automatically', True)) self.server_share_status_label.setText(strings._('closing_automatically', True))
@ -618,7 +621,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.update_downloads_in_progress(self.downloads_in_progress) self.update_downloads_in_progress(self.downloads_in_progress)
elif event["type"] == web.REQUEST_CANCELED: elif event["type"] == self.web.REQUEST_CANCELED:
self.downloads.cancel_download(event["data"]["id"]) self.downloads.cancel_download(event["data"]["id"])
# Update the 'in progress downloads' info # Update the 'in progress downloads' info
self.downloads_in_progress -= 1 self.downloads_in_progress -= 1
@ -627,7 +630,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.systemTray.showMessage(strings._('systray_download_canceled_title', True), strings._('systray_download_canceled_message', True)) self.systemTray.showMessage(strings._('systray_download_canceled_title', True), strings._('systray_download_canceled_message', True))
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(self.web.error404_count, strings._('other_page_loaded', True), event["path"]))
# If the auto-shutdown timer has stopped, stop the server # If the auto-shutdown timer has stopped, stop the server
if self.server_status.status == self.server_status.STATUS_STARTED: if self.server_status.status == self.server_status.STATUS_STARTED:
@ -638,7 +641,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.server_status.server_button.setText(strings._('gui_stop_server_shutdown_timeout', True).format(seconds_remaining)) self.server_status.server_button.setText(strings._('gui_stop_server_shutdown_timeout', True).format(seconds_remaining))
if not self.app.shutdown_timer.is_alive(): if not self.app.shutdown_timer.is_alive():
# If there were no attempts to download the share, or all downloads are done, we can stop # If there were no attempts to download the share, or all downloads are done, we can stop
if web.download_count == 0 or web.done: if self.web.download_count == 0 or self.web.done:
self.server_status.stop_server() self.server_status.stop_server()
self.status_bar.clearMessage() self.status_bar.clearMessage()
self.server_share_status_label.setText(strings._('close_on_timeout', True)) self.server_share_status_label.setText(strings._('close_on_timeout', True))