diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 8a784e4b..99beb0e0 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -39,9 +39,10 @@ def main(cwd=None): os.chdir(cwd) # Parse arguments - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=28)) parser.add_argument('--local-only', action='store_true', dest='local_only', help=strings._("help_local_only")) parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open")) + parser.add_argument('--shutdown-timeout', metavar='', dest='shutdown_timeout', default=0, help=strings._("help_shutdown_timeout")) parser.add_argument('--stealth', action='store_true', dest='stealth', help=strings._("help_stealth")) parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug")) parser.add_argument('--config', metavar='config', default=False, help=strings._('help_config')) @@ -55,6 +56,7 @@ def main(cwd=None): local_only = bool(args.local_only) debug = bool(args.debug) stay_open = bool(args.stay_open) + shutdown_timeout = int(args.shutdown_timeout) stealth = bool(args.stealth) config = args.config @@ -87,7 +89,7 @@ def main(cwd=None): # Start the onionshare app try: - app = OnionShare(onion, local_only, stay_open) + app = OnionShare(onion, local_only, stay_open, shutdown_timeout) app.set_stealth(stealth) app.start_onion_service() except KeyboardInterrupt: @@ -114,6 +116,10 @@ def main(cwd=None): # Wait for web.generate_slug() to finish running time.sleep(0.2) + # start shutdown timer thread + if app.shutdown_timeout > 0: + app.shutdown_timer.start() + if(stealth): print(strings._("give_this_url_stealth")) print('http://{0:s}/{1:s}'.format(app.onion_host, web.slug)) @@ -126,9 +132,17 @@ def main(cwd=None): # Wait for app to close while t.is_alive(): + if app.shutdown_timeout > 0: + # if the shutdown timer was set and has run out, stop the server + if not app.shutdown_timer.is_alive(): + # 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: + print(strings._("close_on_timeout")) + web.stop(app.port) + break # Allow KeyboardInterrupt exception to be handled with threads # https://stackoverflow.com/questions/3788208/python-threading-ignores-keyboardinterrupt-exception - time.sleep(100) + time.sleep(0.2) except KeyboardInterrupt: web.stop(app.port) finally: diff --git a/onionshare/common.py b/onionshare/common.py index 7e102124..3f7bf722 100644 --- a/onionshare/common.py +++ b/onionshare/common.py @@ -26,6 +26,7 @@ import random import socket import sys import tempfile +import threading import time import zipfile @@ -256,3 +257,18 @@ class ZipWriter(object): Close the zip archive. """ self.z.close() + + +class close_after_seconds(threading.Thread): + """ + Background thread sleeps t hours and returns. + """ + def __init__(self, time): + threading.Thread.__init__(self) + self.setDaemon(True) + self.time = time + + def run(self): + log('Shutdown Timer', 'Server will shut down after {} seconds'.format(self.time)) + time.sleep(self.time) + return 1 diff --git a/onionshare/onionshare.py b/onionshare/onionshare.py index 166afffc..44941f97 100644 --- a/onionshare/onionshare.py +++ b/onionshare/onionshare.py @@ -27,7 +27,7 @@ class OnionShare(object): OnionShare is the main application class. Pass in options and run start_onion_service and it will do the magic. """ - def __init__(self, onion, local_only=False, stay_open=False): + def __init__(self, onion, local_only=False, stay_open=False, shutdown_timeout=0): common.log('OnionShare', '__init__') # The Onion object @@ -46,6 +46,11 @@ class OnionShare(object): # automatically close when download is finished self.stay_open = stay_open + # optionally shut down after N hours + self.shutdown_timeout = shutdown_timeout + # init timing thread + self.shutdown_timer = None + def set_stealth(self, stealth): common.log('OnionShare', 'set_stealth', 'stealth={}'.format(stealth)) @@ -65,6 +70,8 @@ class OnionShare(object): self.onion_host = '127.0.0.1:{0:d}'.format(self.port) return + if self.shutdown_timeout > 0: + self.shutdown_timer = common.close_after_seconds(self.shutdown_timeout) self.onion_host = self.onion.start_onion_service(self.port) if self.stealth: diff --git a/onionshare/web.py b/onionshare/web.py index 4ae10221..03da8fd7 100644 --- a/onionshare/web.py +++ b/onionshare/web.py @@ -187,6 +187,7 @@ def check_slug_candidate(slug_candidate, slug_compare=None): # one download at a time. download_in_progress = False +done = False @app.route("/") def index(slug_candidate): @@ -236,7 +237,7 @@ def download(slug_candidate): # Deny new downloads if "Stop After First Download" is checked and there is # currently a download - global stay_open, download_in_progress + global stay_open, download_in_progress, done deny_download = not stay_open and download_in_progress if deny_download: r = make_response(render_template_string(open(common.get_resource_path('html/denied.html')).read())) @@ -267,7 +268,7 @@ def download(slug_candidate): client_cancel = False # Starting a new download - global stay_open, download_in_progress + global stay_open, download_in_progress, done if not stay_open: download_in_progress = True diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py index 4aa4ff83..83d03cc0 100644 --- a/onionshare_gui/__init__.py +++ b/onionshare_gui/__init__.py @@ -61,9 +61,10 @@ def main(): qtapp = Application() # Parse arguments - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=48)) parser.add_argument('--local-only', action='store_true', dest='local_only', help=strings._("help_local_only")) parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open")) + parser.add_argument('--shutdown-timeout', metavar='', dest='shutdown_timeout', default=0, help=strings._("help_shutdown_timeout")) parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug")) parser.add_argument('--filenames', metavar='filenames', nargs='+', help=strings._('help_filename')) parser.add_argument('--config', metavar='config', default=False, help=strings._('help_config')) @@ -78,6 +79,7 @@ def main(): local_only = bool(args.local_only) stay_open = bool(args.stay_open) + shutdown_timeout = int(args.shutdown_timeout) debug = bool(args.debug) # Debug mode? @@ -103,7 +105,7 @@ def main(): # Start the OnionShare app web.set_stay_open(stay_open) - app = OnionShare(onion, local_only, stay_open) + app = OnionShare(onion, local_only, stay_open, shutdown_timeout) # Launch the gui gui = OnionShareGui(onion, qtapp, app, filenames, config) diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 022ca0b0..fd278394 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -316,6 +316,19 @@ class OnionShareGui(QtWidgets.QMainWindow): self.filesize_warning.setText(strings._("large_filesize", True)) self.filesize_warning.show() + if self.server_status.timer_enabled: + # Convert the date value to seconds between now and then + now = QtCore.QDateTime.currentDateTime() + self.timeout = now.secsTo(self.server_status.timeout) + # Set the shutdown timeout value + if self.timeout > 0: + self.app.shutdown_timer = common.close_after_seconds(self.timeout) + self.app.shutdown_timer.start() + # The timeout has actually already passed since the user clicked Start. Probably the Onion service took too long to start. + else: + self.stop_server() + self.start_server_error(strings._('gui_server_started_after_timeout')) + def start_server_error(self, error): """ If there's an error when trying to start the onion service @@ -371,6 +384,7 @@ class OnionShareGui(QtWidgets.QMainWindow): Check for messages communicated from the web app, and update the GUI accordingly. """ self.update() + # scroll to the bottom of the dl progress bar log pane # if a new download has been added if self.new_download: @@ -412,6 +426,8 @@ class OnionShareGui(QtWidgets.QMainWindow): # close on finish? if not web.get_stay_open(): self.server_status.stop_server() + self.server_status.shutdown_timeout_reset() + self.status_bar.showMessage(strings._('closing_automatically', True)) else: if self.server_status.status == self.server_status.STATUS_STOPPED: self.downloads.cancel_download(event["data"]["id"]) @@ -424,6 +440,20 @@ class OnionShareGui(QtWidgets.QMainWindow): 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"])) + # If the auto-shutdown timer has stopped, stop the server + if self.server_status.status == self.server_status.STATUS_STARTED: + if self.app.shutdown_timer and self.server_status.timer_enabled: + if self.timeout > 0: + 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 web.download_count == 0 or web.done: + self.server_status.stop_server() + self.status_bar.showMessage(strings._('close_on_timeout', True)) + self.server_status.shutdown_timeout_reset() + # A download is probably still running - hold off on stopping the share + else: + self.status_bar.showMessage(strings._('timeout_download_still_running', True)) + def copy_url(self): """ When the URL gets copied to the clipboard, display this in the status bar. diff --git a/onionshare_gui/server_status.py b/onionshare_gui/server_status.py index 87143725..e0a944e3 100644 --- a/onionshare_gui/server_status.py +++ b/onionshare_gui/server_status.py @@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ import platform +from .alert import Alert from PyQt5 import QtCore, QtWidgets, QtGui from onionshare import strings, common @@ -44,6 +45,26 @@ class ServerStatus(QtWidgets.QVBoxLayout): self.web = web self.file_selection = file_selection + # Helper boolean as this is used in a few places + self.timer_enabled = False + # Shutdown timeout layout + self.server_shutdown_timeout_checkbox = QtWidgets.QCheckBox() + self.server_shutdown_timeout_checkbox.setCheckState(QtCore.Qt.Unchecked) + self.server_shutdown_timeout_checkbox.toggled.connect(self.shutdown_timeout_toggled) + self.server_shutdown_timeout_checkbox.setText(strings._("gui_settings_shutdown_timeout_choice", True)) + self.server_shutdown_timeout_label = QtWidgets.QLabel(strings._('gui_settings_shutdown_timeout', True)) + self.server_shutdown_timeout = QtWidgets.QDateTimeEdit() + # Set proposed timeout to be 5 minutes into the future + self.server_shutdown_timeout.setDateTime(QtCore.QDateTime.currentDateTime().addSecs(300)) + # Onion services can take a little while to start, so reduce the risk of it expiring too soon by setting the minimum to 2 min from now + self.server_shutdown_timeout.setMinimumDateTime(QtCore.QDateTime.currentDateTime().addSecs(120)) + self.server_shutdown_timeout.setCurrentSectionIndex(4) + self.server_shutdown_timeout_label.hide() + self.server_shutdown_timeout.hide() + shutdown_timeout_layout_group = QtWidgets.QHBoxLayout() + shutdown_timeout_layout_group.addWidget(self.server_shutdown_timeout_checkbox) + shutdown_timeout_layout_group.addWidget(self.server_shutdown_timeout_label) + shutdown_timeout_layout_group.addWidget(self.server_shutdown_timeout) # server layout self.status_image_stopped = QtGui.QImage(common.get_resource_path('images/server_stopped.png')) self.status_image_working = QtGui.QImage(common.get_resource_path('images/server_working.png')) @@ -72,11 +93,35 @@ class ServerStatus(QtWidgets.QVBoxLayout): url_layout.addWidget(self.copy_hidservauth_button) # add the widgets + self.addLayout(shutdown_timeout_layout_group) self.addLayout(server_layout) self.addLayout(url_layout) self.update() + def shutdown_timeout_toggled(self, checked): + """ + Shutdown timer option was toggled. If checked, show the timer settings. + """ + if checked: + self.timer_enabled = True + # Hide the checkbox, show the options + self.server_shutdown_timeout_label.show() + # Reset the default timer to 5 minutes into the future after toggling the option on + self.server_shutdown_timeout.setDateTime(QtCore.QDateTime.currentDateTime().addSecs(300)) + self.server_shutdown_timeout.show() + else: + self.timer_enabled = False + self.server_shutdown_timeout_label.hide() + self.server_shutdown_timeout.hide() + + def shutdown_timeout_reset(self): + """ + Reset the timeout in the UI after stopping a share + """ + self.server_shutdown_timeout.setDateTime(QtCore.QDateTime.currentDateTime().addSecs(300)) + self.server_shutdown_timeout.setMinimumDateTime(QtCore.QDateTime.currentDateTime().addSecs(120)) + def update(self): """ Update the GUI elements based on the current state. @@ -116,21 +161,43 @@ class ServerStatus(QtWidgets.QVBoxLayout): if self.status == self.STATUS_STOPPED: self.server_button.setEnabled(True) self.server_button.setText(strings._('gui_start_server', True)) + self.server_shutdown_timeout.setEnabled(True) + self.server_shutdown_timeout_checkbox.setEnabled(True) + self.server_shutdown_timeout_checkbox.setCheckState(QtCore.Qt.Unchecked) elif self.status == self.STATUS_STARTED: self.server_button.setEnabled(True) self.server_button.setText(strings._('gui_stop_server', True)) + self.server_shutdown_timeout.setEnabled(False) + self.server_shutdown_timeout_checkbox.setEnabled(False) + elif self.status == self.STATUS_WORKING: + self.server_button.setEnabled(False) + self.server_button.setText(strings._('gui_please_wait')) + self.server_shutdown_timeout.setEnabled(False) + self.server_shutdown_timeout_checkbox.setEnabled(False) else: self.server_button.setEnabled(False) self.server_button.setText(strings._('gui_please_wait')) + self.server_shutdown_timeout.setEnabled(False) + self.server_shutdown_timeout_checkbox.setEnabled(False) def server_button_clicked(self): """ Toggle starting or stopping the server. """ if self.status == self.STATUS_STOPPED: - self.start_server() + if self.timer_enabled: + # Get the timeout chosen, stripped of its seconds. This prevents confusion if the share stops at (say) 37 seconds past the minute chosen + self.timeout = self.server_shutdown_timeout.dateTime().toPyDateTime().replace(second=0, microsecond=0) + # If the timeout has actually passed already before the user hit Start, refuse to start the server. + if QtCore.QDateTime.currentDateTime().toPyDateTime() > self.timeout: + Alert(strings._('gui_server_timeout_expired', QtWidgets.QMessageBox.Warning)) + else: + self.start_server() + else: + self.start_server() elif self.status == self.STATUS_STARTED: self.stop_server() + self.shutdown_timeout_reset() def start_server(self): """ diff --git a/share/locale/en.json b/share/locale/en.json index fedd1af4..ff288b6a 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -12,7 +12,9 @@ "not_a_readable_file": "{0:s} is not a readable file.", "download_page_loaded": "Download page loaded", "other_page_loaded": "URL loaded", + "close_on_timeout": "Closing automatically because timeout was reached", "closing_automatically": "Closing automatically because download finished", + "timeout_download_still_running": "Waiting for download to complete before auto-stopping", "large_filesize": "Warning: Sending large files could take hours", "error_tails_invalid_port": "Invalid value, port must be an integer", "error_tails_unknown_root": "Unknown error with Tails root process", @@ -25,6 +27,7 @@ "systray_download_canceled_message": "The user canceled the download", "help_local_only": "Do not attempt to use tor: for development only", "help_stay_open": "Keep onion service running after download has finished", + "help_shutdown_timeout": "Shut down the onion service after N seconds", "help_transparent_torification": "My system is transparently torified", "help_stealth": "Create stealth onion service (advanced)", "help_debug": "Log application errors to stdout, and log web errors to disk", @@ -89,6 +92,8 @@ "gui_settings_button_save": "Save", "gui_settings_button_cancel": "Cancel", "gui_settings_button_help": "Help", + "gui_settings_shutdown_timeout_choice": "Set auto-stop timer?", + "gui_settings_shutdown_timeout": "Stop the share at:", "settings_saved": "Settings saved to {}", "settings_error_unknown": "Can't connect to Tor controller because the settings don't make sense.", "settings_error_automatic": "Can't connect to Tor controller. Is Tor Browser running in the background? If you don't have it you can get it from:\nhttps://www.torproject.org/.", @@ -112,5 +117,7 @@ "gui_tor_connection_ask_open_settings": "Open Settings", "gui_tor_connection_ask_quit": "Quit", "gui_tor_connection_error_settings": "Try adjusting how OnionShare connects to the Tor network in Settings.", + "gui_server_started_after_timeout": "The server started after your chosen auto-timeout.\nPlease start a new share.", + "gui_server_timeout_expired": "The chosen timeout has already expired.\nPlease update the timeout and then you may start sharing.", "share_via_onionshare": "Share via OnionShare" }