Fix flake8 issues found in cli

This commit is contained in:
Micah Lee 2021-04-29 17:13:05 -07:00
parent 343e1be3d0
commit 0a6056e5e6
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
14 changed files with 146 additions and 128 deletions

View File

@ -18,13 +18,22 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import os, sys, time, argparse, threading import os
import sys
import time
import argparse
import threading
from datetime import datetime from datetime import datetime
from datetime import timedelta from datetime import timedelta
from .common import Common, CannotFindTor from .common import Common, CannotFindTor
from .web import Web from .web import Web
from .onion import * from .onion import (
TorErrorProtocolError,
TorTooOldEphemeral,
TorTooOldStealth,
Onion,
)
from .onionshare import OnionShare from .onionshare import OnionShare
from .mode_settings import ModeSettings from .mode_settings import ModeSettings
@ -310,7 +319,7 @@ def main(cwd=None):
# In receive mode, you must allows either text, files, or both # In receive mode, you must allows either text, files, or both
if mode == "receive" and disable_text and disable_files: if mode == "receive" and disable_text and disable_files:
print(f"You cannot disable both text and files") print("You cannot disable both text and files")
sys.exit() sys.exit()
# Create the Web object # Create the Web object
@ -336,7 +345,7 @@ def main(cwd=None):
except KeyboardInterrupt: except KeyboardInterrupt:
print("") print("")
sys.exit() sys.exit()
except Exception as e: except Exception:
sys.exit() sys.exit()
# Start the onionshare app # Start the onionshare app
@ -380,7 +389,9 @@ def main(cwd=None):
) )
print("") print("")
print( print(
"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." "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."
) )
print("") print("")
if mode_settings.get("general", "client_auth"): if mode_settings.get("general", "client_auth"):
@ -474,7 +485,9 @@ def main(cwd=None):
) )
print("") print("")
print( print(
"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." "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."
) )
print("") print("")

View File

