Various safety checks to prevent a share from starting after the timeout has expired. Also enforce that a timeout lands right on the minute and not precisely when the user clicks start (e.g mid-minute), to avoid confusion that a share might be lingering longer than desired

This commit is contained in:
Miguel Jacq 2017-11-09 17:26:32 +11:00
parent 9aabc51edc
commit ac0e375a4b
No known key found for this signature in database
GPG key ID: EEA4341C6D97A0B6
3 changed files with 37 additions and 7 deletions

View file

@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import platform
from .alert import Alert
from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings, common
@ -44,15 +45,19 @@ class ServerStatus(QtWidgets.QVBoxLayout):
self.web = web
self.file_selection = file_selection
# shutdown timeout layout
# 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))
self.server_shutdown_timeout.setMinimumDateTime(QtCore.QDateTime.currentDateTime())
# 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()
@ -99,16 +104,26 @@ class ServerStatus(QtWidgets.QVBoxLayout):
Shutdown timer option was toggled. If checked, hide the option and show the timer settings.
"""
if checked:
self.timer_enabled = True
# Hide the checkbox, show the options
self.server_shutdown_timeout_checkbox.hide()
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_checkbox.show()
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.
@ -171,9 +186,16 @@ class ServerStatus(QtWidgets.QVBoxLayout):
Toggle starting or stopping the server.
"""
if self.status == self.STATUS_STOPPED:
self.start_server()
# 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()
elif self.status == self.STATUS_STARTED:
self.stop_server()
self.shutdown_timeout_reset()
def start_server(self):
"""