2018-09-20 23:58:27 -07:00
|
|
|
import os
|
2018-09-20 23:43:04 -07:00
|
|
|
import tempfile
|
|
|
|
from datetime import datetime
|
2018-09-20 23:58:27 -07:00
|
|
|
from flask import Request, request, render_template, make_response, flash, redirect
|
|
|
|
from werkzeug.utils import secure_filename
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
from .. import strings
|
|
|
|
|
|
|
|
|
2018-09-21 11:14:32 -07:00
|
|
|
class ReceiveModeWeb(object):
|
2018-09-20 23:58:27 -07:00
|
|
|
"""
|
2018-09-21 11:14:32 -07:00
|
|
|
All of the web logic for receive mode
|
2018-09-20 23:58:27 -07:00
|
|
|
"""
|
2018-09-21 11:41:49 -07:00
|
|
|
def __init__(self, common, web):
|
|
|
|
self.common = common
|
|
|
|
self.common.log('ReceiveModeWeb', '__init__')
|
|
|
|
|
2018-09-21 11:14:32 -07:00
|
|
|
self.web = web
|
2018-09-21 11:36:19 -07:00
|
|
|
|
2018-10-01 16:42:54 +10:00
|
|
|
self.can_upload = True
|
2018-09-21 11:36:19 -07:00
|
|
|
self.upload_count = 0
|
2018-10-02 15:41:29 +10:00
|
|
|
self.uploads_in_progress = []
|
2018-09-21 11:36:19 -07:00
|
|
|
|
2018-09-21 11:14:32 -07:00
|
|
|
self.define_routes()
|
|
|
|
|
|
|
|
def define_routes(self):
|
2018-09-20 23:58:27 -07:00
|
|
|
"""
|
2018-09-21 11:14:32 -07:00
|
|
|
The web app routes for receiving files
|
2018-09-20 23:58:27 -07:00
|
|
|
"""
|
2018-09-21 11:14:32 -07:00
|
|
|
def index_logic():
|
|
|
|
self.web.add_request(self.web.REQUEST_LOAD, request.path)
|
|
|
|
|
2018-09-21 11:41:49 -07:00
|
|
|
if self.common.settings.get('public_mode'):
|
2018-09-21 11:14:32 -07:00
|
|
|
upload_action = '/upload'
|
|
|
|
else:
|
|
|
|
upload_action = '/{}/upload'.format(self.web.slug)
|
|
|
|
|
|
|
|
r = make_response(render_template(
|
|
|
|
'receive.html',
|
2018-10-01 16:42:54 +10:00
|
|
|
upload_action=upload_action))
|
2018-09-21 11:14:32 -07:00
|
|
|
return self.web.add_security_headers(r)
|
|
|
|
|
|
|
|
@self.web.app.route("/<slug_candidate>")
|
|
|
|
def index(slug_candidate):
|
2018-10-01 16:42:54 +10:00
|
|
|
if not self.can_upload:
|
|
|
|
return self.web.error403()
|
2018-09-21 11:14:32 -07:00
|
|
|
self.web.check_slug_candidate(slug_candidate)
|
|
|
|
return index_logic()
|
|
|
|
|
|
|
|
@self.web.app.route("/")
|
|
|
|
def index_public():
|
2018-10-01 16:42:54 +10:00
|
|
|
if not self.can_upload:
|
|
|
|
return self.web.error403()
|
2018-09-21 11:41:49 -07:00
|
|
|
if not self.common.settings.get('public_mode'):
|
2018-09-21 11:14:32 -07:00
|
|
|
return self.web.error404()
|
|
|
|
return index_logic()
|
|
|
|
|
|
|
|
|
|
|
|
def upload_logic(slug_candidate=''):
|
|
|
|
"""
|
2019-02-11 22:46:39 -08:00
|
|
|
Handle the upload files POST request, though at this point, the files have
|
|
|
|
already been uploaded and saved to their correct locations.
|
2018-09-21 11:14:32 -07:00
|
|
|
"""
|
|
|
|
files = request.files.getlist('file[]')
|
|
|
|
filenames = []
|
|
|
|
for f in files:
|
|
|
|
if f.filename != '':
|
|
|
|
filename = secure_filename(f.filename)
|
|
|
|
filenames.append(filename)
|
2019-02-11 22:46:39 -08:00
|
|
|
local_path = os.path.join(request.receive_mode_dir, filename)
|
2018-09-21 11:14:32 -07:00
|
|
|
basename = os.path.basename(local_path)
|
|
|
|
|
2018-10-25 21:38:20 -07:00
|
|
|
# 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,
|
|
|
|
'filename': basename,
|
2019-02-11 22:46:39 -08:00
|
|
|
'dir': request.receive_mode_dir
|
2018-10-25 21:38:20 -07:00
|
|
|
})
|
|
|
|
|
2018-09-21 11:41:49 -07:00
|
|
|
self.common.log('ReceiveModeWeb', 'define_routes', '/upload, uploaded {}, saving to {}'.format(f.filename, local_path))
|
2018-09-21 11:14:32 -07:00
|
|
|
print(strings._('receive_mode_received_file').format(local_path))
|
|
|
|
|
2019-02-11 22:46:39 -08:00
|
|
|
if request.upload_error:
|
|
|
|
self.common.log('ReceiveModeWeb', 'define_routes', '/upload, there was an upload error')
|
2019-02-11 23:23:31 -08:00
|
|
|
|
|
|
|
self.web.add_request(self.web.REQUEST_ERROR_DATA_DIR_CANNOT_CREATE, request.path, {
|
|
|
|
"receive_mode_dir": request.receive_mode_dir
|
|
|
|
})
|
|
|
|
print(strings._('error_cannot_create_data_dir').format(request.receive_mode_dir))
|
|
|
|
|
2019-02-11 22:46:39 -08:00
|
|
|
flash('Error uploading, please inform the OnionShare user', 'error')
|
2019-02-11 23:23:31 -08:00
|
|
|
|
2019-02-11 22:46:39 -08:00
|
|
|
if self.common.settings.get('public_mode'):
|
|
|
|
return redirect('/')
|
|
|
|
else:
|
|
|
|
return redirect('/{}'.format(slug_candidate))
|
|
|
|
|
|
|
|
# Note that flash strings are in English, and not translated, on purpose,
|
2018-09-21 11:14:32 -07:00
|
|
|
# to avoid leaking the locale of the OnionShare user
|
|
|
|
if len(filenames) == 0:
|
|
|
|
flash('No files uploaded', 'info')
|
|
|
|
else:
|
|
|
|
for filename in filenames:
|
|
|
|
flash('Sent {}'.format(filename), 'info')
|
|
|
|
|
2018-10-01 18:42:53 +10:00
|
|
|
if self.can_upload:
|
|
|
|
if self.common.settings.get('public_mode'):
|
|
|
|
path = '/'
|
|
|
|
else:
|
|
|
|
path = '/{}'.format(slug_candidate)
|
|
|
|
|
|
|
|
return redirect('{}'.format(path))
|
2018-09-20 23:58:27 -07:00
|
|
|
else:
|
2018-10-01 18:42:53 +10:00
|
|
|
# It was the last upload and the timer ran out
|
|
|
|
if self.common.settings.get('public_mode'):
|
|
|
|
return thankyou_logic(slug_candidate)
|
|
|
|
else:
|
|
|
|
return thankyou_logic()
|
|
|
|
|
|
|
|
def thankyou_logic(slug_candidate=''):
|
|
|
|
r = make_response(render_template(
|
|
|
|
'thankyou.html'))
|
|
|
|
return self.web.add_security_headers(r)
|
2018-09-20 23:58:27 -07:00
|
|
|
|
2018-09-21 11:14:32 -07:00
|
|
|
@self.web.app.route("/<slug_candidate>/upload", methods=['POST'])
|
|
|
|
def upload(slug_candidate):
|
2018-10-01 16:42:54 +10:00
|
|
|
if not self.can_upload:
|
|
|
|
return self.web.error403()
|
2018-09-21 11:14:32 -07:00
|
|
|
self.web.check_slug_candidate(slug_candidate)
|
|
|
|
return upload_logic(slug_candidate)
|
|
|
|
|
|
|
|
@self.web.app.route("/upload", methods=['POST'])
|
|
|
|
def upload_public():
|
2018-10-01 16:42:54 +10:00
|
|
|
if not self.can_upload:
|
|
|
|
return self.web.error403()
|
2018-09-21 11:41:49 -07:00
|
|
|
if not self.common.settings.get('public_mode'):
|
2018-09-21 11:14:32 -07:00
|
|
|
return self.web.error404()
|
|
|
|
return upload_logic()
|
|
|
|
|
|
|
|
|
2018-09-20 23:43:04 -07:00
|
|
|
class ReceiveModeWSGIMiddleware(object):
|
|
|
|
"""
|
|
|
|
Custom WSGI middleware in order to attach the Web object to environ, so
|
|
|
|
ReceiveModeRequest can access it.
|
|
|
|
"""
|
|
|
|
def __init__(self, app, web):
|
|
|
|
self.app = app
|
|
|
|
self.web = web
|
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
environ['web'] = self.web
|
2019-01-20 15:25:36 -08:00
|
|
|
environ['stop_q'] = self.web.stop_q
|
2018-09-20 23:43:04 -07:00
|
|
|
return self.app(environ, start_response)
|
|
|
|
|
|
|
|
|
2019-02-11 22:46:39 -08:00
|
|
|
class ReceiveModeFile(object):
|
2018-09-20 23:43:04 -07:00
|
|
|
"""
|
2019-02-11 22:46:39 -08:00
|
|
|
A custom file object that tells ReceiveModeRequest every time data gets
|
|
|
|
written to it, in order to track the progress of uploads. It starts out with
|
|
|
|
a .part file extension, and when it's complete it removes that extension.
|
2018-09-20 23:43:04 -07:00
|
|
|
"""
|
2019-01-20 15:25:36 -08:00
|
|
|
def __init__(self, request, filename, write_func, close_func):
|
|
|
|
self.onionshare_request = request
|
2018-09-20 23:43:04 -07:00
|
|
|
self.onionshare_filename = filename
|
|
|
|
self.onionshare_write_func = write_func
|
|
|
|
self.onionshare_close_func = close_func
|
|
|
|
|
2019-02-11 23:05:51 -08:00
|
|
|
self.filename = os.path.join(self.onionshare_request.receive_mode_dir, filename)
|
2019-02-11 22:46:39 -08:00
|
|
|
self.filename_in_progress = '{}.part'.format(self.filename)
|
|
|
|
|
|
|
|
# Open the file
|
2019-02-11 23:05:51 -08:00
|
|
|
self.upload_error = False
|
2019-02-11 22:46:39 -08:00
|
|
|
try:
|
|
|
|
self.f = open(self.filename_in_progress, 'wb+')
|
|
|
|
except:
|
|
|
|
# This will only happen if someone is messaging with the data dir while
|
|
|
|
# OnionShare is running, but if it does make sure to throw an error
|
|
|
|
self.upload_error = True
|
|
|
|
self.f = tempfile.TemporaryFile('wb+')
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
# Make all the file-like methods and attributes actually access the
|
|
|
|
# TemporaryFile, except for write
|
|
|
|
attrs = ['closed', 'detach', 'fileno', 'flush', 'isatty', 'mode',
|
|
|
|
'name', 'peek', 'raw', 'read', 'read1', 'readable', 'readinto',
|
|
|
|
'readinto1', 'readline', 'readlines', 'seek', 'seekable', 'tell',
|
|
|
|
'truncate', 'writable', 'writelines']
|
|
|
|
for attr in attrs:
|
|
|
|
setattr(self, attr, getattr(self.f, attr))
|
|
|
|
|
|
|
|
def write(self, b):
|
|
|
|
"""
|
|
|
|
Custom write method that calls out to onionshare_write_func
|
|
|
|
"""
|
2019-02-11 22:46:39 -08:00
|
|
|
if self.upload_error or (not self.onionshare_request.stop_q.empty()):
|
2019-01-20 15:25:36 -08:00
|
|
|
self.close()
|
|
|
|
self.onionshare_request.close()
|
|
|
|
return
|
|
|
|
|
2018-09-20 23:43:04 -07:00
|
|
|
bytes_written = self.f.write(b)
|
|
|
|
self.onionshare_write_func(self.onionshare_filename, bytes_written)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Custom close method that calls out to onionshare_close_func
|
|
|
|
"""
|
|
|
|
self.f.close()
|
2019-02-11 22:46:39 -08:00
|
|
|
|
|
|
|
if not self.upload_error:
|
|
|
|
# Rename the in progress file to the final filename
|
|
|
|
os.rename(self.filename_in_progress, self.filename)
|
|
|
|
|
2018-09-20 23:43:04 -07:00
|
|
|
self.onionshare_close_func(self.onionshare_filename)
|
|
|
|
|
|
|
|
|
|
|
|
class ReceiveModeRequest(Request):
|
|
|
|
"""
|
|
|
|
A custom flask Request object that keeps track of how much data has been
|
|
|
|
uploaded for each file, for receive mode.
|
|
|
|
"""
|
|
|
|
def __init__(self, environ, populate_request=True, shallow=False):
|
|
|
|
super(ReceiveModeRequest, self).__init__(environ, populate_request, shallow)
|
|
|
|
self.web = environ['web']
|
2019-01-20 15:25:36 -08:00
|
|
|
self.stop_q = environ['stop_q']
|
|
|
|
|
|
|
|
self.web.common.log('ReceiveModeRequest', '__init__')
|
|
|
|
|
|
|
|
# Prevent running the close() method more than once
|
|
|
|
self.closed = False
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
# Is this a valid upload request?
|
|
|
|
self.upload_request = False
|
|
|
|
if self.method == 'POST':
|
|
|
|
if self.path == '/{}/upload'.format(self.web.slug):
|
|
|
|
self.upload_request = True
|
|
|
|
else:
|
|
|
|
if self.web.common.settings.get('public_mode'):
|
|
|
|
if self.path == '/upload':
|
|
|
|
self.upload_request = True
|
|
|
|
|
2018-10-02 08:22:08 +10:00
|
|
|
if self.upload_request:
|
2019-02-11 22:46:39 -08:00
|
|
|
# No errors yet
|
|
|
|
self.upload_error = False
|
|
|
|
|
|
|
|
# Figure out what files should be saved
|
|
|
|
now = datetime.now()
|
|
|
|
date_dir = now.strftime("%Y-%m-%d")
|
|
|
|
time_dir = now.strftime("%H.%M.%S")
|
|
|
|
self.receive_mode_dir = os.path.join(self.web.common.settings.get('data_dir'), date_dir, time_dir)
|
|
|
|
|
|
|
|
# Create that directory, which shouldn't exist yet
|
|
|
|
try:
|
|
|
|
os.makedirs(self.receive_mode_dir, 0o700, exist_ok=False)
|
|
|
|
except OSError:
|
|
|
|
# If this directory already exists, maybe someone else is uploading files at
|
|
|
|
# the same second, so use a different name in that case
|
|
|
|
if os.path.exists(self.receive_mode_dir):
|
|
|
|
# Keep going until we find a directory name that's available
|
|
|
|
i = 1
|
|
|
|
while True:
|
|
|
|
new_receive_mode_dir = '{}-{}'.format(self.receive_mode_dir, i)
|
|
|
|
try:
|
|
|
|
os.makedirs(new_receive_mode_dir, 0o700, exist_ok=False)
|
|
|
|
break
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
i += 1
|
|
|
|
# Failsafe
|
|
|
|
if i == 100:
|
|
|
|
self.web.common.log('ReceiveModeRequest', '__init__', 'Error finding available receive mode directory')
|
|
|
|
self.upload_error = True
|
|
|
|
break
|
|
|
|
except PermissionError:
|
|
|
|
self.web.add_request(self.web.REQUEST_ERROR_DATA_DIR_CANNOT_CREATE, request.path, {
|
|
|
|
"receive_mode_dir": self.receive_mode_dir
|
|
|
|
})
|
|
|
|
print(strings._('error_cannot_create_data_dir').format(self.receive_mode_dir))
|
|
|
|
self.web.common.log('ReceiveModeRequest', '__init__', 'Permission denied creating receive mode directory')
|
|
|
|
self.upload_error = True
|
|
|
|
|
|
|
|
# If there's an error so far, finish early
|
|
|
|
if self.upload_error:
|
|
|
|
return
|
|
|
|
|
2018-09-20 23:43:04 -07:00
|
|
|
# A dictionary that maps filenames to the bytes uploaded so far
|
|
|
|
self.progress = {}
|
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
# Prevent new uploads if we've said so (timer expired)
|
|
|
|
if self.web.receive_mode.can_upload:
|
2018-09-20 23:43:04 -07:00
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
# Create an upload_id, attach it to the request
|
|
|
|
self.upload_id = self.web.receive_mode.upload_count
|
2018-09-20 23:43:04 -07:00
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
self.web.receive_mode.upload_count += 1
|
2018-09-20 23:43:04 -07:00
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
# Figure out the content length
|
|
|
|
try:
|
|
|
|
self.content_length = int(self.headers['Content-Length'])
|
|
|
|
except:
|
|
|
|
self.content_length = 0
|
2018-10-01 16:42:54 +10:00
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
print("{}: {}".format(
|
|
|
|
datetime.now().strftime("%b %d, %I:%M%p"),
|
|
|
|
strings._("receive_mode_upload_starting").format(self.web.common.human_readable_filesize(self.content_length))
|
|
|
|
))
|
2018-10-01 16:42:54 +10:00
|
|
|
|
2019-01-20 16:20:36 -08:00
|
|
|
# Don't tell the GUI that a request has started until we start receiving files
|
|
|
|
self.told_gui_about_request = False
|
2018-11-13 14:42:26 +11:00
|
|
|
|
|
|
|
self.previous_file = None
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=None):
|
|
|
|
"""
|
|
|
|
This gets called for each file that gets uploaded, and returns an file-like
|
|
|
|
writable stream.
|
|
|
|
"""
|
|
|
|
if self.upload_request:
|
2019-01-20 16:20:36 -08:00
|
|
|
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,
|
|
|
|
'content_length': self.content_length
|
|
|
|
})
|
2019-01-21 11:16:55 -08:00
|
|
|
self.web.receive_mode.uploads_in_progress.append(self.upload_id)
|
|
|
|
|
2019-01-20 16:20:36 -08:00
|
|
|
self.told_gui_about_request = True
|
|
|
|
|
2019-02-11 23:05:51 -08:00
|
|
|
filename = secure_filename(filename)
|
|
|
|
|
2018-09-20 23:43:04 -07:00
|
|
|
self.progress[filename] = {
|
|
|
|
'uploaded_bytes': 0,
|
|
|
|
'complete': False
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:46:39 -08:00
|
|
|
f = ReceiveModeFile(self, filename, self.file_write_func, self.file_close_func)
|
|
|
|
if f.upload_error:
|
|
|
|
self.web.common.log('ReceiveModeRequest', '_get_file_stream', 'Error creating file')
|
|
|
|
self.upload_error = True
|
|
|
|
return f
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""
|
|
|
|
Closing the request.
|
|
|
|
"""
|
|
|
|
super(ReceiveModeRequest, self).close()
|
2019-01-20 15:25:36 -08:00
|
|
|
|
|
|
|
# Prevent calling this method more than once per request
|
|
|
|
if self.closed:
|
|
|
|
return
|
|
|
|
self.closed = True
|
|
|
|
|
|
|
|
self.web.common.log('ReceiveModeRequest', 'close')
|
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
try:
|
2019-01-21 11:16:55 -08:00
|
|
|
if self.told_gui_about_request:
|
|
|
|
upload_id = self.upload_id
|
2019-01-20 15:25:36 -08:00
|
|
|
|
2019-01-21 17:32:58 -08:00
|
|
|
if not self.web.stop_q.empty():
|
|
|
|
# Inform the GUI that the upload has canceled
|
|
|
|
self.web.add_request(self.web.REQUEST_UPLOAD_CANCELED, self.path, {
|
|
|
|
'id': upload_id
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
# Inform the GUI that the upload has finished
|
|
|
|
self.web.add_request(self.web.REQUEST_UPLOAD_FINISHED, self.path, {
|
|
|
|
'id': upload_id
|
|
|
|
})
|
2019-01-21 11:16:55 -08:00
|
|
|
self.web.receive_mode.uploads_in_progress.remove(upload_id)
|
2019-01-21 17:32:58 -08:00
|
|
|
|
2018-11-13 14:42:26 +11:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
def file_write_func(self, filename, length):
|
|
|
|
"""
|
|
|
|
This function gets called when a specific file is written to.
|
|
|
|
"""
|
2019-01-21 10:56:31 -08:00
|
|
|
if self.closed:
|
|
|
|
return
|
|
|
|
|
2018-09-20 23:43:04 -07:00
|
|
|
if self.upload_request:
|
|
|
|
self.progress[filename]['uploaded_bytes'] += length
|
|
|
|
|
|
|
|
if self.previous_file != filename:
|
|
|
|
self.previous_file = filename
|
|
|
|
|
|
|
|
print('\r=> {:15s} {}'.format(
|
|
|
|
self.web.common.human_readable_filesize(self.progress[filename]['uploaded_bytes']),
|
|
|
|
filename
|
|
|
|
), end='')
|
|
|
|
|
|
|
|
# Update the GUI on the upload progress
|
2019-01-21 11:16:55 -08:00
|
|
|
if self.told_gui_about_request:
|
|
|
|
self.web.add_request(self.web.REQUEST_PROGRESS, self.path, {
|
|
|
|
'id': self.upload_id,
|
|
|
|
'progress': self.progress
|
|
|
|
})
|
2018-09-20 23:43:04 -07:00
|
|
|
|
|
|
|
def file_close_func(self, filename):
|
|
|
|
"""
|
|
|
|
This function gets called when a specific file is closed.
|
|
|
|
"""
|
|
|
|
self.progress[filename]['complete'] = True
|