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
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2018-03-08 10:18:31 -08:00
|
|
|
from . import strings
|
2018-05-19 20:51:01 -07:00
|
|
|
from .common import Common, DownloadsDirErrorCannotCreate, DownloadsDirErrorNotWritable
|
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()
|
|
|
|
|
2017-05-16 11:05:48 -07:00
|
|
|
strings.load_strings(common)
|
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"))
|
2017-11-11 17:12:10 +11:00
|
|
|
parser.add_argument('--shutdown-timeout', metavar='<int>', dest='shutdown_timeout', default=0, help=strings._("help_shutdown_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)
|
2017-11-09 19:50:50 +11:00
|
|
|
shutdown_timeout = int(args.shutdown_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-03-05 07:45:10 -08:00
|
|
|
# Make sure filenames given if not using receiver mode
|
|
|
|
if not receive and len(filenames) == 0:
|
|
|
|
print(strings._('no_filenames'))
|
|
|
|
sys.exit()
|
|
|
|
|
2018-03-06 07:40:57 -08:00
|
|
|
# Validate filenames
|
|
|
|
if not receive:
|
|
|
|
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-03-05 11:06:59 -08:00
|
|
|
# Load settings
|
2018-03-13 03:28:47 -07:00
|
|
|
common.load_settings(config)
|
|
|
|
|
|
|
|
# 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-04-26 09:30:53 -07:00
|
|
|
web = Web(common, False, receive)
|
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:
|
2018-03-13 03:28:47 -07:00
|
|
|
onion.connect(custom_settings=False, config=config)
|
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:
|
2018-05-04 16:35:32 -07:00
|
|
|
app = OnionShare(common, onion, local_only, shutdown_timeout)
|
2017-04-17 19:12:02 -07:00
|
|
|
app.set_stealth(stealth)
|
2018-04-28 15:00:23 -07:00
|
|
|
app.choose_port()
|
2017-04-17 19:12:02 -07:00
|
|
|
app.start_onion_service()
|
|
|
|
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
|
|
|
|
|
|
|
# Prepare files to share
|
2017-01-06 19:00:08 -08:00
|
|
|
print(strings._("preparing_files"))
|
2018-01-14 20:36:43 +11:00
|
|
|
try:
|
2018-09-21 11:14:32 -07:00
|
|
|
web.share_mode.set_file_info(filenames)
|
2018-09-20 23:18:17 -07:00
|
|
|
if web.is_zipped:
|
|
|
|
app.cleanup_filenames.append(web.download_filename)
|
2018-01-14 20:36:43 +11:00
|
|
|
except OSError as e:
|
|
|
|
print(e.strerror)
|
|
|
|
sys.exit(1)
|
2017-01-06 19:00:08 -08:00
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Warn about sending large files over Tor
|
2018-09-20 09:14:56 -07:00
|
|
|
if web.download_filesize >= 157286400: # 150mb
|
2017-01-06 19:00:08 -08:00
|
|
|
print('')
|
|
|
|
print(strings._("large_filesize"))
|
|
|
|
print('')
|
|
|
|
|
2017-04-17 19:12:02 -07:00
|
|
|
# Start OnionShare http service in new thread
|
2018-07-21 17:06:11 +10:00
|
|
|
t = threading.Thread(target=web.start, args=(app.port, stay_open, common.settings.get('public_mode'), common.settings.get('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('')
|
|
|
|
if receive:
|
2018-04-24 17:18:18 -07:00
|
|
|
print(strings._('receive_mode_downloads_dir').format(common.settings.get('downloads_dir')))
|
|
|
|
print('')
|
2018-03-14 07:35:04 -07:00
|
|
|
print(strings._('receive_mode_warning'))
|
|
|
|
print('')
|
|
|
|
|
2018-03-06 03:24:17 -08:00
|
|
|
if stealth:
|
|
|
|
print(strings._("give_this_url_receive_stealth"))
|
2018-04-29 16:44:45 -07:00
|
|
|
print(url)
|
2018-03-06 03:24:17 -08:00
|
|
|
print(app.auth_string)
|
|
|
|
else:
|
|
|
|
print(strings._("give_this_url_receive"))
|
2018-04-29 16:44:45 -07:00
|
|
|
print(url)
|
2017-01-06 19:00:08 -08:00
|
|
|
else:
|
2018-03-06 03:24:17 -08:00
|
|
|
if stealth:
|
|
|
|
print(strings._("give_this_url_stealth"))
|
2018-04-29 16:44:45 -07:00
|
|
|
print(url)
|
2018-03-06 03:24:17 -08:00
|
|
|
print(app.auth_string)
|
|
|
|
else:
|
|
|
|
print(strings._("give_this_url"))
|
2018-04-29 16:44:45 -07:00
|
|
|
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():
|
|
|
|
# If there were no attempts to download the share, or all downloads are done, we can stop
|
|
|
|
if web.download_count == 0 or web.done:
|
|
|
|
print(strings._("close_on_timeout"))
|
|
|
|
web.stop(app.port)
|
|
|
|
break
|
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()
|