onionshare/onionshare_gui/tab/server_status.py

440 lines
16 KiB
Python
Raw Normal View History

2014-09-02 20:30:01 -04:00
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
2018-04-24 13:07:59 -04:00
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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
2018-08-21 05:31:02 -04:00
import textwrap
from PyQt5 import QtCore, QtWidgets, QtGui
2014-08-27 19:11:43 -04:00
from onionshare import strings
2014-08-27 19:11:43 -04:00
from ..widgets import Alert
2019-10-13 00:01:25 -04:00
class ServerStatus(QtWidgets.QWidget):
"""
The server status chunk of the GUI.
"""
2019-10-13 00:01:25 -04:00
server_started = QtCore.pyqtSignal()
server_started_finished = QtCore.pyqtSignal()
server_stopped = QtCore.pyqtSignal()
2018-02-25 01:44:27 -05:00
server_canceled = QtCore.pyqtSignal()
button_clicked = QtCore.pyqtSignal()
url_copied = QtCore.pyqtSignal()
hidservauth_copied = QtCore.pyqtSignal()
STATUS_STOPPED = 0
STATUS_WORKING = 1
STATUS_STARTED = 2
def __init__(
self,
common,
qtapp,
app,
mode_settings,
mode_settings_widget,
file_selection=None,
local_only=False,
):
2014-08-27 19:11:43 -04:00
super(ServerStatus, self).__init__()
self.common = common
self.status = self.STATUS_STOPPED
2019-10-13 00:01:25 -04:00
self.mode = None # Gets set in self.set_mode
2014-08-27 19:11:43 -04:00
self.qtapp = qtapp
self.app = app
self.settings = mode_settings
self.mode_settings_widget = mode_settings_widget
self.web = None
self.autostart_timer_datetime = None
self.local_only = local_only
self.resizeEvent(None)
# Server layout
self.server_button = QtWidgets.QPushButton()
self.server_button.clicked.connect(self.server_button_clicked)
2014-08-27 19:11:43 -04:00
# URL layout
url_font = QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont)
self.url_description = QtWidgets.QLabel()
self.url_description.setWordWrap(True)
self.url_description.setMinimumHeight(50)
self.url = QtWidgets.QLabel()
self.url.setFont(url_font)
self.url.setWordWrap(True)
self.url.setMinimumSize(self.url.sizeHint())
self.url.setStyleSheet(self.common.gui.css["server_status_url"])
2019-10-13 00:01:25 -04:00
self.copy_url_button = QtWidgets.QPushButton(strings._("gui_copy_url"))
self.copy_url_button.setFlat(True)
self.copy_url_button.setStyleSheet(
self.common.gui.css["server_status_url_buttons"]
)
self.copy_url_button.setMinimumHeight(65)
2014-08-27 19:11:43 -04:00
self.copy_url_button.clicked.connect(self.copy_url)
2019-10-13 00:01:25 -04:00
self.copy_hidservauth_button = QtWidgets.QPushButton(
strings._("gui_copy_hidservauth")
)
self.copy_hidservauth_button.setFlat(True)
2019-10-13 00:01:25 -04:00
self.copy_hidservauth_button.setStyleSheet(
self.common.gui.css["server_status_url_buttons"]
2019-10-13 00:01:25 -04:00
)
self.copy_hidservauth_button.clicked.connect(self.copy_hidservauth)
url_buttons_layout = QtWidgets.QHBoxLayout()
url_buttons_layout.addWidget(self.copy_url_button)
url_buttons_layout.addWidget(self.copy_hidservauth_button)
url_buttons_layout.addStretch()
url_layout = QtWidgets.QVBoxLayout()
url_layout.addWidget(self.url_description)
url_layout.addWidget(self.url)
url_layout.addLayout(url_buttons_layout)
2014-08-27 19:11:43 -04:00
# Add the widgets
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.server_button)
layout.addLayout(url_layout)
self.setLayout(layout)
def set_mode(self, share_mode, file_selection=None):
"""
The server status is in share mode.
"""
self.mode = share_mode
if (self.mode == self.common.gui.MODE_SHARE) or (
self.mode == self.common.gui.MODE_WEBSITE
2019-10-13 00:01:25 -04:00
):
self.file_selection = file_selection
self.update()
def resizeEvent(self, event):
"""
When the widget is resized, try and adjust the display of a v3 onion URL.
"""
try:
# Wrap the URL label
2019-10-13 00:01:25 -04:00
url_length = len(self.get_url())
if url_length > 60:
width = self.frameGeometry().width()
if width < 530:
wrapped_onion_url = textwrap.fill(self.get_url(), 46)
self.url.setText(wrapped_onion_url)
else:
self.url.setText(self.get_url())
except:
pass
def show_url(self):
"""
Show the URL in the UI.
"""
self.url_description.show()
2019-10-13 00:01:25 -04:00
info_image = self.common.get_resource_path("images/info.png")
if self.mode == self.common.gui.MODE_SHARE:
2019-10-13 00:01:25 -04:00
self.url_description.setText(
strings._("gui_share_url_description").format(info_image)
)
elif self.mode == self.common.gui.MODE_WEBSITE:
2019-10-13 00:01:25 -04:00
self.url_description.setText(
strings._("gui_website_url_description").format(info_image)
)
else:
2019-10-13 00:01:25 -04:00
self.url_description.setText(
strings._("gui_receive_url_description").format(info_image)
)
# Show a Tool Tip explaining the lifecycle of this URL
if self.settings.get("persistent", "enabled"):
if self.mode == self.common.gui.MODE_SHARE and self.settings.get(
"share", "autostop_sharing"
2019-10-13 00:01:25 -04:00
):
self.url_description.setToolTip(
strings._("gui_url_label_onetime_and_persistent")
)
else:
2019-10-13 00:01:25 -04:00
self.url_description.setToolTip(strings._("gui_url_label_persistent"))
else:
if self.mode == self.common.gui.MODE_SHARE and self.settings.get(
"share", "autostop_sharing"
2019-10-13 00:01:25 -04:00
):
self.url_description.setToolTip(strings._("gui_url_label_onetime"))
else:
2019-10-13 00:01:25 -04:00
self.url_description.setToolTip(strings._("gui_url_label_stay_open"))
self.url.setText(self.get_url())
self.url.show()
self.copy_url_button.show()
if self.settings.get("general", "client_auth"):
self.copy_hidservauth_button.show()
else:
self.copy_hidservauth_button.hide()
def update(self):
"""
Update the GUI elements based on the current state.
"""
self.common.log("ServerStatus", "update")
2018-02-05 12:44:17 -05:00
# Set the URL fields
if self.status == self.STATUS_STARTED:
# The backend Onion may have saved new settings, such as the private key.
# Reload the settings before saving new ones.
self.common.settings.load()
self.show_url()
if self.settings.get("persistent", "enabled"):
if not self.settings.get("persistent", "password"):
self.settings.set("persistent", "password", self.web.password)
self.settings.save()
if self.settings.get("general", "autostop_timer"):
self.server_button.setToolTip(
strings._("gui_stop_server_autostop_timer_tooltip").format(
self.mode_settings_widget.autostop_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
else:
self.url_description.hide()
self.url.hide()
self.copy_url_button.hide()
self.copy_hidservauth_button.hide()
2018-02-05 12:44:17 -05:00
# Button
2019-10-13 00:01:25 -04:00
if (
self.mode == self.common.gui.MODE_SHARE
2019-10-13 00:01:25 -04:00
and self.file_selection.get_num_files() == 0
):
2018-02-05 12:44:17 -05:00
self.server_button.hide()
2019-10-13 00:01:25 -04:00
elif (
self.mode == self.common.gui.MODE_WEBSITE
2019-10-13 00:01:25 -04:00
and self.file_selection.get_num_files() == 0
):
self.server_button.hide()
else:
2018-02-05 12:44:17 -05:00
self.server_button.show()
if self.status == self.STATUS_STOPPED:
2019-10-13 00:01:25 -04:00
self.server_button.setStyleSheet(
self.common.gui.css["server_status_button_stopped"]
2019-10-13 00:01:25 -04:00
)
self.server_button.setEnabled(True)
if self.mode == self.common.gui.MODE_SHARE:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_share_start_server"))
elif self.mode == self.common.gui.MODE_WEBSITE:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_share_start_server"))
else:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_receive_start_server"))
self.server_button.setToolTip("")
elif self.status == self.STATUS_STARTED:
2019-10-13 00:01:25 -04:00
self.server_button.setStyleSheet(
self.common.gui.css["server_status_button_started"]
2019-10-13 00:01:25 -04:00
)
self.server_button.setEnabled(True)
if self.mode == self.common.gui.MODE_SHARE:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_share_stop_server"))
elif self.mode == self.common.gui.MODE_WEBSITE:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_share_stop_server"))
else:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_receive_stop_server"))
elif self.status == self.STATUS_WORKING:
2019-10-13 00:01:25 -04:00
self.server_button.setStyleSheet(
self.common.gui.css["server_status_button_working"]
2019-10-13 00:01:25 -04:00
)
2018-02-25 01:44:27 -05:00
self.server_button.setEnabled(True)
if self.autostart_timer_datetime:
2019-10-13 00:01:25 -04:00
self.server_button.setToolTip(
strings._("gui_start_server_autostart_timer_tooltip").format(
self.mode_settings_widget.autostart_timer_widget.dateTime().toString(
2019-10-13 00:01:25 -04:00
"h:mm AP, MMMM dd, yyyy"
)
)
)
else:
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_please_wait"))
if self.settings.get("general", "autostart_timer"):
self.server_button.setToolTip(
strings._("gui_start_server_autostart_timer_tooltip").format(
self.mode_settings_widget.autostart_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
else:
2019-10-13 00:01:25 -04:00
self.server_button.setStyleSheet(
self.common.gui.css["server_status_button_working"]
2019-10-13 00:01:25 -04:00
)
self.server_button.setEnabled(False)
2019-10-13 00:01:25 -04:00
self.server_button.setText(strings._("gui_please_wait"))
def server_button_clicked(self):
"""
Toggle starting or stopping the server.
"""
if self.status == self.STATUS_STOPPED:
can_start = True
if self.settings.get("general", "autostart_timer"):
if self.local_only:
2019-10-13 00:01:25 -04:00
self.autostart_timer_datetime = (
self.mode_settings_widget.autostart_timer_widget.dateTime().toPyDateTime()
2019-10-13 00:01:25 -04:00
)
else:
2019-10-13 00:01:25 -04:00
self.autostart_timer_datetime = (
self.mode_settings_widget.autostart_timer_widget.dateTime()
2019-10-13 00:01:25 -04:00
.toPyDateTime()
.replace(second=0, microsecond=0)
)
# If the timer has actually passed already before the user hit Start, refuse to start the server.
2019-10-13 00:01:25 -04:00
if (
QtCore.QDateTime.currentDateTime().toPyDateTime()
> self.autostart_timer_datetime
):
can_start = False
2019-10-13 00:01:25 -04:00
Alert(
self.common,
strings._("gui_server_autostart_timer_expired"),
QtWidgets.QMessageBox.Warning,
)
if self.settings.get("general", "autostop_timer"):
if self.local_only:
2019-10-13 00:01:25 -04:00
self.autostop_timer_datetime = (
self.mode_settings_widget.autostop_timer_widget.dateTime().toPyDateTime()
2019-10-13 00:01:25 -04:00
)
else:
# Get the timer chosen, stripped of its seconds. This prevents confusion if the share stops at (say) 37 seconds past the minute chosen
2019-10-13 00:01:25 -04:00
self.autostop_timer_datetime = (
self.mode_settings_widget.autostop_timer_widget.dateTime()
2019-10-13 00:01:25 -04:00
.toPyDateTime()
.replace(second=0, microsecond=0)
)
# If the timer has actually passed already before the user hit Start, refuse to start the server.
2019-10-13 00:01:25 -04:00
if (
QtCore.QDateTime.currentDateTime().toPyDateTime()
> self.autostop_timer_datetime
):
can_start = False
2019-10-13 00:01:25 -04:00
Alert(
self.common,
strings._("gui_server_autostop_timer_expired"),
QtWidgets.QMessageBox.Warning,
)
if self.settings.get("general", "autostart_timer"):
if self.autostop_timer_datetime <= self.autostart_timer_datetime:
2019-10-13 00:01:25 -04:00
Alert(
self.common,
strings._(
"gui_autostop_timer_cant_be_earlier_than_autostart_timer"
),
QtWidgets.QMessageBox.Warning,
)
can_start = False
if can_start:
self.start_server()
elif self.status == self.STATUS_STARTED:
self.stop_server()
2018-02-25 01:44:27 -05:00
elif self.status == self.STATUS_WORKING:
self.cancel_server()
self.button_clicked.emit()
2014-08-27 19:11:43 -04:00
def start_server(self):
"""
Start the server.
"""
self.status = self.STATUS_WORKING
self.update()
self.server_started.emit()
2014-08-27 19:11:43 -04:00
def start_server_finished(self):
"""
The server has finished starting.
"""
self.status = self.STATUS_STARTED
self.copy_url()
self.update()
self.server_started_finished.emit()
2014-08-27 19:11:43 -04:00
def stop_server(self):
"""
Stop the server.
"""
self.status = self.STATUS_WORKING
self.mode_settings_widget.autostart_timer_reset()
self.mode_settings_widget.autostop_timer_reset()
self.update()
self.server_stopped.emit()
2014-08-27 19:11:43 -04:00
2018-02-25 01:44:27 -05:00
def cancel_server(self):
"""
Cancel the server.
"""
2019-10-13 00:01:25 -04:00
self.common.log(
"ServerStatus", "cancel_server", "Canceling the server mid-startup"
)
2018-02-25 01:44:27 -05:00
self.status = self.STATUS_WORKING
self.autostart_timer_reset()
self.autostop_timer_reset()
2018-02-25 01:44:27 -05:00
self.update()
self.server_canceled.emit()
def stop_server_finished(self):
"""
The server has finished stopping.
"""
self.status = self.STATUS_STOPPED
self.update()
2014-08-27 19:11:43 -04:00
def copy_url(self):
"""
Copy the onionshare URL to the clipboard.
"""
clipboard = self.qtapp.clipboard()
clipboard.setText(self.get_url())
self.url_copied.emit()
def copy_hidservauth(self):
"""
Copy the HidServAuth line to the clipboard.
"""
clipboard = self.qtapp.clipboard()
clipboard.setText(self.app.auth_string)
self.hidservauth_copied.emit()
def get_url(self):
"""
Returns the OnionShare URL.
"""
if self.settings.get("general", "public"):
url = f"http://{self.app.onion_host}"
else:
url = f"http://onionshare:{self.web.password}@{self.app.onion_host}"
return url