Merge branch 'mig5-713_optional_slugs' into receiver-mode-gui

This commit is contained in:
Micah Lee 2018-09-15 19:59:57 -07:00
commit 13652e3bde
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
13 changed files with 122 additions and 72 deletions

View File

@ -128,7 +128,7 @@ def main(cwd=None):
print('')
# Start OnionShare http service in new thread
t = threading.Thread(target=web.start, args=(app.port, stay_open, common.settings.get('slug')))
t = threading.Thread(target=web.start, args=(app.port, stay_open, common.settings.get('public_mode'), common.settings.get('slug')))
t.daemon = True
t.start()
@ -147,7 +147,7 @@ def main(cwd=None):
common.settings.save()
# Build the URL
if receive and common.settings.get('receive_public_mode'):
if common.settings.get('public_mode'):
url = 'http://{0:s}'.format(app.onion_host)
else:
url = 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug)

View File

@ -69,11 +69,11 @@ class Settings(object):
'tor_bridges_use_custom_bridges': '',
'save_private_key': False,
'private_key': '',
'public_mode': False,
'slug': '',
'hidservauth_string': '',
'downloads_dir': self.build_default_downloads_dir(),
'receive_allow_receiver_shutdown': True,
'receive_public_mode': False
'receive_allow_receiver_shutdown': True
}
self._settings = {}
self.fill_in_defaults()

View File

@ -143,11 +143,19 @@ class Web(object):
"""
@self.app.route("/<slug_candidate>")
def index(slug_candidate):
self.check_slug_candidate(slug_candidate)
return index_logic()
@self.app.route("/")
def index_public():
if not self.common.settings.get('public_mode'):
return self.error404()
return index_logic()
def index_logic(slug_candidate=''):
"""
Render the template for the onionshare landing page.
"""
self.check_slug_candidate(slug_candidate)
self.add_request(Web.REQUEST_LOAD, request.path)
# Deny new downloads if "Stop After First Download" is checked and there is
@ -158,22 +166,39 @@ class Web(object):
return self.add_security_headers(r)
# If download is allowed to continue, serve download page
r = make_response(render_template(
'send.html',
slug=self.slug,
file_info=self.file_info,
filename=os.path.basename(self.zip_filename),
filesize=self.zip_filesize,
filesize_human=self.common.human_readable_filesize(self.zip_filesize)))
if self.slug:
r = make_response(render_template(
'send.html',
slug=self.slug,
file_info=self.file_info,
filename=os.path.basename(self.zip_filename),
filesize=self.zip_filesize,
filesize_human=self.common.human_readable_filesize(self.zip_filesize)))
else:
# If download is allowed to continue, serve download page
r = make_response(render_template(
'send.html',
file_info=self.file_info,
filename=os.path.basename(self.zip_filename),
filesize=self.zip_filesize,
filesize_human=self.common.human_readable_filesize(self.zip_filesize)))
return self.add_security_headers(r)
@self.app.route("/<slug_candidate>/download")
def download(slug_candidate):
self.check_slug_candidate(slug_candidate)
return download_logic()
@self.app.route("/download")
def download_public():
if not self.common.settings.get('public_mode'):
return self.error404()
return download_logic()
def download_logic(slug_candidate=''):
"""
Download the zip file.
"""
self.check_slug_candidate(slug_candidate)
# Deny new downloads if "Stop After First Download" is checked and there is
# currently a download
deny_download = not self.stay_open and self.download_in_progress
@ -288,7 +313,7 @@ class Web(object):
def index_logic():
self.add_request(Web.REQUEST_LOAD, request.path)
if self.common.settings.get('receive_public_mode'):
if self.common.settings.get('public_mode'):
upload_action = '/upload'
close_action = '/close'
else:
@ -309,7 +334,7 @@ class Web(object):
@self.app.route("/")
def index_public():
if not self.common.settings.get('receive_public_mode'):
if not self.common.settings.get('public_mode'):
return self.error404()
return index_logic()
@ -332,7 +357,7 @@ class Web(object):
valid = False
if not valid:
flash('Error uploading, please inform the OnionShare user', 'error')
if self.common.settings.get('receive_public_mode'):
if self.common.settings.get('public_mode'):
return redirect('/')
else:
return redirect('/{}'.format(slug_candidate))
@ -395,7 +420,7 @@ class Web(object):
for filename in filenames:
flash('Sent {}'.format(filename), 'info')
if self.common.settings.get('receive_public_mode'):
if self.common.settings.get('public_mode'):
return redirect('/')
else:
return redirect('/{}'.format(slug_candidate))
@ -407,7 +432,7 @@ class Web(object):
@self.app.route("/upload", methods=['POST'])
def upload_public():
if not self.common.settings.get('receive_public_mode'):
if not self.common.settings.get('public_mode'):
return self.error404()
return upload_logic()
@ -428,7 +453,7 @@ class Web(object):
@self.app.route("/close", methods=['POST'])
def close_public():
if not self.common.settings.get('receive_public_mode'):
if not self.common.settings.get('public_mode'):
return self.error404()
return close_logic()
@ -458,7 +483,7 @@ class Web(object):
self.error404_count += 1
# In receive mode, with public mode enabled, skip rate limiting 404s
if not (self.receive_mode and self.common.settings.get('receive_public_mode')):
if not self.common.settings.get('public_mode'):
if self.error404_count == 20:
self.add_request(Web.REQUEST_RATE_LIMIT, request.path)
self.force_shutdown()
@ -544,10 +569,14 @@ class Web(object):
self.app.logger.addHandler(log_handler)
def check_slug_candidate(self, slug_candidate, slug_compare=None):
if not slug_compare:
slug_compare = self.slug
if not hmac.compare_digest(slug_compare, slug_candidate):
self.common.log('Web', 'check_slug_candidate: slug_candidate={}, slug_compare={}'.format(slug_candidate, slug_compare))
if self.common.settings.get('public_mode'):
abort(404)
else:
if not slug_compare:
slug_compare = self.slug
if not hmac.compare_digest(slug_compare, slug_candidate):
abort(404)
def force_shutdown(self):
"""
@ -563,12 +592,13 @@ class Web(object):
pass
self.running = False
def start(self, port, stay_open=False, persistent_slug=None):
def start(self, port, stay_open=False, public_mode=False, persistent_slug=None):
"""
Start the flask web server.
"""
self.common.log('Web', 'start', 'port={}, stay_open={}, persistent_slug={}'.format(port, stay_open, persistent_slug))
self.generate_slug(persistent_slug)
if not public_mode:
self.generate_slug(persistent_slug)
self.stay_open = stay_open
@ -719,7 +749,7 @@ class ReceiveModeRequest(Request):
if self.path == '/{}/upload'.format(self.web.slug):
self.upload_request = True
else:
if self.web.common.settings.get('receive_public_mode'):
if self.web.common.settings.get('public_mode'):
if self.path == '/upload':
self.upload_request = True

