mirror of
https://github.com/onionshare/onionshare.git
synced 2024-12-26 07:49:48 -05:00
Implements a shutdown timer to stop a share automatically (downloaded or not) after N hours
This commit is contained in:
parent
4387589b4f
commit
32108dcca2
@ -42,6 +42,7 @@ def main(cwd=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
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='shutdown_timeout', dest='shutdown_timeout', 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 = float(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:
|
||||
@ -106,7 +108,7 @@ def main(cwd=None):
|
||||
print('')
|
||||
|
||||
# Start OnionShare http service in new thread
|
||||
t = threading.Thread(target=web.start, args=(app.port, app.stay_open))
|
||||
t = threading.Thread(target=web.start, args=(app.port, app.stay_open, app.shutdown_timeout))
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
@ -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,15 @@ 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():
|
||||
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:
|
||||
|
@ -27,6 +27,7 @@ import random
|
||||
import socket
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import zipfile
|
||||
|
||||
@ -254,3 +255,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(3600 * self.time))
|
||||
time.sleep(3600 * self.time) # seconds -> hours
|
||||
return 1
|
||||
|
@ -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:
|
||||
|
@ -135,6 +135,13 @@ def get_stay_open():
|
||||
"""
|
||||
return stay_open
|
||||
|
||||
shutdown_timeout = 0
|
||||
def set_shutdown_timeout(new_shutdown_timeout):
|
||||
"""
|
||||
Set shutdown_timeout variable.
|
||||
"""
|
||||
global shutdown_timeout
|
||||
shutdown_timeout = new_shutdown_timeout
|
||||
|
||||
# Are we running in GUI mode?
|
||||
gui_mode = False
|
||||
@ -361,13 +368,14 @@ def force_shutdown():
|
||||
func()
|
||||
|
||||
|
||||
def start(port, stay_open=False):
|
||||
def start(port, stay_open=False, shutdown_timeout=0):
|
||||
"""
|
||||
Start the flask web server.
|
||||
"""
|
||||
generate_slug()
|
||||
|
||||
set_stay_open(stay_open)
|
||||
set_shutdown_timeout(shutdown_timeout)
|
||||
|
||||
# In Whonix, listen on 0.0.0.0 instead of 127.0.0.1 (#220)
|
||||
if os.path.exists('/usr/share/anon-ws-base-files/workstation'):
|
||||
|
@ -64,6 +64,7 @@ def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
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='shutdown_timeout', 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 = float(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)
|
||||
|
@ -258,7 +258,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
|
||||
self.app.stay_open = not self.settings.get('close_after_first_download')
|
||||
|
||||
# start onionshare http service in new thread
|
||||
t = threading.Thread(target=web.start, args=(self.app.port, self.app.stay_open))
|
||||
t = threading.Thread(target=web.start, args=(self.app.port, self.app.stay_open, self.app.shutdown_timeout))
|
||||
t.daemon = True
|
||||
t.start()
|
||||
# wait for modules in thread to load, preventing a thread-related cx_Freeze crash
|
||||
@ -371,6 +371,14 @@ class OnionShareGui(QtWidgets.QMainWindow):
|
||||
Check for messages communicated from the web app, and update the GUI accordingly.
|
||||
"""
|
||||
self.update()
|
||||
|
||||
# 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.server_shutdown_timeout.value() > 0:
|
||||
if not self.app.shutdown_timer.is_alive():
|
||||
self.server_status.stop_server()
|
||||
self.status_bar.showMessage(strings._('close_on_timeout',True))
|
||||
|
||||
# scroll to the bottom of the dl progress bar log pane
|
||||
# if a new download has been added
|
||||
if self.new_download:
|
||||
|
@ -44,6 +44,20 @@ class ServerStatus(QtWidgets.QVBoxLayout):
|
||||
self.web = web
|
||||
self.file_selection = file_selection
|
||||
|
||||
# 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.QDoubleSpinBox()
|
||||
self.server_shutdown_timeout.setRange(0,100)
|
||||
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 +86,25 @@ 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, hide the option and show the timer settings.
|
||||
"""
|
||||
if checked:
|
||||
self.server_shutdown_timeout_checkbox.hide()
|
||||
self.server_shutdown_timeout_label.show()
|
||||
self.server_shutdown_timeout.show()
|
||||
else:
|
||||
self.server_shutdown_timeout_checkbox.show()
|
||||
self.server_shutdown_timeout_label.hide()
|
||||
self.server_shutdown_timeout.hide()
|
||||
|
||||
def update(self):
|
||||
"""
|
||||
Update the GUI elements based on the current state.
|
||||
@ -116,12 +144,16 @@ 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.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)
|
||||
else:
|
||||
self.server_button.setEnabled(False)
|
||||
self.server_button.setText(strings._('gui_please_wait'))
|
||||
self.server_shutdown_timeout.setEnabled(False)
|
||||
|
||||
def server_button_clicked(self):
|
||||
"""
|
||||
@ -145,6 +177,10 @@ class ServerStatus(QtWidgets.QVBoxLayout):
|
||||
The server has finished starting.
|
||||
"""
|
||||
self.status = self.STATUS_STARTED
|
||||
# Set the shutdown timeout value
|
||||
if self.server_shutdown_timeout.value() > 0:
|
||||
self.app.shutdown_timer = common.close_after_seconds(self.server_shutdown_timeout.value())
|
||||
self.app.shutdown_timer.start()
|
||||
self.copy_url()
|
||||
self.update()
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
"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",
|
||||
"large_filesize": "Warning: Sending large files could take hours",
|
||||
"error_tails_invalid_port": "Invalid value, port must be an integer",
|
||||
@ -25,6 +26,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 hours",
|
||||
"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 +91,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": "Auto-stop share in (hours):",
|
||||
"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/.",
|
||||
|
Loading…
Reference in New Issue
Block a user