2020-08-27 19:13:08 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
|
|
|
Copyright (C) 2014-2020 Micah Lee, et al. <micah@micahflee.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2019-06-05 07:47:41 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import mimetypes
|
2019-09-02 22:45:14 -04:00
|
|
|
import gzip
|
2019-09-01 18:44:44 -04:00
|
|
|
from flask import Response, request, render_template, make_response
|
2019-06-05 07:47:41 -04:00
|
|
|
|
2019-09-01 18:44:44 -04:00
|
|
|
|
2019-09-01 18:05:53 -04:00
|
|
|
class SendBaseModeWeb:
|
2019-06-05 07:47:41 -04:00
|
|
|
"""
|
2019-09-01 18:05:53 -04:00
|
|
|
All of the web logic shared between share and website mode (modes where the user sends files)
|
2019-06-05 07:47:41 -04:00
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2019-06-05 07:47:41 -04:00
|
|
|
def __init__(self, common, web):
|
2019-09-01 18:05:53 -04:00
|
|
|
super(SendBaseModeWeb, self).__init__()
|
2019-06-05 07:47:41 -04:00
|
|
|
self.common = common
|
|
|
|
self.web = web
|
|
|
|
|
|
|
|
# Information about the file to be shared
|
|
|
|
self.is_zipped = False
|
|
|
|
self.download_filename = None
|
|
|
|
self.download_filesize = None
|
|
|
|
self.gzip_filename = None
|
|
|
|
self.gzip_filesize = None
|
|
|
|
self.zip_writer = None
|
|
|
|
|
2019-11-02 18:37:21 -04:00
|
|
|
# If autostop_sharing, only allow one download at a time
|
2019-06-05 07:47:41 -04:00
|
|
|
self.download_in_progress = False
|
|
|
|
|
2019-09-09 02:35:05 -04:00
|
|
|
# This tracks the history id
|
|
|
|
self.cur_history_id = 0
|
|
|
|
|
2019-06-13 06:33:34 -04:00
|
|
|
self.define_routes()
|
2019-09-01 23:36:30 -04:00
|
|
|
self.init()
|
2019-06-05 07:47:41 -04:00
|
|
|
|
2019-06-13 06:33:34 -04:00
|
|
|
def set_file_info(self, filenames, processed_size_callback=None):
|
|
|
|
"""
|
|
|
|
Build a data structure that describes the list of files
|
|
|
|
"""
|
2019-06-14 12:21:12 -04:00
|
|
|
# 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]):
|
2019-10-13 00:01:25 -04:00
|
|
|
filenames = [
|
|
|
|
os.path.join(filenames[0], x) for x in os.listdir(filenames[0])
|
|
|
|
]
|
2019-06-13 06:33:34 -04:00
|
|
|
|
2019-09-01 23:36:30 -04:00
|
|
|
# Re-initialize
|
2019-10-13 00:01:25 -04:00
|
|
|
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
|
2019-09-03 23:52:49 -04:00
|
|
|
self.cleanup_filenames = []
|
2019-09-04 01:18:30 -04:00
|
|
|
self.cur_history_id = 0
|
2019-10-13 00:01:25 -04:00
|
|
|
self.file_info = {"files": [], "dirs": []}
|
2019-09-03 23:52:49 -04:00
|
|
|
self.gzip_individual_files = {}
|
2019-09-01 23:36:30 -04:00
|
|
|
self.init()
|
|
|
|
|
2019-09-01 23:53:21 -04:00
|
|
|
# Build the file list
|
2019-06-14 12:21:12 -04:00
|
|
|
for filename in filenames:
|
2019-10-13 00:01:25 -04:00
|
|
|
basename = os.path.basename(filename.rstrip("/"))
|
2019-06-14 12:21:12 -04:00
|
|
|
|
|
|
|
# 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):
|
2019-10-13 00:01:25 -04:00
|
|
|
self.root_files[basename + "/"] = filename
|
2019-06-14 12:21:12 -04:00
|
|
|
|
|
|
|
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"
|
2019-10-13 00:01:25 -04:00
|
|
|
normalized_root = os.path.join(
|
|
|
|
basename, root[len(filename) :].lstrip("/")
|
|
|
|
).rstrip("/")
|
2019-06-14 12:21:12 -04:00
|
|
|
|
|
|
|
# Add the dir itself
|
2019-10-13 00:01:25 -04:00
|
|
|
self.files[normalized_root + "/"] = root
|
2019-06-14 12:21:12 -04:00
|
|
|
|
|
|
|
# Add the files in this dir
|
|
|
|
for nested_filename in nested_filenames:
|
2019-10-13 00:01:25 -04:00
|
|
|
self.files[
|
|
|
|
os.path.join(normalized_root, nested_filename)
|
|
|
|
] = os.path.join(root, nested_filename)
|
2019-06-14 12:21:12 -04:00
|
|
|
|
2019-09-01 23:53:21 -04:00
|
|
|
self.set_file_info_custom(filenames, processed_size_callback)
|
2019-06-14 12:21:12 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def directory_listing(self, filenames, path="", filesystem_path=None):
|
2019-09-04 00:59:49 -04:00
|
|
|
# Tell the GUI about the directory listing
|
2019-09-04 01:18:30 -04:00
|
|
|
history_id = self.cur_history_id
|
|
|
|
self.cur_history_id += 1
|
2019-10-13 00:01:25 -04:00
|
|
|
self.web.add_request(
|
|
|
|
self.web.REQUEST_INDIVIDUAL_FILE_STARTED,
|
2019-10-20 13:15:16 -04:00
|
|
|
f"/{path}",
|
2019-10-13 00:01:25 -04:00
|
|
|
{"id": history_id, "method": request.method, "status_code": 200},
|
|
|
|
)
|
|
|
|
|
|
|
|
breadcrumbs = [("☗", "/")]
|
|
|
|
parts = path.split("/")[:-1]
|
2019-09-15 19:44:24 -04:00
|
|
|
for i in range(len(parts)):
|
2019-10-20 13:15:16 -04:00
|
|
|
breadcrumbs.append((parts[i], f"/{'/'.join(parts[0 : i + 1])}/"))
|
2019-09-15 19:44:24 -04:00
|
|
|
breadcrumbs_leaf = breadcrumbs.pop()[0]
|
|
|
|
|
2019-09-03 23:52:49 -04:00
|
|
|
# If filesystem_path is None, this is the root directory listing
|
|
|
|
files, dirs = self.build_directory_listing(filenames, filesystem_path)
|
2019-10-13 00:01:25 -04:00
|
|
|
r = self.directory_listing_template(
|
|
|
|
path, files, dirs, breadcrumbs, breadcrumbs_leaf
|
|
|
|
)
|
2019-09-03 23:52:49 -04:00
|
|
|
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:
|
2019-10-13 00:01:25 -04:00
|
|
|
dirs.append({"basename": filename})
|
2019-09-03 23:52:49 -04:00
|
|
|
else:
|
|
|
|
size = os.path.getsize(this_filesystem_path)
|
|
|
|
size_human = self.common.human_readable_filesize(size)
|
2019-10-13 00:01:25 -04:00
|
|
|
files.append({"basename": filename, "size_human": size_human})
|
2019-09-03 23:52:49 -04:00
|
|
|
return files, dirs
|
2019-09-02 22:45:14 -04:00
|
|
|
|
|
|
|
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:
|
2019-10-13 00:01:25 -04:00
|
|
|
gzip_filename = tempfile.mkstemp("wb+")[1]
|
2019-09-02 22:45:14 -04:00
|
|
|
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)
|
|
|
|
|
2019-09-04 00:46:32 -04:00
|
|
|
path = request.path
|
|
|
|
|
|
|
|
# Tell GUI the individual file started
|
2019-09-04 01:18:30 -04:00
|
|
|
history_id = self.cur_history_id
|
|
|
|
self.cur_history_id += 1
|
2019-10-18 23:50:40 -04:00
|
|
|
|
|
|
|
# Only GET requests are allowed, any other method should fail
|
|
|
|
if request.method != "GET":
|
|
|
|
return self.web.error405(history_id)
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
self.web.add_request(
|
|
|
|
self.web.REQUEST_INDIVIDUAL_FILE_STARTED,
|
|
|
|
path,
|
|
|
|
{"id": history_id, "filesize": filesize},
|
|
|
|
)
|
2019-09-04 00:46:32 -04:00
|
|
|
|
2019-09-02 22:45:14 -04:00
|
|
|
def generate():
|
|
|
|
chunk_size = 102400 # 100kb
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
fp = open(file_to_download, "rb")
|
2019-09-02 22:45:14 -04:00
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
chunk = fp.read(chunk_size)
|
2019-10-13 00:01:25 -04:00
|
|
|
if chunk == b"":
|
2019-09-02 22:45:14 -04:00
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
yield chunk
|
|
|
|
|
2019-09-04 00:46:32 -04:00
|
|
|
# Tell GUI the progress
|
2019-09-02 22:45:14 -04:00
|
|
|
downloaded_bytes = fp.tell()
|
|
|
|
percent = (1.0 * downloaded_bytes / filesize) * 100
|
2019-10-13 00:01:25 -04:00
|
|
|
if (
|
|
|
|
not self.web.is_gui
|
|
|
|
or self.common.platform == "Linux"
|
|
|
|
or self.common.platform == "BSD"
|
|
|
|
):
|
2019-09-02 22:45:14 -04:00
|
|
|
sys.stdout.write(
|
2019-10-13 00:01:25 -04:00
|
|
|
"\r{0:s}, {1:.2f}% ".format(
|
|
|
|
self.common.human_readable_filesize(
|
|
|
|
downloaded_bytes
|
|
|
|
),
|
|
|
|
percent,
|
|
|
|
)
|
|
|
|
)
|
2019-09-02 22:45:14 -04:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
self.web.add_request(
|
|
|
|
self.web.REQUEST_INDIVIDUAL_FILE_PROGRESS,
|
|
|
|
path,
|
|
|
|
{
|
|
|
|
"id": history_id,
|
|
|
|
"bytes": downloaded_bytes,
|
|
|
|
"filesize": filesize,
|
|
|
|
},
|
|
|
|
)
|
2019-09-02 22:45:14 -04:00
|
|
|
done = False
|
|
|
|
except:
|
|
|
|
# Looks like the download was canceled
|
|
|
|
done = True
|
|
|
|
|
2019-09-04 00:46:32 -04:00
|
|
|
# Tell the GUI the individual file was canceled
|
2019-10-13 00:01:25 -04:00
|
|
|
self.web.add_request(
|
|
|
|
self.web.REQUEST_INDIVIDUAL_FILE_CANCELED,
|
|
|
|
path,
|
|
|
|
{"id": history_id},
|
|
|
|
)
|
2019-09-02 22:45:14 -04:00
|
|
|
|
|
|
|
fp.close()
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.common.platform != "Darwin":
|
2019-09-02 22:45:14 -04:00
|
|
|
sys.stdout.write("\n")
|
|
|
|
|
|
|
|
basename = os.path.basename(filesystem_path)
|
|
|
|
|
|
|
|
r = Response(generate())
|
|
|
|
if use_gzip:
|
2019-10-13 00:01:25 -04:00
|
|
|
r.headers.set("Content-Encoding", "gzip")
|
|
|
|
r.headers.set("Content-Length", filesize)
|
|
|
|
r.headers.set("Content-Disposition", "inline", filename=basename)
|
2019-09-02 22:45:14 -04:00
|
|
|
r = self.web.add_security_headers(r)
|
|
|
|
(content_type, _) = mimetypes.guess_type(basename, strict=False)
|
|
|
|
if content_type is not None:
|
2019-10-13 00:01:25 -04:00
|
|
|
r.headers.set("Content-Type", content_type)
|
2019-09-02 22:45:14 -04:00
|
|
|
return r
|
|
|
|
|
|
|
|
def should_use_gzip(self):
|
|
|
|
"""
|
|
|
|
Should we use gzip for this browser?
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
return (not self.is_zipped) and (
|
|
|
|
"gzip" in request.headers.get("Accept-Encoding", "").lower()
|
|
|
|
)
|
2019-09-02 22:45:14 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def _gzip_compress(
|
|
|
|
self, input_filename, output_filename, level, processed_size_callback=None
|
|
|
|
):
|
2019-09-02 22:45:14 -04:00
|
|
|
"""
|
|
|
|
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
|
2019-10-13 00:01:25 -04:00
|
|
|
blocksize = 1 << 16 # 64kB
|
|
|
|
with open(input_filename, "rb") as input_file:
|
|
|
|
output_file = gzip.open(output_filename, "wb", level)
|
2019-09-02 22:45:14 -04:00
|
|
|
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()
|
2019-09-03 23:52:49 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def render_logic(self, path=""):
|
2019-09-03 23:52:49 -04:00
|
|
|
"""
|
|
|
|
Inherited class will implement this.
|
|
|
|
"""
|
2019-09-09 02:35:05 -04:00
|
|
|
pass
|