View File

@ -144,13 +144,14 @@ class Mode(QtWidgets.QWidget):
self.app.choose_port()
# Start http service in new thread
t = threading.Thread(target=self.web.start, args=(self.app.port, not self.common.settings.get('close_after_first_download'), self.common.settings.get('slug')))
t = threading.Thread(target=self.web.start, args=(self.app.port, not self.common.settings.get('close_after_first_download'), self.common.settings.get('public_mode'), self.common.settings.get('slug')))
t.daemon = True
t.start()
# Wait for the web app slug to generate before continuing
while self.web.slug == None:
time.sleep(0.1)
if not self.common.settings.get('public_mode'):
while self.web.slug == None:
time.sleep(0.1)
# Now start the onion service
try:

View File

@ -314,7 +314,7 @@ class ServerStatus(QtWidgets.QWidget):
"""
Returns the OnionShare URL.
"""
if self.mode == ServerStatus.MODE_RECEIVE and self.common.settings.get('receive_public_mode'):
if self.common.settings.get('public_mode'):
url = 'http://{0:s}'.format(self.app.onion_host)
else:
url = 'http://{0:s}/{1:s}'.format(self.app.onion_host, self.web.slug)

View File

@ -52,6 +52,25 @@ class SettingsDialog(QtWidgets.QDialog):
self.system = platform.system()
# General options
# Whether or not to save the Onion private key for reuse (persistent URL mode)
self.save_private_key_checkbox = QtWidgets.QCheckBox()
self.save_private_key_checkbox.setCheckState(QtCore.Qt.Unchecked)
self.save_private_key_checkbox.setText(strings._("gui_save_private_key_checkbox", True))
# Use a slug
self.public_mode_checkbox = QtWidgets.QCheckBox()
self.public_mode_checkbox.setCheckState(QtCore.Qt.Unchecked)
self.public_mode_checkbox.setText(strings._("gui_settings_public_mode_checkbox", True))
# General options layout
general_group_layout = QtWidgets.QVBoxLayout()
general_group_layout.addWidget(self.save_private_key_checkbox)
general_group_layout.addWidget(self.public_mode_checkbox)
general_group = QtWidgets.QGroupBox(strings._("gui_settings_general_label", True))
general_group.setLayout(general_group_layout)
# Sharing options
# Close after first download
@ -64,16 +83,10 @@ class SettingsDialog(QtWidgets.QDialog):
self.shutdown_timeout_checkbox.setCheckState(QtCore.Qt.Checked)
self.shutdown_timeout_checkbox.setText(strings._("gui_settings_shutdown_timeout_checkbox", True))
# Whether or not to save the Onion private key for reuse
self.save_private_key_checkbox = QtWidgets.QCheckBox()
self.save_private_key_checkbox.setCheckState(QtCore.Qt.Unchecked)
self.save_private_key_checkbox.setText(strings._("gui_save_private_key_checkbox", True))
# Sharing options layout
sharing_group_layout = QtWidgets.QVBoxLayout()
sharing_group_layout.addWidget(self.close_after_first_download_checkbox)
sharing_group_layout.addWidget(self.shutdown_timeout_checkbox)
sharing_group_layout.addWidget(self.save_private_key_checkbox)
sharing_group = QtWidgets.QGroupBox(strings._("gui_settings_sharing_label", True))
sharing_group.setLayout(sharing_group_layout)
@ -93,16 +106,10 @@ class SettingsDialog(QtWidgets.QDialog):
self.receive_allow_receiver_shutdown_checkbox.setCheckState(QtCore.Qt.Checked)
self.receive_allow_receiver_shutdown_checkbox.setText(strings._("gui_settings_receive_allow_receiver_shutdown_checkbox", True))
# Use a slug
self.receive_public_mode_checkbox = QtWidgets.QCheckBox()
self.receive_public_mode_checkbox.setCheckState(QtCore.Qt.Checked)
self.receive_public_mode_checkbox.setText(strings._("gui_settings_receive_public_mode_checkbox", True))
# Receiving options layout
receiving_group_layout = QtWidgets.QVBoxLayout()
receiving_group_layout.addLayout(downloads_layout)
receiving_group_layout.addWidget(self.receive_allow_receiver_shutdown_checkbox)
receiving_group_layout.addWidget(self.receive_public_mode_checkbox)
receiving_group = QtWidgets.QGroupBox(strings._("gui_settings_receiving_label", True))
receiving_group.setLayout(receiving_group_layout)
@ -365,6 +372,7 @@ class SettingsDialog(QtWidgets.QDialog):
# Layout
left_col_layout = QtWidgets.QVBoxLayout()
left_col_layout.addWidget(general_group)
left_col_layout.addWidget(sharing_group)
left_col_layout.addWidget(receiving_group)
left_col_layout.addWidget(stealth_group)
@ -419,11 +427,11 @@ class SettingsDialog(QtWidgets.QDialog):
else:
self.receive_allow_receiver_shutdown_checkbox.setCheckState(QtCore.Qt.Unchecked)
receive_public_mode = self.old_settings.get('receive_public_mode')
if receive_public_mode:
self.receive_public_mode_checkbox.setCheckState(QtCore.Qt.Checked)
public_mode = self.old_settings.get('public_mode')
if public_mode:
self.public_mode_checkbox.setCheckState(QtCore.Qt.Checked)
else:
self.receive_public_mode_checkbox.setCheckState(QtCore.Qt.Unchecked)
self.public_mode_checkbox.setCheckState(QtCore.Qt.Unchecked)
use_stealth = self.old_settings.get('use_stealth')
if use_stealth:
@ -795,7 +803,7 @@ class SettingsDialog(QtWidgets.QDialog):
settings.set('hidservauth_string', '')
settings.set('downloads_dir', self.downloads_dir_lineedit.text())
settings.set('receive_allow_receiver_shutdown', self.receive_allow_receiver_shutdown_checkbox.isChecked())
settings.set('receive_public_mode', self.receive_public_mode_checkbox.isChecked())
settings.set('public_mode', self.public_mode_checkbox.isChecked())
settings.set('use_stealth', self.stealth_checkbox.isChecked())
# Always unset the HidServAuth if Stealth mode is unset
if not self.stealth_checkbox.isChecked():