@ -383,7 +383,7 @@ class Common:
try: try:
xdg_config_home = os.environ["XDG_CONFIG_HOME"] xdg_config_home = os.environ["XDG_CONFIG_HOME"]
onionshare_data_dir = f"{xdg_config_home}/onionshare" onionshare_data_dir = f"{xdg_config_home}/onionshare"
except: except Exception:
onionshare_data_dir = os.path.expanduser("~/.config/onionshare") onionshare_data_dir = os.path.expanduser("~/.config/onionshare")
elif self.platform == "Darwin": elif self.platform == "Darwin":
onionshare_data_dir = os.path.expanduser( onionshare_data_dir = os.path.expanduser(
@ -393,7 +393,7 @@ class Common:
try: try:
xdg_config_home = os.environ["XDG_CONFIG_HOME"] xdg_config_home = os.environ["XDG_CONFIG_HOME"]
onionshare_data_dir = f"{xdg_config_home}/onionshare" onionshare_data_dir = f"{xdg_config_home}/onionshare"
except: except Exception:
onionshare_data_dir = os.path.expanduser("~/.config/onionshare") onionshare_data_dir = os.path.expanduser("~/.config/onionshare")
# Modify the data dir if running tests # Modify the data dir if running tests

View File

@ -129,7 +129,7 @@ class ModeSettings:
self.fill_in_defaults() self.fill_in_defaults()
self.common.log("ModeSettings", "load", f"loaded {self.filename}") self.common.log("ModeSettings", "load", f"loaded {self.filename}")
return return
except: except Exception:
pass pass
# If loading settings didn't work, create the settings file # If loading settings didn't work, create the settings file

View File

@ -222,7 +222,7 @@ class Onion(object):
) )
try: try:
self.tor_socks_port = self.common.get_available_port(1000, 65535) self.tor_socks_port = self.common.get_available_port(1000, 65535)
except: except Exception:
print("OnionShare port not available") print("OnionShare port not available")
raise PortNotAvailable() raise PortNotAvailable()
self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc") self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")
@ -244,7 +244,7 @@ class Onion(object):
proc.terminate() proc.terminate()
proc.wait() proc.wait()
break break
except: except Exception:
pass pass
if self.common.platform == "Windows" or self.common.platform == "Darwin": if self.common.platform == "Windows" or self.common.platform == "Darwin":
@ -255,7 +255,7 @@ class Onion(object):
torrc_template += "ControlPort {{control_port}}\n" torrc_template += "ControlPort {{control_port}}\n"
try: try:
self.tor_control_port = self.common.get_available_port(1000, 65535) self.tor_control_port = self.common.get_available_port(1000, 65535)
except: except Exception:
print("OnionShare port not available") print("OnionShare port not available")
raise PortNotAvailable() raise PortNotAvailable()
self.tor_control_socket = None self.tor_control_socket = None
@ -428,7 +428,7 @@ class Onion(object):
try: try:
self.c = Controller.from_port(port=int(env_port)) self.c = Controller.from_port(port=int(env_port))
found_tor = True found_tor = True
except: except Exception:
pass pass
else: else:
@ -438,7 +438,7 @@ class Onion(object):
for port in ports: for port in ports:
self.c = Controller.from_port(port=port) self.c = Controller.from_port(port=port)
found_tor = True found_tor = True
except: except Exception:
pass pass
# If this still didn't work, try guessing the default socket file path # If this still didn't work, try guessing the default socket file path
@ -452,7 +452,7 @@ class Onion(object):
self.c = Controller.from_socket_file(path=socket_file_path) self.c = Controller.from_socket_file(path=socket_file_path)
found_tor = True found_tor = True
except: except Exception:
pass pass
# If connecting to default control ports failed, so let's try # If connecting to default control ports failed, so let's try
@ -474,14 +474,14 @@ class Onion(object):
self.c = Controller.from_socket_file(path=socket_file_path) self.c = Controller.from_socket_file(path=socket_file_path)
except: except Exception:
print(automatic_error) print(automatic_error)
raise TorErrorAutomatic() raise TorErrorAutomatic()
# Try authenticating # Try authenticating
try: try:
self.c.authenticate() self.c.authenticate()
except: except Exception:
print(automatic_error) print(automatic_error)
raise TorErrorAutomatic() raise TorErrorAutomatic()
@ -504,7 +504,7 @@ class Onion(object):
print(invalid_settings_error) print(invalid_settings_error)
raise TorErrorInvalidSetting() raise TorErrorInvalidSetting()
except: except Exception:
if self.settings.get("connection_type") == "control_port": if self.settings.get("connection_type") == "control_port":
print( print(
"Can't connect to the Tor controller at {}:{}.".format( "Can't connect to the Tor controller at {}:{}.".format(
@ -582,7 +582,7 @@ class Onion(object):
tmp_service_id = res.service_id tmp_service_id = res.service_id
self.c.remove_ephemeral_hidden_service(tmp_service_id) self.c.remove_ephemeral_hidden_service(tmp_service_id)
self.supports_stealth = True self.supports_stealth = True
except: except Exception:
# ephemeral stealth onion services are not supported # ephemeral stealth onion services are not supported
self.supports_stealth = False self.supports_stealth = False
@ -708,7 +708,7 @@ class Onion(object):
self.c.remove_ephemeral_hidden_service( self.c.remove_ephemeral_hidden_service(
mode_settings.get("general", "service_id") mode_settings.get("general", "service_id")
) )
except: except Exception:
self.common.log( self.common.log(
"Onion", "stop_onion_service", f"failed to remove {onion_host}" "Onion", "stop_onion_service", f"failed to remove {onion_host}"
) )
@ -729,12 +729,12 @@ class Onion(object):
"Onion", "cleanup", f"trying to remove onion {onion_host}" "Onion", "cleanup", f"trying to remove onion {onion_host}"
) )
self.c.remove_ephemeral_hidden_service(service_id) self.c.remove_ephemeral_hidden_service(service_id)
except: except Exception:
self.common.log( self.common.log(
"Onion", "cleanup", f"failed to remove onion {onion_host}" "Onion", "cleanup", f"failed to remove onion {onion_host}"
) )
pass pass
except: except Exception:
pass pass
if stop_tor: if stop_tor:
@ -777,7 +777,7 @@ class Onion(object):
) )
symbols_i = (symbols_i + 1) % len(symbols) symbols_i = (symbols_i + 1) % len(symbols)
time.sleep(1) time.sleep(1)
except: except Exception:
pass pass
self.tor_proc.terminate() self.tor_proc.terminate()
@ -797,7 +797,7 @@ class Onion(object):
"cleanup", "cleanup",
"Tried to kill tor process but it's still running", "Tried to kill tor process but it's still running",
) )
except: except Exception:
self.common.log( self.common.log(
"Onion", "cleanup", "Exception while killing tor process" "Onion", "cleanup", "Exception while killing tor process"
) )
@ -810,7 +810,7 @@ class Onion(object):
# Delete the temporary tor data directory # Delete the temporary tor data directory
if self.use_tmp_dir: if self.use_tmp_dir:
self.tor_data_directory.cleanup() self.tor_data_directory.cleanup()
except: except Exception:
pass pass
def get_tor_socks_port(self): def get_tor_socks_port(self):
@ -835,5 +835,5 @@ class Onion(object):
key = RSA.importKey(base64.b64decode(key)) key = RSA.importKey(base64.b64decode(key))
# Is this a v2 Onion key? (1024 bits) If so, we should keep using it. # Is this a v2 Onion key? (1024 bits) If so, we should keep using it.
return key.n.bit_length() == 1024 return key.n.bit_length() == 1024
except: except Exception:
return False return False

View File

@ -18,7 +18,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import os, shutil import os
import shutil
from .common import AutoStopTimer from .common import AutoStopTimer
@ -57,7 +58,7 @@ class OnionShare(object):
""" """
try: try:
self.port = self.common.get_available_port(17600, 17650) self.port = self.common.get_available_port(17600, 17650)
except: except Exception:
raise OSError("Cannot find an available OnionShare port") raise OSError("Cannot find an available OnionShare port")
def start_onion_service(self, mode, mode_settings, await_publication=True): def start_onion_service(self, mode, mode_settings, await_publication=True):
@ -102,7 +103,7 @@ class OnionShare(object):
os.remove(filename) os.remove(filename)
elif os.path.isdir(filename): elif os.path.isdir(filename):
shutil.rmtree(filename) shutil.rmtree(filename)
except: except Exception:
# Don't crash if file is still in use # Don't crash if file is still in use
pass pass
self.cleanup_filenames = [] self.cleanup_filenames = []

View File

@ -22,12 +22,6 @@ import json
import os import os
import locale import locale
try:
# We only need pwd module in macOS, and it's not available in Windows
import pwd
except:
pass
class Settings(object): class Settings(object):
""" """
@ -166,13 +160,13 @@ class Settings(object):
with open(self.filename, "r") as f: with open(self.filename, "r") as f:
self._settings = json.load(f) self._settings = json.load(f)
self.fill_in_defaults() self.fill_in_defaults()
except: except Exception:
pass pass
# Make sure data_dir exists # Make sure data_dir exists
try: try:
os.makedirs(self.get("data_dir"), exist_ok=True) os.makedirs(self.get("data_dir"), exist_ok=True)
except: except Exception:
pass pass
def save(self): def save(self):
@ -191,7 +185,7 @@ class Settings(object):
if key == "control_port_port" or key == "socks_port": if key == "control_port_port" or key == "socks_port":
try: try:
val = int(val) val = int(val)
except: except Exception:
if key == "control_port_port": if key == "control_port_port":
val = self.default_settings["control_port_port"] val = self.default_settings["control_port_port"]
elif key == "socks_port": elif key == "socks_port":

View File

@ -33,8 +33,6 @@ uploads files:
- new ReceiveModeRequest object is created - new ReceiveModeRequest object is created
- creates a directory based on the timestamp - creates a directory based on the timestamp
-
""" """
@ -303,7 +301,7 @@ class ReceiveModeFile(object):
self.upload_error = False self.upload_error = False
try: try:
self.f = open(self.filename_in_progress, "wb+") self.f = open(self.filename_in_progress, "wb+")
except: except Exception:
# This will only happen if someone is messing with the data dir while # This will only happen if someone is messing with the data dir while
# OnionShare is running, but if it does make sure to throw an error # OnionShare is running, but if it does make sure to throw an error
self.upload_error = True self.upload_error = True
@ -351,7 +349,7 @@ class ReceiveModeFile(object):
bytes_written = self.f.write(b) bytes_written = self.f.write(b)
self.onionshare_write_func(self.onionshare_filename, bytes_written) self.onionshare_write_func(self.onionshare_filename, bytes_written)
except: except Exception:
self.upload_error = True self.upload_error = True
def close(self): def close(self):
@ -365,7 +363,7 @@ class ReceiveModeFile(object):
# Rename the in progress file to the final filename # Rename the in progress file to the final filename
os.rename(self.filename_in_progress, self.filename) os.rename(self.filename_in_progress, self.filename)
except: except Exception:
self.upload_error = True self.upload_error = True
self.onionshare_close_func(self.onionshare_filename, self.upload_error) self.onionshare_close_func(self.onionshare_filename, self.upload_error)
@ -465,7 +463,7 @@ class ReceiveModeRequest(Request):
# Figure out the content length # Figure out the content length
try: try:
self.content_length = int(self.headers["Content-Length"]) self.content_length = int(self.headers["Content-Length"])
except: except Exception:
self.content_length = 0 self.content_length = 0
date_str = datetime.now().strftime("%b %d, %I:%M%p") date_str = datetime.now().strftime("%b %d, %I:%M%p")

View File

@ -242,7 +242,7 @@ class SendBaseModeWeb:
}, },
) )
done = False done = False
except: except Exception:
# Looks like the download was canceled # Looks like the download was canceled
done = True done = True

View File

@ -44,7 +44,7 @@ def make_etag(data):
else: else:
break break
hash_value = binascii.hexlify(hasher.digest()).decode('utf-8') hash_value = binascii.hexlify(hasher.digest()).decode("utf-8")
return '"sha256:{}"'.format(hash_value) return '"sha256:{}"'.format(hash_value)
@ -53,13 +53,13 @@ def parse_range_header(range_header: str, target_size: int) -> list:
if range_header is None: if range_header is None:
return [(0, end_index)] return [(0, end_index)]
bytes_ = 'bytes=' bytes_ = "bytes="
if not range_header.startswith(bytes_): if not range_header.startswith(bytes_):
abort(416) abort(416)
ranges = [] ranges = []
for range_ in range_header[len(bytes_):].split(','): for range_ in range_header[len(bytes_) :].split(","):
split = range_.split('-') split = range_.split("-")
if len(split) == 1: if len(split) == 1:
try: try:
start = int(split[0]) start = int(split[0])
@ -194,13 +194,17 @@ class ShareModeWeb(SendBaseModeWeb):
etag = self.download_etag etag = self.download_etag
# for range requests # for range requests
range_, status_code = self.get_range_and_status_code(self.filesize, etag, self.last_modified) range_, status_code = self.get_range_and_status_code(
self.filesize, etag, self.last_modified
)
# Tell GUI the download started # Tell GUI the download started
history_id = self.cur_history_id history_id = self.cur_history_id
self.cur_history_id += 1 self.cur_history_id += 1
self.web.add_request( self.web.add_request(
self.web.REQUEST_STARTED, request_path, {"id": history_id, "use_gzip": use_gzip} self.web.REQUEST_STARTED,
request_path,
{"id": history_id, "use_gzip": use_gzip},
) )
basename = os.path.basename(self.download_filename) basename = os.path.basename(self.download_filename)
@ -209,32 +213,41 @@ class ShareModeWeb(SendBaseModeWeb):
r = Response() r = Response()
else: else:
r = Response( r = Response(
self.generate(shutdown_func, range_, file_to_download, request_path, self.generate(
history_id, self.filesize)) shutdown_func,
range_,
file_to_download,
request_path,
history_id,
self.filesize,
)
)
if use_gzip: if use_gzip:
r.headers.set('Content-Encoding', 'gzip') r.headers.set("Content-Encoding", "gzip")
r.headers.set('Content-Length', range_[1] - range_[0] + 1) r.headers.set("Content-Length", range_[1] - range_[0] + 1)
filename_dict = { filename_dict = {
"filename": unidecode(basename), "filename": unidecode(basename),
"filename*": "UTF-8''%s" % url_quote(basename), "filename*": "UTF-8''%s" % url_quote(basename),
} }
r.headers.set('Content-Disposition', 'attachment', **filename_dict) r.headers.set("Content-Disposition", "attachment", **filename_dict)
r = self.web.add_security_headers(r) r = self.web.add_security_headers(r)
# guess content type # guess content type
(content_type, _) = mimetypes.guess_type(basename, strict=False) (content_type, _) = mimetypes.guess_type(basename, strict=False)
if content_type is not None: if content_type is not None:
r.headers.set('Content-Type', content_type) r.headers.set("Content-Type", content_type)
r.headers.set('Accept-Ranges', 'bytes') r.headers.set("Accept-Ranges", "bytes")
r.headers.set('ETag', etag) r.headers.set("ETag", etag)
r.headers.set('Last-Modified', http_date(self.last_modified)) r.headers.set("Last-Modified", http_date(self.last_modified))
# we need to set this for range requests # we need to set this for range requests
r.headers.set('Vary', 'Accept-Encoding') r.headers.set("Vary", "Accept-Encoding")
if status_code == 206: if status_code == 206:
r.headers.set('Content-Range', r.headers.set(
'bytes {}-{}/{}'.format(range_[0], range_[1], self.filesize)) "Content-Range",
"bytes {}-{}/{}".format(range_[0], range_[1], self.filesize),
)
r.status_code = status_code r.status_code = status_code
@ -244,17 +257,19 @@ class ShareModeWeb(SendBaseModeWeb):
def get_range_and_status_code(cls, dl_size, etag, last_modified): def get_range_and_status_code(cls, dl_size, etag, last_modified):
use_default_range = True use_default_range = True
status_code = 200 status_code = 200
range_header = request.headers.get('Range') range_header = request.headers.get("Range")
# range requests are only allowed for get # range requests are only allowed for get
if request.method == 'GET': if request.method == "GET":
ranges = parse_range_header(range_header, dl_size) ranges = parse_range_header(range_header, dl_size)
if not (len(ranges) == 1 and ranges[0][0] == 0 and ranges[0][1] == dl_size - 1): if not (
len(ranges) == 1 and ranges[0][0] == 0 and ranges[0][1] == dl_size - 1
):
use_default_range = False use_default_range = False
status_code = 206 status_code = 206
if range_header: if range_header:
if_range = request.headers.get('If-Range') if_range = request.headers.get("If-Range")
if if_range and if_range != etag: if if_range and if_range != etag:
use_default_range = True use_default_range = True
status_code = 200 status_code = 200
@ -266,11 +281,11 @@ class ShareModeWeb(SendBaseModeWeb):
abort(416) # We don't support multipart range requests yet abort(416) # We don't support multipart range requests yet
range_ = ranges[0] range_ = ranges[0]
etag_header = request.headers.get('ETag') etag_header = request.headers.get("ETag")
if etag_header is not None and etag_header != etag: if etag_header is not None and etag_header != etag:
abort(412) abort(412)
if_unmod = request.headers.get('If-Unmodified-Since') if_unmod = request.headers.get("If-Unmodified-Since")
if if_unmod: if if_unmod:
if_date = parse_date(if_unmod) if_date = parse_date(if_unmod)
if if_date and if_date > last_modified: if if_date and if_date > last_modified:
@ -280,10 +295,12 @@ class ShareModeWeb(SendBaseModeWeb):
return range_, status_code return range_, status_code
def generate(self, shutdown_func, range_, file_to_download, path, history_id, filesize): def generate(
self, shutdown_func, range_, file_to_download, path, history_id, filesize
):
# The user hasn't canceled the download # The user hasn't canceled the download
self.client_cancel = False self.client_cancel = False
# Starting a new download # Starting a new download
if self.web.settings.get("share", "autostop_sharing"): if self.web.settings.get("share", "autostop_sharing"):
self.download_in_progress = True self.download_in_progress = True
@ -326,9 +343,7 @@ class ShareModeWeb(SendBaseModeWeb):
): ):
sys.stdout.write( sys.stdout.write(
"\r{0:s}, {1:.2f}% ".format( "\r{0:s}, {1:.2f}% ".format(
self.common.human_readable_filesize( self.common.human_readable_filesize(downloaded_bytes),
downloaded_bytes
),
percent, percent,
) )
) )
@ -337,10 +352,14 @@ class ShareModeWeb(SendBaseModeWeb):
self.web.add_request( self.web.add_request(
self.web.REQUEST_PROGRESS, self.web.REQUEST_PROGRESS,
path, path,
{"id": history_id, "bytes": downloaded_bytes, 'total_bytes': filesize,}, {
"id": history_id,
"bytes": downloaded_bytes,
"total_bytes": filesize,
},
) )
self.web.done = False self.web.done = False
except: except Exception:
# looks like the download was canceled # looks like the download was canceled
self.web.done = True self.web.done = True
canceled = True canceled = True
@ -367,10 +386,9 @@ class ShareModeWeb(SendBaseModeWeb):
if shutdown_func is None: if shutdown_func is None:
raise RuntimeError("Not running with the Werkzeug Server") raise RuntimeError("Not running with the Werkzeug Server")
shutdown_func() shutdown_func()
except: except Exception:
pass pass
def directory_listing_template( def directory_listing_template(
self, path, files, dirs, breadcrumbs, breadcrumbs_leaf self, path, files, dirs, breadcrumbs, breadcrumbs_leaf
): ):
@ -466,7 +484,7 @@ class ShareModeWeb(SendBaseModeWeb):
if len(self.file_info["files"]) == 1 and len(self.file_info["dirs"]) == 0: if len(self.file_info["files"]) == 1 and len(self.file_info["dirs"]) == 0:
self.download_filename = self.file_info["files"][0]["filename"] self.download_filename = self.file_info["files"][0]["filename"]
self.download_filesize = self.file_info["files"][0]["size"] self.download_filesize = self.file_info["files"][0]["size"]
with open(self.download_filename, 'rb') as f: with open(self.download_filename, "rb") as f:
self.download_etag = make_etag(f) self.download_etag = make_etag(f)
# Compress the file with gzip now, so we don't have to do it on each request # Compress the file with gzip now, so we don't have to do it on each request
@ -475,7 +493,7 @@ class ShareModeWeb(SendBaseModeWeb):
self.download_filename, self.gzip_filename, 6, processed_size_callback self.download_filename, self.gzip_filename, 6, processed_size_callback
) )
self.gzip_filesize = os.path.getsize(self.gzip_filename) self.gzip_filesize = os.path.getsize(self.gzip_filename)
with open(self.gzip_filename, 'rb') as f: with open(self.gzip_filename, "rb") as f:
self.gzip_etag = make_etag(f) self.gzip_etag = make_etag(f)
# Make sure the gzip file gets cleaned up when onionshare stops # Make sure the gzip file gets cleaned up when onionshare stops
@ -502,7 +520,7 @@ class ShareModeWeb(SendBaseModeWeb):
self.zip_writer.close() self.zip_writer.close()
self.download_filesize = os.path.getsize(self.download_filename) self.download_filesize = os.path.getsize(self.download_filename)
with open(self.download_filename, 'rb') as f: with open(self.download_filename, "rb") as f:
self.download_etag = make_etag(f) self.download_etag = make_etag(f)
# Make sure the zip file gets cleaned up when onionshare stops # Make sure the zip file gets cleaned up when onionshare stops

