2014-09-02 17:30:01 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 12:10:42 -07:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2018-04-24 10:07:59 -07:00
|
|
|
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
2014-09-02 12:10:42 -07:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
"""
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2017-04-17 19:28:51 -07:00
|
|
|
import os, sys, time, argparse, threading
|
2019-03-11 15:55:17 +11:00
|
|
|
from datetime import datetime
|
|
|
|
from datetime import timedelta
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2018-03-08 10:18:31 -08:00
|
|
|
from . import strings
|
2018-10-25 21:13:16 -07:00
|
|
|
from .common import Common
|
2018-03-05 11:06:59 -08:00
|
|
|
from .web import Web
|
2017-04-08 18:10:17 -07:00
|
|
|
from .onion import *
|
2017-04-17 19:28:51 -07:00
|
|
|
from .onionshare import OnionShare
|
2017-01-06 19:00:08 -08:00
|
|
|
|
|
|
|
def main(cwd=None):
|
|
|
|
"""
|
|
|
|
The main() function implements all of the logic that the command-line version of
|
|
|
|
onionshare uses.
|
|
|
|
"""
|
2018-03-08 10:18:31 -08:00
|
|
|
common = Common()
|
|
|
|
|
2018-10-01 15:32:53 +10:00
|
|
|
# Load the default settings and strings early, for the sake of being able to parse options.
|
|
|
|
# These won't be in the user's chosen locale necessarily, but we need to parse them
|
|
|
|
# early in order to even display the option to pass alternate settings (which might
|
|
|
|
# contain a preferred locale).
|
|
|
|
# If an alternate --config is passed, we'll reload strings later.
|
|
|
|
common.load_settings()
|
2017-05-16 11:05:48 -07:00
|
|
|
strings.load_strings(common)
|
2018-09-30 17:06:29 -07:00
|
|
|
|
|
|
|
# Display OnionShare banner
|
2018-03-08 10:18:31 -08:00
|
|
|
print(strings._('version_string').format(common.version))
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# OnionShare CLI in OSX needs to change current working directory (#132)
|
2018-03-08 10:18:31 -08:00
|
|
|
if common.platform == 'Darwin':
|
2017-01-06 19:00:08 -08:00
|
|
|
if cwd:
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Parse arguments
|
2017-11-12 10:40:04 +11:00
|
|
|
parser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=28))
|
2017-01-06 19:00:08 -08:00
|
|
|
parser.add_argument('--local-only', action='store_true', dest='local_only', help=strings._("help_local_only"))
|
|
|
|
parser.add_argument('--stay-open', action='store_true', dest='stay_open', help=strings._("help_stay_open"))
|
2019-03-24 18:19:50 +11:00
|
|
|
parser.add_argument('--autostart-timer', metavar='<int>', dest='autostart_timer', default=0, help=strings._("help_autostart_timer"))
|
|
|
|
parser.add_argument('--autostop-timer', metavar='<int>', dest='autostop_timer', default=0, help=strings._("help_autostop_timer"))
|
2019-03-12 15:29:07 +11:00
|
|
|
parser.add_argument('--connect-timeout', metavar='<int>', dest='connect_timeout', default=120, help=strings._("help_connect_timeout"))
|
2017-01-06 19:00:08 -08:00
|
|
|
parser.add_argument('--stealth', action='store_true', dest='stealth', help=strings._("help_stealth"))
|
2018-03-05 07:45:10 -08:00
|
|
|
parser.add_argument('--receive', action='store_true', dest='receive', help=strings._("help_receive"))
|
2017-06-01 17:35:27 +10:00
|
|
|
parser.add_argument('--config', metavar='config', default=False, help=strings._('help_config'))
|
2018-03-05 07:45:10 -08:00
|
|
|
parser.add_argument('--debug', action='store_true', dest='debug', help=strings._("help_debug"))
|
|
|
|
parser.add_argument('filename', metavar='filename', nargs='*', help=strings._('help_filename'))
|
2017-01-06 19:00:08 -08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
filenames = args.filename
|
|
|
|
for i in range(len(filenames)):
|
|
|
|
filenames[i] = os.path.abspath(filenames[i])
|
|
|
|
|
|
|
|
local_only = bool(args.local_only)
|
|
|
|
debug = bool(args.debug)
|
|
|
|
stay_open = bool(args.stay_open)
|
2019-03-24 18:19:50 +11:00
|
|
|
autostart_timer = int(args.autostart_timer)
|
|
|
|
autostop_timer = int(args.autostop_timer)
|
2019-03-12 15:29:07 +11:00
|
|
|
connect_timeout = int(args.connect_timeout)
|
2017-01-06 19:00:08 -08:00
|
|
|
stealth = bool(args.stealth)
|
2018-03-05 07:45:10 -08:00
|
|
|
receive = bool(args.receive)
|
2017-06-01 17:35:27 +10:00
|
|
|
config = args.config
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2018-09-21 11:19:36 -07:00
|
|
|
if receive:
|
|
|
|
mode = 'receive'
|
|
|
|
else:
|
|
|
|
mode = 'share'
|
|
|
|
|
2018-03-05 07:45:10 -08:00
|
|
|
# Make sure filenames given if not using receiver mode
|
2018-09-21 11:19:36 -07:00
|
|
|
if mode == 'share' and len(filenames) == 0:
|
2018-09-21 13:58:42 -07:00
|
|
|
parser.print_help()
|
2018-03-05 07:45:10 -08:00
|
|
|
sys.exit()
|
|
|
|
|
2018-03-06 07:40:57 -08:00
|
|
|
# Validate filenames
|
2018-09-21 11:19:36 -07:00
|
|
|
if mode == 'share':
|
2018-03-06 07:40:57 -08:00
|
|
|
valid = True
|
|
|
|
for filename in filenames:
|
|
|
|
if not os.path.isfile(filename) and not os.path.isdir(filename):
|
|
|
|
print(strings._("not_a_file").format(filename))
|
|
|
|
valid = False
|
|
|
|
if not os.access(filename, os.R_OK):
|
|
|
|
print(strings._("not_a_readable_file").format(filename))
|
|
|
|
valid = False
|
|
|
|
if not valid:
|
|
|
|
sys.exit()
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2018-09-30 17:06:29 -07:00
|
|
|
# Re-load settings, if a custom config was passed in
|
|
|
|
if config:
|
|
|
|
common.load_settings(config)
|
2018-10-01 15:32:53 +10:00
|
|
|
# Re-load the strings, in case the provided config has changed locale
|
|
|
|
strings.load_strings(common)
|
2018-03-13 03:28:47 -07:00
|
|
|
|
|
|
|
# Debug mode?
|
|
|
|
common.debug = debug
|
2018-01-15 10:01:34 +11:00
|
|
|
|
2018-03-05 11:06:59 -08:00
|
|
|
# Create the Web object
|
2018-09-21 11:19:36 -07:00
|
|
|
web = Web(common, False, mode)
|
2018-03-05 11:06:59 -08:00
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Start the Onion object
|
2018-03-08 10:18:31 -08:00
|
|
|
onion = Onion(common)
|
2017-01-06 19:00:08 -08:00
|
|
|
try:
|
2019-03-12 15:29:07 +11:00
|
|
|
onion.connect(custom_settings=False, config=config, connect_timeout=connect_timeout)
|
2017-01-06 19:00:08 -08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("")
|
|
|
|
sys.exit()
|
2018-04-28 13:59:36 -07:00
|
|
|
except Exception as e:
|
|
|
|
sys.exit(e.args[0])
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Start the onionshare app
|
|
|
|
try:
|
2019-03-05 10:28:27 +11:00
|
|
|
common.settings.load()
|
|
|
|
if not common.settings.get('public_mode'):
|
|
|
|
web.generate_slug(common.settings.get('slug'))
|
|
|
|
else:
|
|
|
|
web.slug = None
|
2019-03-24 18:19:50 +11:00
|
|
|
app = OnionShare(common, onion, local_only, autostop_timer)
|
2017-04-17 19:12:02 -07:00
|
|
|
app.set_stealth(stealth)
|
2018-04-28 15:00:23 -07:00
|
|
|
app.choose_port()
|
2019-03-05 10:28:27 +11:00
|
|
|
# Delay the startup if a startup timer was set
|
2019-03-24 18:19:50 +11:00
|
|
|
if autostart_timer > 0:
|
2019-03-24 16:35:53 +11:00
|
|
|
# Can't set a schedule that is later than the shutdown timer
|
2019-03-24 18:19:50 +11:00
|
|
|
if app.shutdown_timeout > 0 and app.shutdown_timeout < autostart_timer:
|
2019-03-24 16:35:53 +11:00
|
|
|
print(strings._('gui_timeout_cant_be_earlier_than_startup'))
|
|
|
|
sys.exit()
|
|
|
|
|
2019-03-05 10:28:27 +11:00
|
|
|
app.start_onion_service(False, True)
|
|
|
|
if common.settings.get('public_mode'):
|
|
|
|
url = 'http://{0:s}'.format(app.onion_host)
|
|
|
|
else:
|
|
|
|
url = 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug)
|
2019-03-24 18:19:50 +11:00
|
|
|
schedule = datetime.now() + timedelta(seconds=autostart_timer)
|
2019-03-11 15:55:17 +11:00
|
|
|
if mode == 'receive':
|
|
|
|
print(strings._('receive_mode_data_dir').format(common.settings.get('data_dir')))
|
|
|
|
print('')
|
|
|
|
print(strings._('receive_mode_warning'))
|
|
|
|
print('')
|
|
|
|
if stealth:
|
|
|
|
print(strings._("give_this_scheduled_url_receive_stealth").format(schedule.strftime("%b %d, %I:%M:%S%p")))
|
|
|
|
print(app.auth_string)
|
|
|
|
else:
|
|
|
|
print(strings._("give_this_scheduled_url_receive").format(schedule.strftime("%b %d, %I:%M:%S%p")))
|
|
|
|
else:
|
|
|
|
if stealth:
|
|
|
|
print(strings._("give_this_scheduled_url_share_stealth").format(schedule.strftime("%b %d, %I:%M:%S%p")))
|
|
|
|
print(app.auth_string)
|
|
|
|
else:
|
|
|
|
print(strings._("give_this_scheduled_url_share").format(schedule.strftime("%b %d, %I:%M:%S%p")))
|
|
|
|
print(url)
|
|
|
|
print('')
|
|
|
|
print(strings._("waiting_for_scheduled_time"))
|
|
|
|
app.onion.cleanup(False)
|
2019-03-24 18:19:50 +11:00
|
|
|
time.sleep(autostart_timer)
|
2019-03-05 10:28:27 +11:00
|
|
|
app.start_onion_service()
|
|
|
|
else:
|
|
|
|
app.start_onion_service()
|
2017-04-17 19:12:02 -07:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("")
|
|
|
|
sys.exit()
|
2018-09-18 17:17:25 -07:00
|
|
|
except (TorTooOld, TorErrorProtocolError) as e:
|
|
|
|
print("")
|
|
|
|
print(e.args[0])
|
|
|
|
sys.exit()
|
2017-04-17 19:12:02 -07:00
|
|
|
|
2018-09-21 11:19:36 -07:00
|
|
|
if mode == 'share':
|
|
|
|
# Prepare files to share
|
|
|
|
print(strings._("preparing_files"))
|
|
|
|
try:
|
|
|
|
web.share_mode.set_file_info(filenames)
|
2018-09-21 12:29:23 -07:00
|
|
|
app.cleanup_filenames += web.share_mode.cleanup_filenames
|
2018-09-21 11:19:36 -07:00
|
|
|
except OSError as e:
|
|
|
|
print(e.strerror)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Warn about sending large files over Tor
|
2018-09-21 11:36:19 -07:00
|
|
|
if web.share_mode.download_filesize >= 157286400: # 150mb
|
2018-09-21 11:19:36 -07:00
|
|
|
print('')
|
|
|
|
print(strings._("large_filesize"))
|
|
|
|
print('')
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Start OnionShare http service in new thread
|
2019-03-05 10:28:27 +11:00
|
|
|
t = threading.Thread(target=web.start, args=(app.port, stay_open, common.settings.get('public_mode'), web.slug))
|
2017-01-06 19:00:08 -08:00
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
try: # Trap Ctrl-C
|
2017-02-22 14:10:06 -08:00
|
|
|
# Wait for web.generate_slug() to finish running
|
|
|
|
time.sleep(0.2)
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2017-11-08 20:25:59 +11:00
|
|
|
# start shutdown timer thread
|
|
|
|
if app.shutdown_timeout > 0:
|
|
|
|
app.shutdown_timer.start()
|
|
|
|
|
2018-01-15 10:01:34 +11:00
|
|
|
# Save the web slug if we are using a persistent private key
|
2018-03-13 03:28:47 -07:00
|
|
|
if common.settings.get('save_private_key'):
|
|
|
|
if not common.settings.get('slug'):
|
|
|
|
common.settings.set('slug', web.slug)
|
|
|
|
common.settings.save()
|
2018-01-15 10:01:34 +11:00
|
|
|
|
2018-04-29 16:44:45 -07:00
|
|
|
# Build the URL
|
2018-07-21 17:06:11 +10:00
|
|
|
if common.settings.get('public_mode'):
|
2018-04-29 16:44:45 -07:00
|
|
|
url = 'http://{0:s}'.format(app.onion_host)
|
|
|
|
else:
|
|
|
|
url = 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug)
|
|
|
|
|
2018-03-06 03:24:17 -08:00
|
|
|
print('')
|
2019-03-24 18:19:50 +11:00
|
|
|
if autostart_timer > 0:
|
2019-03-11 15:55:17 +11:00
|
|
|
print(strings._('server_started'))
|
2017-01-06 19:00:08 -08:00
|
|
|
else:
|
2019-03-11 15:55:17 +11:00
|
|
|
if mode == 'receive':
|
|
|
|
print(strings._('receive_mode_data_dir').format(common.settings.get('data_dir')))
|
|
|
|
print('')
|
|
|
|
print(strings._('receive_mode_warning'))
|
|
|
|
print('')
|
|
|
|
|
|
|
|
if stealth:
|
|
|
|
print(strings._("give_this_url_receive_stealth"))
|
|
|
|
print(url)
|
|
|
|
print(app.auth_string)
|
|
|
|
else:
|
|
|
|
print(strings._("give_this_url_receive"))
|
|
|
|
print(url)
|
2018-03-06 03:24:17 -08:00
|
|
|
else:
|
2019-03-11 15:55:17 +11:00
|
|
|
if stealth:
|
|
|
|
print(strings._("give_this_url_stealth"))
|
|
|
|
print(url)
|
|
|
|
print(app.auth_string)
|
|
|
|
else:
|
|
|
|
print(strings._("give_this_url"))
|
|
|
|
print(url)
|
2017-01-06 19:00:08 -08:00
|
|
|
print('')
|
|
|
|
print(strings._("ctrlc_to_stop"))
|
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Wait for app to close
|
2017-01-06 19:00:08 -08:00
|
|
|
while t.is_alive():
|
2017-11-08 20:25:59 +11:00
|
|
|
if app.shutdown_timeout > 0:
|
|
|
|
# if the shutdown timer was set and has run out, stop the server
|
2017-12-05 11:18:26 +11:00
|
|
|
if not app.shutdown_timer.is_alive():
|
2018-09-21 11:36:19 -07:00
|
|
|
if mode == 'share':
|
|
|
|
# If there were no attempts to download the share, or all downloads are done, we can stop
|
|
|
|
if web.share_mode.download_count == 0 or web.done:
|
|
|
|
print(strings._("close_on_timeout"))
|
|
|
|
web.stop(app.port)
|
|
|
|
break
|
2018-10-02 15:41:29 +10:00
|
|
|
if mode == 'receive':
|
|
|
|
if web.receive_mode.upload_count == 0 or not web.receive_mode.uploads_in_progress:
|
|
|
|
print(strings._("close_on_timeout"))
|
|
|
|
web.stop(app.port)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
web.receive_mode.can_upload = False
|
2017-04-17 19:12:02 -07:00
|
|
|
# Allow KeyboardInterrupt exception to be handled with threads
|
2017-02-22 13:35:34 -08:00
|
|
|
# https://stackoverflow.com/questions/3788208/python-threading-ignores-keyboardinterrupt-exception
|
2017-11-08 20:25:59 +11:00
|
|
|
time.sleep(0.2)
|
2017-01-06 19:00:08 -08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
web.stop(app.port)
|
|
|
|
finally:
|
2017-04-17 19:12:02 -07:00
|
|
|
# Shutdown
|
2017-01-06 19:00:08 -08:00
|
|
|
app.cleanup()
|
2017-05-14 17:21:13 -07:00
|
|
|
onion.cleanup()
|
2017-01-06 19:00:08 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|