From 30dc17df27d4d79fed6ee8be552ad2a1da72edd1 Mon Sep 17 00:00:00 2001 From: hiro Date: Wed, 5 Jun 2019 13:47:41 +0200 Subject: [PATCH 01/48] Start code sharing between WebsiteMode and ShareMode --- onionshare/web/base_mode.py | 45 ++++++++++++++++++++++++++++++++++ onionshare/web/share_mode.py | 27 +++----------------- onionshare/web/website_mode.py | 16 +++--------- 3 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 onionshare/web/base_mode.py diff --git a/onionshare/web/base_mode.py b/onionshare/web/base_mode.py new file mode 100644 index 00000000..fb1043d7 --- /dev/null +++ b/onionshare/web/base_mode.py @@ -0,0 +1,45 @@ +import os +import sys +import tempfile +import mimetypes +from flask import Response, request, render_template, make_response + +from .. import strings + +class BaseModeWeb(object): + """ + All of the web logic shared between share and website mode + """ + def __init__(self, common, web): + super(BaseModeWeb, self).__init__() + self.common = common + self.web = web + + # Information about the file to be shared + self.file_info = [] + self.is_zipped = False + self.download_filename = None + self.download_filesize = None + self.gzip_filename = None + self.gzip_filesize = None + self.zip_writer = None + + # Dictionary mapping file paths to filenames on disk + self.files = {} + + self.visit_count = 0 + self.download_count = 0 + + # If "Stop After First Download" is checked (stay_open == False), only allow + # one download at a time. + self.download_in_progress = False + + # Reset assets path + self.web.app.static_folder=self.common.get_resource_path('static') + + + def init(self): + """ + Add custom initialization here. + """ + pass diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 0dfa7e0a..779d0a4b 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -6,38 +6,17 @@ import mimetypes import gzip from flask import Response, request, render_template, make_response +from .base_mode import BaseModeWeb from .. import strings -class ShareModeWeb(object): +class ShareModeWeb(BaseModeWeb): """ All of the web logic for share mode """ - def __init__(self, common, web): - self.common = common + def init(self): self.common.log('ShareModeWeb', '__init__') - self.web = web - - # Information about the file to be shared - self.file_info = [] - self.is_zipped = False - self.download_filename = None - self.download_filesize = None - self.gzip_filename = None - self.gzip_filesize = None - self.zip_writer = None - - self.download_count = 0 - - # If "Stop After First Download" is checked (stay_open == False), only allow - # one download at a time. - self.download_in_progress = False - - # Reset assets path - self.web.app.static_folder=self.common.get_resource_path('static') - - self.define_routes() def define_routes(self): diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index d2cd6db9..f61da569 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -4,26 +4,18 @@ import tempfile import mimetypes from flask import Response, request, render_template, make_response, send_from_directory +from .base_mode import BaseModeWeb from .. import strings -class WebsiteModeWeb(object): +class WebsiteModeWeb(BaseModeWeb): """ All of the web logic for share mode """ - def __init__(self, common, web): - self.common = common + def init(self): + self.common.log('WebsiteModeWeb', '__init__') - self.web = web - - # Dictionary mapping file paths to filenames on disk - self.files = {} - self.visit_count = 0 - - # Reset assets path - self.web.app.static_folder=self.common.get_resource_path('static') - self.define_routes() def define_routes(self): From 4551716a9edd4f105c136bd174d7e814a1697e99 Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 13 Jun 2019 12:33:34 +0200 Subject: [PATCH 02/48] Refactor set_file_list between website and share mode --- onionshare/web/base_mode.py | 31 ++++++++++++++++++++++++++++--- onionshare/web/share_mode.py | 15 ++------------- onionshare/web/website_mode.py | 20 ++++++-------------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/onionshare/web/base_mode.py b/onionshare/web/base_mode.py index fb1043d7..8843d198 100644 --- a/onionshare/web/base_mode.py +++ b/onionshare/web/base_mode.py @@ -26,7 +26,9 @@ class BaseModeWeb(object): # Dictionary mapping file paths to filenames on disk self.files = {} - + self.cleanup_filenames = [] + self.file_info = {'files': [], 'dirs': []} + self.visit_count = 0 self.download_count = 0 @@ -34,8 +36,7 @@ class BaseModeWeb(object): # one download at a time. self.download_in_progress = False - # Reset assets path - self.web.app.static_folder=self.common.get_resource_path('static') + self.define_routes() def init(self): @@ -43,3 +44,27 @@ class BaseModeWeb(object): Add custom initialization here. """ pass + + def set_file_info(self, filenames, processed_size_callback=None): + """ + Build a data structure that describes the list of files + """ + if self.web.mode == 'website': + self.common.log("WebsiteModeWeb", "set_file_info") + self.web.cancel_compression = True + + # This is only the root files and dirs, as opposed to all of them + self.root_files = {} + + # If there's just one folder, replace filenames with a list of files inside that folder + if len(filenames) == 1 and os.path.isdir(filenames[0]): + filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] + + self.build_file_list(filenames) + + elif self.web.mode == 'share': + self.common.log("ShareModeWeb", "set_file_info") + self.web.cancel_compression = False + self.build_zipfile_list(filenames, processed_size_callback) + + return True diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 779d0a4b..68763357 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -177,19 +177,8 @@ class ShareModeWeb(BaseModeWeb): r.headers.set('Content-Type', content_type) return r - def set_file_info(self, filenames, processed_size_callback=None): - """ - Using the list of filenames being shared, fill in details that the web - page will need to display. This includes zipping up the file in order to - get the zip file's name and size. - """ - self.common.log("ShareModeWeb", "set_file_info") - self.web.cancel_compression = False - - self.cleanup_filenames = [] - - # build file info list - self.file_info = {'files': [], 'dirs': []} + def build_zipfile_list(self, filenames, processed_size_callback=None): + self.common.log("ShareModeWeb", "build_file_list") for filename in filenames: info = { 'filename': filename, diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index f61da569..4c024849 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -10,12 +10,14 @@ from .. import strings class WebsiteModeWeb(BaseModeWeb): """ - All of the web logic for share mode + All of the web logic for website mode """ def init(self): - self.common.log('WebsiteModeWeb', '__init__') + # Reset assets path + self.web.app.static_folder=self.common.get_resource_path('share/static') + self.define_routes() def define_routes(self): @@ -127,22 +129,12 @@ class WebsiteModeWeb(BaseModeWeb): static_url_path=self.web.static_url_path)) return self.web.add_security_headers(r) - def set_file_info(self, filenames): + def build_file_list(self, filenames): """ Build a data structure that describes the list of files that make up the static website. """ - self.common.log("WebsiteModeWeb", "set_file_info") - - # This is a dictionary that maps HTTP routes to filenames on disk - self.files = {} - - # This is only the root files and dirs, as opposed to all of them - self.root_files = {} - - # If there's just one folder, replace filenames with a list of files inside that folder - if len(filenames) == 1 and os.path.isdir(filenames[0]): - filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] + self.common.log("WebsiteModeWeb", "build_file_list") # Loop through the files for filename in filenames: From 324c6579c24372b617cbf9549459025d305e6112 Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 13 Jun 2019 12:41:12 +0200 Subject: [PATCH 03/48] Move directory_listing function --- onionshare/web/base_mode.py | 36 +++++++++++++++++++++++++++++++++- onionshare/web/website_mode.py | 33 +------------------------------ 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/onionshare/web/base_mode.py b/onionshare/web/base_mode.py index 8843d198..46e63e1a 100644 --- a/onionshare/web/base_mode.py +++ b/onionshare/web/base_mode.py @@ -4,7 +4,7 @@ import tempfile import mimetypes from flask import Response, request, render_template, make_response -from .. import strings + from .. import strings class BaseModeWeb(object): """ @@ -45,6 +45,40 @@ class BaseModeWeb(object): """ pass + + def directory_listing(self, path, filenames, filesystem_path=None): + # If filesystem_path is None, this is the root directory listing + files = [] + dirs = [] + + for filename in filenames: + if filesystem_path: + this_filesystem_path = os.path.join(filesystem_path, filename) + else: + this_filesystem_path = self.files[filename] + + is_dir = os.path.isdir(this_filesystem_path) + + if is_dir: + dirs.append({ + 'basename': filename + }) + else: + size = os.path.getsize(this_filesystem_path) + size_human = self.common.human_readable_filesize(size) + files.append({ + 'basename': filename, + 'size_human': size_human + }) + + r = make_response(render_template('listing.html', + path=path, + files=files, + dirs=dirs, + static_url_path=self.web.static_url_path)) + return self.web.add_security_headers(r) + + def set_file_info(self, filenames, processed_size_callback=None): """ Build a data structure that describes the list of files diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 4c024849..287acbd9 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -97,38 +97,7 @@ class WebsiteModeWeb(BaseModeWeb): # If the path isn't found, throw a 404 return self.web.error404() - def directory_listing(self, path, filenames, filesystem_path=None): - # If filesystem_path is None, this is the root directory listing - files = [] - dirs = [] - - for filename in filenames: - if filesystem_path: - this_filesystem_path = os.path.join(filesystem_path, filename) - else: - this_filesystem_path = self.files[filename] - - is_dir = os.path.isdir(this_filesystem_path) - - if is_dir: - dirs.append({ - 'basename': filename - }) - else: - size = os.path.getsize(this_filesystem_path) - size_human = self.common.human_readable_filesize(size) - files.append({ - 'basename': filename, - 'size_human': size_human - }) - - r = make_response(render_template('listing.html', - path=path, - files=files, - dirs=dirs, - static_url_path=self.web.static_url_path)) - return self.web.add_security_headers(r) - + def build_file_list(self, filenames): """ Build a data structure that describes the list of files that make up From e6f114c677775f01bc95f7496eea4aecb59c9d64 Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 13 Jun 2019 21:47:49 +0200 Subject: [PATCH 04/48] Refactor directory_listing function --- onionshare/web/base_mode.py | 43 +++++++++++++++------------------- onionshare/web/share_mode.py | 11 ++------- onionshare/web/website_mode.py | 31 ++++++++++++++++++++---- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/onionshare/web/base_mode.py b/onionshare/web/base_mode.py index 46e63e1a..ff7e11be 100644 --- a/onionshare/web/base_mode.py +++ b/onionshare/web/base_mode.py @@ -4,7 +4,7 @@ import tempfile import mimetypes from flask import Response, request, render_template, make_response - from .. import strings +from .. import strings class BaseModeWeb(object): """ @@ -46,36 +46,31 @@ class BaseModeWeb(object): pass - def directory_listing(self, path, filenames, filesystem_path=None): + def directory_listing(self, path='', filenames=[], filesystem_path=None): # If filesystem_path is None, this is the root directory listing files = [] dirs = [] + r = '' - for filename in filenames: - if filesystem_path: - this_filesystem_path = os.path.join(filesystem_path, filename) - else: - this_filesystem_path = self.files[filename] + if self.web.mode == 'website': + files, dirs = build_directory_listing(filenames) - is_dir = os.path.isdir(this_filesystem_path) + r = make_response(render_template('listing.html', + path=path, + files=files, + dirs=dirs, + static_url_path=self.web.static_url_path)) - if is_dir: - dirs.append({ - 'basename': filename - }) - else: - size = os.path.getsize(this_filesystem_path) - size_human = self.common.human_readable_filesize(size) - files.append({ - 'basename': filename, - 'size_human': size_human - }) + elif self.web.mode == 'share': + r = make_response(render_template( + 'send.html', + file_info=self.file_info, + filename=os.path.basename(self.download_filename), + filesize=self.filesize, + filesize_human=self.common.human_readable_filesize(self.download_filesize), + is_zipped=self.is_zipped, + static_url_path=self.web.static_url_path)) - r = make_response(render_template('listing.html', - path=path, - files=files, - dirs=dirs, - static_url_path=self.web.static_url_path)) return self.web.add_security_headers(r) diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 68763357..cb3bba50 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -44,15 +44,8 @@ class ShareModeWeb(BaseModeWeb): else: self.filesize = self.download_filesize - r = make_response(render_template( - 'send.html', - file_info=self.file_info, - filename=os.path.basename(self.download_filename), - filesize=self.filesize, - filesize_human=self.common.human_readable_filesize(self.download_filesize), - is_zipped=self.is_zipped, - static_url_path=self.web.static_url_path)) - return self.web.add_security_headers(r) + return self.directory_listing() + @self.web.app.route("/download") def download(): diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 287acbd9..5183bebc 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -14,12 +14,9 @@ class WebsiteModeWeb(BaseModeWeb): """ def init(self): self.common.log('WebsiteModeWeb', '__init__') - - # Reset assets path - self.web.app.static_folder=self.common.get_resource_path('share/static') - self.define_routes() + def define_routes(self): """ The web app routes for sharing a website @@ -56,6 +53,7 @@ class WebsiteModeWeb(BaseModeWeb): # Render it dirname = os.path.dirname(self.files[index_path]) basename = os.path.basename(self.files[index_path]) + return send_from_directory(dirname, basename) else: @@ -80,6 +78,7 @@ class WebsiteModeWeb(BaseModeWeb): return self.web.error404() else: # Special case loading / + if path == '': index_path = 'index.html' if index_path in self.files: @@ -97,7 +96,29 @@ class WebsiteModeWeb(BaseModeWeb): # If the path isn't found, throw a 404 return self.web.error404() - + def build_directory_listing(self, filenames): + for filename in filenames: + if filesystem_path: + this_filesystem_path = os.path.join(filesystem_path, filename) + else: + this_filesystem_path = self.files[filename] + + is_dir = os.path.isdir(this_filesystem_path) + + if is_dir: + dirs.append({ + 'basename': filename + }) + else: + size = os.path.getsize(this_filesystem_path) + size_human = self.common.human_readable_filesize(size) + files.append({ + 'basename': filename, + 'size_human': size_human + }) + return files, dirs + + def build_file_list(self, filenames): """ Build a data structure that describes the list of files that make up From 347b25d5a06576f5e2a4107441579e6aa78a0483 Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 13 Jun 2019 22:56:48 +0200 Subject: [PATCH 05/48] Revert "Generate a new static_url_path each time the server is stopped and started again" This change creates problems with how website mode renders assets. This reverts commit ae110026e72bc7bd38aa515f52fb52aa3236e8b1. --- onionshare/web/web.py | 18 +++++------------- onionshare_gui/threads.py | 3 --- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 1d2a3fec..17dd8c15 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -51,12 +51,16 @@ class Web(object): self.common = common self.common.log('Web', '__init__', 'is_gui={}, mode={}'.format(is_gui, mode)) + # The static URL path has a 128-bit random number in it to avoid having name + # collisions with files that might be getting shared + self.static_url_path = '/static_{}'.format(self.common.random_string(16)) + # The flask app self.app = Flask(__name__, + static_url_path=self.static_url_path, static_folder=self.common.get_resource_path('static'), template_folder=self.common.get_resource_path('templates')) self.app.secret_key = self.common.random_string(8) - self.generate_static_url_path() self.auth = HTTPBasicAuth() self.auth.error_handler(self.error401) @@ -225,18 +229,6 @@ class Web(object): self.password = self.common.build_password() self.common.log('Web', 'generate_password', 'built random password: "{}"'.format(self.password)) - def generate_static_url_path(self): - # The static URL path has a 128-bit random number in it to avoid having name - # collisions with files that might be getting shared - self.static_url_path = '/static_{}'.format(self.common.random_string(16)) - self.common.log('Web', 'generate_static_url_path', 'new static_url_path is {}'.format(self.static_url_path)) - - # Update the flask route to handle the new static URL path - self.app.static_url_path = self.static_url_path - self.app.add_url_rule( - self.static_url_path + '/', - endpoint='static', view_func=self.app.send_static_file) - def verbose_mode(self): """ Turn on verbose mode, which will log flask errors to a file. diff --git a/onionshare_gui/threads.py b/onionshare_gui/threads.py index 57e0f0af..bee1b6bc 100644 --- a/onionshare_gui/threads.py +++ b/onionshare_gui/threads.py @@ -42,9 +42,6 @@ class OnionThread(QtCore.QThread): def run(self): self.mode.common.log('OnionThread', 'run') - # Make a new static URL path for each new share - self.mode.web.generate_static_url_path() - # Choose port and password early, because we need them to exist in advance for scheduled shares self.mode.app.stay_open = not self.mode.common.settings.get('close_after_first_download') if not self.mode.app.port: From 7b8a83854dac9c840924a430b45ff716119e5b56 Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 13 Jun 2019 22:58:33 +0200 Subject: [PATCH 06/48] Remove reset of web app path in receive mode --- onionshare/web/receive_mode.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index 3f848d2f..b444deb2 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -18,9 +18,6 @@ class ReceiveModeWeb(object): self.web = web - # Reset assets path - self.web.app.static_folder=self.common.get_resource_path('static') - self.can_upload = True self.upload_count = 0 self.uploads_in_progress = [] @@ -34,7 +31,7 @@ class ReceiveModeWeb(object): @self.web.app.route("/") def index(): self.web.add_request(self.web.REQUEST_LOAD, request.path) - r = make_response(render_template('receive.html', + r = make_response(render_template('receive.html', static_url_path=self.web.static_url_path)) return self.web.add_security_headers(r) From 5c0c45a6debb19e127999d8d20cc97ebb9d05138 Mon Sep 17 00:00:00 2001 From: hiro Date: Fri, 14 Jun 2019 18:21:12 +0200 Subject: [PATCH 07/48] Clean up rendering logic between share and website mode --- onionshare/web/base_mode.py | 149 +++++++++++++++++++++++++++++---- onionshare/web/share_mode.py | 9 +- onionshare/web/website_mode.py | 114 +------------------------ share/templates/send.html | 17 ++-- 4 files changed, 151 insertions(+), 138 deletions(-) diff --git a/onionshare/web/base_mode.py b/onionshare/web/base_mode.py index ff7e11be..905414f6 100644 --- a/onionshare/web/base_mode.py +++ b/onionshare/web/base_mode.py @@ -2,7 +2,7 @@ import os import sys import tempfile import mimetypes -from flask import Response, request, render_template, make_response +from flask import Response, request, render_template, make_response, send_from_directory from .. import strings @@ -26,6 +26,8 @@ class BaseModeWeb(object): # Dictionary mapping file paths to filenames on disk self.files = {} + # This is only the root files and dirs, as opposed to all of them + self.root_files = {} self.cleanup_filenames = [] self.file_info = {'files': [], 'dirs': []} @@ -46,15 +48,15 @@ class BaseModeWeb(object): pass - def directory_listing(self, path='', filenames=[], filesystem_path=None): + def directory_listing(self, filenames, path='', filesystem_path=None): # If filesystem_path is None, this is the root directory listing files = [] dirs = [] r = '' - if self.web.mode == 'website': - files, dirs = build_directory_listing(filenames) + files, dirs = self.build_directory_listing(filenames, filesystem_path) + if self.web.mode == 'website': r = make_response(render_template('listing.html', path=path, files=files, @@ -65,6 +67,8 @@ class BaseModeWeb(object): r = make_response(render_template( 'send.html', file_info=self.file_info, + files=files, + dirs=dirs, filename=os.path.basename(self.download_filename), filesize=self.filesize, filesize_human=self.common.human_readable_filesize(self.download_filesize), @@ -74,26 +78,141 @@ class BaseModeWeb(object): return self.web.add_security_headers(r) + def build_directory_listing(self, filenames, filesystem_path): + files = [] + dirs = [] + + for filename in filenames: + if filesystem_path: + this_filesystem_path = os.path.join(filesystem_path, filename) + else: + this_filesystem_path = self.files[filename] + + is_dir = os.path.isdir(this_filesystem_path) + + if is_dir: + dirs.append({ + 'basename': filename + }) + else: + size = os.path.getsize(this_filesystem_path) + size_human = self.common.human_readable_filesize(size) + files.append({ + 'basename': filename, + 'size_human': size_human + }) + return files, dirs + + def set_file_info(self, filenames, processed_size_callback=None): """ Build a data structure that describes the list of files """ - if self.web.mode == 'website': - self.common.log("WebsiteModeWeb", "set_file_info") - self.web.cancel_compression = True - # This is only the root files and dirs, as opposed to all of them - self.root_files = {} + # If there's just one folder, replace filenames with a list of files inside that folder + if len(filenames) == 1 and os.path.isdir(filenames[0]): + filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] - # If there's just one folder, replace filenames with a list of files inside that folder - if len(filenames) == 1 and os.path.isdir(filenames[0]): - filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] + self.build_file_list(filenames) - self.build_file_list(filenames) - - elif self.web.mode == 'share': + if self.web.mode == 'share': self.common.log("ShareModeWeb", "set_file_info") self.web.cancel_compression = False self.build_zipfile_list(filenames, processed_size_callback) + elif self.web.mode == 'website': + self.common.log("WebsiteModeWeb", "set_file_info") + self.web.cancel_compression = True + return True + + + def build_file_list(self, filenames): + """ + Build a data structure that describes the list of files that make up + the static website. + """ + self.common.log("BaseModeWeb", "build_file_list") + + # Loop through the files + for filename in filenames: + basename = os.path.basename(filename.rstrip('/')) + + # If it's a filename, add it + if os.path.isfile(filename): + self.files[basename] = filename + self.root_files[basename] = filename + + # If it's a directory, add it recursively + elif os.path.isdir(filename): + self.root_files[basename + '/'] = filename + + for root, _, nested_filenames in os.walk(filename): + # Normalize the root path. So if the directory name is "/home/user/Documents/some_folder", + # and it has a nested folder foobar, the root is "/home/user/Documents/some_folder/foobar". + # The normalized_root should be "some_folder/foobar" + normalized_root = os.path.join(basename, root[len(filename):].lstrip('/')).rstrip('/') + + # Add the dir itself + self.files[normalized_root + '/'] = root + + # Add the files in this dir + for nested_filename in nested_filenames: + self.files[os.path.join(normalized_root, nested_filename)] = os.path.join(root, nested_filename) + + return True + + def render_logic(self, path=''): + if path in self.files: + filesystem_path = self.files[path] + + # If it's a directory + if os.path.isdir(filesystem_path): + # Is there an index.html? + index_path = os.path.join(path, 'index.html') + if self.web.mode == 'website' and index_path in self.files: + # Render it + dirname = os.path.dirname(self.files[index_path]) + basename = os.path.basename(self.files[index_path]) + + return send_from_directory(dirname, basename) + + else: + # Otherwise, render directory listing + filenames = [] + for filename in os.listdir(filesystem_path): + if os.path.isdir(os.path.join(filesystem_path, filename)): + filenames.append(filename + '/') + else: + filenames.append(filename) + filenames.sort() + return self.directory_listing(filenames, path, filesystem_path) + + # If it's a file + elif os.path.isfile(filesystem_path): + dirname = os.path.dirname(filesystem_path) + basename = os.path.basename(filesystem_path) + return send_from_directory(dirname, basename) + + # If it's not a directory or file, throw a 404 + else: + return self.web.error404() + else: + # Special case loading / + + if path == '': + index_path = 'index.html' + if self.web.mode == 'website' and index_path in self.files: + # Render it + dirname = os.path.dirname(self.files[index_path]) + basename = os.path.basename(self.files[index_path]) + return send_from_directory(dirname, basename) + else: + # Root directory listing + filenames = list(self.root_files) + filenames.sort() + return self.directory_listing(filenames, path) + + else: + # If the path isn't found, throw a 404 + return self.web.error404() diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index cb3bba50..afcbdcd9 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -23,8 +23,9 @@ class ShareModeWeb(BaseModeWeb): """ The web app routes for sharing files """ - @self.web.app.route("/") - def index(): + @self.web.app.route('/', defaults={'path': ''}) + @self.web.app.route('/') + def index(path): """ Render the template for the onionshare landing page. """ @@ -44,7 +45,7 @@ class ShareModeWeb(BaseModeWeb): else: self.filesize = self.download_filesize - return self.directory_listing() + return self.render_logic(path) @self.web.app.route("/download") @@ -171,7 +172,7 @@ class ShareModeWeb(BaseModeWeb): return r def build_zipfile_list(self, filenames, processed_size_callback=None): - self.common.log("ShareModeWeb", "build_file_list") + self.common.log("ShareModeWeb", "build_zipfile_list") for filename in filenames: info = { 'filename': filename, diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 5183bebc..b8e4dfdf 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -2,7 +2,7 @@ import os import sys import tempfile import mimetypes -from flask import Response, request, render_template, make_response, send_from_directory +from flask import Response, request, render_template, make_response from .base_mode import BaseModeWeb from .. import strings @@ -42,114 +42,4 @@ class WebsiteModeWeb(BaseModeWeb): 'action': 'visit' }) - if path in self.files: - filesystem_path = self.files[path] - - # If it's a directory - if os.path.isdir(filesystem_path): - # Is there an index.html? - index_path = os.path.join(path, 'index.html') - if index_path in self.files: - # Render it - dirname = os.path.dirname(self.files[index_path]) - basename = os.path.basename(self.files[index_path]) - - return send_from_directory(dirname, basename) - - else: - # Otherwise, render directory listing - filenames = [] - for filename in os.listdir(filesystem_path): - if os.path.isdir(os.path.join(filesystem_path, filename)): - filenames.append(filename + '/') - else: - filenames.append(filename) - filenames.sort() - return self.directory_listing(path, filenames, filesystem_path) - - # If it's a file - elif os.path.isfile(filesystem_path): - dirname = os.path.dirname(filesystem_path) - basename = os.path.basename(filesystem_path) - return send_from_directory(dirname, basename) - - # If it's not a directory or file, throw a 404 - else: - return self.web.error404() - else: - # Special case loading / - - if path == '': - index_path = 'index.html' - if index_path in self.files: - # Render it - dirname = os.path.dirname(self.files[index_path]) - basename = os.path.basename(self.files[index_path]) - return send_from_directory(dirname, basename) - else: - # Root directory listing - filenames = list(self.root_files) - filenames.sort() - return self.directory_listing(path, filenames) - - else: - # If the path isn't found, throw a 404 - return self.web.error404() - - def build_directory_listing(self, filenames): - for filename in filenames: - if filesystem_path: - this_filesystem_path = os.path.join(filesystem_path, filename) - else: - this_filesystem_path = self.files[filename] - - is_dir = os.path.isdir(this_filesystem_path) - - if is_dir: - dirs.append({ - 'basename': filename - }) - else: - size = os.path.getsize(this_filesystem_path) - size_human = self.common.human_readable_filesize(size) - files.append({ - 'basename': filename, - 'size_human': size_human - }) - return files, dirs - - - def build_file_list(self, filenames): - """ - Build a data structure that describes the list of files that make up - the static website. - """ - self.common.log("WebsiteModeWeb", "build_file_list") - - # Loop through the files - for filename in filenames: - basename = os.path.basename(filename.rstrip('/')) - - # If it's a filename, add it - if os.path.isfile(filename): - self.files[basename] = filename - self.root_files[basename] = filename - - # If it's a directory, add it recursively - elif os.path.isdir(filename): - self.root_files[basename + '/'] = filename - - for root, _, nested_filenames in os.walk(filename): - # Normalize the root path. So if the directory name is "/home/user/Documents/some_folder", - # and it has a nested folder foobar, the root is "/home/user/Documents/some_folder/foobar". - # The normalized_root should be "some_folder/foobar" - normalized_root = os.path.join(basename, root[len(filename):].lstrip('/')).rstrip('/') - - # Add the dir itself - self.files[normalized_root + '/'] = root - - # Add the files in this dir - for nested_filename in nested_filenames: - self.files[os.path.join(normalized_root, nested_filename)] = os.path.join(root, nested_filename) - - return True + return self.render_logic(path) diff --git a/share/templates/send.html b/share/templates/send.html index e0076c0f..490fddf4 100644 --- a/share/templates/send.html +++ b/share/templates/send.html @@ -28,24 +28,27 @@ Size - {% for info in file_info.dirs %} + {% for info in dirs %} - {{ info.basename }} + + {{ info.basename }} + - {{ info.size_human }} - + — {% endfor %} - {% for info in file_info.files %} + + {% for info in files %} - {{ info.basename }} + + {{ info.basename }} + {{ info.size_human }} - {% endfor %} From f2bd2e943d74e50d80a166ca40760330052204f0 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 18:05:53 -0400 Subject: [PATCH 08/48] Rename BaseModeWeb to SendBaseModeWeb, because this code is only actually shared by send modes (Share and Website, not Receive) --- onionshare/web/receive_mode.py | 2 +- onionshare/web/{base_mode.py => send_base_mode.py} | 6 +++--- onionshare/web/share_mode.py | 4 ++-- onionshare/web/website_mode.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename onionshare/web/{base_mode.py => send_base_mode.py} (97%) diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index b444deb2..d2b03da0 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -8,7 +8,7 @@ from werkzeug.utils import secure_filename from .. import strings -class ReceiveModeWeb(object): +class ReceiveModeWeb: """ All of the web logic for receive mode """ diff --git a/onionshare/web/base_mode.py b/onionshare/web/send_base_mode.py similarity index 97% rename from onionshare/web/base_mode.py rename to onionshare/web/send_base_mode.py index 905414f6..0ca1b306 100644 --- a/onionshare/web/base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -6,12 +6,12 @@ from flask import Response, request, render_template, make_response, send_from_d from .. import strings -class BaseModeWeb(object): +class SendBaseModeWeb: """ - All of the web logic shared between share and website mode + All of the web logic shared between share and website mode (modes where the user sends files) """ def __init__(self, common, web): - super(BaseModeWeb, self).__init__() + super(SendBaseModeWeb, self).__init__() self.common = common self.web = web diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index afcbdcd9..1ed8c29a 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -6,11 +6,11 @@ import mimetypes import gzip from flask import Response, request, render_template, make_response -from .base_mode import BaseModeWeb +from .send_base_mode import SendBaseModeWeb from .. import strings -class ShareModeWeb(BaseModeWeb): +class ShareModeWeb(SendBaseModeWeb): """ All of the web logic for share mode """ diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index b8e4dfdf..f38daa06 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -4,11 +4,11 @@ import tempfile import mimetypes from flask import Response, request, render_template, make_response -from .base_mode import BaseModeWeb +from .send_base_mode import SendBaseModeWeb from .. import strings -class WebsiteModeWeb(BaseModeWeb): +class WebsiteModeWeb(SendBaseModeWeb): """ All of the web logic for website mode """ From bffbc1930d9848b4c84476d09e37ac64204e6204 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 18:44:44 -0400 Subject: [PATCH 09/48] Move all mode-specific code out of SendBaseModeWeb and into inherited methods in WebsiteModeWeb and ShareModeWeb --- onionshare/web/send_base_mode.py | 117 ++++--------------- onionshare/web/share_mode.py | 63 ++++++++-- onionshare/web/website_mode.py | 74 ++++++++++-- onionshare_gui/mode/share_mode/threads.py | 8 +- onionshare_gui/mode/website_mode/__init__.py | 8 +- 5 files changed, 151 insertions(+), 119 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 0ca1b306..80f9e315 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -2,10 +2,11 @@ import os import sys import tempfile import mimetypes -from flask import Response, request, render_template, make_response, send_from_directory +from flask import Response, request, render_template, make_response from .. import strings + class SendBaseModeWeb: """ All of the web logic shared between share and website mode (modes where the user sends files) @@ -40,44 +41,29 @@ class SendBaseModeWeb: self.define_routes() - def init(self): + self.common.log('SendBaseModeWeb', '__init__') + self.define_routes() + + def define_routes(self): """ - Add custom initialization here. + Inherited class will implement this """ pass + def directory_listing_template(self): + """ + Inherited class will implement this. It should call render_template and return + the response. + """ + pass def directory_listing(self, filenames, path='', filesystem_path=None): # If filesystem_path is None, this is the root directory listing - files = [] - dirs = [] - r = '' - files, dirs = self.build_directory_listing(filenames, filesystem_path) - - if self.web.mode == 'website': - r = make_response(render_template('listing.html', - path=path, - files=files, - dirs=dirs, - static_url_path=self.web.static_url_path)) - - elif self.web.mode == 'share': - r = make_response(render_template( - 'send.html', - file_info=self.file_info, - files=files, - dirs=dirs, - filename=os.path.basename(self.download_filename), - filesize=self.filesize, - filesize_human=self.common.human_readable_filesize(self.download_filesize), - is_zipped=self.is_zipped, - static_url_path=self.web.static_url_path)) - + r = self.directory_listing_template(path, files, dirs) return self.web.add_security_headers(r) - def build_directory_listing(self, filenames, filesystem_path): files = [] dirs = [] @@ -103,6 +89,11 @@ class SendBaseModeWeb: }) return files, dirs + def set_file_info_custom(self, filenames, processed_size_callback): + """ + Inherited class will implement this. + """ + pass def set_file_info(self, filenames, processed_size_callback=None): """ @@ -114,18 +105,7 @@ class SendBaseModeWeb: filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] self.build_file_list(filenames) - - if self.web.mode == 'share': - self.common.log("ShareModeWeb", "set_file_info") - self.web.cancel_compression = False - self.build_zipfile_list(filenames, processed_size_callback) - - elif self.web.mode == 'website': - self.common.log("WebsiteModeWeb", "set_file_info") - self.web.cancel_compression = True - - return True - + self.set_file_info_custom(filenames, processed_size_callback) def build_file_list(self, filenames): """ @@ -163,56 +143,7 @@ class SendBaseModeWeb: return True def render_logic(self, path=''): - if path in self.files: - filesystem_path = self.files[path] - - # If it's a directory - if os.path.isdir(filesystem_path): - # Is there an index.html? - index_path = os.path.join(path, 'index.html') - if self.web.mode == 'website' and index_path in self.files: - # Render it - dirname = os.path.dirname(self.files[index_path]) - basename = os.path.basename(self.files[index_path]) - - return send_from_directory(dirname, basename) - - else: - # Otherwise, render directory listing - filenames = [] - for filename in os.listdir(filesystem_path): - if os.path.isdir(os.path.join(filesystem_path, filename)): - filenames.append(filename + '/') - else: - filenames.append(filename) - filenames.sort() - return self.directory_listing(filenames, path, filesystem_path) - - # If it's a file - elif os.path.isfile(filesystem_path): - dirname = os.path.dirname(filesystem_path) - basename = os.path.basename(filesystem_path) - return send_from_directory(dirname, basename) - - # If it's not a directory or file, throw a 404 - else: - return self.web.error404() - else: - # Special case loading / - - if path == '': - index_path = 'index.html' - if self.web.mode == 'website' and index_path in self.files: - # Render it - dirname = os.path.dirname(self.files[index_path]) - basename = os.path.basename(self.files[index_path]) - return send_from_directory(dirname, basename) - else: - # Root directory listing - filenames = list(self.root_files) - filenames.sort() - return self.directory_listing(filenames, path) - - else: - # If the path isn't found, throw a 404 - return self.web.error404() + """ + Inherited class will implement this. + """ + pass diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 1ed8c29a..8558a996 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -4,7 +4,7 @@ import tempfile import zipfile import mimetypes import gzip -from flask import Response, request, render_template, make_response +from flask import Response, request, render_template, make_response, send_from_directory from .send_base_mode import SendBaseModeWeb from .. import strings @@ -14,11 +14,6 @@ class ShareModeWeb(SendBaseModeWeb): """ All of the web logic for share mode """ - def init(self): - self.common.log('ShareModeWeb', '__init__') - - self.define_routes() - def define_routes(self): """ The web app routes for sharing files @@ -47,7 +42,6 @@ class ShareModeWeb(SendBaseModeWeb): return self.render_logic(path) - @self.web.app.route("/download") def download(): """ @@ -171,6 +165,61 @@ class ShareModeWeb(SendBaseModeWeb): r.headers.set('Content-Type', content_type) return r + def directory_listing_template(self, path, files, dirs): + return make_response(render_template( + 'send.html', + file_info=self.file_info, + files=files, + dirs=dirs, + filename=os.path.basename(self.download_filename), + filesize=self.filesize, + filesize_human=self.common.human_readable_filesize(self.download_filesize), + is_zipped=self.is_zipped, + static_url_path=self.web.static_url_path)) + + def set_file_info_custom(self, filenames, processed_size_callback): + self.common.log("ShareModeWeb", "set_file_info_custom") + self.web.cancel_compression = False + self.build_zipfile_list(filenames, processed_size_callback) + + def render_logic(self, path=''): + if path in self.files: + filesystem_path = self.files[path] + + # If it's a directory + if os.path.isdir(filesystem_path): + # Render directory listing + filenames = [] + for filename in os.listdir(filesystem_path): + if os.path.isdir(os.path.join(filesystem_path, filename)): + filenames.append(filename + '/') + else: + filenames.append(filename) + filenames.sort() + return self.directory_listing(filenames, path) + + # If it's a file + elif os.path.isfile(filesystem_path): + dirname = os.path.dirname(filesystem_path) + basename = os.path.basename(filesystem_path) + return send_from_directory(dirname, basename) + + # If it's not a directory or file, throw a 404 + else: + return self.web.error404() + else: + # Special case loading / + + if path == '': + # Root directory listing + filenames = list(self.root_files) + filenames.sort() + return self.directory_listing(filenames, path) + + else: + # If the path isn't found, throw a 404 + return self.web.error404() + def build_zipfile_list(self, filenames, processed_size_callback=None): self.common.log("ShareModeWeb", "build_zipfile_list") for filename in filenames: diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index f38daa06..bb712a59 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -2,7 +2,7 @@ import os import sys import tempfile import mimetypes -from flask import Response, request, render_template, make_response +from flask import Response, request, render_template, make_response, send_from_directory from .send_base_mode import SendBaseModeWeb from .. import strings @@ -12,16 +12,10 @@ class WebsiteModeWeb(SendBaseModeWeb): """ All of the web logic for website mode """ - def init(self): - self.common.log('WebsiteModeWeb', '__init__') - self.define_routes() - - def define_routes(self): """ The web app routes for sharing a website """ - @self.web.app.route('/', defaults={'path': ''}) @self.web.app.route('/') def path_public(path): @@ -43,3 +37,69 @@ class WebsiteModeWeb(SendBaseModeWeb): }) return self.render_logic(path) + + def directory_listing_template(self, path, files, dirs): + return make_response(render_template('listing.html', + path=path, + files=files, + dirs=dirs, + static_url_path=self.web.static_url_path)) + + def set_file_info_custom(self, filenames, processed_size_callback): + self.common.log("WebsiteModeWeb", "set_file_info_custom") + self.web.cancel_compression = True + + def render_logic(self, path=''): + if path in self.files: + filesystem_path = self.files[path] + + # If it's a directory + if os.path.isdir(filesystem_path): + # Is there an index.html? + index_path = os.path.join(path, 'index.html') + if index_path in self.files: + # Render it + dirname = os.path.dirname(self.files[index_path]) + basename = os.path.basename(self.files[index_path]) + + return send_from_directory(dirname, basename) + + else: + # Otherwise, render directory listing + filenames = [] + for filename in os.listdir(filesystem_path): + if os.path.isdir(os.path.join(filesystem_path, filename)): + filenames.append(filename + '/') + else: + filenames.append(filename) + filenames.sort() + return self.directory_listing(filenames, path, filesystem_path) + + # If it's a file + elif os.path.isfile(filesystem_path): + dirname = os.path.dirname(filesystem_path) + basename = os.path.basename(filesystem_path) + return send_from_directory(dirname, basename) + + # If it's not a directory or file, throw a 404 + else: + return self.web.error404() + else: + # Special case loading / + + if path == '': + index_path = 'index.html' + if index_path in self.files: + # Render it + dirname = os.path.dirname(self.files[index_path]) + basename = os.path.basename(self.files[index_path]) + return send_from_directory(dirname, basename) + else: + # Root directory listing + filenames = list(self.root_files) + filenames.sort() + return self.directory_listing(filenames, path, filesystem_path) + + else: + # If the path isn't found, throw a 404 + return self.web.error404() diff --git a/onionshare_gui/mode/share_mode/threads.py b/onionshare_gui/mode/share_mode/threads.py index 24e2c242..fed362eb 100644 --- a/onionshare_gui/mode/share_mode/threads.py +++ b/onionshare_gui/mode/share_mode/threads.py @@ -41,12 +41,8 @@ class CompressThread(QtCore.QThread): self.mode.common.log('CompressThread', 'run') try: - if self.mode.web.share_mode.set_file_info(self.mode.filenames, processed_size_callback=self.set_processed_size): - self.success.emit() - else: - # Cancelled - pass - + self.mode.web.share_mode.set_file_info(self.mode.filenames, processed_size_callback=self.set_processed_size) + self.success.emit() self.mode.app.cleanup_filenames += self.mode.web.share_mode.cleanup_filenames except OSError as e: self.error.emit(e.strerror) diff --git a/onionshare_gui/mode/website_mode/__init__.py b/onionshare_gui/mode/website_mode/__init__.py index 50af4725..9f01cabc 100644 --- a/onionshare_gui/mode/website_mode/__init__.py +++ b/onionshare_gui/mode/website_mode/__init__.py @@ -165,12 +165,8 @@ class WebsiteMode(Mode): Step 3 in starting the server. Display large filesize warning, if applicable. """ - - if self.web.website_mode.set_file_info(self.filenames): - self.success.emit() - else: - # Cancelled - pass + self.web.website_mode.set_file_info(self.filenames) + self.success.emit() def start_server_error_custom(self): """ From 1e83a1bfd635f0aa56ad5ee85c4c52fed36bcb8d Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 16:02:10 -0700 Subject: [PATCH 10/48] Oops, need to call directory_listing with filesystem_path --- onionshare/web/share_mode.py | 2 +- onionshare/web/website_mode.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 8558a996..6f847fe7 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -196,7 +196,7 @@ class ShareModeWeb(SendBaseModeWeb): else: filenames.append(filename) filenames.sort() - return self.directory_listing(filenames, path) + return self.directory_listing(filenames, path, filesystem_path) # If it's a file elif os.path.isfile(filesystem_path): diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index bb712a59..9ddbf89b 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -98,7 +98,7 @@ class WebsiteModeWeb(SendBaseModeWeb): # Root directory listing filenames = list(self.root_files) filenames.sort() - return self.directory_listing(filenames, path, filesystem_path) + return self.directory_listing(filenames, path) else: # If the path isn't found, throw a 404 From 2143d7016e80b39646e21a7948608498700f3586 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 16:03:57 -0700 Subject: [PATCH 11/48] Add Web.generate_static_url_path back, so each share has its own static path --- onionshare/web/web.py | 19 +++++++++++++------ onionshare_gui/threads.py | 3 +++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 17dd8c15..8d5a6af5 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -30,7 +30,7 @@ except: pass -class Web(object): +class Web: """ The Web object is the OnionShare web server, powered by flask """ @@ -51,16 +51,12 @@ class Web(object): self.common = common self.common.log('Web', '__init__', 'is_gui={}, mode={}'.format(is_gui, mode)) - # The static URL path has a 128-bit random number in it to avoid having name - # collisions with files that might be getting shared - self.static_url_path = '/static_{}'.format(self.common.random_string(16)) - # The flask app self.app = Flask(__name__, - static_url_path=self.static_url_path, static_folder=self.common.get_resource_path('static'), template_folder=self.common.get_resource_path('templates')) self.app.secret_key = self.common.random_string(8) + self.generate_static_url_path() self.auth = HTTPBasicAuth() self.auth.error_handler(self.error401) @@ -127,6 +123,17 @@ class Web(object): elif self.mode == 'share': self.share_mode = ShareModeWeb(self.common, self) + def generate_static_url_path(self): + # The static URL path has a 128-bit random number in it to avoid having name + # collisions with files that might be getting shared + self.static_url_path = '/static_{}'.format(self.common.random_string(16)) + self.common.log('Web', 'generate_static_url_path', 'new static_url_path is {}'.format(self.static_url_path)) + + # Update the flask route to handle the new static URL path + self.app.static_url_path = self.static_url_path + self.app.add_url_rule( + self.static_url_path + '/', + endpoint='static', view_func=self.app.send_static_file) def define_common_routes(self): """ diff --git a/onionshare_gui/threads.py b/onionshare_gui/threads.py index bee1b6bc..57e0f0af 100644 --- a/onionshare_gui/threads.py +++ b/onionshare_gui/threads.py @@ -42,6 +42,9 @@ class OnionThread(QtCore.QThread): def run(self): self.mode.common.log('OnionThread', 'run') + # Make a new static URL path for each new share + self.mode.web.generate_static_url_path() + # Choose port and password early, because we need them to exist in advance for scheduled shares self.mode.app.stay_open = not self.mode.common.settings.get('close_after_first_download') if not self.mode.app.port: From 15e8ec432112b9101a226063ca3c7e380f852caa Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 16:13:05 -0700 Subject: [PATCH 12/48] Change link style for directory listing --- share/static/css/style.css | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/share/static/css/style.css b/share/static/css/style.css index f2ded524..a904c035 100644 --- a/share/static/css/style.css +++ b/share/static/css/style.css @@ -222,3 +222,12 @@ li.info { color: #666666; margin: 0 0 20px 0; } + +a { + text-decoration: none; + color: #1c1ca0; +} + +a:visited { + color: #601ca0; +} \ No newline at end of file From 77d5b29c76c6c0dc76c223d416b3204fa12060ff Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 19:59:00 -0700 Subject: [PATCH 13/48] Clear the file list every time a share starts --- onionshare/web/send_base_mode.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 80f9e315..68f6aeca 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -29,6 +29,7 @@ class SendBaseModeWeb: self.files = {} # This is only the root files and dirs, as opposed to all of them self.root_files = {} + self.cleanup_filenames = [] self.file_info = {'files': [], 'dirs': []} @@ -114,6 +115,10 @@ class SendBaseModeWeb: """ self.common.log("BaseModeWeb", "build_file_list") + # Clear the list of files + self.files = {} + self.root_files = {} + # Loop through the files for filename in filenames: basename = os.path.basename(filename.rstrip('/')) From 1ceaaaf533c25af1ae4b4e20cc5f43d23783a8cd Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 20:15:30 -0700 Subject: [PATCH 14/48] Add new "Allow downloading of individual files" checkbox to share settings, and only allow it to be enabled when "Stop sharing after files have been sent" is unchecked --- onionshare/settings.py | 1 + onionshare_gui/settings_dialog.py | 25 +++++++++++++++++++++++++ share/locale/en.json | 1 + 3 files changed, 27 insertions(+) diff --git a/onionshare/settings.py b/onionshare/settings.py index 762c6dc2..d76e4855 100644 --- a/onionshare/settings.py +++ b/onionshare/settings.py @@ -98,6 +98,7 @@ class Settings(object): 'auth_type': 'no_auth', 'auth_password': '', 'close_after_first_download': True, + 'share_allow_downloading_individual_files': True, 'autostop_timer': False, 'autostart_timer': False, 'use_stealth': False, diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index ae5f5acf..cabddc9b 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -204,10 +204,17 @@ class SettingsDialog(QtWidgets.QDialog): self.close_after_first_download_checkbox = QtWidgets.QCheckBox() self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Checked) self.close_after_first_download_checkbox.setText(strings._("gui_settings_close_after_first_download_option")) + self.close_after_first_download_checkbox.toggled.connect(self.close_after_first_download_toggled) + + # Close after first download + self.allow_downloading_individual_files_checkbox = QtWidgets.QCheckBox() + self.allow_downloading_individual_files_checkbox.setCheckState(QtCore.Qt.Checked) + self.allow_downloading_individual_files_checkbox.setText(strings._("gui_settings_allow_downloading_individual_files_option")) # Sharing options layout sharing_group_layout = QtWidgets.QVBoxLayout() sharing_group_layout.addWidget(self.close_after_first_download_checkbox) + sharing_group_layout.addWidget(self.allow_downloading_individual_files_checkbox) sharing_group = QtWidgets.QGroupBox(strings._("gui_settings_sharing_label")) sharing_group.setLayout(sharing_group_layout) @@ -503,8 +510,16 @@ class SettingsDialog(QtWidgets.QDialog): close_after_first_download = self.old_settings.get('close_after_first_download') if close_after_first_download: self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Checked) + self.allow_downloading_individual_files_checkbox.setEnabled(False) else: self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Unchecked) + self.allow_downloading_individual_files_checkbox.setEnabled(True) + + allow_downloading_individual_files = self.old_settings.get('share_allow_downloading_individual_files') + if allow_downloading_individual_files: + self.allow_downloading_individual_files_checkbox.setCheckState(QtCore.Qt.Checked) + else: + self.allow_downloading_individual_files_checkbox.setCheckState(QtCore.Qt.Unchecked) autostart_timer = self.old_settings.get('autostart_timer') if autostart_timer: @@ -629,6 +644,15 @@ class SettingsDialog(QtWidgets.QDialog): self.connect_to_tor_label.show() self.onion_settings_widget.hide() + def close_after_first_download_toggled(self, checked): + """ + Stop sharing after files have been sent was toggled. If checked, disable allow downloading of individual files. + """ + self.common.log('SettingsDialog', 'close_after_first_download_toggled') + if checked: + self.allow_downloading_individual_files_checkbox.setEnabled(False) + else: + self.allow_downloading_individual_files_checkbox.setEnabled(True) def connection_type_bundled_toggled(self, checked): """ @@ -956,6 +980,7 @@ class SettingsDialog(QtWidgets.QDialog): settings.load() # To get the last update timestamp settings.set('close_after_first_download', self.close_after_first_download_checkbox.isChecked()) + settings.set('share_allow_downloading_individual_files', self.allow_downloading_individual_files_checkbox.isChecked()) settings.set('autostart_timer', self.autostart_timer_checkbox.isChecked()) settings.set('autostop_timer', self.autostop_timer_checkbox.isChecked()) diff --git a/share/locale/en.json b/share/locale/en.json index 2063a415..23f4dd14 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -52,6 +52,7 @@ "gui_settings_onion_label": "Onion settings", "gui_settings_sharing_label": "Sharing settings", "gui_settings_close_after_first_download_option": "Stop sharing after files have been sent", + "gui_settings_allow_downloading_individual_files_option": "Allow downloading of individual files", "gui_settings_connection_type_label": "How should OnionShare connect to Tor?", "gui_settings_connection_type_bundled_option": "Use the Tor version built into OnionShare", "gui_settings_connection_type_automatic_option": "Attempt auto-configuration with Tor Browser", From fdb94eba0c823099edef1875e9f820e228fe3f93 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 20:36:30 -0700 Subject: [PATCH 15/48] Only allow downloading of individual files if it is enabled in settings, and stop sharing automatically isn't --- onionshare/web/send_base_mode.py | 11 +++++++++-- onionshare/web/share_mode.py | 20 +++++++++++++++----- onionshare/web/website_mode.py | 3 +++ share/static/css/style.css | 4 ++++ share/templates/send.html | 4 ++++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 68f6aeca..6deb38ac 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -41,10 +41,13 @@ class SendBaseModeWeb: self.download_in_progress = False self.define_routes() + self.init() def init(self): - self.common.log('SendBaseModeWeb', '__init__') - self.define_routes() + """ + Inherited class will implement this + """ + pass def define_routes(self): """ @@ -105,6 +108,10 @@ class SendBaseModeWeb: if len(filenames) == 1 and os.path.isdir(filenames[0]): filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] + # Re-initialize + self.init() + + # Build the file list self.build_file_list(filenames) self.set_file_info_custom(filenames, processed_size_callback) diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 6f847fe7..c3066a03 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -14,6 +14,12 @@ class ShareModeWeb(SendBaseModeWeb): """ All of the web logic for share mode """ + def init(self): + self.common.log('ShareModeWeb', 'init') + # If "Stop sharing after files have been sent" is unchecked and "Allow downloading of individual files" is checked + self.download_individual_files = not self.common.settings.get('close_after_first_download') \ + and self.common.settings.get('share_allow_downloading_individual_files') + def define_routes(self): """ The web app routes for sharing files @@ -26,7 +32,7 @@ class ShareModeWeb(SendBaseModeWeb): """ self.web.add_request(self.web.REQUEST_LOAD, request.path) - # Deny new downloads if "Stop After First Download" is checked and there is + # Deny new downloads if "Stop sharing after files have been sent" is checked and there is # currently a download deny_download = not self.web.stay_open and self.download_in_progress if deny_download: @@ -175,7 +181,8 @@ class ShareModeWeb(SendBaseModeWeb): filesize=self.filesize, filesize_human=self.common.human_readable_filesize(self.download_filesize), is_zipped=self.is_zipped, - static_url_path=self.web.static_url_path)) + static_url_path=self.web.static_url_path, + download_individual_files=self.download_individual_files)) def set_file_info_custom(self, filenames, processed_size_callback): self.common.log("ShareModeWeb", "set_file_info_custom") @@ -200,9 +207,12 @@ class ShareModeWeb(SendBaseModeWeb): # If it's a file elif os.path.isfile(filesystem_path): - dirname = os.path.dirname(filesystem_path) - basename = os.path.basename(filesystem_path) - return send_from_directory(dirname, basename) + if self.download_individual_files: + dirname = os.path.dirname(filesystem_path) + basename = os.path.basename(filesystem_path) + return send_from_directory(dirname, basename) + else: + return self.web.error404() # If it's not a directory or file, throw a 404 else: diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 9ddbf89b..82cebdb7 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -12,6 +12,9 @@ class WebsiteModeWeb(SendBaseModeWeb): """ All of the web logic for website mode """ + def init(self): + pass + def define_routes(self): """ The web app routes for sharing a website diff --git a/share/static/css/style.css b/share/static/css/style.css index a904c035..bc986e57 100644 --- a/share/static/css/style.css +++ b/share/static/css/style.css @@ -56,6 +56,10 @@ header .right ul li { cursor: pointer; } +a.button:visited { + color: #ffffff; +} + .close-button { color: #ffffff; background-color: #c90c0c; diff --git a/share/templates/send.html b/share/templates/send.html index 490fddf4..916b3bfe 100644 --- a/share/templates/send.html +++ b/share/templates/send.html @@ -44,9 +44,13 @@ + {% if download_individual_files %} {{ info.basename }} + {% else %} + {{ info.basename }} + {% endif %} {{ info.size_human }} From 9604ee388127d12021e8c96118bf34222467457c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 20:45:19 -0700 Subject: [PATCH 16/48] Load default settings in CLI mode, of config is not passed in --- onionshare/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 7a1bf170..0003106f 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -109,6 +109,8 @@ def main(cwd=None): # Re-load settings, if a custom config was passed in if config: common.load_settings(config) + else: + common.load_settings() # Verbose mode? common.verbose = verbose From 833fd04ef0874100b42e79f7a490667720726d3c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 20:46:27 -0700 Subject: [PATCH 17/48] Fix TestSettings.test_init test --- tests/test_onionshare_settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_onionshare_settings.py b/tests/test_onionshare_settings.py index 05878899..54c09686 100644 --- a/tests/test_onionshare_settings.py +++ b/tests/test_onionshare_settings.py @@ -51,6 +51,7 @@ class TestSettings: 'auth_type': 'no_auth', 'auth_password': '', 'close_after_first_download': True, + 'share_allow_downloading_individual_files': True, 'autostop_timer': False, 'autostart_timer': False, 'use_stealth': False, From 1232eb107241c3a3446444fb1bb6e90e45565dbe Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 20:53:21 -0700 Subject: [PATCH 18/48] Merge SendBaseModeWeb.build_file_list into SendBaseModeWeb.set_file_info function --- onionshare/web/send_base_mode.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 6deb38ac..6468258a 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -111,22 +111,11 @@ class SendBaseModeWeb: # Re-initialize self.init() - # Build the file list - self.build_file_list(filenames) - self.set_file_info_custom(filenames, processed_size_callback) - - def build_file_list(self, filenames): - """ - Build a data structure that describes the list of files that make up - the static website. - """ - self.common.log("BaseModeWeb", "build_file_list") - # Clear the list of files self.files = {} self.root_files = {} - # Loop through the files + # Build the file list for filename in filenames: basename = os.path.basename(filename.rstrip('/')) @@ -152,7 +141,7 @@ class SendBaseModeWeb: for nested_filename in nested_filenames: self.files[os.path.join(normalized_root, nested_filename)] = os.path.join(root, nested_filename) - return True + self.set_file_info_custom(filenames, processed_size_callback) def render_logic(self, path=''): """ From 113cd7eb4b7cc2623c7f12c8ad8782ec0bca321c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 1 Sep 2019 21:22:59 -0700 Subject: [PATCH 19/48] Remove the "Allow downloading individual files" setting altogether, and make it just automatically enabled if "Stop sharing..." is disabled --- onionshare/settings.py | 1 - onionshare/web/share_mode.py | 5 ++--- onionshare_gui/settings_dialog.py | 26 -------------------------- share/locale/en.json | 1 - tests/GuiShareTest.py | 4 ++-- tests/test_onionshare_settings.py | 1 - 6 files changed, 4 insertions(+), 34 deletions(-) diff --git a/onionshare/settings.py b/onionshare/settings.py index d76e4855..762c6dc2 100644 --- a/onionshare/settings.py +++ b/onionshare/settings.py @@ -98,7 +98,6 @@ class Settings(object): 'auth_type': 'no_auth', 'auth_password': '', 'close_after_first_download': True, - 'share_allow_downloading_individual_files': True, 'autostop_timer': False, 'autostart_timer': False, 'use_stealth': False, diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index c3066a03..b478fbd4 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -16,9 +16,8 @@ class ShareModeWeb(SendBaseModeWeb): """ def init(self): self.common.log('ShareModeWeb', 'init') - # If "Stop sharing after files have been sent" is unchecked and "Allow downloading of individual files" is checked - self.download_individual_files = not self.common.settings.get('close_after_first_download') \ - and self.common.settings.get('share_allow_downloading_individual_files') + # Allow downloading individual files if "Stop sharing after files have been sent" is unchecked + self.download_individual_files = not self.common.settings.get('close_after_first_download') def define_routes(self): """ diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index cabddc9b..6ffd4523 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -204,17 +204,10 @@ class SettingsDialog(QtWidgets.QDialog): self.close_after_first_download_checkbox = QtWidgets.QCheckBox() self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Checked) self.close_after_first_download_checkbox.setText(strings._("gui_settings_close_after_first_download_option")) - self.close_after_first_download_checkbox.toggled.connect(self.close_after_first_download_toggled) - - # Close after first download - self.allow_downloading_individual_files_checkbox = QtWidgets.QCheckBox() - self.allow_downloading_individual_files_checkbox.setCheckState(QtCore.Qt.Checked) - self.allow_downloading_individual_files_checkbox.setText(strings._("gui_settings_allow_downloading_individual_files_option")) # Sharing options layout sharing_group_layout = QtWidgets.QVBoxLayout() sharing_group_layout.addWidget(self.close_after_first_download_checkbox) - sharing_group_layout.addWidget(self.allow_downloading_individual_files_checkbox) sharing_group = QtWidgets.QGroupBox(strings._("gui_settings_sharing_label")) sharing_group.setLayout(sharing_group_layout) @@ -510,16 +503,8 @@ class SettingsDialog(QtWidgets.QDialog): close_after_first_download = self.old_settings.get('close_after_first_download') if close_after_first_download: self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Checked) - self.allow_downloading_individual_files_checkbox.setEnabled(False) else: self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Unchecked) - self.allow_downloading_individual_files_checkbox.setEnabled(True) - - allow_downloading_individual_files = self.old_settings.get('share_allow_downloading_individual_files') - if allow_downloading_individual_files: - self.allow_downloading_individual_files_checkbox.setCheckState(QtCore.Qt.Checked) - else: - self.allow_downloading_individual_files_checkbox.setCheckState(QtCore.Qt.Unchecked) autostart_timer = self.old_settings.get('autostart_timer') if autostart_timer: @@ -644,16 +629,6 @@ class SettingsDialog(QtWidgets.QDialog): self.connect_to_tor_label.show() self.onion_settings_widget.hide() - def close_after_first_download_toggled(self, checked): - """ - Stop sharing after files have been sent was toggled. If checked, disable allow downloading of individual files. - """ - self.common.log('SettingsDialog', 'close_after_first_download_toggled') - if checked: - self.allow_downloading_individual_files_checkbox.setEnabled(False) - else: - self.allow_downloading_individual_files_checkbox.setEnabled(True) - def connection_type_bundled_toggled(self, checked): """ Connection type bundled was toggled. If checked, hide authentication fields. @@ -980,7 +955,6 @@ class SettingsDialog(QtWidgets.QDialog): settings.load() # To get the last update timestamp settings.set('close_after_first_download', self.close_after_first_download_checkbox.isChecked()) - settings.set('share_allow_downloading_individual_files', self.allow_downloading_individual_files_checkbox.isChecked()) settings.set('autostart_timer', self.autostart_timer_checkbox.isChecked()) settings.set('autostop_timer', self.autostop_timer_checkbox.isChecked()) diff --git a/share/locale/en.json b/share/locale/en.json index 23f4dd14..2063a415 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -52,7 +52,6 @@ "gui_settings_onion_label": "Onion settings", "gui_settings_sharing_label": "Sharing settings", "gui_settings_close_after_first_download_option": "Stop sharing after files have been sent", - "gui_settings_allow_downloading_individual_files_option": "Allow downloading of individual files", "gui_settings_connection_type_label": "How should OnionShare connect to Tor?", "gui_settings_connection_type_bundled_option": "Use the Tor version built into OnionShare", "gui_settings_connection_type_automatic_option": "Attempt auto-configuration with Tor Browser", diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index 64e57b9f..70ae43fd 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -44,7 +44,7 @@ class GuiShareTest(GuiBaseTest): self.file_selection_widget_has_files(0) - def file_selection_widget_readd_files(self): + def file_selection_widget_read_files(self): '''Re-add some files to the list so we can share''' self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts') self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/test.txt') @@ -117,7 +117,7 @@ class GuiShareTest(GuiBaseTest): self.history_is_visible(self.gui.share_mode) self.deleting_all_files_hides_delete_button() self.add_a_file_and_delete_using_its_delete_widget() - self.file_selection_widget_readd_files() + self.file_selection_widget_read_files() def run_all_share_mode_started_tests(self, public_mode, startup_time=2000): diff --git a/tests/test_onionshare_settings.py b/tests/test_onionshare_settings.py index 54c09686..05878899 100644 --- a/tests/test_onionshare_settings.py +++ b/tests/test_onionshare_settings.py @@ -51,7 +51,6 @@ class TestSettings: 'auth_type': 'no_auth', 'auth_password': '', 'close_after_first_download': True, - 'share_allow_downloading_individual_files': True, 'autostop_timer': False, 'autostart_timer': False, 'use_stealth': False, From bd329c487cc838ab643e1fd350099bcd2af32cd8 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 2 Sep 2019 18:01:56 +1000 Subject: [PATCH 20/48] Register a history item when an individual file is viewed that does not match a 'reserved' path --- onionshare_gui/mode/history.py | 31 ++++++++++++++++++++++ onionshare_gui/mode/share_mode/__init__.py | 11 +++++++- share/locale/en.json | 3 +++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index 51b36f9a..c2c696fc 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -341,6 +341,37 @@ class ReceiveHistoryItem(HistoryItem): self.label.setText(self.get_canceled_label_text(self.started)) +class IndividualFileHistoryItem(HistoryItem): + """ + Individual file history item, for share mode viewing of individual files + """ + def __init__(self, common, path): + super(IndividualFileHistoryItem, self).__init__() + self.status = HistoryItem.STATUS_STARTED + self.common = common + + self.visited = time.time() + self.visited_dt = datetime.fromtimestamp(self.visited) + + # Labels + self.timestamp_label = QtWidgets.QLabel(self.visited_dt.strftime("%b %d, %I:%M%p")) + self.path_viewed_label = QtWidgets.QLabel(strings._('gui_individual_file_download').format(path)) + + # Layout + layout = QtWidgets.QVBoxLayout() + layout.addWidget(self.timestamp_label) + layout.addWidget(self.path_viewed_label) + self.setLayout(layout) + + + def update(self): + self.label.setText(self.get_finished_label_text(self.started_dt)) + self.status = HistoryItem.STATUS_FINISHED + + def cancel(self): + self.progress_bar.setFormat(strings._('gui_canceled')) + self.status = HistoryItem.STATUS_CANCELED + class VisitHistoryItem(HistoryItem): """ Download history item, for share mode diff --git a/onionshare_gui/mode/share_mode/__init__.py b/onionshare_gui/mode/share_mode/__init__.py index 143fd577..dd4ec1ab 100644 --- a/onionshare_gui/mode/share_mode/__init__.py +++ b/onionshare_gui/mode/share_mode/__init__.py @@ -28,7 +28,7 @@ from onionshare.web import Web from ..file_selection import FileSelection from .threads import CompressThread from .. import Mode -from ..history import History, ToggleHistory, ShareHistoryItem +from ..history import History, ToggleHistory, ShareHistoryItem, IndividualFileHistoryItem from ...widgets import Alert @@ -230,6 +230,15 @@ class ShareMode(Mode): Handle REQUEST_LOAD event. """ self.system_tray.showMessage(strings._('systray_page_loaded_title'), strings._('systray_page_loaded_message')) + if not event["path"].startswith(('/favicon.ico', '/download', self.web.static_url_path)) and event["path"] != '/': + + item = IndividualFileHistoryItem(self.common, event["path"]) + + self.history.add(0, item) + self.toggle_history.update_indicator(True) + self.history.completed_count += 1 + self.history.update_completed() + self.system_tray.showMessage(strings._('systray_individual_file_downloaded_title'), strings._('systray_individual_file_downloaded_message').format(event["path"])) def handle_request_started(self, event): """ diff --git a/share/locale/en.json b/share/locale/en.json index 2063a415..c26577b2 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -160,6 +160,8 @@ "systray_receive_started_message": "Someone is sending files to you", "systray_website_started_title": "Starting sharing website", "systray_website_started_message": "Someone is visiting your website", + "systray_individual_file_downloaded_title": "Individual file loaded", + "systray_individual_file_downloaded_message": "Individual file {} viewed", "gui_all_modes_history": "History", "gui_all_modes_clear_history": "Clear All", "gui_all_modes_transfer_started": "Started {}", @@ -176,6 +178,7 @@ "gui_receive_mode_no_files": "No Files Received Yet", "gui_receive_mode_autostop_timer_waiting": "Waiting to finish receiving", "gui_visit_started": "Someone has visited your website {}", + "gui_individual_file_download": "Viewed {}", "receive_mode_upload_starting": "Upload of total size {} is starting", "days_first_letter": "d", "hours_first_letter": "h", From 0abac29b09092fc9f1516573f08c45edbef768c3 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 11:19:42 +1000 Subject: [PATCH 21/48] Add tests to check that hyperlink to a shared file exists when in stay_open mode (and that the file is downloadable individually when so), and not if not --- tests/GuiShareTest.py | 47 +++++++++++++++++++ ...ode_individual_file_view_stay_open_test.py | 26 ++++++++++ ...re_share_mode_individual_file_view_test.py | 26 ++++++++++ 3 files changed, 99 insertions(+) create mode 100644 tests/local_onionshare_share_mode_individual_file_view_stay_open_test.py create mode 100644 tests/local_onionshare_share_mode_individual_file_view_test.py diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index 70ae43fd..b6f50a28 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -81,6 +81,35 @@ class GuiShareTest(GuiBaseTest): QtTest.QTest.qWait(2000) self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8')) + def individual_file_is_viewable_or_not(self, public_mode, stay_open): + '''Test whether an individual file is viewable (when in stay_open mode) and that it isn't (when not in stay_open mode)''' + url = "http://127.0.0.1:{}".format(self.gui.app.port) + download_file_url = "http://127.0.0.1:{}/test.txt".format(self.gui.app.port) + if public_mode: + r = requests.get(url) + else: + r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password)) + + if stay_open: + self.assertTrue('a href="test.txt"' in r.text) + + if public_mode: + r = requests.get(download_file_url) + else: + r = requests.get(download_file_url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password)) + + tmp_file = tempfile.NamedTemporaryFile() + with open(tmp_file.name, 'wb') as f: + f.write(r.content) + + with open(tmp_file.name, 'r') as f: + self.assertEqual('onionshare', f.read()) + else: + self.assertFalse('a href="/test.txt"' in r.text) + self.download_share(public_mode) + + QtTest.QTest.qWait(2000) + def hit_401(self, public_mode): '''Test that the server stops after too many 401s, or doesn't when in public_mode''' url = "http://127.0.0.1:{}/".format(self.gui.app.port) @@ -147,6 +176,18 @@ class GuiShareTest(GuiBaseTest): self.server_is_started(self.gui.share_mode) self.history_indicator(self.gui.share_mode, public_mode) + def run_all_share_mode_individual_file_download_tests(self, public_mode, stay_open): + """Tests in share mode after downloading a share""" + self.web_page(self.gui.share_mode, 'Total size', public_mode) + self.individual_file_is_viewable_or_not(public_mode, stay_open) + self.history_widgets_present(self.gui.share_mode) + self.server_is_stopped(self.gui.share_mode, stay_open) + self.web_server_is_stopped() + self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) + self.add_button_visible() + self.server_working_on_start_button_pressed(self.gui.share_mode) + self.server_is_started(self.gui.share_mode) + self.history_indicator(self.gui.share_mode, public_mode) def run_all_share_mode_tests(self, public_mode, stay_open): """End-to-end share tests""" @@ -155,6 +196,12 @@ class GuiShareTest(GuiBaseTest): self.run_all_share_mode_download_tests(public_mode, stay_open) + def run_all_share_mode_individual_file_tests(self, public_mode, stay_open): + """Tests in share mode when viewing an individual file""" + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(public_mode) + self.run_all_share_mode_individual_file_download_tests(public_mode, stay_open) + def run_all_large_file_tests(self, public_mode, stay_open): """Same as above but with a larger file""" self.run_all_share_mode_setup_tests() diff --git a/tests/local_onionshare_share_mode_individual_file_view_stay_open_test.py b/tests/local_onionshare_share_mode_individual_file_view_stay_open_test.py new file mode 100644 index 00000000..4e026e16 --- /dev/null +++ b/tests/local_onionshare_share_mode_individual_file_view_stay_open_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeIndividualFileViewStayOpenTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": False, + } + cls.gui = GuiShareTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + @pytest.mark.gui + @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_share_mode_individual_file_tests(False, True) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_individual_file_view_test.py b/tests/local_onionshare_share_mode_individual_file_view_test.py new file mode 100644 index 00000000..2bdccaec --- /dev/null +++ b/tests/local_onionshare_share_mode_individual_file_view_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeIndividualFileViewTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": True, + } + cls.gui = GuiShareTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + @pytest.mark.gui + @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_share_mode_individual_file_tests(False, False) + +if __name__ == "__main__": + unittest.main() From 93a63098dea5c1aeb45d174da91e18b75cfd9e37 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 11:51:59 +1000 Subject: [PATCH 22/48] Add a basic website test --- tests/GuiBaseTest.py | 4 + tests/GuiWebsiteTest.py | 98 +++++++++++++++++++++ tests/local_onionshare_website_mode_test.py | 25 ++++++ 3 files changed, 127 insertions(+) create mode 100644 tests/GuiWebsiteTest.py create mode 100644 tests/local_onionshare_website_mode_test.py diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index 2f340396..d2a43362 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -14,6 +14,7 @@ from onionshare.web import Web from onionshare_gui import Application, OnionShare, OnionShareGui from onionshare_gui.mode.share_mode import ShareMode from onionshare_gui.mode.receive_mode import ReceiveMode +from onionshare_gui.mode.website_mode import WebsiteMode class GuiBaseTest(object): @@ -103,6 +104,9 @@ class GuiBaseTest(object): if type(mode) == ShareMode: QtTest.QTest.mouseClick(self.gui.share_mode_button, QtCore.Qt.LeftButton) self.assertTrue(self.gui.mode, self.gui.MODE_SHARE) + if type(mode) == WebsiteMode: + QtTest.QTest.mouseClick(self.gui.website_mode_button, QtCore.Qt.LeftButton) + self.assertTrue(self.gui.mode, self.gui.MODE_WEBSITE) def click_toggle_history(self, mode): diff --git a/tests/GuiWebsiteTest.py b/tests/GuiWebsiteTest.py new file mode 100644 index 00000000..1f0eb310 --- /dev/null +++ b/tests/GuiWebsiteTest.py @@ -0,0 +1,98 @@ +import json +import os +import requests +import socks +import zipfile +import tempfile +from PyQt5 import QtCore, QtTest +from onionshare import strings +from onionshare.common import Common +from onionshare.settings import Settings +from onionshare.onion import Onion +from onionshare.web import Web +from onionshare_gui import Application, OnionShare, OnionShareGui +from onionshare_gui.mode.website_mode import WebsiteMode +from .GuiShareTest import GuiShareTest + +class GuiWebsiteTest(GuiShareTest): + @staticmethod + def set_up(test_settings): + '''Create GUI with given settings''' + # Create our test file + testfile = open('/tmp/index.html', 'w') + testfile.write('This is a test website hosted by OnionShare') + testfile.close() + + common = Common() + common.settings = Settings(common) + common.define_css() + strings.load_strings(common) + + # Get all of the settings in test_settings + test_settings['data_dir'] = '/tmp/OnionShare' + for key, val in common.settings.default_settings.items(): + if key not in test_settings: + test_settings[key] = val + + # Start the Onion + testonion = Onion(common) + global qtapp + qtapp = Application(common) + app = OnionShare(common, testonion, True, 0) + + web = Web(common, False, True) + open('/tmp/settings.json', 'w').write(json.dumps(test_settings)) + + gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/index.html'], '/tmp/settings.json', True) + return gui + + @staticmethod + def tear_down(): + '''Clean up after tests''' + try: + os.remove('/tmp/index.html') + os.remove('/tmp/settings.json') + except: + pass + + + def view_website(self, public_mode): + '''Test that we can download the share''' + url = "http://127.0.0.1:{}/".format(self.gui.app.port) + if public_mode: + r = requests.get(url) + else: + r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.website_mode.server_status.web.password)) + + QtTest.QTest.qWait(2000) + self.assertTrue('This is a test website hosted by OnionShare' in r.text) + + def run_all_website_mode_setup_tests(self): + """Tests in website mode prior to starting a share""" + self.click_mode(self.gui.website_mode) + self.file_selection_widget_has_files(1) + self.history_is_not_visible(self.gui.website_mode) + self.click_toggle_history(self.gui.website_mode) + self.history_is_visible(self.gui.website_mode) + + def run_all_website_mode_started_tests(self, public_mode, startup_time=2000): + """Tests in website mode after starting a share""" + self.server_working_on_start_button_pressed(self.gui.website_mode) + self.server_status_indicator_says_starting(self.gui.website_mode) + self.add_delete_buttons_hidden() + self.settings_button_is_hidden() + self.server_is_started(self.gui.website_mode, startup_time) + self.web_server_is_running() + self.have_a_password(self.gui.website_mode, public_mode) + self.url_description_shown(self.gui.website_mode) + self.have_copy_url_button(self.gui.website_mode, public_mode) + self.server_status_indicator_says_started(self.gui.website_mode) + + + def run_all_website_mode_download_tests(self, public_mode, stay_open): + """Tests in website mode after viewing the site""" + self.run_all_website_mode_setup_tests() + self.run_all_website_mode_started_tests(public_mode, startup_time=2000) + self.view_website(public_mode) + self.history_widgets_present(self.gui.website_mode) + diff --git a/tests/local_onionshare_website_mode_test.py b/tests/local_onionshare_website_mode_test.py new file mode 100644 index 00000000..5a6dc10f --- /dev/null +++ b/tests/local_onionshare_website_mode_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiWebsiteTest import GuiWebsiteTest + +class LocalWebsiteModeTest(unittest.TestCase, GuiWebsiteTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = GuiWebsiteTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiWebsiteTest.tear_down() + + @pytest.mark.gui + @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") + def test_gui(self): + #self.run_all_common_setup_tests() + self.run_all_website_mode_download_tests(False, False) + +if __name__ == "__main__": + unittest.main() From 6da58edcda06cb4a612ea009184cdacf8ee84e0c Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 11:53:17 +1000 Subject: [PATCH 23/48] remove unnecessary import from GuiWebSiteTest class --- tests/GuiWebsiteTest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/GuiWebsiteTest.py b/tests/GuiWebsiteTest.py index 1f0eb310..e3d63547 100644 --- a/tests/GuiWebsiteTest.py +++ b/tests/GuiWebsiteTest.py @@ -11,7 +11,6 @@ from onionshare.settings import Settings from onionshare.onion import Onion from onionshare.web import Web from onionshare_gui import Application, OnionShare, OnionShareGui -from onionshare_gui.mode.website_mode import WebsiteMode from .GuiShareTest import GuiShareTest class GuiWebsiteTest(GuiShareTest): From f4a6c2de010047ab2ffffbeb216e9ded5b6447e0 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 12:00:23 +1000 Subject: [PATCH 24/48] Aww. Adjust the website test html code since my easter egg didn't work --- tests/GuiWebsiteTest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/GuiWebsiteTest.py b/tests/GuiWebsiteTest.py index e3d63547..6697c5b9 100644 --- a/tests/GuiWebsiteTest.py +++ b/tests/GuiWebsiteTest.py @@ -19,7 +19,7 @@ class GuiWebsiteTest(GuiShareTest): '''Create GUI with given settings''' # Create our test file testfile = open('/tmp/index.html', 'w') - testfile.write('This is a test website hosted by OnionShare') + testfile.write('

