Generate a new static_url_path each time the server is stopped and started again

This commit is contained in:
Micah Lee 2019-05-29 18:21:53 -07:00
parent c53ecb0a03
commit 50b2311409
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
2 changed files with 16 additions and 5 deletions

View File

@ -51,16 +51,12 @@ class Web(object):
self.common = common self.common = common
self.common.log('Web', '__init__', 'is_gui={}, mode={}'.format(is_gui, mode)) 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 # The flask app
self.app = Flask(__name__, self.app = Flask(__name__,
static_url_path=self.static_url_path,
static_folder=self.common.get_resource_path('static'), static_folder=self.common.get_resource_path('static'),
template_folder=self.common.get_resource_path('templates')) template_folder=self.common.get_resource_path('templates'))
self.app.secret_key = self.common.random_string(8) self.app.secret_key = self.common.random_string(8)
self.generate_static_url_path()
self.auth = HTTPBasicAuth() self.auth = HTTPBasicAuth()
self.auth.error_handler(self.error401) self.auth.error_handler(self.error401)
@ -238,6 +234,18 @@ class Web(object):
self.password = self.common.build_password() self.password = self.common.build_password()
self.common.log('Web', 'generate_password', 'built random password: "{}"'.format(self.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 + '/<path:filename>',
endpoint='static', view_func=self.app.send_static_file)
def verbose_mode(self): def verbose_mode(self):
""" """
Turn on verbose mode, which will log flask errors to a file. Turn on verbose mode, which will log flask errors to a file.

View File

@ -42,6 +42,9 @@ class OnionThread(QtCore.QThread):
def run(self): def run(self):
self.mode.common.log('OnionThread', 'run') 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 # 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') self.mode.app.stay_open = not self.mode.common.settings.get('close_after_first_download')
if not self.mode.app.port: if not self.mode.app.port: