onionshare/cli/onionshare_cli/__init__.py

503 lines
17 KiB
Python
Raw Normal View History

2014-09-02 20:30:01 -04:00
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
2021-02-22 16:35:14 -05:00
Copyright (C) 2014-2021 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/>.
"""
import os, sys, time, argparse, threading
from datetime import datetime
from datetime import timedelta
from .common import Common, CannotFindTor
from .web import Web
from .onion import *
from .onionshare import OnionShare
2019-11-02 17:56:40 -04:00
from .mode_settings import ModeSettings
def build_url(mode_settings, app, web):
# Build the URL
if mode_settings.get("general", "public"):
return f"http://{app.onion_host}"
else:
return f"http://onionshare:{web.password}@{app.onion_host}"
def main(cwd=None):
"""
The main() function implements all of the logic that the command-line version of
onionshare uses.
"""
common = Common()
common.display_banner()
# OnionShare CLI in OSX needs to change current working directory (#132)
2019-10-13 00:01:25 -04:00
if common.platform == "Darwin":
if cwd:
os.chdir(cwd)
# Parse arguments
2019-10-13 00:01:25 -04:00
parser = argparse.ArgumentParser(
formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=28)
)
# Select modes
parser.add_argument(
"--receive", action="store_true", dest="receive", help="Receive files"
)
parser.add_argument(
"--website", action="store_true", dest="website", help="Publish website"
)
parser.add_argument(
"--chat", action="store_true", dest="chat", help="Start chat server"
)
# Tor connection-related args
2019-10-13 00:01:25 -04:00
parser.add_argument(
"--local-only",
action="store_true",
dest="local_only",
default=False,
2019-10-13 00:01:25 -04:00
help="Don't use Tor (only for development)",
)
parser.add_argument(
"--connect-timeout",
metavar="SECONDS",
dest="connect_timeout",
default=120,
help="Give up connecting to Tor after a given amount of seconds (default: 120)",
)
parser.add_argument(
"--config",
metavar="FILENAME",
default=None,
help="Filename of custom global settings",
)
# Persistent file
parser.add_argument(
"--persistent",
metavar="FILENAME",
default=None,
help="Filename of persistent session",
)
# General args
parser.add_argument(
"--public",
2019-10-13 00:01:25 -04:00
action="store_true",
dest="public",
default=False,
help="Don't use a password",
2019-10-13 00:01:25 -04:00
)
parser.add_argument(
"--auto-start-timer",
metavar="SECONDS",
2019-10-13 00:01:25 -04:00
dest="autostart_timer",
default=0,
help="Start onion service at scheduled time (N seconds from now)",
2019-10-13 00:01:25 -04:00
)
parser.add_argument(
"--auto-stop-timer",
metavar="SECONDS",
2019-10-13 00:01:25 -04:00
dest="autostop_timer",
default=0,
help="Stop onion service at schedule time (N seconds from now)",
2019-10-13 00:01:25 -04:00
)
parser.add_argument(
"--legacy",
action="store_true",
dest="legacy",
default=False,
help="Use legacy address (v2 onion service, not recommended)",
2019-10-13 00:01:25 -04:00
)
parser.add_argument(
"--client-auth",
2019-10-13 00:01:25 -04:00
action="store_true",
dest="client_auth",
default=False,
help="Use client authorization (requires --legacy)",
2019-10-13 00:01:25 -04:00
)
# Share args
2019-10-13 00:01:25 -04:00
parser.add_argument(
"--no-autostop-sharing",
2019-10-13 00:01:25 -04:00
action="store_true",
dest="no_autostop_sharing",
default=False,
help="Share files: Continue sharing after files have been sent (default is to stop sharing)",
2019-10-13 00:01:25 -04:00
)
# Receive args
2019-10-13 00:01:25 -04:00
parser.add_argument(
"--data-dir",
metavar="data_dir",
default=None,
help="Receive files: Save files received to this directory",
2019-10-13 00:01:25 -04:00
)
2021-04-11 16:33:58 -04:00
parser.add_argument(
"--webhook-url",
metavar="webhook_url",
default=None,
help="Receive files: URL to receive webhook notifications",
)
# Website args
2019-10-13 00:01:25 -04:00
parser.add_argument(
"--disable_csp",
action="store_true",
dest="disable_csp",
2019-10-13 00:01:25 -04:00
default=False,
help="Publish website: Disable Content Security Policy header (allows your website to use third-party resources)",
2019-10-13 00:01:25 -04:00
)
# Other
2019-10-13 00:01:25 -04:00
parser.add_argument(
"-v",
"--verbose",
action="store_true",
dest="verbose",
help="Log OnionShare errors to stdout, and web errors to disk",
)
parser.add_argument(
"filename",
metavar="filename",
nargs="*",
help="List of files or folders to share",
)
args = parser.parse_args()
filenames = args.filename
for i in range(len(filenames)):
filenames[i] = os.path.abspath(filenames[i])
2018-03-05 10:45:10 -05:00
receive = bool(args.receive)
website = bool(args.website)
chat = bool(args.chat)
local_only = bool(args.local_only)
connect_timeout = int(args.connect_timeout)
config_filename = args.config
persistent_filename = args.persistent
public = bool(args.public)
autostart_timer = int(args.autostart_timer)
autostop_timer = int(args.autostop_timer)
legacy = bool(args.legacy)
client_auth = bool(args.client_auth)
autostop_sharing = not bool(args.no_autostop_sharing)
data_dir = args.data_dir
2021-04-11 16:33:58 -04:00
webhook_url = args.webhook_url
disable_csp = bool(args.disable_csp)
verbose = bool(args.verbose)
if receive:
2019-10-13 00:01:25 -04:00
mode = "receive"
elif website:
2019-10-13 00:01:25 -04:00
mode = "website"
elif chat:
mode = "chat"
else:
2019-10-13 00:01:25 -04:00
mode = "share"
# Verbose mode?
common.verbose = verbose
# client_auth can only be set if legacy is also set
if client_auth and not legacy:
print(
"Client authentication (--client-auth) is only supported with with legacy onion services (--legacy)"
)
sys.exit()
# Re-load settings, if a custom config was passed in
if config_filename:
common.load_settings(config_filename)
else:
common.load_settings()
2019-11-02 17:56:40 -04:00
# Mode settings
if persistent_filename:
mode_settings = ModeSettings(common, persistent_filename)
mode_settings.set("persistent", "enabled", True)
mode_settings.set("persistent", "mode", mode)
else:
mode_settings = ModeSettings(common)
if mode_settings.just_created:
# This means the mode settings were just created, not loaded from disk
mode_settings.set("general", "public", public)
mode_settings.set("general", "autostart_timer", autostart_timer)
mode_settings.set("general", "autostop_timer", autostop_timer)
mode_settings.set("general", "legacy", legacy)
mode_settings.set("general", "client_auth", client_auth)
if mode == "share":
mode_settings.set("share", "autostop_sharing", autostop_sharing)
if mode == "receive":
if data_dir:
mode_settings.set("receive", "data_dir", data_dir)
2021-04-11 16:33:58 -04:00
if webhook_url:
mode_settings.set("receive", "webhook_url", webhook_url)
if mode == "website":
mode_settings.set("website", "disable_csp", disable_csp)
else:
# See what the persistent mode was
mode = mode_settings.get("persistent", "mode")
2019-11-28 22:30:48 -05:00
# In share and website mode, you must supply a list of filenames
if mode == "share" or mode == "website":
# Unless you passed in a persistent filename, in which case get the filenames from
# the mode settings
2021-02-22 16:35:14 -05:00
if (
persistent_filename
and not mode_settings.just_created
and len(filenames) != 0
):
filenames = mode_settings.get(mode, "filenames")
else:
# Make sure filenames given if not using receiver mode
if len(filenames) == 0:
if persistent_filename:
mode_settings.delete()
parser.print_help()
sys.exit()
# Validate filenames
valid = True
for filename in filenames:
if not os.path.isfile(filename) and not os.path.isdir(filename):
print(f"{filename} is not a valid file.")
valid = False
if not os.access(filename, os.R_OK):
print(f"{filename} is not a readable file.")
valid = False
if not valid:
sys.exit()
2019-11-02 17:56:40 -04:00
# Save the filenames in persistent file
if persistent_filename:
mode_settings.set(mode, "filenames", filenames)
# Create the Web object
2019-11-02 17:56:40 -04:00
web = Web(common, False, mode_settings, mode)
# Start the Onion object
try:
onion = Onion(common, use_tmp_dir=True)
except CannotFindTor:
print("You must install tor to use OnionShare from the command line")
if common.platform == "Darwin":
print("In macOS, you can do this with Homebrew (https://brew.sh):")
print(" brew install tor")
sys.exit()
try:
2019-10-13 00:01:25 -04:00
onion.connect(
2019-11-02 17:56:40 -04:00
custom_settings=False,
config=config_filename,
2019-11-02 17:56:40 -04:00
connect_timeout=connect_timeout,
local_only=local_only,
2019-10-13 00:01:25 -04:00
)
except KeyboardInterrupt:
print("")
sys.exit()
except Exception as e:
sys.exit()
# Start the onionshare app
try:
common.settings.load()
2021-04-11 16:33:58 -04:00
if mode_settings.get("general", "public"):
2019-05-21 01:18:49 -04:00
web.password = None
2021-04-11 16:33:58 -04:00
else:
web.generate_password(mode_settings.get("onion", "password"))
# Receive mode needs to know the tor proxy details for webhooks
if mode == "receive":
if local_only:
web.proxies = None
else:
(socks_address, socks_port) = onion.get_tor_socks_port()
web.proxies = {
"http": f"socks5h://{socks_address}:{socks_port}",
"https": f"socks5h://{socks_address}:{socks_port}",
2021-04-11 16:33:58 -04:00
}
app = OnionShare(common, onion, local_only, autostop_timer)
app.choose_port()
# Delay the startup if a startup timer was set
if autostart_timer > 0:
# Can't set a schedule that is later than the auto-stop timer
if autostop_timer > 0 and autostop_timer < autostart_timer:
2019-10-13 00:01:25 -04:00
print(
"The auto-stop time can't be the same or earlier than the auto-start time. Please update it to start sharing."
)
sys.exit()
app.start_onion_service(mode, mode_settings, False)
url = build_url(mode_settings, app, web)
schedule = datetime.now() + timedelta(seconds=autostart_timer)
2019-10-13 00:01:25 -04:00
if mode == "receive":
print(
f"Files sent to you appear in this folder: {mode_settings.get('receive', 'data_dir')}"
2019-10-13 00:01:25 -04:00
)
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."
)
print("")
if mode_settings.get("general", "client_auth"):
2019-10-13 00:01:25 -04:00
print(
f"Give this address and HidServAuth lineto your sender, and tell them it won't be accessible until: {schedule.strftime('%I:%M:%S%p, %b %d, %y')}"
2019-10-13 00:01:25 -04:00
)
print(app.auth_string)
else:
2019-10-13 00:01:25 -04:00
print(
f"Give this address to your sender, and tell them it won't be accessible until: {schedule.strftime('%I:%M:%S%p, %b %d, %y')}"
2019-10-13 00:01:25 -04:00
)
else:
if mode_settings.get("general", "client_auth"):
2019-10-13 00:01:25 -04:00
print(
f"Give this address and HidServAuth line to your recipient, and tell them it won't be accessible until: {schedule.strftime('%I:%M:%S%p, %b %d, %y')}"
2019-10-13 00:01:25 -04:00
)
print(app.auth_string)
else:
2019-10-13 00:01:25 -04:00
print(
f"Give this address to your recipient, and tell them it won't be accessible until: {schedule.strftime('%I:%M:%S%p, %b %d, %y')}"
2019-10-13 00:01:25 -04:00
)
print(url)
2019-10-13 00:01:25 -04:00
print("")
2019-04-19 20:31:34 -04:00
print("Waiting for the scheduled time before starting...")
app.onion.cleanup(False)
time.sleep(autostart_timer)
app.start_onion_service(mode, mode_settings)
else:
app.start_onion_service(mode, mode_settings)
except KeyboardInterrupt:
print("")
sys.exit()
except (TorTooOldEphemeral, TorTooOldStealth, TorErrorProtocolError) as e:
print("")
print(e.args[0])
sys.exit()
2019-10-13 00:01:25 -04:00
if mode == "website":
# Prepare files to share
try:
web.website_mode.set_file_info(filenames)
except OSError as e:
print(e.strerror)
sys.exit(1)
2019-10-13 00:01:25 -04:00
if mode == "share":
# Prepare files to share
2019-04-19 20:31:34 -04:00
print("Compressing files.")
try:
web.share_mode.set_file_info(filenames)
app.cleanup_filenames += web.share_mode.cleanup_filenames
except OSError as e:
print(e.strerror)
sys.exit(1)
# Warn about sending large files over Tor
if web.share_mode.download_filesize >= 157286400: # 150mb
2019-10-13 00:01:25 -04:00
print("")
2019-04-19 20:31:34 -04:00
print("Warning: Sending a large share could take hours")
2019-10-13 00:01:25 -04:00
print("")
# Start OnionShare http service in new thread
t = threading.Thread(target=web.start, args=(app.port,))
t.daemon = True
t.start()
try: # Trap Ctrl-C
2019-05-21 01:18:49 -04:00
# Wait for web.generate_password() to finish running
time.sleep(0.2)
# start auto-stop timer thread
if app.autostop_timer > 0:
app.autostop_timer_thread.start()
2019-05-21 01:18:49 -04:00
# Save the web password if we are using a persistent private key
if mode_settings.get("persistent", "enabled"):
if not mode_settings.get("onion", "password"):
mode_settings.set("onion", "password", web.password)
# mode_settings.save()
# Build the URL
url = build_url(mode_settings, app, web)
2019-10-13 00:01:25 -04:00
print("")
if autostart_timer > 0:
2019-04-19 20:31:34 -04:00
print("Server started")
else:
2019-10-13 00:01:25 -04:00
if mode == "receive":
print(
f"Files sent to you appear in this folder: {mode_settings.get('receive', 'data_dir')}"
2019-10-13 00:01:25 -04:00
)
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."
)
print("")
if mode_settings.get("general", "client_auth"):
2019-04-19 20:31:34 -04:00
print("Give this address and HidServAuth to the sender:")
print(url)
print(app.auth_string)
else:
2019-04-19 20:31:34 -04:00
print("Give this address to the sender:")
print(url)
else:
if mode_settings.get("general", "client_auth"):
2019-04-19 20:31:34 -04:00
print("Give this address and HidServAuth line to the recipient:")
print(url)
print(app.auth_string)
else:
2019-04-19 20:31:34 -04:00
print("Give this address to the recipient:")
print(url)
2019-10-13 00:01:25 -04:00
print("")
2019-04-19 20:31:34 -04:00
print("Press Ctrl+C to stop the server")
# Wait for app to close
while t.is_alive():
if app.autostop_timer > 0:
# if the auto-stop timer was set and has run out, stop the server
if not app.autostop_timer_thread.is_alive():
2019-10-13 00:01:25 -04:00
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.cur_history_id == 0 or web.done:
2019-04-19 20:31:34 -04:00
print("Stopped because auto-stop timer ran out")
web.stop(app.port)
break
2019-10-13 00:01:25 -04:00
if mode == "receive":
if (
web.receive_mode.cur_history_id == 0
or not web.receive_mode.uploads_in_progress
):
2019-04-19 20:31:34 -04:00
print("Stopped because auto-stop timer ran out")
web.stop(app.port)
break
2021-03-10 03:40:06 -05:00
web.receive_mode.can_upload = False
# Allow KeyboardInterrupt exception to be handled with threads
# https://stackoverflow.com/questions/3788208/python-threading-ignores-keyboardinterrupt-exception
time.sleep(0.2)
except KeyboardInterrupt:
web.stop(app.port)
finally:
# Shutdown
app.cleanup()
onion.cleanup()
2019-10-13 00:01:25 -04:00
if __name__ == "__main__":
main()