This is a test website hosted by OnionShare

') testfile.close() common = Common() @@ -64,7 +64,7 @@ class GuiWebsiteTest(GuiShareTest): r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.website_mode.server_status.web.password)) QtTest.QTest.qWait(2000) - self.assertTrue('This is a test website hosted by OnionShare' in r.text) + self.assertTrue('This is a test website hosted by OnionShare' in r.text) def run_all_website_mode_setup_tests(self): """Tests in website mode prior to starting a share""" From 608e0eccc6082145eae13843bba2943b5369b7a8 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 12:23:27 +1000 Subject: [PATCH 25/48] Extend coverage of website mode tests --- tests/GuiBaseTest.py | 5 ++++- tests/GuiShareTest.py | 9 ++------- tests/GuiWebsiteTest.py | 7 +++++-- tests/TorGuiShareTest.py | 2 +- tests/local_onionshare_website_mode_test.py | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index d2a43362..f478dd94 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -202,6 +202,9 @@ class GuiBaseTest(object): else: self.assertIsNone(mode.server_status.web.password, r'(\w+)-(\w+)') + def add_button_visible(self, mode): + '''Test that the add button should be visible''' + self.assertTrue(mode.server_status.file_selection.add_button.isVisible()) def url_description_shown(self, mode): '''Test that the URL label is showing''' @@ -253,7 +256,7 @@ class GuiBaseTest(object): def server_is_stopped(self, mode, stay_open): '''Test that the server stops when we click Stop''' - if type(mode) == ReceiveMode or (type(mode) == ShareMode and stay_open): + if type(mode) == ReceiveMode or (type(mode) == ShareMode and stay_open) or (type(mode) == WebsiteMode): QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton) self.assertEqual(mode.server_status.status, 0) diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index b6f50a28..34573a19 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -130,11 +130,6 @@ class GuiShareTest(GuiBaseTest): self.web_server_is_stopped() - def add_button_visible(self): - '''Test that the add button should be visible''' - self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) - - # 'Grouped' tests follow from here def run_all_share_mode_setup_tests(self): @@ -171,7 +166,7 @@ class GuiShareTest(GuiBaseTest): self.server_is_stopped(self.gui.share_mode, stay_open) self.web_server_is_stopped() self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) - self.add_button_visible() + self.add_button_visible(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_is_started(self.gui.share_mode) self.history_indicator(self.gui.share_mode, public_mode) @@ -184,7 +179,7 @@ class GuiShareTest(GuiBaseTest): self.server_is_stopped(self.gui.share_mode, stay_open) self.web_server_is_stopped() self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) - self.add_button_visible() + self.add_button_visible(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_is_started(self.gui.share_mode) self.history_indicator(self.gui.share_mode, public_mode) diff --git a/tests/GuiWebsiteTest.py b/tests/GuiWebsiteTest.py index 6697c5b9..7b88bfdf 100644 --- a/tests/GuiWebsiteTest.py +++ b/tests/GuiWebsiteTest.py @@ -54,7 +54,6 @@ class GuiWebsiteTest(GuiShareTest): except: pass - def view_website(self, public_mode): '''Test that we can download the share''' url = "http://127.0.0.1:{}/".format(self.gui.app.port) @@ -88,10 +87,14 @@ class GuiWebsiteTest(GuiShareTest): self.server_status_indicator_says_started(self.gui.website_mode) - def run_all_website_mode_download_tests(self, public_mode, stay_open): + def run_all_website_mode_download_tests(self, public_mode): """Tests in website mode after viewing the site""" self.run_all_website_mode_setup_tests() self.run_all_website_mode_started_tests(public_mode, startup_time=2000) self.view_website(public_mode) self.history_widgets_present(self.gui.website_mode) + self.server_is_stopped(self.gui.website_mode, False) + self.web_server_is_stopped() + self.server_status_indicator_says_closed(self.gui.website_mode, False) + self.add_button_visible(self.gui.website_mode) diff --git a/tests/TorGuiShareTest.py b/tests/TorGuiShareTest.py index 352707eb..cfce9d4e 100644 --- a/tests/TorGuiShareTest.py +++ b/tests/TorGuiShareTest.py @@ -67,7 +67,7 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest): self.server_is_stopped(self.gui.share_mode, stay_open) self.web_server_is_stopped() self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) - self.add_button_visible() + self.add_button_visible(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_is_started(self.gui.share_mode, startup_time=45000) self.history_indicator(self.gui.share_mode, public_mode) diff --git a/tests/local_onionshare_website_mode_test.py b/tests/local_onionshare_website_mode_test.py index 5a6dc10f..051adb3c 100644 --- a/tests/local_onionshare_website_mode_test.py +++ b/tests/local_onionshare_website_mode_test.py @@ -19,7 +19,7 @@ class LocalWebsiteModeTest(unittest.TestCase, GuiWebsiteTest): @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") def test_gui(self): #self.run_all_common_setup_tests() - self.run_all_website_mode_download_tests(False, False) + self.run_all_website_mode_download_tests(False) if __name__ == "__main__": unittest.main() From 0bde2e91482cda84539080016b13edc57552ce1c Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 12:35:30 +1000 Subject: [PATCH 26/48] Don't show the IndividualFile History item if we are not in 'stay open' mode, or else 404 requests create History noise --- onionshare_gui/mode/share_mode/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onionshare_gui/mode/share_mode/__init__.py b/onionshare_gui/mode/share_mode/__init__.py index dd4ec1ab..a9752174 100644 --- a/onionshare_gui/mode/share_mode/__init__.py +++ b/onionshare_gui/mode/share_mode/__init__.py @@ -230,7 +230,7 @@ class ShareMode(Mode): Handle REQUEST_LOAD event. """ self.system_tray.showMessage(strings._('systray_page_loaded_title'), strings._('systray_page_loaded_message')) - if not event["path"].startswith(('/favicon.ico', '/download', self.web.static_url_path)) and event["path"] != '/': + if not self.common.settings.get('close_after_first_download') and not event["path"].startswith(('/favicon.ico', '/download', self.web.static_url_path)) and event["path"] != '/': item = IndividualFileHistoryItem(self.common, event["path"]) From 174dc79a2560e37f8b55c6316e73f31373d96f8a Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 12:36:05 +1000 Subject: [PATCH 27/48] Test to make sure that we *can't* download an individual file when not in stay_open mode, not just that the hyperlink is not present in the page markup --- tests/GuiShareTest.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index 34573a19..be3f42e3 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -105,6 +105,11 @@ class GuiShareTest(GuiBaseTest): with open(tmp_file.name, 'r') as f: self.assertEqual('onionshare', f.read()) else: + if public_mode: + r = requests.get(download_file_url) + else: + r = requests.get(download_file_url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password)) + self.assertEqual(r.status_code, 404) self.assertFalse('a href="/test.txt"' in r.text) self.download_share(public_mode) From 04eabbb833a884c86cddd5cfce2d805797150cdb Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 12:38:20 +1000 Subject: [PATCH 28/48] Check for the (absence of) hyperlink in page markup before we move on to trying to download the individual file --- tests/GuiShareTest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index be3f42e3..f8fefe60 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -105,12 +105,12 @@ class GuiShareTest(GuiBaseTest): with open(tmp_file.name, 'r') as f: self.assertEqual('onionshare', f.read()) else: + self.assertFalse('a href="/test.txt"' in r.text) if public_mode: r = requests.get(download_file_url) else: r = requests.get(download_file_url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password)) self.assertEqual(r.status_code, 404) - self.assertFalse('a href="/test.txt"' in r.text) self.download_share(public_mode) QtTest.QTest.qWait(2000) From c86ad56a5605daa2082f66830147889b86d5fb0f Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Mon, 2 Sep 2019 19:45:14 -0700 Subject: [PATCH 29/48] When downloading individual files in either share or website mode, gzip the file if needed, and stream the file in such a way that a progress bar is possible --- onionshare/web/send_base_mode.py | 112 +++++++++++++++++++++++++++++++ onionshare/web/share_mode.py | 36 ++-------- onionshare/web/website_mode.py | 17 ++--- 3 files changed, 121 insertions(+), 44 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 6468258a..88dbd008 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -2,6 +2,7 @@ import os import sys import tempfile import mimetypes +import gzip from flask import Response, request, render_template, make_response from .. import strings @@ -148,3 +149,114 @@ class SendBaseModeWeb: Inherited class will implement this. """ pass + + def stream_individual_file(self, filesystem_path): + """ + Return a flask response that's streaming the download of an individual file, and gzip + compressing it if the browser supports it. + """ + use_gzip = self.should_use_gzip() + + # gzip compress the individual file, if it hasn't already been compressed + if use_gzip: + if filesystem_path not in self.gzip_individual_files: + gzip_filename = tempfile.mkstemp('wb+')[1] + self._gzip_compress(filesystem_path, gzip_filename, 6, None) + self.gzip_individual_files[filesystem_path] = gzip_filename + + # Make sure the gzip file gets cleaned up when onionshare stops + self.cleanup_filenames.append(gzip_filename) + + file_to_download = self.gzip_individual_files[filesystem_path] + filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) + else: + file_to_download = filesystem_path + filesize = os.path.getsize(filesystem_path) + + # TODO: Tell GUI the download started + #self.web.add_request(self.web.REQUEST_STARTED, path, { + # 'id': download_id, + # 'use_gzip': use_gzip + #}) + + def generate(): + chunk_size = 102400 # 100kb + + fp = open(file_to_download, 'rb') + done = False + canceled = False + while not done: + chunk = fp.read(chunk_size) + if chunk == b'': + done = True + else: + try: + yield chunk + + # TODO: Tell GUI the progress + downloaded_bytes = fp.tell() + percent = (1.0 * downloaded_bytes / filesize) * 100 + if not self.web.is_gui or self.common.platform == 'Linux' or self.common.platform == 'BSD': + sys.stdout.write( + "\r{0:s}, {1:.2f}% ".format(self.common.human_readable_filesize(downloaded_bytes), percent)) + sys.stdout.flush() + + #self.web.add_request(self.web.REQUEST_PROGRESS, path, { + # 'id': download_id, + # 'bytes': downloaded_bytes + # }) + done = False + except: + # Looks like the download was canceled + done = True + canceled = True + + # TODO: Tell the GUI the download has canceled + #self.web.add_request(self.web.REQUEST_CANCELED, path, { + # 'id': download_id + #}) + + fp.close() + + if self.common.platform != 'Darwin': + sys.stdout.write("\n") + + basename = os.path.basename(filesystem_path) + + r = Response(generate()) + if use_gzip: + r.headers.set('Content-Encoding', 'gzip') + r.headers.set('Content-Length', filesize) + r.headers.set('Content-Disposition', 'inline', filename=basename) + r = self.web.add_security_headers(r) + (content_type, _) = mimetypes.guess_type(basename, strict=False) + if content_type is not None: + r.headers.set('Content-Type', content_type) + return r + + def should_use_gzip(self): + """ + Should we use gzip for this browser? + """ + return (not self.is_zipped) and ('gzip' in request.headers.get('Accept-Encoding', '').lower()) + + def _gzip_compress(self, input_filename, output_filename, level, processed_size_callback=None): + """ + Compress a file with gzip, without loading the whole thing into memory + Thanks: https://stackoverflow.com/questions/27035296/python-how-to-gzip-a-large-text-file-without-memoryerror + """ + bytes_processed = 0 + blocksize = 1 << 16 # 64kB + with open(input_filename, 'rb') as input_file: + output_file = gzip.open(output_filename, 'wb', level) + while True: + if processed_size_callback is not None: + processed_size_callback(bytes_processed) + + block = input_file.read(blocksize) + if len(block) == 0: + break + output_file.write(block) + bytes_processed += blocksize + + output_file.close() diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index b478fbd4..07cf0548 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -3,8 +3,7 @@ import sys import tempfile import zipfile import mimetypes -import gzip -from flask import Response, request, render_template, make_response, send_from_directory +from flask import Response, request, render_template, make_response from .send_base_mode import SendBaseModeWeb from .. import strings @@ -16,8 +15,10 @@ class ShareModeWeb(SendBaseModeWeb): """ def init(self): self.common.log('ShareModeWeb', 'init') + # Allow downloading individual files if "Stop sharing after files have been sent" is unchecked self.download_individual_files = not self.common.settings.get('close_after_first_download') + self.gzip_individual_files = {} def define_routes(self): """ @@ -207,9 +208,7 @@ class ShareModeWeb(SendBaseModeWeb): # If it's a file elif os.path.isfile(filesystem_path): if self.download_individual_files: - dirname = os.path.dirname(filesystem_path) - basename = os.path.basename(filesystem_path) - return send_from_directory(dirname, basename) + return self.stream_individual_file(filesystem_path) else: return self.web.error404() @@ -287,33 +286,6 @@ class ShareModeWeb(SendBaseModeWeb): return True - def should_use_gzip(self): - """ - Should we use gzip for this browser? - """ - return (not self.is_zipped) and ('gzip' in request.headers.get('Accept-Encoding', '').lower()) - - def _gzip_compress(self, input_filename, output_filename, level, processed_size_callback=None): - """ - Compress a file with gzip, without loading the whole thing into memory - Thanks: https://stackoverflow.com/questions/27035296/python-how-to-gzip-a-large-text-file-without-memoryerror - """ - bytes_processed = 0 - blocksize = 1 << 16 # 64kB - with open(input_filename, 'rb') as input_file: - output_file = gzip.open(output_filename, 'wb', level) - while True: - if processed_size_callback is not None: - processed_size_callback(bytes_processed) - - block = input_file.read(blocksize) - if len(block) == 0: - break - output_file.write(block) - bytes_processed += blocksize - - output_file.close() - class ZipWriter(object): """ diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 82cebdb7..e409e7be 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -2,7 +2,7 @@ import os import sys import tempfile import mimetypes -from flask import Response, request, render_template, make_response, send_from_directory +from flask import Response, request, render_template, make_response from .send_base_mode import SendBaseModeWeb from .. import strings @@ -13,7 +13,7 @@ class WebsiteModeWeb(SendBaseModeWeb): All of the web logic for website mode """ def init(self): - pass + self.gzip_individual_files = {} def define_routes(self): """ @@ -62,10 +62,7 @@ class WebsiteModeWeb(SendBaseModeWeb): index_path = os.path.join(path, 'index.html') if index_path in self.files: # Render it - dirname = os.path.dirname(self.files[index_path]) - basename = os.path.basename(self.files[index_path]) - - return send_from_directory(dirname, basename) + return self.stream_individual_file(filesystem_path) else: # Otherwise, render directory listing @@ -80,9 +77,7 @@ class WebsiteModeWeb(SendBaseModeWeb): # If it's a file elif os.path.isfile(filesystem_path): - dirname = os.path.dirname(filesystem_path) - basename = os.path.basename(filesystem_path) - return send_from_directory(dirname, basename) + return self.stream_individual_file(filesystem_path) # If it's not a directory or file, throw a 404 else: @@ -94,9 +89,7 @@ class WebsiteModeWeb(SendBaseModeWeb): index_path = 'index.html' if index_path in self.files: # Render it - dirname = os.path.dirname(self.files[index_path]) - basename = os.path.basename(self.files[index_path]) - return send_from_directory(dirname, basename) + return self.stream_individual_file(self.files[index_path]) else: # Root directory listing filenames = list(self.root_files) From 7a6d34103d0388399c1a90e5a1aa884497e2024b Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Tue, 3 Sep 2019 17:02:00 +1000 Subject: [PATCH 30/48] Reset the ToggleHistory indicator count/label when a share starts. Add a test for this --- onionshare_gui/mode/receive_mode/__init__.py | 2 ++ onionshare_gui/mode/share_mode/__init__.py | 2 ++ onionshare_gui/mode/website_mode/__init__.py | 2 ++ tests/GuiBaseTest.py | 3 +++ tests/GuiShareTest.py | 1 + 5 files changed, 10 insertions(+) diff --git a/onionshare_gui/mode/receive_mode/__init__.py b/onionshare_gui/mode/receive_mode/__init__.py index dbc0bc73..0010fbd2 100644 --- a/onionshare_gui/mode/receive_mode/__init__.py +++ b/onionshare_gui/mode/receive_mode/__init__.py @@ -212,6 +212,8 @@ class ReceiveMode(Mode): Set the info counters back to zero. """ self.history.reset() + self.toggle_history.indicator_count = 0 + self.toggle_history.update_indicator() def update_primary_action(self): self.common.log('ReceiveMode', 'update_primary_action') diff --git a/onionshare_gui/mode/share_mode/__init__.py b/onionshare_gui/mode/share_mode/__init__.py index a9752174..56aa1364 100644 --- a/onionshare_gui/mode/share_mode/__init__.py +++ b/onionshare_gui/mode/share_mode/__init__.py @@ -334,6 +334,8 @@ class ShareMode(Mode): Set the info counters back to zero. """ self.history.reset() + self.toggle_history.indicator_count = 0 + self.toggle_history.update_indicator() @staticmethod def _compute_total_size(filenames): diff --git a/onionshare_gui/mode/website_mode/__init__.py b/onionshare_gui/mode/website_mode/__init__.py index 9f01cabc..8ac88c8c 100644 --- a/onionshare_gui/mode/website_mode/__init__.py +++ b/onionshare_gui/mode/website_mode/__init__.py @@ -258,6 +258,8 @@ class WebsiteMode(Mode): Set the info counters back to zero. """ self.history.reset() + self.toggle_history.indicator_count = 0 + self.toggle_history.update_indicator() @staticmethod def _compute_total_size(filenames): diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index f478dd94..9a69619b 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -170,6 +170,9 @@ class GuiBaseTest(object): QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton) self.assertEqual(mode.server_status.status, 1) + def toggle_indicator_is_reset(self, mode): + self.assertEqual(mode.toggle_history.indicator_count, 0) + self.assertFalse(mode.toggle_history.indicator_label.isVisible()) def server_status_indicator_says_starting(self, mode): '''Test that the Server Status indicator shows we are Starting''' diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index f8fefe60..038f052b 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -173,6 +173,7 @@ class GuiShareTest(GuiBaseTest): self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) self.add_button_visible(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode) + self.toggle_indicator_is_reset(self.gui.share_mode) self.server_is_started(self.gui.share_mode) self.history_indicator(self.gui.share_mode, public_mode) From 727a3956dbdeabab22c04f5133e0930655753ffb Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 3 Sep 2019 20:52:49 -0700 Subject: [PATCH 31/48] Make it so all of the state variables, including self.file_info get reset in SendBaseModeWEeb.set_file_info, which fixes the bug where old files you were sharing would end up in new zip files --- onionshare/web/send_base_mode.py | 154 +++++++++++++++---------------- onionshare/web/share_mode.py | 1 - onionshare/web/website_mode.py | 2 +- 3 files changed, 73 insertions(+), 84 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 88dbd008..a34aedfd 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -18,7 +18,6 @@ class SendBaseModeWeb: self.web = web # Information about the file to be shared - self.file_info = [] self.is_zipped = False self.download_filename = None self.download_filesize = None @@ -26,17 +25,6 @@ class SendBaseModeWeb: self.gzip_filesize = None self.zip_writer = None - # Dictionary mapping file paths to filenames on disk - self.files = {} - # This is only the root files and dirs, as opposed to all of them - self.root_files = {} - - self.cleanup_filenames = [] - self.file_info = {'files': [], 'dirs': []} - - self.visit_count = 0 - self.download_count = 0 - # If "Stop After First Download" is checked (stay_open == False), only allow # one download at a time. self.download_in_progress = False @@ -44,24 +32,51 @@ class SendBaseModeWeb: self.define_routes() self.init() - def init(self): + def set_file_info(self, filenames, processed_size_callback=None): """ - Inherited class will implement this + Build a data structure that describes the list of files """ - pass + # If there's just one folder, replace filenames with a list of files inside that folder + if len(filenames) == 1 and os.path.isdir(filenames[0]): + filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] - def define_routes(self): - """ - Inherited class will implement this - """ - pass + # Re-initialize + self.files = {} # Dictionary mapping file paths to filenames on disk + self.root_files = {} # This is only the root files and dirs, as opposed to all of them + self.cleanup_filenames = [] + self.visit_count = 0 + self.download_count = 0 + self.file_info = {'files': [], 'dirs': []} + self.gzip_individual_files = {} + self.init() - def directory_listing_template(self): - """ - Inherited class will implement this. It should call render_template and return - the response. - """ - pass + # Build the file list + for filename in filenames: + basename = os.path.basename(filename.rstrip('/')) + + # If it's a filename, add it + if os.path.isfile(filename): + self.files[basename] = filename + self.root_files[basename] = filename + + # If it's a directory, add it recursively + elif os.path.isdir(filename): + self.root_files[basename + '/'] = filename + + for root, _, nested_filenames in os.walk(filename): + # Normalize the root path. So if the directory name is "/home/user/Documents/some_folder", + # and it has a nested folder foobar, the root is "/home/user/Documents/some_folder/foobar". + # The normalized_root should be "some_folder/foobar" + normalized_root = os.path.join(basename, root[len(filename):].lstrip('/')).rstrip('/') + + # Add the dir itself + self.files[normalized_root + '/'] = root + + # Add the files in this dir + for nested_filename in nested_filenames: + self.files[os.path.join(normalized_root, nested_filename)] = os.path.join(root, nested_filename) + + self.set_file_info_custom(filenames, processed_size_callback) def directory_listing(self, filenames, path='', filesystem_path=None): # If filesystem_path is None, this is the root directory listing @@ -94,62 +109,6 @@ class SendBaseModeWeb: }) return files, dirs - def set_file_info_custom(self, filenames, processed_size_callback): - """ - Inherited class will implement this. - """ - pass - - def set_file_info(self, filenames, processed_size_callback=None): - """ - Build a data structure that describes the list of files - """ - - # If there's just one folder, replace filenames with a list of files inside that folder - if len(filenames) == 1 and os.path.isdir(filenames[0]): - filenames = [os.path.join(filenames[0], x) for x in os.listdir(filenames[0])] - - # Re-initialize - self.init() - - # Clear the list of files - self.files = {} - self.root_files = {} - - # Build the file list - for filename in filenames: - basename = os.path.basename(filename.rstrip('/')) - - # If it's a filename, add it - if os.path.isfile(filename): - self.files[basename] = filename - self.root_files[basename] = filename - - # If it's a directory, add it recursively - elif os.path.isdir(filename): - self.root_files[basename + '/'] = filename - - for root, _, nested_filenames in os.walk(filename): - # Normalize the root path. So if the directory name is "/home/user/Documents/some_folder", - # and it has a nested folder foobar, the root is "/home/user/Documents/some_folder/foobar". - # The normalized_root should be "some_folder/foobar" - normalized_root = os.path.join(basename, root[len(filename):].lstrip('/')).rstrip('/') - - # Add the dir itself - self.files[normalized_root + '/'] = root - - # Add the files in this dir - for nested_filename in nested_filenames: - self.files[os.path.join(normalized_root, nested_filename)] = os.path.join(root, nested_filename) - - self.set_file_info_custom(filenames, processed_size_callback) - - def render_logic(self, path=''): - """ - Inherited class will implement this. - """ - pass - def stream_individual_file(self, filesystem_path): """ Return a flask response that's streaming the download of an individual file, and gzip @@ -260,3 +219,34 @@ class SendBaseModeWeb: bytes_processed += blocksize output_file.close() + + def init(self): + """ + Inherited class will implement this + """ + pass + + def define_routes(self): + """ + Inherited class will implement this + """ + pass + + def directory_listing_template(self): + """ + Inherited class will implement this. It should call render_template and return + the response. + """ + pass + + def set_file_info_custom(self, filenames, processed_size_callback): + """ + Inherited class will implement this. + """ + pass + + def render_logic(self, path=''): + """ + Inherited class will implement this. + """ + pass \ No newline at end of file diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 07cf0548..60620e2a 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -18,7 +18,6 @@ class ShareModeWeb(SendBaseModeWeb): # Allow downloading individual files if "Stop sharing after files have been sent" is unchecked self.download_individual_files = not self.common.settings.get('close_after_first_download') - self.gzip_individual_files = {} def define_routes(self): """ diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index e409e7be..28f2607d 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -13,7 +13,7 @@ class WebsiteModeWeb(SendBaseModeWeb): All of the web logic for website mode """ def init(self): - self.gzip_individual_files = {} + pass def define_routes(self): """ From 37a2f6369c02530a7edcb1a4a11d884d761945cf Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 3 Sep 2019 21:46:32 -0700 Subject: [PATCH 32/48] Start making IndividualFileHistoryItem widgets appear in the history, and make non-GET requests return 405 Method Not Allowed --- onionshare/web/send_base_mode.py | 41 +++++--- onionshare/web/web.py | 32 +++--- onionshare_gui/mode/__init__.py | 47 ++++++++- onionshare_gui/mode/history.py | 103 ++++++++++++------- onionshare_gui/mode/share_mode/__init__.py | 15 --- onionshare_gui/mode/website_mode/__init__.py | 17 +-- onionshare_gui/onionshare_gui.py | 11 +- share/locale/en.json | 1 - share/templates/405.html | 19 ++++ 9 files changed, 185 insertions(+), 101 deletions(-) create mode 100644 share/templates/405.html diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index a34aedfd..402bc32f 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -132,18 +132,28 @@ class SendBaseModeWeb: file_to_download = filesystem_path filesize = os.path.getsize(filesystem_path) - # TODO: Tell GUI the download started - #self.web.add_request(self.web.REQUEST_STARTED, path, { - # 'id': download_id, - # 'use_gzip': use_gzip - #}) + # Each download has a unique id + download_id = self.download_count + self.download_count += 1 + + path = request.path + + # Tell GUI the individual file started + self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, path, { + 'id': download_id, + 'filesize': filesize, + 'method': request.method + }) + + # Only GET requests are allowed, any other method should fail + if request.method != "GET": + return self.web.error405() def generate(): chunk_size = 102400 # 100kb fp = open(file_to_download, 'rb') done = False - canceled = False while not done: chunk = fp.read(chunk_size) if chunk == b'': @@ -152,7 +162,7 @@ class SendBaseModeWeb: try: yield chunk - # TODO: Tell GUI the progress + # Tell GUI the progress downloaded_bytes = fp.tell() percent = (1.0 * downloaded_bytes / filesize) * 100 if not self.web.is_gui or self.common.platform == 'Linux' or self.common.platform == 'BSD': @@ -160,20 +170,19 @@ class SendBaseModeWeb: "\r{0:s}, {1:.2f}% ".format(self.common.human_readable_filesize(downloaded_bytes), percent)) sys.stdout.flush() - #self.web.add_request(self.web.REQUEST_PROGRESS, path, { - # 'id': download_id, - # 'bytes': downloaded_bytes - # }) + self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_PROGRESS, path, { + 'id': download_id, + 'bytes': downloaded_bytes + }) done = False except: # Looks like the download was canceled done = True - canceled = True - # TODO: Tell the GUI the download has canceled - #self.web.add_request(self.web.REQUEST_CANCELED, path, { - # 'id': download_id - #}) + # Tell the GUI the individual file was canceled + self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_CANCELED, path, { + 'id': download_id + }) fp.close() diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 8d5a6af5..5a96b324 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -37,15 +37,18 @@ class Web: REQUEST_LOAD = 0 REQUEST_STARTED = 1 REQUEST_PROGRESS = 2 - REQUEST_OTHER = 3 - REQUEST_CANCELED = 4 - REQUEST_RATE_LIMIT = 5 - REQUEST_UPLOAD_FILE_RENAMED = 6 - REQUEST_UPLOAD_SET_DIR = 7 - REQUEST_UPLOAD_FINISHED = 8 - REQUEST_UPLOAD_CANCELED = 9 - REQUEST_ERROR_DATA_DIR_CANNOT_CREATE = 10 - REQUEST_INVALID_PASSWORD = 11 + REQUEST_CANCELED = 3 + REQUEST_RATE_LIMIT = 4 + REQUEST_UPLOAD_FILE_RENAMED = 5 + REQUEST_UPLOAD_SET_DIR = 6 + REQUEST_UPLOAD_FINISHED = 7 + REQUEST_UPLOAD_CANCELED = 8 + REQUEST_INDIVIDUAL_FILE_STARTED = 9 + REQUEST_INDIVIDUAL_FILE_PROGRESS = 10 + REQUEST_INDIVIDUAL_FILE_CANCELED = 11 + REQUEST_ERROR_DATA_DIR_CANNOT_CREATE = 12 + REQUEST_OTHER = 13 + REQUEST_INVALID_PASSWORD = 14 def __init__(self, common, is_gui, mode='share'): self.common = common @@ -193,15 +196,18 @@ class Web: r = make_response(render_template('401.html', static_url_path=self.static_url_path), 401) return self.add_security_headers(r) + def error403(self): + self.add_request(Web.REQUEST_OTHER, request.path) + r = make_response(render_template('403.html', static_url_path=self.static_url_path), 403) + return self.add_security_headers(r) + def error404(self): self.add_request(Web.REQUEST_OTHER, request.path) r = make_response(render_template('404.html', static_url_path=self.static_url_path), 404) return self.add_security_headers(r) - def error403(self): - self.add_request(Web.REQUEST_OTHER, request.path) - - r = make_response(render_template('403.html', static_url_path=self.static_url_path), 403) + def error405(self): + r = make_response(render_template('405.html', static_url_path=self.static_url_path), 405) return self.add_security_headers(r) def add_security_headers(self, r): diff --git a/onionshare_gui/mode/__init__.py b/onionshare_gui/mode/__init__.py index e92e36f8..b5a95f41 100644 --- a/onionshare_gui/mode/__init__.py +++ b/onionshare_gui/mode/__init__.py @@ -22,6 +22,8 @@ from PyQt5 import QtCore, QtWidgets, QtGui from onionshare import strings from onionshare.common import AutoStopTimer +from .history import IndividualFileHistoryItem + from ..server_status import ServerStatus from ..threads import OnionThread from ..threads import AutoStartTimer @@ -29,7 +31,7 @@ from ..widgets import Alert class Mode(QtWidgets.QWidget): """ - The class that ShareMode and ReceiveMode inherit from. + The class that all modes inherit from """ start_server_finished = QtCore.pyqtSignal() stop_server_finished = QtCore.pyqtSignal() @@ -417,3 +419,46 @@ class Mode(QtWidgets.QWidget): Handle REQUEST_UPLOAD_CANCELED event. """ pass + + def handle_request_individual_file_started(self, event): + """ + Handle REQUEST_INDVIDIDUAL_FILES_STARTED event. + Used in both Share and Website modes, so implemented here. + """ + item = IndividualFileHistoryItem(self.common, event["data"], event["path"]) + self.history.add(event["data"]["id"], item) + self.toggle_history.update_indicator(True) + self.history.in_progress_count += 1 + self.history.update_in_progress() + + def handle_request_individual_file_progress(self, event): + """ + Handle REQUEST_INDVIDIDUAL_FILES_PROGRESS event. + Used in both Share and Website modes, so implemented here. + """ + self.history.update(event["data"]["id"], event["data"]["bytes"]) + + # Is the download complete? + if event["data"]["bytes"] == self.web.share_mode.filesize: + # Update completed and in progress labels + self.history.completed_count += 1 + self.history.in_progress_count -= 1 + self.history.update_completed() + self.history.update_in_progress() + + else: + if self.server_status.status == self.server_status.STATUS_STOPPED: + self.history.cancel(event["data"]["id"]) + self.history.in_progress_count = 0 + self.history.update_in_progress() + + def handle_request_individual_file_canceled(self, event): + """ + Handle REQUEST_INDVIDIDUAL_FILES_CANCELED event. + Used in both Share and Website modes, so implemented here. + """ + self.history.cancel(event["data"]["id"]) + + # Update in progress count + self.history.in_progress_count -= 1 + self.history.update_in_progress() diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index c2c696fc..a9fbbb36 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -345,61 +345,88 @@ class IndividualFileHistoryItem(HistoryItem): """ Individual file history item, for share mode viewing of individual files """ - def __init__(self, common, path): + def __init__(self, common, data, path): super(IndividualFileHistoryItem, self).__init__() self.status = HistoryItem.STATUS_STARTED self.common = common - self.visited = time.time() - self.visited_dt = datetime.fromtimestamp(self.visited) + self.id = id + self.path = path + self.method = data['method'] + self.total_bytes = data['filesize'] + self.downloaded_bytes = 0 + self.started = time.time() + self.started_dt = datetime.fromtimestamp(self.started) + self.status = HistoryItem.STATUS_STARTED # Labels - self.timestamp_label = QtWidgets.QLabel(self.visited_dt.strftime("%b %d, %I:%M%p")) - self.path_viewed_label = QtWidgets.QLabel(strings._('gui_individual_file_download').format(path)) + self.timestamp_label = QtWidgets.QLabel(self.started_dt.strftime("%b %d, %I:%M%p")) + self.method_label = QtWidgets.QLabel("{} {}".format(self.method, self.path)) + self.status_label = QtWidgets.QLabel() + + # Progress bar + self.progress_bar = QtWidgets.QProgressBar() + self.progress_bar.setTextVisible(True) + self.progress_bar.setAttribute(QtCore.Qt.WA_DeleteOnClose) + self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter) + self.progress_bar.setMinimum(0) + self.progress_bar.setMaximum(data['filesize']) + self.progress_bar.setValue(0) + self.progress_bar.setStyleSheet(self.common.css['downloads_uploads_progress_bar']) + self.progress_bar.total_bytes = data['filesize'] + + # Text layout + labels_layout = QtWidgets.QHBoxLayout() + labels_layout.addWidget(self.timestamp_label) + labels_layout.addWidget(self.method_label) + labels_layout.addWidget(self.status_label) # Layout layout = QtWidgets.QVBoxLayout() - layout.addWidget(self.timestamp_label) - layout.addWidget(self.path_viewed_label) + layout.addLayout(labels_layout) + layout.addWidget(self.progress_bar) self.setLayout(layout) + # All non-GET requests are error 405 Method Not Allowed + if self.method.lower() != 'get': + self.status_label.setText("405") + self.progress_bar.hide() + else: + # Start at 0 + self.update(0) - def update(self): - self.label.setText(self.get_finished_label_text(self.started_dt)) - self.status = HistoryItem.STATUS_FINISHED + def update(self, downloaded_bytes): + self.downloaded_bytes = downloaded_bytes + + self.progress_bar.setValue(downloaded_bytes) + if downloaded_bytes == self.progress_bar.total_bytes: + self.progress_bar.hide() + self.status = HistoryItem.STATUS_FINISHED + + else: + elapsed = time.time() - self.started + if elapsed < 10: + # Wait a couple of seconds for the download rate to stabilize. + # This prevents a "Windows copy dialog"-esque experience at + # the beginning of the download. + pb_fmt = strings._('gui_all_modes_progress_starting').format( + self.common.human_readable_filesize(downloaded_bytes)) + else: + pb_fmt = strings._('gui_all_modes_progress_eta').format( + self.common.human_readable_filesize(downloaded_bytes), + self.estimated_time_remaining) + + self.progress_bar.setFormat(pb_fmt) def cancel(self): self.progress_bar.setFormat(strings._('gui_canceled')) self.status = HistoryItem.STATUS_CANCELED -class VisitHistoryItem(HistoryItem): - """ - Download history item, for share mode - """ - def __init__(self, common, id, total_bytes): - super(VisitHistoryItem, self).__init__() - self.status = HistoryItem.STATUS_STARTED - self.common = common - - self.id = id - self.visited = time.time() - self.visited_dt = datetime.fromtimestamp(self.visited) - - # Label - self.label = QtWidgets.QLabel(strings._('gui_visit_started').format(self.visited_dt.strftime("%b %d, %I:%M%p"))) - - # Layout - layout = QtWidgets.QVBoxLayout() - layout.addWidget(self.label) - self.setLayout(layout) - - def update(self): - self.label.setText(self.get_finished_label_text(self.started_dt)) - self.status = HistoryItem.STATUS_FINISHED - - def cancel(self): - self.progress_bar.setFormat(strings._('gui_canceled')) - self.status = HistoryItem.STATUS_CANCELED + @property + def estimated_time_remaining(self): + return self.common.estimated_time_remaining(self.downloaded_bytes, + self.total_bytes, + self.started) class HistoryItemList(QtWidgets.QScrollArea): """ diff --git a/onionshare_gui/mode/share_mode/__init__.py b/onionshare_gui/mode/share_mode/__init__.py index 56aa1364..b5da0cd3 100644 --- a/onionshare_gui/mode/share_mode/__init__.py +++ b/onionshare_gui/mode/share_mode/__init__.py @@ -225,21 +225,6 @@ class ShareMode(Mode): """ self.primary_action.hide() - def handle_request_load(self, event): - """ - Handle REQUEST_LOAD event. - """ - self.system_tray.showMessage(strings._('systray_page_loaded_title'), strings._('systray_page_loaded_message')) - if not self.common.settings.get('close_after_first_download') and not event["path"].startswith(('/favicon.ico', '/download', self.web.static_url_path)) and event["path"] != '/': - - item = IndividualFileHistoryItem(self.common, event["path"]) - - self.history.add(0, item) - self.toggle_history.update_indicator(True) - self.history.completed_count += 1 - self.history.update_completed() - self.system_tray.showMessage(strings._('systray_individual_file_downloaded_title'), strings._('systray_individual_file_downloaded_message').format(event["path"])) - def handle_request_started(self, event): """ Handle REQUEST_STARTED event. diff --git a/onionshare_gui/mode/website_mode/__init__.py b/onionshare_gui/mode/website_mode/__init__.py index 8ac88c8c..3d4497f0 100644 --- a/onionshare_gui/mode/website_mode/__init__.py +++ b/onionshare_gui/mode/website_mode/__init__.py @@ -30,7 +30,7 @@ from onionshare.web import Web from ..file_selection import FileSelection from .. import Mode -from ..history import History, ToggleHistory, VisitHistoryItem +from ..history import History, ToggleHistory from ...widgets import Alert class WebsiteMode(Mode): @@ -204,21 +204,6 @@ class WebsiteMode(Mode): """ self.system_tray.showMessage(strings._('systray_site_loaded_title'), strings._('systray_site_loaded_message')) - def handle_request_started(self, event): - """ - Handle REQUEST_STARTED event. - """ - if ( (event["path"] == '') or (event["path"].find(".htm") != -1 ) ): - item = VisitHistoryItem(self.common, event["data"]["id"], 0) - - self.history.add(event["data"]["id"], item) - self.toggle_history.update_indicator(True) - self.history.completed_count += 1 - self.history.update_completed() - - self.system_tray.showMessage(strings._('systray_website_started_title'), strings._('systray_website_started_message')) - - def on_reload_settings(self): """ If there were some files listed for sharing, we should be ok to re-enable diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index bed86895..20873bc8 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -383,7 +383,7 @@ class OnionShareGui(QtWidgets.QMainWindow): self.share_mode.server_status.autostart_timer_container.hide() self.receive_mode.server_status.autostart_timer_container.hide() self.website_mode.server_status.autostart_timer_container.hide() - + d = SettingsDialog(self.common, self.onion, self.qtapp, self.config, self.local_only) d.settings_saved.connect(reload_settings) d.exec_() @@ -470,6 +470,15 @@ class OnionShareGui(QtWidgets.QMainWindow): elif event["type"] == Web.REQUEST_UPLOAD_CANCELED: mode.handle_request_upload_canceled(event) + elif event["type"] == Web.REQUEST_INDIVIDUAL_FILE_STARTED: + mode.handle_request_individual_file_started(event) + + elif event["type"] == Web.REQUEST_INDIVIDUAL_FILE_PROGRESS: + mode.handle_request_individual_file_progress(event) + + elif event["type"] == Web.REQUEST_INDIVIDUAL_FILE_CANCELED: + mode.handle_request_individual_file_canceled(event) + if event["type"] == Web.REQUEST_ERROR_DATA_DIR_CANNOT_CREATE: Alert(self.common, strings._('error_cannot_create_data_dir').format(event["data"]["receive_mode_dir"])) diff --git a/share/locale/en.json b/share/locale/en.json index c26577b2..5fbf88f9 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -178,7 +178,6 @@ "gui_receive_mode_no_files": "No Files Received Yet", "gui_receive_mode_autostop_timer_waiting": "Waiting to finish receiving", "gui_visit_started": "Someone has visited your website {}", - "gui_individual_file_download": "Viewed {}", "receive_mode_upload_starting": "Upload of total size {} is starting", "days_first_letter": "d", "hours_first_letter": "h", diff --git a/share/templates/405.html b/share/templates/405.html new file mode 100644 index 00000000..55493ae7 --- /dev/null +++ b/share/templates/405.html @@ -0,0 +1,19 @@ + + + + + OnionShare: 405 Method Not Allowed + + + + + +
+
+

+

405 Method Not Allowed

+
+
+ + + From 11860b55f2144120b2af09cb0b1314ae479a1aff Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 3 Sep 2019 21:59:49 -0700 Subject: [PATCH 33/48] Show IndividualFileHistoryItem widgets for directory listings --- onionshare/web/send_base_mode.py | 15 ++++++++++---- onionshare_gui/mode/history.py | 35 +++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 402bc32f..eb6525d1 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -79,6 +79,15 @@ class SendBaseModeWeb: self.set_file_info_custom(filenames, processed_size_callback) def directory_listing(self, filenames, path='', filesystem_path=None): + # Tell the GUI about the directory listing + download_id = self.download_count + self.download_count += 1 + self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, '/{}'.format(path), { + 'id': download_id, + 'method': request.method, + 'directory_listing': True + }) + # If filesystem_path is None, this is the root directory listing files, dirs = self.build_directory_listing(filenames, filesystem_path) r = self.directory_listing_template(path, files, dirs) @@ -132,13 +141,11 @@ class SendBaseModeWeb: file_to_download = filesystem_path filesize = os.path.getsize(filesystem_path) - # Each download has a unique id - download_id = self.download_count - self.download_count += 1 - path = request.path # Tell GUI the individual file started + download_id = self.download_count + self.download_count += 1 self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, path, { 'id': download_id, 'filesize': filesize, diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index a9fbbb36..ce783d46 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -352,34 +352,34 @@ class IndividualFileHistoryItem(HistoryItem): self.id = id self.path = path - self.method = data['method'] - self.total_bytes = data['filesize'] + self.total_bytes = 0 self.downloaded_bytes = 0 + self.method = data['method'] self.started = time.time() self.started_dt = datetime.fromtimestamp(self.started) self.status = HistoryItem.STATUS_STARTED + self.directory_listing = 'directory_listing' in data + # Labels self.timestamp_label = QtWidgets.QLabel(self.started_dt.strftime("%b %d, %I:%M%p")) self.method_label = QtWidgets.QLabel("{} {}".format(self.method, self.path)) - self.status_label = QtWidgets.QLabel() + self.status_code_label = QtWidgets.QLabel() # Progress bar self.progress_bar = QtWidgets.QProgressBar() self.progress_bar.setTextVisible(True) self.progress_bar.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.progress_bar.setAlignment(QtCore.Qt.AlignHCenter) - self.progress_bar.setMinimum(0) - self.progress_bar.setMaximum(data['filesize']) self.progress_bar.setValue(0) self.progress_bar.setStyleSheet(self.common.css['downloads_uploads_progress_bar']) - self.progress_bar.total_bytes = data['filesize'] # Text layout labels_layout = QtWidgets.QHBoxLayout() labels_layout.addWidget(self.timestamp_label) labels_layout.addWidget(self.method_label) - labels_layout.addWidget(self.status_label) + labels_layout.addWidget(self.status_code_label) + labels_layout.addStretch() # Layout layout = QtWidgets.QVBoxLayout() @@ -389,11 +389,26 @@ class IndividualFileHistoryItem(HistoryItem): # All non-GET requests are error 405 Method Not Allowed if self.method.lower() != 'get': - self.status_label.setText("405") + self.status_code_label.setText("405") + self.status = HistoryItem.STATUS_FINISHED self.progress_bar.hide() + return + + # Is this a directory listing? + if self.directory_listing: + self.status_code_label.setText("200") + self.status = HistoryItem.STATUS_FINISHED + self.progress_bar.hide() + return + else: - # Start at 0 - self.update(0) + self.total_bytes = data['filesize'] + self.progress_bar.setMinimum(0) + self.progress_bar.setMaximum(data['filesize']) + self.progress_bar.total_bytes = data['filesize'] + + # Start at 0 + self.update(0) def update(self, downloaded_bytes): self.downloaded_bytes = downloaded_bytes From 54ba711cbf1bd7a6f43944495935a19e7d3941e3 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 3 Sep 2019 22:18:30 -0700 Subject: [PATCH 34/48] Rename download_count/download_id, upload_count/upload_id, and visit_count/visit_id to simply cur_history_id/history_id, and make all errors create IndividualFileHistoryItem widgets --- onionshare/web/receive_mode.py | 24 ++++++++++------------ onionshare/web/send_base_mode.py | 21 +++++++++---------- onionshare/web/share_mode.py | 14 ++++++------- onionshare/web/web.py | 35 ++++++++++++++++++++++++++++++++ onionshare/web/website_mode.py | 11 ---------- onionshare_gui/mode/history.py | 7 ++++--- 6 files changed, 66 insertions(+), 46 deletions(-) diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index d2b03da0..5029232f 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -19,7 +19,6 @@ class ReceiveModeWeb: self.web = web self.can_upload = True - self.upload_count = 0 self.uploads_in_progress = [] self.define_routes() @@ -52,7 +51,7 @@ class ReceiveModeWeb: # Tell the GUI the receive mode directory for this file self.web.add_request(self.web.REQUEST_UPLOAD_SET_DIR, request.path, { - 'id': request.upload_id, + 'id': request.history_id, 'filename': basename, 'dir': request.receive_mode_dir }) @@ -272,10 +271,9 @@ class ReceiveModeRequest(Request): # Prevent new uploads if we've said so (timer expired) if self.web.receive_mode.can_upload: - # Create an upload_id, attach it to the request - self.upload_id = self.web.receive_mode.upload_count - - self.web.receive_mode.upload_count += 1 + # Create an history_id, attach it to the request + self.history_id = self.web.receive_mode.cur_history_id + self.web.receive_mode.cur_history_id += 1 # Figure out the content length try: @@ -302,10 +300,10 @@ class ReceiveModeRequest(Request): if not self.told_gui_about_request: # Tell the GUI about the request self.web.add_request(self.web.REQUEST_STARTED, self.path, { - 'id': self.upload_id, + 'id': self.history_id, 'content_length': self.content_length }) - self.web.receive_mode.uploads_in_progress.append(self.upload_id) + self.web.receive_mode.uploads_in_progress.append(self.history_id) self.told_gui_about_request = True @@ -337,19 +335,19 @@ class ReceiveModeRequest(Request): try: if self.told_gui_about_request: - upload_id = self.upload_id + history_id = self.history_id if not self.web.stop_q.empty() or not self.progress[self.filename]['complete']: # Inform the GUI that the upload has canceled self.web.add_request(self.web.REQUEST_UPLOAD_CANCELED, self.path, { - 'id': upload_id + 'id': history_id }) else: # Inform the GUI that the upload has finished self.web.add_request(self.web.REQUEST_UPLOAD_FINISHED, self.path, { - 'id': upload_id + 'id': history_id }) - self.web.receive_mode.uploads_in_progress.remove(upload_id) + self.web.receive_mode.uploads_in_progress.remove(history_id) except AttributeError: pass @@ -375,7 +373,7 @@ class ReceiveModeRequest(Request): # Update the GUI on the upload progress if self.told_gui_about_request: self.web.add_request(self.web.REQUEST_PROGRESS, self.path, { - 'id': self.upload_id, + 'id': self.history_id, 'progress': self.progress }) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index eb6525d1..3a01cb8f 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -44,8 +44,7 @@ class SendBaseModeWeb: self.files = {} # Dictionary mapping file paths to filenames on disk self.root_files = {} # This is only the root files and dirs, as opposed to all of them self.cleanup_filenames = [] - self.visit_count = 0 - self.download_count = 0 + self.cur_history_id = 0 self.file_info = {'files': [], 'dirs': []} self.gzip_individual_files = {} self.init() @@ -80,12 +79,12 @@ class SendBaseModeWeb: def directory_listing(self, filenames, path='', filesystem_path=None): # Tell the GUI about the directory listing - download_id = self.download_count - self.download_count += 1 + history_id = self.cur_history_id + self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, '/{}'.format(path), { - 'id': download_id, + 'id': history_id, 'method': request.method, - 'directory_listing': True + 'status_code': 200 }) # If filesystem_path is None, this is the root directory listing @@ -144,10 +143,10 @@ class SendBaseModeWeb: path = request.path # Tell GUI the individual file started - download_id = self.download_count - self.download_count += 1 + history_id = self.cur_history_id + self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, path, { - 'id': download_id, + 'id': history_id, 'filesize': filesize, 'method': request.method }) @@ -178,7 +177,7 @@ class SendBaseModeWeb: sys.stdout.flush() self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_PROGRESS, path, { - 'id': download_id, + 'id': history_id, 'bytes': downloaded_bytes }) done = False @@ -188,7 +187,7 @@ class SendBaseModeWeb: # Tell the GUI the individual file was canceled self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_CANCELED, path, { - 'id': download_id + 'id': history_id }) fp.close() diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index 60620e2a..c9d9b229 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -60,10 +60,6 @@ class ShareModeWeb(SendBaseModeWeb): static_url_path=self.web.static_url_path)) return self.web.add_security_headers(r) - # Each download has a unique id - download_id = self.download_count - self.download_count += 1 - # Prepare some variables to use inside generate() function below # which is outside of the request context shutdown_func = request.environ.get('werkzeug.server.shutdown') @@ -81,8 +77,10 @@ class ShareModeWeb(SendBaseModeWeb): self.filesize = self.download_filesize # Tell GUI the download started + history_id = self.cur_history_id + self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_STARTED, path, { - 'id': download_id, + 'id': history_id, 'use_gzip': use_gzip }) @@ -102,7 +100,7 @@ class ShareModeWeb(SendBaseModeWeb): # The user has canceled the download, so stop serving the file if not self.web.stop_q.empty(): self.web.add_request(self.web.REQUEST_CANCELED, path, { - 'id': download_id + 'id': history_id }) break @@ -124,7 +122,7 @@ class ShareModeWeb(SendBaseModeWeb): sys.stdout.flush() self.web.add_request(self.web.REQUEST_PROGRESS, path, { - 'id': download_id, + 'id': history_id, 'bytes': downloaded_bytes }) self.web.done = False @@ -135,7 +133,7 @@ class ShareModeWeb(SendBaseModeWeb): # tell the GUI the download has canceled self.web.add_request(self.web.REQUEST_CANCELED, path, { - 'id': download_id + 'id': history_id }) fp.close() diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 5a96b324..c4d5385f 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -63,6 +63,9 @@ class Web: self.auth = HTTPBasicAuth() self.auth.error_handler(self.error401) + # This tracks the history id + self.cur_history_id = 0 + # Verbose mode? if self.common.verbose: self.verbose_mode() @@ -193,20 +196,52 @@ class Web: self.force_shutdown() print("Someone has made too many wrong attempts to guess your password, so OnionShare has stopped the server. Start sharing again and send the recipient a new address to share.") + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 401 + }) + r = make_response(render_template('401.html', static_url_path=self.static_url_path), 401) return self.add_security_headers(r) def error403(self): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 403 + }) + self.add_request(Web.REQUEST_OTHER, request.path) r = make_response(render_template('403.html', static_url_path=self.static_url_path), 403) return self.add_security_headers(r) def error404(self): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 404 + }) + self.add_request(Web.REQUEST_OTHER, request.path) r = make_response(render_template('404.html', static_url_path=self.static_url_path), 404) return self.add_security_headers(r) def error405(self): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'method': request.method, + 'status_code': 405 + }) + r = make_response(render_template('405.html', static_url_path=self.static_url_path), 405) return self.add_security_headers(r) diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 28f2607d..55e5c1d4 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -28,17 +28,6 @@ class WebsiteModeWeb(SendBaseModeWeb): """ Render the onionshare website. """ - - # Each download has a unique id - visit_id = self.visit_count - self.visit_count += 1 - - # Tell GUI the page has been visited - self.web.add_request(self.web.REQUEST_STARTED, path, { - 'id': visit_id, - 'action': 'visit' - }) - return self.render_logic(path) def directory_listing_template(self, path, files, dirs): diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index ce783d46..cd8fe529 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -394,9 +394,9 @@ class IndividualFileHistoryItem(HistoryItem): self.progress_bar.hide() return - # Is this a directory listing? - if self.directory_listing: - self.status_code_label.setText("200") + # Is a status code already sent? + if 'status_code' in data: + self.status_code_label.setText("{}".format(data['status_code'])) self.status = HistoryItem.STATUS_FINISHED self.progress_bar.hide() return @@ -415,6 +415,7 @@ class IndividualFileHistoryItem(HistoryItem): self.progress_bar.setValue(downloaded_bytes) if downloaded_bytes == self.progress_bar.total_bytes: + self.status_code_label.setText("200") self.progress_bar.hide() self.status = HistoryItem.STATUS_FINISHED From 4ee6647ee5e638db1f0dc9245edaa0f9b7d80ed7 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 3 Sep 2019 22:20:52 -0700 Subject: [PATCH 35/48] Rename a few more count variables to cur_history_id --- onionshare/__init__.py | 4 ++-- onionshare_gui/mode/receive_mode/__init__.py | 4 ++-- onionshare_gui/mode/share_mode/__init__.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/onionshare/__init__.py b/onionshare/__init__.py index 0003106f..7e7798f8 100644 --- a/onionshare/__init__.py +++ b/onionshare/__init__.py @@ -262,12 +262,12 @@ def main(cwd=None): if not app.autostop_timer_thread.is_alive(): if mode == 'share' or (mode == 'website'): # If there were no attempts to download the share, or all downloads are done, we can stop - if web.share_mode.download_count == 0 or web.done: + if web.share_mode.cur_history_id == 0 or web.done: print("Stopped because auto-stop timer ran out") web.stop(app.port) break if mode == 'receive': - if web.receive_mode.upload_count == 0 or not web.receive_mode.uploads_in_progress: + if web.receive_mode.cur_history_id == 0 or not web.receive_mode.uploads_in_progress: print("Stopped because auto-stop timer ran out") web.stop(app.port) break diff --git a/onionshare_gui/mode/receive_mode/__init__.py b/onionshare_gui/mode/receive_mode/__init__.py index 0010fbd2..ecbfa54a 100644 --- a/onionshare_gui/mode/receive_mode/__init__.py +++ b/onionshare_gui/mode/receive_mode/__init__.py @@ -97,7 +97,7 @@ class ReceiveMode(Mode): The auto-stop timer expired, should we stop the server? Returns a bool """ # If there were no attempts to upload files, or all uploads are done, we can stop - if self.web.receive_mode.upload_count == 0 or not self.web.receive_mode.uploads_in_progress: + if self.web.receive_mode.cur_history_id == 0 or not self.web.receive_mode.uploads_in_progress: self.server_status.stop_server() self.server_status_label.setText(strings._('close_on_autostop_timer')) return True @@ -112,7 +112,7 @@ class ReceiveMode(Mode): Starting the server. """ # Reset web counters - self.web.receive_mode.upload_count = 0 + self.web.receive_mode.cur_history_id = 0 self.web.reset_invalid_passwords() # Hide and reset the uploads if we have previously shared diff --git a/onionshare_gui/mode/share_mode/__init__.py b/onionshare_gui/mode/share_mode/__init__.py index b5da0cd3..35a2045d 100644 --- a/onionshare_gui/mode/share_mode/__init__.py +++ b/onionshare_gui/mode/share_mode/__init__.py @@ -132,7 +132,7 @@ class ShareMode(Mode): The auto-stop timer expired, should we stop the server? Returns a bool """ # If there were no attempts to download the share, or all downloads are done, we can stop - if self.web.share_mode.download_count == 0 or self.web.done: + if self.web.share_mode.cur_history_id == 0 or self.web.done: self.server_status.stop_server() self.server_status_label.setText(strings._('close_on_autostop_timer')) return True @@ -146,7 +146,7 @@ class ShareMode(Mode): Starting the server. """ # Reset web counters - self.web.share_mode.download_count = 0 + self.web.share_mode.cur_history_id = 0 self.web.reset_invalid_passwords() # Hide and reset the downloads if we have previously shared From bef116760d42b489290e69bad511f8f0d451cf6a Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 3 Sep 2019 22:31:13 -0700 Subject: [PATCH 36/48] Make the IndividualFileHistoryItem widgets have color --- onionshare/common.py | 20 +++++++++++++++++++- onionshare_gui/mode/history.py | 18 +++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/onionshare/common.py b/onionshare/common.py index 27e8efc2..06563461 100644 --- a/onionshare/common.py +++ b/onionshare/common.py @@ -203,7 +203,7 @@ class Common(object): border: 0px; }""", - # Common styles between ShareMode and ReceiveMode and their child widgets + # Common styles between modes and their child widgets 'mode_info_label': """ QLabel { font-size: 12px; @@ -310,6 +310,24 @@ class Common(object): width: 10px; }""", + 'history_individual_file_timestamp_label': """ + QLabel { + color: #666666; + }""", + + 'history_individual_file_request_label': """ + QLabel { }""", + + 'history_individual_file_status_code_label_2xx': """ + QLabel { + color: #008800; + }""", + + 'history_individual_file_status_code_label_4xx': """ + QLabel { + color: #cc0000; + }""", + # Share mode and child widget styles 'share_zip_progess_bar': """ QProgressBar { diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index cd8fe529..797950ab 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -363,7 +363,9 @@ class IndividualFileHistoryItem(HistoryItem): # Labels self.timestamp_label = QtWidgets.QLabel(self.started_dt.strftime("%b %d, %I:%M%p")) - self.method_label = QtWidgets.QLabel("{} {}".format(self.method, self.path)) + self.timestamp_label.setStyleSheet(self.common.css['history_individual_file_timestamp_label']) + self.request_label = QtWidgets.QLabel("{} {}".format(self.method, self.path)) + self.request_label.setStyleSheet(self.common.css['history_individual_file_request_label']) self.status_code_label = QtWidgets.QLabel() # Progress bar @@ -377,7 +379,7 @@ class IndividualFileHistoryItem(HistoryItem): # Text layout labels_layout = QtWidgets.QHBoxLayout() labels_layout.addWidget(self.timestamp_label) - labels_layout.addWidget(self.method_label) + labels_layout.addWidget(self.request_label) labels_layout.addWidget(self.status_code_label) labels_layout.addStretch() @@ -387,16 +389,13 @@ class IndividualFileHistoryItem(HistoryItem): layout.addWidget(self.progress_bar) self.setLayout(layout) - # All non-GET requests are error 405 Method Not Allowed - if self.method.lower() != 'get': - self.status_code_label.setText("405") - self.status = HistoryItem.STATUS_FINISHED - self.progress_bar.hide() - return - # Is a status code already sent? if 'status_code' in data: self.status_code_label.setText("{}".format(data['status_code'])) + if data['status_code'] >= 200 and data['status_code'] < 300: + self.status_code_label.setStyleSheet(self.common.css['history_individual_file_status_code_label_2xx']) + if data['status_code'] >= 400 and data['status_code'] < 500: + self.status_code_label.setStyleSheet(self.common.css['history_individual_file_status_code_label_4xx']) self.status = HistoryItem.STATUS_FINISHED self.progress_bar.hide() return @@ -416,6 +415,7 @@ class IndividualFileHistoryItem(HistoryItem): self.progress_bar.setValue(downloaded_bytes) if downloaded_bytes == self.progress_bar.total_bytes: self.status_code_label.setText("200") + self.status_code_label.setStyleSheet(self.common.css['history_individual_file_status_code_label_2xx']) self.progress_bar.hide() self.status = HistoryItem.STATUS_FINISHED From bc210c954d7d7be034aa898c8a240c8abdd8b2e8 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 09:35:44 -0700 Subject: [PATCH 37/48] Remove method from IndividualFileHistoryItem, and only display these widgets on 200 and 404 requests, not all of the others --- onionshare/common.py | 3 --- onionshare/web/send_base_mode.py | 3 +-- onionshare/web/web.py | 24 ------------------------ onionshare_gui/mode/history.py | 6 ++---- 4 files changed, 3 insertions(+), 33 deletions(-) diff --git a/onionshare/common.py b/onionshare/common.py index 06563461..ab503fdc 100644 --- a/onionshare/common.py +++ b/onionshare/common.py @@ -315,9 +315,6 @@ class Common(object): color: #666666; }""", - 'history_individual_file_request_label': """ - QLabel { }""", - 'history_individual_file_status_code_label_2xx': """ QLabel { color: #008800; diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 3a01cb8f..6a0390ab 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -147,8 +147,7 @@ class SendBaseModeWeb: self.cur_history_id += 1 self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, path, { 'id': history_id, - 'filesize': filesize, - 'method': request.method + 'filesize': filesize }) # Only GET requests are allowed, any other method should fail diff --git a/onionshare/web/web.py b/onionshare/web/web.py index c4d5385f..610c14c2 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -196,26 +196,10 @@ class Web: self.force_shutdown() print("Someone has made too many wrong attempts to guess your password, so OnionShare has stopped the server. Start sharing again and send the recipient a new address to share.") - history_id = self.cur_history_id - self.cur_history_id += 1 - self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { - 'id': history_id, - 'method': request.method, - 'status_code': 401 - }) - r = make_response(render_template('401.html', static_url_path=self.static_url_path), 401) return self.add_security_headers(r) def error403(self): - history_id = self.cur_history_id - self.cur_history_id += 1 - self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { - 'id': history_id, - 'method': request.method, - 'status_code': 403 - }) - self.add_request(Web.REQUEST_OTHER, request.path) r = make_response(render_template('403.html', static_url_path=self.static_url_path), 403) return self.add_security_headers(r) @@ -234,14 +218,6 @@ class Web: return self.add_security_headers(r) def error405(self): - history_id = self.cur_history_id - self.cur_history_id += 1 - self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { - 'id': history_id, - 'method': request.method, - 'status_code': 405 - }) - r = make_response(render_template('405.html', static_url_path=self.static_url_path), 405) return self.add_security_headers(r) diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index 797950ab..2fd7cddb 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -354,7 +354,6 @@ class IndividualFileHistoryItem(HistoryItem): self.path = path self.total_bytes = 0 self.downloaded_bytes = 0 - self.method = data['method'] self.started = time.time() self.started_dt = datetime.fromtimestamp(self.started) self.status = HistoryItem.STATUS_STARTED @@ -364,8 +363,7 @@ class IndividualFileHistoryItem(HistoryItem): # Labels self.timestamp_label = QtWidgets.QLabel(self.started_dt.strftime("%b %d, %I:%M%p")) self.timestamp_label.setStyleSheet(self.common.css['history_individual_file_timestamp_label']) - self.request_label = QtWidgets.QLabel("{} {}".format(self.method, self.path)) - self.request_label.setStyleSheet(self.common.css['history_individual_file_request_label']) + self.path_label = QtWidgets.QLabel("{}".format(self.path)) self.status_code_label = QtWidgets.QLabel() # Progress bar @@ -379,7 +377,7 @@ class IndividualFileHistoryItem(HistoryItem): # Text layout labels_layout = QtWidgets.QHBoxLayout() labels_layout.addWidget(self.timestamp_label) - labels_layout.addWidget(self.request_label) + labels_layout.addWidget(self.path_label) labels_layout.addWidget(self.status_code_label) labels_layout.addStretch() From 79f563e443a548ffa8091c931de284de78a94f75 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 09:45:53 -0700 Subject: [PATCH 38/48] Make sure IndividualFileHistoryItem widgets display properly in receive mode too --- onionshare/web/receive_mode.py | 9 +++++++++ onionshare/web/web.py | 1 - 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index 5029232f..8604a889 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -21,6 +21,8 @@ class ReceiveModeWeb: self.can_upload = True self.uploads_in_progress = [] + self.cur_history_id = 0 + self.define_routes() def define_routes(self): @@ -29,6 +31,13 @@ class ReceiveModeWeb: """ @self.web.app.route("/") def index(): + history_id = self.cur_history_id + self.cur_history_id += 1 + self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { + 'id': history_id, + 'status_code': 200 + }) + self.web.add_request(self.web.REQUEST_LOAD, request.path) r = make_response(render_template('receive.html', static_url_path=self.web.static_url_path)) diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 610c14c2..6cd30c93 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -209,7 +209,6 @@ class Web: self.cur_history_id += 1 self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { 'id': history_id, - 'method': request.method, 'status_code': 404 }) From bd3a7fe1f7ccdaeda97110fbb241e23f83062dac Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 11:58:44 -0700 Subject: [PATCH 39/48] Don't consider individual downloads in the in_progress counter --- onionshare/web/send_base_mode.py | 3 ++- onionshare_gui/mode/__init__.py | 22 ++-------------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index 6a0390ab..a6ad2307 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -177,7 +177,8 @@ class SendBaseModeWeb: self.web.add_request(self.web.REQUEST_INDIVIDUAL_FILE_PROGRESS, path, { 'id': history_id, - 'bytes': downloaded_bytes + 'bytes': downloaded_bytes, + 'filesize': filesize }) done = False except: diff --git a/onionshare_gui/mode/__init__.py b/onionshare_gui/mode/__init__.py index b5a95f41..69ad00e6 100644 --- a/onionshare_gui/mode/__init__.py +++ b/onionshare_gui/mode/__init__.py @@ -427,9 +427,6 @@ class Mode(QtWidgets.QWidget): """ item = IndividualFileHistoryItem(self.common, event["data"], event["path"]) self.history.add(event["data"]["id"], item) - self.toggle_history.update_indicator(True) - self.history.in_progress_count += 1 - self.history.update_in_progress() def handle_request_individual_file_progress(self, event): """ @@ -438,19 +435,8 @@ class Mode(QtWidgets.QWidget): """ self.history.update(event["data"]["id"], event["data"]["bytes"]) - # Is the download complete? - if event["data"]["bytes"] == self.web.share_mode.filesize: - # Update completed and in progress labels - self.history.completed_count += 1 - self.history.in_progress_count -= 1 - self.history.update_completed() - self.history.update_in_progress() - - else: - if self.server_status.status == self.server_status.STATUS_STOPPED: - self.history.cancel(event["data"]["id"]) - self.history.in_progress_count = 0 - self.history.update_in_progress() + if self.server_status.status == self.server_status.STATUS_STOPPED: + self.history.cancel(event["data"]["id"]) def handle_request_individual_file_canceled(self, event): """ @@ -458,7 +444,3 @@ class Mode(QtWidgets.QWidget): Used in both Share and Website modes, so implemented here. """ self.history.cancel(event["data"]["id"]) - - # Update in progress count - self.history.in_progress_count -= 1 - self.history.update_in_progress() From 04d49dc3bd6f448cefd5e51ea83d1538b9de8f76 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 12:02:17 -0700 Subject: [PATCH 40/48] Add individual downloads label to settings dialog --- onionshare_gui/settings_dialog.py | 2 ++ share/locale/en.json | 1 + 2 files changed, 3 insertions(+) diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index 6ffd4523..5dbc31d2 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -204,10 +204,12 @@ class SettingsDialog(QtWidgets.QDialog): self.close_after_first_download_checkbox = QtWidgets.QCheckBox() self.close_after_first_download_checkbox.setCheckState(QtCore.Qt.Checked) self.close_after_first_download_checkbox.setText(strings._("gui_settings_close_after_first_download_option")) + individual_downloads_label = QtWidgets.QLabel(strings._("gui_settings_individual_downloads_label")) # Sharing options layout sharing_group_layout = QtWidgets.QVBoxLayout() sharing_group_layout.addWidget(self.close_after_first_download_checkbox) + sharing_group_layout.addWidget(individual_downloads_label) sharing_group = QtWidgets.QGroupBox(strings._("gui_settings_sharing_label")) sharing_group.setLayout(sharing_group_layout) diff --git a/share/locale/en.json b/share/locale/en.json index 5fbf88f9..c84c5538 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -52,6 +52,7 @@ "gui_settings_onion_label": "Onion settings", "gui_settings_sharing_label": "Sharing settings", "gui_settings_close_after_first_download_option": "Stop sharing after files have been sent", + "gui_settings_individual_downloads_label": "Uncheck to allow downloading individual files", "gui_settings_connection_type_label": "How should OnionShare connect to Tor?", "gui_settings_connection_type_bundled_option": "Use the Tor version built into OnionShare", "gui_settings_connection_type_automatic_option": "Attempt auto-configuration with Tor Browser", From 8aa871b2772d8e4abfcfade685fe358a5c784b33 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 17:24:18 -0700 Subject: [PATCH 41/48] Add web requests counter icon to history widget --- onionshare_gui/mode/history.py | 37 +++++++++++++++++++++------ share/images/share_requests.png | Bin 0 -> 738 bytes share/images/share_requests_none.png | Bin 0 -> 754 bytes share/locale/en.json | 1 + 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 share/images/share_requests.png create mode 100644 share/images/share_requests_none.png diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index 2fd7cddb..650e57be 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -237,6 +237,7 @@ class ReceiveHistoryItemFile(QtWidgets.QWidget): elif self.common.platform == 'Windows': subprocess.Popen(['explorer', '/select,{}'.format(abs_filename)]) + class ReceiveHistoryItem(HistoryItem): def __init__(self, common, id, content_length): super(ReceiveHistoryItem, self).__init__() @@ -442,6 +443,7 @@ class IndividualFileHistoryItem(HistoryItem): self.total_bytes, self.started) + class HistoryItemList(QtWidgets.QScrollArea): """ List of items @@ -524,12 +526,15 @@ class History(QtWidgets.QWidget): # In progress and completed counters self.in_progress_count = 0 self.completed_count = 0 + self.requests_count = 0 - # In progress and completed labels + # In progress, completed, and requests labels self.in_progress_label = QtWidgets.QLabel() self.in_progress_label.setStyleSheet(self.common.css['mode_info_label']) self.completed_label = QtWidgets.QLabel() self.completed_label.setStyleSheet(self.common.css['mode_info_label']) + self.requests_label = QtWidgets.QLabel() + self.requests_label.setStyleSheet(self.common.css['mode_info_label']) # Header self.header_label = QtWidgets.QLabel(header_text) @@ -543,6 +548,7 @@ class History(QtWidgets.QWidget): header_layout.addStretch() header_layout.addWidget(self.in_progress_label) header_layout.addWidget(self.completed_label) + header_layout.addWidget(self.requests_label) header_layout.addWidget(clear_button) # When there are no items @@ -621,6 +627,10 @@ class History(QtWidgets.QWidget): self.completed_count = 0 self.update_completed() + # Reset web requests counter + self.requests_count = 0 + self.update_requests() + def update_completed(self): """ Update the 'completed' widget. @@ -636,14 +646,25 @@ class History(QtWidgets.QWidget): """ Update the 'in progress' widget. """ - if self.mode != 'website': - if self.in_progress_count == 0: - image = self.common.get_resource_path('images/share_in_progress_none.png') - else: - image = self.common.get_resource_path('images/share_in_progress.png') + if self.in_progress_count == 0: + image = self.common.get_resource_path('images/share_in_progress_none.png') + else: + image = self.common.get_resource_path('images/share_in_progress.png') - self.in_progress_label.setText(' {1:d}'.format(image, self.in_progress_count)) - self.in_progress_label.setToolTip(strings._('history_in_progress_tooltip').format(self.in_progress_count)) + self.in_progress_label.setText(' {1:d}'.format(image, self.in_progress_count)) + self.in_progress_label.setToolTip(strings._('history_in_progress_tooltip').format(self.in_progress_count)) + + def update_requests(self): + """ + Update the 'web requests' widget. + """ + if self.requests_count == 0: + image = self.common.get_resource_path('images/share_requests_none.png') + else: + image = self.common.get_resource_path('images/share_requests.png') + + self.requests_label.setText(' {1:d}'.format(image, self.in_progress_count)) + self.requests_label.setToolTip(strings._('history_requests_tooltip').format(self.in_progress_count)) class ToggleHistory(QtWidgets.QPushButton): diff --git a/share/images/share_requests.png b/share/images/share_requests.png new file mode 100644 index 0000000000000000000000000000000000000000..4965744d57bebe31125cca767c4a1bb63dcfbd08 GIT binary patch literal 738 zcmV<80v-K{P)EX>4Tx04R}tkv&MmKp2MKrfO9x4t5Z6$WWauh>AFB6^c+H)C#RSm|Xe?O&XFE z7e~Rh;NZ_<)xpJCR|i)?5c~mgadlF3krKa43N2#1?&BYF{Svtpa+Scy zv49FR$gUs!4}SO7%1=&sN#Quq`QkVqBS2^uXw)3%``B?BCqVESxYAqxN*$Q_B)!(s zqDMggHgIv>(v&^mat9cEGGtSBr64UKp9kL0=$o>@z%9_b=Jl<4j?)JqO}$Fq00)P_ zXo0fVecs*O-nV~in*I9$wKj5FNcgT>00006VoOIv00000008+zyMF)x010qNS#tmY zE+YT{E+YYWr9XB6000McNliru;|U1>4>Ds@qc{Kn0N+VOK~yNumC~V315pqK(DOD8 zLEuQk2Y^C!HD(oYBZ_{4t9`M@ro(R5YrQ%rus4qkA9aWuT+ z0mt~l`6yzXhN1EkV_XgDMS&TXU6m8om<|k6tV3I3R-nXV7!I%+G^~OXp0e;y6vz<^ z-Y^;Hli-9Wr@Ak%aku3IHzTw-+wxo6A~x6rF^`zT$n(&euF9MA$7vh|{p&Qo0Z!;Y U2m*6azW@LL07*qoM6N<$f^Tm@n*aa+ literal 0 HcmV?d00001 diff --git a/share/images/share_requests_none.png b/share/images/share_requests_none.png new file mode 100644 index 0000000000000000000000000000000000000000..93a71ef3455a614f95a1f1270585d0b1c121d5dc GIT binary patch literal 754 zcmVEX>4Tx04R}tkv&MmKp2MKrfO9x4t5Z6$WWauh>AFB6^c+H)C#RSm|Xe?O&XFE z7e~Rh;NZ_<)xpJCR|i)?5c~mgadlF3krKa43N2#1?&BYF{Svtpa+Scy zv49FR$gUs!4}SO7%1=&sN#Quq`QkVqBS2^uXw)3%``B?BCqVESxYAqxN*$Q_B)!(s zqDMggHgIv>(v&^mat9cEGGtSBr64UKp9kL0=$o>@z%9_b=Jl<4j?)JqO}$Fq00)P_ zXo0fVecs*O-nV~in*I9$wKj5FNcgT>00006VoOIv00000008+zyMF)x010qNS#tmY zE+YT{E+YYWr9XB6000McNliru;|U1>4<`P2LqQOQ;qSHu zK~N+O4}e3oVkHCvn4$oS<%XaMJOBy|u3~ux=##7{0t4m>NC=h?(u9B+adG9io%zq1 z$!sN5*Yz?^af~%oc*ZTpIp+`P2E2taj5XyyFh* zm|?#cYwQHD<*)iUX}wQ7IKXwzxxx!ZEntL~=w0KWgCXt%Sj9uj@DMe@3_Jd*4yLgd zykXGl527ZRX;s(tIj-g$aM6qV@6LJFakrE*ODQ$j#2&s|ANPfX2A4VKr!JIInv~K6 kpOLl_hF9F+Ea&_Lzx&EXpjdPhH~;_u07*qoM6N<$g0PxPp8x;= literal 0 HcmV?d00001 diff --git a/share/locale/en.json b/share/locale/en.json index c84c5538..aab6153d 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -134,6 +134,7 @@ "gui_file_info_single": "{} file, {}", "history_in_progress_tooltip": "{} in progress", "history_completed_tooltip": "{} completed", + "history_requests_tooltip": "{} web requests", "error_cannot_create_data_dir": "Could not create OnionShare data folder: {}", "gui_receive_mode_warning": "Receive mode lets people upload files to your computer.

Some files can potentially take control of your computer if you open them. Only open things from people you trust, or if you know what you are doing.", "gui_mode_share_button": "Share Files", From 3422cf6ea89ec50895f19a3c4a067372ad3788d4 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 17:27:24 -0700 Subject: [PATCH 42/48] Rename images from share_ to history_, because they are used in all modes --- onionshare_gui/mode/history.py | 12 ++++++------ .../{share_completed.png => history_completed.png} | Bin ...ompleted_none.png => history_completed_none.png} | Bin ...hare_in_progress.png => history_in_progress.png} | Bin ...ogress_none.png => history_in_progress_none.png} | Bin .../{share_requests.png => history_requests.png} | Bin ..._requests_none.png => history_requests_none.png} | Bin 7 files changed, 6 insertions(+), 6 deletions(-) rename share/images/{share_completed.png => history_completed.png} (100%) rename share/images/{share_completed_none.png => history_completed_none.png} (100%) rename share/images/{share_in_progress.png => history_in_progress.png} (100%) rename share/images/{share_in_progress_none.png => history_in_progress_none.png} (100%) rename share/images/{share_requests.png => history_requests.png} (100%) rename share/images/{share_requests_none.png => history_requests_none.png} (100%) diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index 650e57be..568bda7b 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -636,9 +636,9 @@ class History(QtWidgets.QWidget): Update the 'completed' widget. """ if self.completed_count == 0: - image = self.common.get_resource_path('images/share_completed_none.png') + image = self.common.get_resource_path('images/history_completed_none.png') else: - image = self.common.get_resource_path('images/share_completed.png') + image = self.common.get_resource_path('images/history_completed.png') self.completed_label.setText(' {1:d}'.format(image, self.completed_count)) self.completed_label.setToolTip(strings._('history_completed_tooltip').format(self.completed_count)) @@ -647,9 +647,9 @@ class History(QtWidgets.QWidget): Update the 'in progress' widget. """ if self.in_progress_count == 0: - image = self.common.get_resource_path('images/share_in_progress_none.png') + image = self.common.get_resource_path('images/history_in_progress_none.png') else: - image = self.common.get_resource_path('images/share_in_progress.png') + image = self.common.get_resource_path('images/history_in_progress.png') self.in_progress_label.setText(' {1:d}'.format(image, self.in_progress_count)) self.in_progress_label.setToolTip(strings._('history_in_progress_tooltip').format(self.in_progress_count)) @@ -659,9 +659,9 @@ class History(QtWidgets.QWidget): Update the 'web requests' widget. """ if self.requests_count == 0: - image = self.common.get_resource_path('images/share_requests_none.png') + image = self.common.get_resource_path('images/history_requests_none.png') else: - image = self.common.get_resource_path('images/share_requests.png') + image = self.common.get_resource_path('images/history_requests.png') self.requests_label.setText(' {1:d}'.format(image, self.in_progress_count)) self.requests_label.setToolTip(strings._('history_requests_tooltip').format(self.in_progress_count)) diff --git a/share/images/share_completed.png b/share/images/history_completed.png similarity index 100% rename from share/images/share_completed.png rename to share/images/history_completed.png diff --git a/share/images/share_completed_none.png b/share/images/history_completed_none.png similarity index 100% rename from share/images/share_completed_none.png rename to share/images/history_completed_none.png diff --git a/share/images/share_in_progress.png b/share/images/history_in_progress.png similarity index 100% rename from share/images/share_in_progress.png rename to share/images/history_in_progress.png diff --git a/share/images/share_in_progress_none.png b/share/images/history_in_progress_none.png similarity index 100% rename from share/images/share_in_progress_none.png rename to share/images/history_in_progress_none.png diff --git a/share/images/share_requests.png b/share/images/history_requests.png similarity index 100% rename from share/images/share_requests.png rename to share/images/history_requests.png diff --git a/share/images/share_requests_none.png b/share/images/history_requests_none.png similarity index 100% rename from share/images/share_requests_none.png rename to share/images/history_requests_none.png From 8cc1aa48bbff38f522207f51bed44f009e66b4ba Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 8 Sep 2019 17:39:31 -0700 Subject: [PATCH 43/48] Make web requests indicator icon increment on web requests --- onionshare_gui/mode/__init__.py | 4 ++++ onionshare_gui/mode/history.py | 6 +++--- onionshare_gui/mode/website_mode/__init__.py | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/onionshare_gui/mode/__init__.py b/onionshare_gui/mode/__init__.py index 69ad00e6..3ef285c4 100644 --- a/onionshare_gui/mode/__init__.py +++ b/onionshare_gui/mode/__init__.py @@ -425,6 +425,10 @@ class Mode(QtWidgets.QWidget): Handle REQUEST_INDVIDIDUAL_FILES_STARTED event. Used in both Share and Website modes, so implemented here. """ + self.toggle_history.update_indicator(True) + self.history.requests_count += 1 + self.history.update_requests() + item = IndividualFileHistoryItem(self.common, event["data"], event["path"]) self.history.add(event["data"]["id"], item) diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index 568bda7b..5dad9614 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -663,8 +663,8 @@ class History(QtWidgets.QWidget): else: image = self.common.get_resource_path('images/history_requests.png') - self.requests_label.setText(' {1:d}'.format(image, self.in_progress_count)) - self.requests_label.setToolTip(strings._('history_requests_tooltip').format(self.in_progress_count)) + self.requests_label.setText(' {1:d}'.format(image, self.requests_count)) + self.requests_label.setToolTip(strings._('history_requests_tooltip').format(self.requests_count)) class ToggleHistory(QtWidgets.QPushButton): @@ -697,7 +697,7 @@ class ToggleHistory(QtWidgets.QPushButton): def update_indicator(self, increment=False): """ Update the display of the indicator count. If increment is True, then - only increment the counter if Downloads is hidden. + only increment the counter if History is hidden. """ if increment and not self.history_widget.isVisible(): self.indicator_count += 1 diff --git a/onionshare_gui/mode/website_mode/__init__.py b/onionshare_gui/mode/website_mode/__init__.py index 3d4497f0..b277b6c3 100644 --- a/onionshare_gui/mode/website_mode/__init__.py +++ b/onionshare_gui/mode/website_mode/__init__.py @@ -80,6 +80,8 @@ class WebsiteMode(Mode): strings._('gui_all_modes_history'), 'website' ) + self.history.in_progress_label.hide() + self.history.completed_label.hide() self.history.hide() # Info label From 4a4437394d05bd9f48d315eeb39c752f3aad645e Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 9 Sep 2019 12:19:39 +1000 Subject: [PATCH 44/48] Fix tests in Receive Mode that actually do increment the history item widget count where they didn't previously (due to an additional GET that follows the 302 redirect of a POST request on upload) --- tests/GuiBaseTest.py | 4 ++-- tests/GuiReceiveTest.py | 30 ++---------------------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index 9a69619b..4f087431 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -116,7 +116,7 @@ class GuiBaseTest(object): self.assertEqual(mode.history.isVisible(), not currently_visible) - def history_indicator(self, mode, public_mode): + def history_indicator(self, mode, public_mode, indicator_count="1"): '''Test that we can make sure the history is toggled off, do an action, and the indiciator works''' # Make sure history is toggled off if mode.history.isVisible(): @@ -147,7 +147,7 @@ class GuiBaseTest(object): # Indicator should be visible, have a value of "1" self.assertTrue(mode.toggle_history.indicator_label.isVisible()) - self.assertEqual(mode.toggle_history.indicator_label.text(), "1") + self.assertEqual(mode.toggle_history.indicator_label.text(), indicator_count) # Toggle history back on, indicator should be hidden again QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) diff --git a/tests/GuiReceiveTest.py b/tests/GuiReceiveTest.py index c4bfa884..ef420ec2 100644 --- a/tests/GuiReceiveTest.py +++ b/tests/GuiReceiveTest.py @@ -66,31 +66,6 @@ class GuiReceiveTest(GuiBaseTest): r = requests.get('http://127.0.0.1:{}/close'.format(self.gui.app.port)) self.assertEqual(r.status_code, 401) - def uploading_zero_files_shouldnt_change_ui(self, mode, public_mode): - '''If you submit the receive mode form without selecting any files, the UI shouldn't get updated''' - url = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port) - - # What were the counts before submitting the form? - before_in_progress_count = mode.history.in_progress_count - before_completed_count = mode.history.completed_count - before_number_of_history_items = len(mode.history.item_list.items) - - # Click submit without including any files a few times - if public_mode: - r = requests.post(url, files={}) - r = requests.post(url, files={}) - r = requests.post(url, files={}) - else: - auth = requests.auth.HTTPBasicAuth('onionshare', mode.web.password) - r = requests.post(url, files={}, auth=auth) - r = requests.post(url, files={}, auth=auth) - r = requests.post(url, files={}, auth=auth) - - # The counts shouldn't change - self.assertEqual(mode.history.in_progress_count, before_in_progress_count) - self.assertEqual(mode.history.completed_count, before_completed_count) - self.assertEqual(len(mode.history.item_list.items), before_number_of_history_items) - # 'Grouped' tests follow from here def run_all_receive_mode_setup_tests(self, public_mode): @@ -127,14 +102,13 @@ class GuiReceiveTest(GuiBaseTest): # Test uploading the same file twice at the same time, and make sure no collisions self.upload_file(public_mode, '/tmp/test.txt', 'test.txt', True) self.counter_incremented(self.gui.receive_mode, 6) - self.uploading_zero_files_shouldnt_change_ui(self.gui.receive_mode, public_mode) - self.history_indicator(self.gui.receive_mode, public_mode) + self.history_indicator(self.gui.receive_mode, public_mode, "2") self.server_is_stopped(self.gui.receive_mode, False) self.web_server_is_stopped() self.server_status_indicator_says_closed(self.gui.receive_mode, False) self.server_working_on_start_button_pressed(self.gui.receive_mode) self.server_is_started(self.gui.receive_mode) - self.history_indicator(self.gui.receive_mode, public_mode) + self.history_indicator(self.gui.receive_mode, public_mode, "2") def run_all_receive_mode_unwritable_dir_tests(self, public_mode): '''Attempt to upload (unwritable) files in receive mode and stop the share''' From 2c87ea55ff3c433a387e7b5a66758ae9fef8ee8c Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 9 Sep 2019 16:35:05 +1000 Subject: [PATCH 45/48] Fix the discrepancy between SendBaseModeWeb and Web objects' separate cur_history_id attibutes, ensuring that when we call web.error404() we send a new history_id integer for communicating back to the frontend. Add tests for this --- onionshare/web/send_base_mode.py | 5 +++- onionshare/web/share_mode.py | 12 ++++++--- onionshare/web/web.py | 7 +---- onionshare_gui/mode/history.py | 10 +++---- tests/GuiBaseTest.py | 4 +++ tests/GuiReceiveTest.py | 9 +++++++ tests/GuiShareTest.py | 9 +++++++ ...hare_receive_mode_clear_all_button_test.py | 25 ++++++++++++++++++ ...nshare_share_mode_clear_all_button_test.py | 26 +++++++++++++++++++ 9 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 tests/local_onionshare_receive_mode_clear_all_button_test.py create mode 100644 tests/local_onionshare_share_mode_clear_all_button_test.py diff --git a/onionshare/web/send_base_mode.py b/onionshare/web/send_base_mode.py index a6ad2307..67fb26d0 100644 --- a/onionshare/web/send_base_mode.py +++ b/onionshare/web/send_base_mode.py @@ -29,6 +29,9 @@ class SendBaseModeWeb: # one download at a time. self.download_in_progress = False + # This tracks the history id + self.cur_history_id = 0 + self.define_routes() self.init() @@ -264,4 +267,4 @@ class SendBaseModeWeb: """ Inherited class will implement this. """ - pass \ No newline at end of file + pass diff --git a/onionshare/web/share_mode.py b/onionshare/web/share_mode.py index c9d9b229..f52bc2c7 100644 --- a/onionshare/web/share_mode.py +++ b/onionshare/web/share_mode.py @@ -207,11 +207,15 @@ class ShareModeWeb(SendBaseModeWeb): if self.download_individual_files: return self.stream_individual_file(filesystem_path) else: - return self.web.error404() + history_id = self.cur_history_id + self.cur_history_id += 1 + return self.web.error404(history_id) # If it's not a directory or file, throw a 404 else: - return self.web.error404() + history_id = self.cur_history_id + self.cur_history_id += 1 + return self.web.error404(history_id) else: # Special case loading / @@ -223,7 +227,9 @@ class ShareModeWeb(SendBaseModeWeb): else: # If the path isn't found, throw a 404 - return self.web.error404() + history_id = self.cur_history_id + self.cur_history_id += 1 + return self.web.error404(history_id) def build_zipfile_list(self, filenames, processed_size_callback=None): self.common.log("ShareModeWeb", "build_zipfile_list") diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 6cd30c93..2b0d2812 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -63,9 +63,6 @@ class Web: self.auth = HTTPBasicAuth() self.auth.error_handler(self.error401) - # This tracks the history id - self.cur_history_id = 0 - # Verbose mode? if self.common.verbose: self.verbose_mode() @@ -204,9 +201,7 @@ class Web: r = make_response(render_template('403.html', static_url_path=self.static_url_path), 403) return self.add_security_headers(r) - def error404(self): - history_id = self.cur_history_id - self.cur_history_id += 1 + def error404(self, history_id): self.add_request(self.REQUEST_INDIVIDUAL_FILE_STARTED, '{}'.format(request.path), { 'id': history_id, 'status_code': 404 diff --git a/onionshare_gui/mode/history.py b/onionshare_gui/mode/history.py index 5dad9614..b8baebd1 100644 --- a/onionshare_gui/mode/history.py +++ b/onionshare_gui/mode/history.py @@ -539,17 +539,17 @@ class History(QtWidgets.QWidget): # Header self.header_label = QtWidgets.QLabel(header_text) self.header_label.setStyleSheet(self.common.css['downloads_uploads_label']) - clear_button = QtWidgets.QPushButton(strings._('gui_all_modes_clear_history')) - clear_button.setStyleSheet(self.common.css['downloads_uploads_clear']) - clear_button.setFlat(True) - clear_button.clicked.connect(self.reset) + self.clear_button = QtWidgets.QPushButton(strings._('gui_all_modes_clear_history')) + self.clear_button.setStyleSheet(self.common.css['downloads_uploads_clear']) + self.clear_button.setFlat(True) + self.clear_button.clicked.connect(self.reset) header_layout = QtWidgets.QHBoxLayout() header_layout.addWidget(self.header_label) header_layout.addStretch() header_layout.addWidget(self.in_progress_label) header_layout.addWidget(self.completed_label) header_layout.addWidget(self.requests_label) - header_layout.addWidget(clear_button) + header_layout.addWidget(self.clear_button) # When there are no items self.empty_image = QtWidgets.QLabel() diff --git a/tests/GuiBaseTest.py b/tests/GuiBaseTest.py index 4f087431..3e82769a 100644 --- a/tests/GuiBaseTest.py +++ b/tests/GuiBaseTest.py @@ -285,6 +285,10 @@ class GuiBaseTest(object): else: self.assertEqual(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically')) + def clear_all_history_items(self, mode, count): + if count == 0: + QtTest.QTest.mouseClick(mode.history.clear_button, QtCore.Qt.LeftButton) + self.assertEquals(len(mode.history.item_list.items.keys()), count) # Auto-stop timer tests def set_timeout(self, mode, timeout): diff --git a/tests/GuiReceiveTest.py b/tests/GuiReceiveTest.py index ef420ec2..80e05250 100644 --- a/tests/GuiReceiveTest.py +++ b/tests/GuiReceiveTest.py @@ -127,3 +127,12 @@ class GuiReceiveTest(GuiBaseTest): self.autostop_timer_widget_hidden(self.gui.receive_mode) self.server_timed_out(self.gui.receive_mode, 15000) self.web_server_is_stopped() + + def run_all_clear_all_button_tests(self, public_mode): + """Test the Clear All history button""" + self.run_all_receive_mode_setup_tests(public_mode) + self.upload_file(public_mode, '/tmp/test.txt', 'test.txt') + self.history_widgets_present(self.gui.receive_mode) + self.clear_all_history_items(self.gui.receive_mode, 0) + self.upload_file(public_mode, '/tmp/test.txt', 'test.txt') + self.clear_all_history_items(self.gui.receive_mode, 2) diff --git a/tests/GuiShareTest.py b/tests/GuiShareTest.py index 038f052b..6925defa 100644 --- a/tests/GuiShareTest.py +++ b/tests/GuiShareTest.py @@ -196,6 +196,15 @@ class GuiShareTest(GuiBaseTest): self.run_all_share_mode_started_tests(public_mode) self.run_all_share_mode_download_tests(public_mode, stay_open) + def run_all_clear_all_button_tests(self, public_mode, stay_open): + """Test the Clear All history button""" + self.run_all_share_mode_setup_tests() + self.run_all_share_mode_started_tests(public_mode) + self.individual_file_is_viewable_or_not(public_mode, stay_open) + self.history_widgets_present(self.gui.share_mode) + self.clear_all_history_items(self.gui.share_mode, 0) + self.individual_file_is_viewable_or_not(public_mode, stay_open) + self.clear_all_history_items(self.gui.share_mode, 2) def run_all_share_mode_individual_file_tests(self, public_mode, stay_open): """Tests in share mode when viewing an individual file""" diff --git a/tests/local_onionshare_receive_mode_clear_all_button_test.py b/tests/local_onionshare_receive_mode_clear_all_button_test.py new file mode 100644 index 00000000..f93d4fe1 --- /dev/null +++ b/tests/local_onionshare_receive_mode_clear_all_button_test.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiReceiveTest import GuiReceiveTest + +class LocalReceiveModeClearAllButtonTest(unittest.TestCase, GuiReceiveTest): + @classmethod + def setUpClass(cls): + test_settings = { + } + cls.gui = GuiReceiveTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiReceiveTest.tear_down() + + @pytest.mark.gui + @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_clear_all_button_tests(False) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/local_onionshare_share_mode_clear_all_button_test.py b/tests/local_onionshare_share_mode_clear_all_button_test.py new file mode 100644 index 00000000..caed342d --- /dev/null +++ b/tests/local_onionshare_share_mode_clear_all_button_test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +import pytest +import unittest + +from .GuiShareTest import GuiShareTest + +class LocalShareModeClearAllButtonTest(unittest.TestCase, GuiShareTest): + @classmethod + def setUpClass(cls): + test_settings = { + "close_after_first_download": False, + } + cls.gui = GuiShareTest.set_up(test_settings) + + @classmethod + def tearDownClass(cls): + GuiShareTest.tear_down() + + @pytest.mark.gui + @pytest.mark.skipif(pytest.__version__ < '2.9', reason="requires newer pytest") + def test_gui(self): + self.run_all_common_setup_tests() + self.run_all_clear_all_button_tests(False, True) + +if __name__ == "__main__": + unittest.main() From f908a1f3839c200bd47a2cef4d2a62cbc3c3c39e Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 9 Sep 2019 16:43:09 +1000 Subject: [PATCH 46/48] remove unnecessary import of IndividualFileHistoryItem from share_mode/__init__.py --- onionshare_gui/mode/share_mode/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onionshare_gui/mode/share_mode/__init__.py b/onionshare_gui/mode/share_mode/__init__.py index 35a2045d..28b439af 100644 --- a/onionshare_gui/mode/share_mode/__init__.py +++ b/onionshare_gui/mode/share_mode/__init__.py @@ -28,7 +28,7 @@ from onionshare.web import Web from ..file_selection import FileSelection from .threads import CompressThread from .. import Mode -from ..history import History, ToggleHistory, ShareHistoryItem, IndividualFileHistoryItem +from ..history import History, ToggleHistory, ShareHistoryItem from ...widgets import Alert From 36fdd3f1d515167e41d82c4805cbfcedb30df8e2 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 9 Sep 2019 17:22:18 +1000 Subject: [PATCH 47/48] Ensure we increment and return the history_id when throwing error404() in website mode. Add a route for /favicon.ico unless we are in website mode (website might have its own favicon) --- onionshare/web/web.py | 7 ++++++- onionshare/web/website_mode.py | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/onionshare/web/web.py b/onionshare/web/web.py index 2b0d2812..ca63e520 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -10,7 +10,7 @@ from distutils.version import LooseVersion as Version from urllib.request import urlopen import flask -from flask import Flask, request, render_template, abort, make_response, __version__ as flask_version +from flask import Flask, request, render_template, abort, make_response, send_file, __version__ as flask_version from flask_httpauth import HTTPBasicAuth from .. import strings @@ -178,6 +178,11 @@ class Web: return "" abort(404) + if self.mode != 'website': + @self.app.route("/favicon.ico") + def favicon(): + return send_file('{}/img/favicon.ico'.format(self.common.get_resource_path('static'))) + def error401(self): auth = request.authorization if auth: diff --git a/onionshare/web/website_mode.py b/onionshare/web/website_mode.py index 55e5c1d4..0b7602ea 100644 --- a/onionshare/web/website_mode.py +++ b/onionshare/web/website_mode.py @@ -70,7 +70,9 @@ class WebsiteModeWeb(SendBaseModeWeb): # If it's not a directory or file, throw a 404 else: - return self.web.error404() + history_id = self.cur_history_id + self.cur_history_id += 1 + return self.web.error404(history_id) else: # Special case loading / @@ -87,4 +89,6 @@ class WebsiteModeWeb(SendBaseModeWeb): else: # If the path isn't found, throw a 404 - return self.web.error404() + history_id = self.cur_history_id + self.cur_history_id += 1 + return self.web.error404(history_id) From d2b3f0c2edbf7dd4474a772aabef4d6f187892b5 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 15 Sep 2019 14:46:29 -0700 Subject: [PATCH 48/48] Allow 404 errors to work in receive mode --- onionshare/web/receive_mode.py | 1 + onionshare/web/web.py | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/onionshare/web/receive_mode.py b/onionshare/web/receive_mode.py index 8604a889..83040683 100644 --- a/onionshare/web/receive_mode.py +++ b/onionshare/web/receive_mode.py @@ -21,6 +21,7 @@ class ReceiveModeWeb: self.can_upload = True self.uploads_in_progress = [] + # This tracks the history id self.cur_history_id = 0 self.define_routes() diff --git a/onionshare/web/web.py b/onionshare/web/web.py index ca63e520..ecd9edc2 100644 --- a/onionshare/web/web.py +++ b/onionshare/web/web.py @@ -119,12 +119,23 @@ class Web: # Create the mode web object, which defines its own routes self.share_mode = None self.receive_mode = None - if self.mode == 'receive': + self.website_mode = None + if self.mode == 'share': + self.share_mode = ShareModeWeb(self.common, self) + elif self.mode == 'receive': self.receive_mode = ReceiveModeWeb(self.common, self) elif self.mode == 'website': self.website_mode = WebsiteModeWeb(self.common, self) - elif self.mode == 'share': - self.share_mode = ShareModeWeb(self.common, self) + + def get_mode(self): + if self.mode == 'share': + return self.share_mode + elif self.mode == 'receive': + return self.receive_mode + elif self.mode == 'website': + return self.website_mode + else: + return None def generate_static_url_path(self): # The static URL path has a 128-bit random number in it to avoid having name @@ -166,7 +177,10 @@ class Web: @self.app.errorhandler(404) def not_found(e): - return self.error404() + mode = self.get_mode() + history_id = mode.cur_history_id + mode.cur_history_id += 1 + return self.error404(history_id) @self.app.route("//shutdown") def shutdown(password_candidate):