View File

@ -80,6 +80,7 @@
"gui_settings_autoupdate_timestamp": "Last checked: {}",
"gui_settings_autoupdate_timestamp_never": "Never",
"gui_settings_autoupdate_check_button": "Check For Upgrades",
"gui_settings_general_label": "General options",
"gui_settings_sharing_label": "Sharing options",
"gui_settings_close_after_first_download_option": "Stop sharing after first download",
"gui_settings_connection_type_label": "How should OnionShare connect to Tor?",
@ -167,7 +168,7 @@
"gui_settings_downloads_label": "Save files to",
"gui_settings_downloads_button": "Browse",
"gui_settings_receive_allow_receiver_shutdown_checkbox": "Receive mode can be stopped by the sender",
"gui_settings_receive_public_mode_checkbox": "Receive mode is open to the public\n(don't prevent people from guessing the OnionShare address)",
"gui_settings_public_mode_checkbox": "OnionShare is open to the public\n(don't prevent people from guessing the OnionShare address)",
"systray_close_server_title": "OnionShare Server Closed",
"systray_close_server_message": "A user closed the server",
"systray_page_loaded_title": "OnionShare Page Loaded",

View File

@ -172,23 +172,23 @@ li.info {
min-height: 400px;
}
.closed {
.info {
text-align: center;
}
.closed img {
.info img {
width: 120px;
height: 120px;
}
.closed .closed-header {
.info .info-header {
font-size: 30px;
font-weight: normal;
color: #666666;
margin: 0 0 10px 0;
}
.closed .closed-description {
.info .info-description {
color: #666666;
margin: 0 0 20px 0;
}

View File

@ -1,10 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>OnionShare: Error 404</title>
<link href="/static/img/favicon.ico" rel="icon" type="image/x-icon" />
</head>
<body>
<p>Error 404: You probably typed the OnionShare address wrong</p>
</body>
<head>
<title>OnionShare: 404 Not Found</title>
<link href="/static/img/favicon.ico" rel="icon" type="image/x-icon" />
<link href="/static/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="info-wrapper">
<div class="info">
<p><img class="logo" src="/static/img/logo_large.png" title="OnionShare"></p>
<p class="info-header">404 Not Found</p>
</div>
</div>
</body>
</html>

View File

@ -11,11 +11,11 @@
<h1>OnionShare</h1>
</header>
<div class="closed-wrapper">
<div class="closed">
<div class="info-wrapper">
<div class="info">
<p><img class="logo" src="/static/img/logo_large.png" title="OnionShare"></p>
<p class="closed-header">Thank you for using OnionShare</p>
<p class="closed-description">You may now close this window.</p>
<p class="info-header">Thank you for using OnionShare</p>
<p class="info-description">You may now close this window.</p>
</div>
</div>
</body>

View File

@ -13,7 +13,11 @@
<div class="right">
<ul>
<li>Total size: <strong>{{ filesize_human }}</strong> (compressed)</li>
{% if slug %}
<li><a class="button" href='/{{ slug }}/download'>Download Files</a></li>
{% else %}
<li><a class="button" href='/download'>Download Files</a></li>
{% endif %}
</ul>
</div>
<img class="logo" src="/static/img/logo.png" title="OnionShare">

View File

@ -65,7 +65,7 @@ class TestSettings:
'hidservauth_string': '',
'downloads_dir': os.path.expanduser('~/OnionShare'),
'receive_allow_receiver_shutdown': True,
'receive_public_mode': False
'public_mode': False
}
def test_fill_in_defaults(self, settings_obj):

View File

@ -167,10 +167,10 @@ class TestWeb:
# Should redirect to index, and server should still be running
assert res.status_code == 302
assert web.running == True
def test_receive_mode_receive_public_mode_on(self, common_obj):
def test_public_mode_on(self, common_obj):
web = web_obj(common_obj, True)
common_obj.settings.set('receive_public_mode', True)
common_obj.settings.set('public_mode', True)
with web.app.test_client() as c:
# Upload page should be accessible from both / and /[slug]
@ -181,10 +181,10 @@ class TestWeb:
res = c.get('/{}'.format(web.slug))
data2 = res.get_data()
assert res.status_code == 200
def test_receive_mode_receive_public_mode_off(self, common_obj):
def test_public_mode_off(self, common_obj):
web = web_obj(common_obj, True)
common_obj.settings.set('receive_public_mode', False)
common_obj.settings.set('public_mode', False)
with web.app.test_client() as c:
# / should be a 404