View File

@ -41,6 +41,7 @@ from .receive_mode import ReceiveModeWeb, ReceiveModeWSGIMiddleware, ReceiveMode
from .website_mode import WebsiteModeWeb from .website_mode import WebsiteModeWeb
from .chat_mode import ChatModeWeb from .chat_mode import ChatModeWeb
# Stub out flask's show_server_banner function, to avoiding showing warnings that # Stub out flask's show_server_banner function, to avoiding showing warnings that
# are not applicable to OnionShare # are not applicable to OnionShare
def stubbed_show_server_banner(env, debug, app_import_path, eager_loading): def stubbed_show_server_banner(env, debug, app_import_path, eager_loading):
@ -49,7 +50,7 @@ def stubbed_show_server_banner(env, debug, app_import_path, eager_loading):
try: try:
flask.cli.show_server_banner = stubbed_show_server_banner flask.cli.show_server_banner = stubbed_show_server_banner
except: except Exception:
pass pass
@ -327,7 +328,7 @@ class Web:
def generate_password(self, saved_password=None): def generate_password(self, saved_password=None):
self.common.log("Web", "generate_password", f"saved_password={saved_password}") self.common.log("Web", "generate_password", f"saved_password={saved_password}")
if saved_password != None and saved_password != "": if saved_password is not None and saved_password != "":
self.password = saved_password self.password = saved_password
self.common.log( self.common.log(
"Web", "Web",
@ -363,7 +364,7 @@ class Web:
if func is None and self.mode != "chat": if func is None and self.mode != "chat":
raise RuntimeError("Not running with the Werkzeug Server") raise RuntimeError("Not running with the Werkzeug Server")
func() func()
except: except Exception:
pass pass
self.running = False self.running = False

View File

@ -25,7 +25,12 @@ version = "2.3.1"
setuptools.setup( setuptools.setup(
name="onionshare-cli", name="onionshare-cli",
version=version, version=version,
description="OnionShare lets you securely and anonymously send and receive files. It works by starting a web server, making it accessible as a Tor onion service, and generating an unguessable web address so others can download files from you, or upload files to you. It does _not_ require setting up a separate server or using a third party file-sharing service.", description=(
"OnionShare lets you securely and anonymously send and receive files. It works by starting a web server, "
"making it accessible as a Tor onion service, and generating an unguessable web address so others can "
"download files from you, or upload files to you. It does _not_ require setting up a separate server or "
"using a third party file-sharing service."
),
author="Micah Lee", author="Micah Lee",
author_email="micah@micahflee.com", author_email="micah@micahflee.com",
maintainer="Micah Lee", maintainer="Micah Lee",

View File

@ -1,11 +1,4 @@
import sys import sys
# Force tests to look for resources in the source code tree
sys.onionshare_dev_mode = True
# Let OnionShare know the tests are running, to avoid colliding with settings files
sys.onionshare_test_mode = True
import os import os
import shutil import shutil
import tempfile import tempfile
@ -14,6 +7,11 @@ import pytest
from onionshare_cli import common, web from onionshare_cli import common, web
# Force tests to look for resources in the source code tree
sys.onionshare_dev_mode = True
# Let OnionShare know the tests are running, to avoid colliding with settings files
sys.onionshare_test_mode = True
# The temporary directory for CLI tests # The temporary directory for CLI tests
test_temp_dir = None test_temp_dir = None
@ -45,7 +43,7 @@ def temp_dir():
@pytest.fixture @pytest.fixture
def temp_dir_1024(temp_dir): def temp_dir_1024(temp_dir):
""" Create a temporary directory that has a single file of a """Create a temporary directory that has a single file of a
particular size (1024 bytes). particular size (1024 bytes).
""" """
@ -59,7 +57,7 @@ def temp_dir_1024(temp_dir):
# pytest > 2.9 only needs @pytest.fixture # pytest > 2.9 only needs @pytest.fixture
@pytest.yield_fixture @pytest.yield_fixture
def temp_dir_1024_delete(temp_dir): def temp_dir_1024_delete(temp_dir):
""" Create a temporary directory that has a single file of a """Create a temporary directory that has a single file of a
particular size (1024 bytes). The temporary directory (including particular size (1024 bytes). The temporary directory (including
the file inside) will be deleted after fixture usage. the file inside) will be deleted after fixture usage.
""" """
@ -73,7 +71,7 @@ def temp_dir_1024_delete(temp_dir):
@pytest.fixture @pytest.fixture
def temp_file_1024(temp_dir): def temp_file_1024(temp_dir):
""" Create a temporary file of a particular size (1024 bytes). """ """Create a temporary file of a particular size (1024 bytes)."""
with tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) as tmp_file: with tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) as tmp_file:
tmp_file.write(b"*" * 1024) tmp_file.write(b"*" * 1024)
@ -117,7 +115,7 @@ def default_zw():
tmp_dir = os.path.dirname(zw.zip_filename) tmp_dir = os.path.dirname(zw.zip_filename)
try: try:
shutil.rmtree(tmp_dir, ignore_errors=True) shutil.rmtree(tmp_dir, ignore_errors=True)
except: except Exception:
pass pass
@ -189,10 +187,3 @@ def time_strftime(monkeypatch):
@pytest.fixture @pytest.fixture
def common_obj(): def common_obj():
return common.Common() return common.Common()
@pytest.fixture
def settings_obj(sys_onionshare_dev_mode, platform_linux):
_common = common.Common()
_common.version = "DUMMY_VERSION_1.2.3"
return settings.Settings(_common)

View File

@ -42,7 +42,7 @@ RANDOM_STR_REGEX = re.compile(r"^[a-z2-7]+$")
def web_obj(temp_dir, common_obj, mode, num_files=0): def web_obj(temp_dir, common_obj, mode, num_files=0):
""" Creates a Web object, in either share mode or receive mode, ready for testing """ """Creates a Web object, in either share mode or receive mode, ready for testing"""
common_obj.settings = Settings(common_obj) common_obj.settings = Settings(common_obj)
mode_settings = ModeSettings(common_obj) mode_settings = ModeSettings(common_obj)
web = Web(common_obj, False, mode_settings, mode) web = Web(common_obj, False, mode_settings, mode)
@ -100,7 +100,7 @@ class TestWeb:
web = web_obj(temp_dir, common_obj, "share", 3) web = web_obj(temp_dir, common_obj, "share", 3)
web.settings.set("share", "autostop_sharing", True) web.settings.set("share", "autostop_sharing", True)
assert web.running == True assert web.running is True
with web.app.test_client() as c: with web.app.test_client() as c:
# Download the first time # Download the first time
@ -112,7 +112,7 @@ class TestWeb:
or res.mimetype == "application/x-zip-compressed" or res.mimetype == "application/x-zip-compressed"
) )
assert web.running == False assert web.running is False
def test_share_mode_autostop_sharing_off( def test_share_mode_autostop_sharing_off(
self, temp_dir, common_obj, temp_file_1024 self, temp_dir, common_obj, temp_file_1024
@ -120,7 +120,7 @@ class TestWeb:
web = web_obj(temp_dir, common_obj, "share", 3) web = web_obj(temp_dir, common_obj, "share", 3)
web.settings.set("share", "autostop_sharing", False) web.settings.set("share", "autostop_sharing", False)
assert web.running == True assert web.running is True
with web.app.test_client() as c: with web.app.test_client() as c:
# Download the first time # Download the first time
@ -131,7 +131,7 @@ class TestWeb:
res.mimetype == "application/zip" res.mimetype == "application/zip"
or res.mimetype == "application/x-zip-compressed" or res.mimetype == "application/x-zip-compressed"
) )
assert web.running == True assert web.running is True
def test_receive_mode(self, temp_dir, common_obj): def test_receive_mode(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive") web = web_obj(temp_dir, common_obj, "receive")
@ -183,7 +183,7 @@ class TestWeb:
assert res.status_code == 200 assert res.status_code == 200
assert webhook_url == "http://127.0.0.1:1337/example" assert webhook_url == "http://127.0.0.1:1337/example"
assert webhook_data == "1 file uploaded to OnionShare" assert webhook_data == "1 file submitted to OnionShare"
def test_public_mode_on(self, temp_dir, common_obj): def test_public_mode_on(self, temp_dir, common_obj):
web = web_obj(temp_dir, common_obj, "receive") web = web_obj(temp_dir, common_obj, "receive")
@ -192,7 +192,7 @@ class TestWeb:
with web.app.test_client() as c: with web.app.test_client() as c:
# Loading / should work without auth # Loading / should work without auth
res = c.get("/") res = c.get("/")
data1 = res.get_data() res.get_data()
assert res.status_code == 200 assert res.status_code == 200
def test_public_mode_off(self, temp_dir, common_obj): def test_public_mode_off(self, temp_dir, common_obj):

View File

@ -1,7 +1,4 @@
import pytest import pytest
import subprocess
from tempfile import NamedTemporaryFile
from werkzeug.exceptions import RequestedRangeNotSatisfiable from werkzeug.exceptions import RequestedRangeNotSatisfiable
from onionshare_cli.web.share_mode import parse_range_header from onionshare_cli.web.share_mode import parse_range_header
@ -9,24 +6,24 @@ from onionshare_cli.web.share_mode import parse_range_header
VALID_RANGES = [ VALID_RANGES = [
(None, 500, [(0, 499)]), (None, 500, [(0, 499)]),
('bytes=0', 500, [(0, 499)]), ("bytes=0", 500, [(0, 499)]),
('bytes=100', 500, [(100, 499)]), ("bytes=100", 500, [(100, 499)]),
('bytes=100-', 500, [(100, 499)]), # not in the RFC, but how curl sends ("bytes=100-", 500, [(100, 499)]), # not in the RFC, but how curl sends
('bytes=0-99', 500, [(0, 99)]), ("bytes=0-99", 500, [(0, 99)]),
('bytes=0-599', 500, [(0, 499)]), ("bytes=0-599", 500, [(0, 499)]),
('bytes=0-0', 500, [(0, 0)]), ("bytes=0-0", 500, [(0, 0)]),
('bytes=-100', 500, [(400, 499)]), ("bytes=-100", 500, [(400, 499)]),
('bytes=0-99,100-199', 500, [(0, 199)]), ("bytes=0-99,100-199", 500, [(0, 199)]),
('bytes=0-100,100-199', 500, [(0, 199)]), ("bytes=0-100,100-199", 500, [(0, 199)]),
('bytes=0-99,101-199', 500, [(0, 99), (101, 199)]), ("bytes=0-99,101-199", 500, [(0, 99), (101, 199)]),
('bytes=0-199,100-299', 500, [(0, 299)]), ("bytes=0-199,100-299", 500, [(0, 299)]),
('bytes=0-99,200-299', 500, [(0, 99), (200, 299)]), ("bytes=0-99,200-299", 500, [(0, 99), (200, 299)]),
] ]
INVALID_RANGES = [ INVALID_RANGES = [
'bytes=200-100', "bytes=200-100",
'bytes=0-100,300-200', "bytes=0-100,300-200",
] ]
@ -38,4 +35,4 @@ def test_parse_ranges():
for invalid in INVALID_RANGES: for invalid in INVALID_RANGES:
with pytest.raises(RequestedRangeNotSatisfiable): with pytest.raises(RequestedRangeNotSatisfiable):
parse_range_header(invalid, 500) parse_range_header(invalid, 500)