2014-09-02 20:30:01 -04:00
# -*- coding: utf-8 -*-
2014-09-02 15:10:42 -04:00
"""
OnionShare | https : / / onionshare . org /
2018-04-24 13:07:59 -04:00
Copyright ( C ) 2014 - 2018 Micah Lee < micah @micahflee.com >
2014-09-02 15:10:42 -04: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 22:00:08 -05:00
2017-04-17 22:28:51 -04:00
import os , sys , time , argparse , threading
2019-03-11 00:55:17 -04:00
from datetime import datetime
from datetime import timedelta
2017-01-06 22:00:08 -05:00
2018-10-26 00:13:16 -04:00
from . common import Common
2018-03-05 14:06:59 -05:00
from . web import Web
2017-04-08 21:10:17 -04:00
from . onion import *
2017-04-17 22:28:51 -04:00
from . onionshare import OnionShare
2017-01-06 22:00:08 -05:00
2019-05-21 13:08:54 -04:00
def build_url ( common , app , web ) :
# Build the URL
2019-10-13 00:01:25 -04:00
if common . settings . get ( " public_mode " ) :
2019-10-20 13:15:16 -04:00
return f " http:// { app . onion_host } "
2019-05-21 13:08:54 -04:00
else :
2019-10-20 13:15:16 -04:00
return f " http://onionshare: { web . password } @ { app . onion_host } "
2019-05-21 13:08:54 -04:00
2017-01-06 22:00:08 -05:00
def main ( cwd = None ) :
"""
The main ( ) function implements all of the logic that the command - line version of
onionshare uses .
"""
2018-03-08 13:18:31 -05:00
common = Common ( )
2018-09-30 20:06:29 -04:00
# Display OnionShare banner
2019-10-20 13:15:16 -04:00
print ( f " OnionShare { common . version } | https://onionshare.org/ " )
2017-01-06 22:00:08 -05:00
2017-04-17 22:12:02 -04:00
# OnionShare CLI in OSX needs to change current working directory (#132)
2019-10-13 00:01:25 -04:00
if common . platform == " Darwin " :
2017-01-06 22:00:08 -05:00
if cwd :
os . chdir ( cwd )
2017-04-17 22:12:02 -04:00
# Parse arguments
2019-10-13 00:01:25 -04:00
parser = argparse . ArgumentParser (
formatter_class = lambda prog : argparse . HelpFormatter ( prog , max_help_position = 28 )
)
parser . add_argument (
" --local-only " ,
action = " store_true " ,
dest = " local_only " ,
help = " Don ' t use Tor (only for development) " ,
)
parser . add_argument (
" --stay-open " ,
action = " store_true " ,
dest = " stay_open " ,
help = " Continue sharing after files have been sent " ,
)
parser . add_argument (
" --auto-start-timer " ,
metavar = " <int> " ,
dest = " autostart_timer " ,
default = 0 ,
help = " Schedule this share to start N seconds from now " ,
)
parser . add_argument (
" --auto-stop-timer " ,
metavar = " <int> " ,
dest = " autostop_timer " ,
default = 0 ,
help = " Stop sharing after a given amount of seconds " ,
)
parser . add_argument (
" --connect-timeout " ,
metavar = " <int> " ,
dest = " connect_timeout " ,
default = 120 ,
help = " Give up connecting to Tor after a given amount of seconds (default: 120) " ,
)
parser . add_argument (
" --stealth " ,
action = " store_true " ,
dest = " stealth " ,
help = " Use client authorization (advanced) " ,
)
parser . add_argument (
" --receive " ,
action = " store_true " ,
dest = " receive " ,
help = " Receive shares instead of sending them " ,
)
parser . add_argument (
" --website " ,
action = " store_true " ,
dest = " website " ,
help = " Publish a static website " ,
)
parser . add_argument (
" --config " ,
metavar = " config " ,
default = False ,
help = " Custom JSON config file location (optional) " ,
)
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 " ,
)
2017-01-06 22:00:08 -05: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 )
2019-04-18 22:53:21 -04:00
verbose = bool ( args . verbose )
2017-01-06 22:00:08 -05:00
stay_open = bool ( args . stay_open )
2019-03-24 03:19:50 -04:00
autostart_timer = int ( args . autostart_timer )
autostop_timer = int ( args . autostop_timer )
2019-03-12 00:29:07 -04:00
connect_timeout = int ( args . connect_timeout )
2017-01-06 22:00:08 -05:00
stealth = bool ( args . stealth )
2018-03-05 10:45:10 -05:00
receive = bool ( args . receive )
2019-04-19 08:25:42 -04:00
website = bool ( args . website )
2017-06-01 03:35:27 -04:00
config = args . config
2017-01-06 22:00:08 -05:00
2018-09-21 14:19:36 -04:00
if receive :
2019-10-13 00:01:25 -04:00
mode = " receive "
2019-04-19 08:25:42 -04:00
elif website :
2019-10-13 00:01:25 -04:00
mode = " website "
2018-09-21 14:19:36 -04:00
else :
2019-10-13 00:01:25 -04:00
mode = " share "
2018-09-21 14:19:36 -04:00
2019-09-15 20:06:03 -04:00
# In share an website mode, you must supply a list of filenames
2019-10-13 00:01:25 -04:00
if mode == " share " or mode == " website " :
2019-09-15 20:06:03 -04:00
# Make sure filenames given if not using receiver mode
if len ( filenames ) == 0 :
parser . print_help ( )
sys . exit ( )
2018-03-05 10:45:10 -05:00
2019-09-15 20:06:03 -04:00
# Validate filenames
2018-03-06 10:40:57 -05:00
valid = True
for filename in filenames :
if not os . path . isfile ( filename ) and not os . path . isdir ( filename ) :
2019-10-20 13:15:16 -04:00
print ( f " { filename } is not a valid file. " )
2018-03-06 10:40:57 -05:00
valid = False
if not os . access ( filename , os . R_OK ) :
2019-10-20 13:15:16 -04:00
print ( f " { filename } is not a readable file. " )
2018-03-06 10:40:57 -05:00
valid = False
if not valid :
sys . exit ( )
2017-01-06 22:00:08 -05:00
2018-09-30 20:06:29 -04:00
# Re-load settings, if a custom config was passed in
if config :
common . load_settings ( config )
2019-09-01 23:45:19 -04:00
else :
common . load_settings ( )
2018-03-13 06:28:47 -04:00
2019-04-18 22:53:21 -04:00
# Verbose mode?
common . verbose = verbose
2018-01-14 18:01:34 -05:00
2018-03-05 14:06:59 -05:00
# Create the Web object
2018-09-21 14:19:36 -04:00
web = Web ( common , False , mode )
2018-03-05 14:06:59 -05:00
2017-04-17 22:12:02 -04:00
# Start the Onion object
2018-03-08 13:18:31 -05:00
onion = Onion ( common )
2017-01-06 22:00:08 -05:00
try :
2019-10-13 00:01:25 -04:00
onion . connect (
custom_settings = False , config = config , connect_timeout = connect_timeout
)
2017-01-06 22:00:08 -05:00
except KeyboardInterrupt :
print ( " " )
sys . exit ( )
2018-04-28 16:59:36 -04:00
except Exception as e :
sys . exit ( e . args [ 0 ] )
2017-01-06 22:00:08 -05:00
2017-04-17 22:12:02 -04:00
# Start the onionshare app
try :
2019-03-04 18:28:27 -05:00
common . settings . load ( )
2019-10-13 00:01:25 -04:00
if not common . settings . get ( " public_mode " ) :
web . generate_password ( common . settings . get ( " password " ) )
2019-03-04 18:28:27 -05:00
else :
2019-05-21 01:18:49 -04:00
web . password = None
2019-03-24 03:19:50 -04:00
app = OnionShare ( common , onion , local_only , autostop_timer )
2017-04-17 22:12:02 -04:00
app . set_stealth ( stealth )
2018-04-28 18:00:23 -04:00
app . choose_port ( )
2019-05-20 22:22:03 -04:00
2019-03-04 18:28:27 -05:00
# Delay the startup if a startup timer was set
2019-03-24 03:19:50 -04:00
if autostart_timer > 0 :
2019-03-25 00:05:54 -04:00
# Can't set a schedule that is later than the auto-stop timer
if app . autostop_timer > 0 and app . 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. "
)
2019-03-24 01:35:53 -04:00
sys . exit ( )
2019-03-04 18:28:27 -05:00
app . start_onion_service ( False , True )
2019-05-21 13:08:54 -04:00
url = build_url ( common , app , web )
2019-03-24 03:19:50 -04:00
schedule = datetime . now ( ) + timedelta ( seconds = autostart_timer )
2019-10-13 00:01:25 -04:00
if mode == " receive " :
print (
2019-10-20 13:15:16 -04:00
f " Files sent to you appear in this folder: { common . settings . get ( ' 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 ( " " )
2019-03-11 00:55:17 -04:00
if stealth :
2019-10-13 00:01:25 -04:00
print (
2019-10-20 13:15:16 -04:00
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
)
2019-03-11 00:55:17 -04:00
print ( app . auth_string )
else :
2019-10-13 00:01:25 -04:00
print (
2019-10-20 13:15:16 -04:00
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
)
2019-03-11 00:55:17 -04:00
else :
if stealth :
2019-10-13 00:01:25 -04:00
print (
2019-10-20 13:15:16 -04:00
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
)
2019-03-11 00:55:17 -04:00
print ( app . auth_string )
else :
2019-10-13 00:01:25 -04:00
print (
2019-10-20 13:15:16 -04:00
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
)
2019-03-11 00:55:17 -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... " )
2019-03-11 00:55:17 -04:00
app . onion . cleanup ( False )
2019-03-24 03:19:50 -04:00
time . sleep ( autostart_timer )
2019-03-04 18:28:27 -05:00
app . start_onion_service ( )
else :
app . start_onion_service ( )
2017-04-17 22:12:02 -04:00
except KeyboardInterrupt :
print ( " " )
sys . exit ( )
2018-09-18 20:17:25 -04:00
except ( TorTooOld , TorErrorProtocolError ) as e :
print ( " " )
print ( e . args [ 0 ] )
sys . exit ( )
2017-04-17 22:12:02 -04:00
2019-10-13 00:01:25 -04:00
if mode == " website " :
2019-04-19 08:25:42 -04:00
# 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 " :
2018-09-21 14:19:36 -04:00
# Prepare files to share
2019-04-19 20:31:34 -04:00
print ( " Compressing files. " )
2018-09-21 14:19:36 -04:00
try :
web . share_mode . set_file_info ( filenames )
2018-09-21 15:29:23 -04:00
app . cleanup_filenames + = web . share_mode . cleanup_filenames
2018-09-21 14:19:36 -04:00
except OSError as e :
print ( e . strerror )
sys . exit ( 1 )
# Warn about sending large files over Tor
2018-09-21 14:36:19 -04:00
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 ( " " )
2017-01-06 22:00:08 -05:00
2017-04-17 22:12:02 -04:00
# Start OnionShare http service in new thread
2019-10-13 00:01:25 -04:00
t = threading . Thread (
target = web . start ,
args = ( app . port , stay_open , common . settings . get ( " public_mode " ) , web . password ) ,
)
2017-01-06 22:00:08 -05:00
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
2017-02-22 17:10:06 -05:00
time . sleep ( 0.2 )
2017-01-06 22:00:08 -05:00
2019-03-25 00:05:54 -04:00
# start auto-stop timer thread
if app . autostop_timer > 0 :
app . autostop_timer_thread . start ( )
2017-11-08 04:25:59 -05:00
2019-05-21 01:18:49 -04:00
# Save the web password if we are using a persistent private key
2019-10-13 00:01:25 -04:00
if common . settings . get ( " save_private_key " ) :
if not common . settings . get ( " password " ) :
common . settings . set ( " password " , web . password )
2018-03-13 06:28:47 -04:00
common . settings . save ( )
2018-01-14 18:01:34 -05:00
2019-05-21 13:08:54 -04:00
# Build the URL
url = build_url ( common , app , web )
2019-10-13 00:01:25 -04:00
print ( " " )
2019-03-24 03:19:50 -04:00
if autostart_timer > 0 :
2019-04-19 20:31:34 -04:00
print ( " Server started " )
2017-01-06 22:00:08 -05:00
else :
2019-10-13 00:01:25 -04:00
if mode == " receive " :
print (
2019-10-20 13:15:16 -04:00
f " Files sent to you appear in this folder: { common . settings . get ( ' 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 ( " " )
2019-03-11 00:55:17 -04:00
if stealth :
2019-04-19 20:31:34 -04:00
print ( " Give this address and HidServAuth to the sender: " )
2019-03-11 00:55:17 -04:00
print ( url )
print ( app . auth_string )
else :
2019-04-19 20:31:34 -04:00
print ( " Give this address to the sender: " )
2019-03-11 00:55:17 -04:00
print ( url )
2018-03-06 06:24:17 -05:00
else :
2019-03-11 00:55:17 -04:00
if stealth :
2019-04-19 20:31:34 -04:00
print ( " Give this address and HidServAuth line to the recipient: " )
2019-03-11 00:55:17 -04:00
print ( url )
print ( app . auth_string )
else :
2019-04-19 20:31:34 -04:00
print ( " Give this address to the recipient: " )
2019-03-11 00:55:17 -04:00
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 " )
2017-01-06 22:00:08 -05:00
2017-04-17 22:12:02 -04:00
# Wait for app to close
2017-01-06 22:00:08 -05:00
while t . is_alive ( ) :
2019-03-25 00:05:54 -04:00
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 " ) :
2018-09-21 14:36:19 -04:00
# If there were no attempts to download the share, or all downloads are done, we can stop
2019-09-04 01:20:52 -04:00
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 " )
2018-09-21 14:36:19 -04:00
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 " )
2018-10-02 01:41:29 -04:00
web . stop ( app . port )
break
else :
web . receive_mode . can_upload = False
2017-04-17 22:12:02 -04:00
# Allow KeyboardInterrupt exception to be handled with threads
2017-02-22 16:35:34 -05:00
# https://stackoverflow.com/questions/3788208/python-threading-ignores-keyboardinterrupt-exception
2017-11-08 04:25:59 -05:00
time . sleep ( 0.2 )
2017-01-06 22:00:08 -05:00
except KeyboardInterrupt :
web . stop ( app . port )
finally :
2017-04-17 22:12:02 -04:00
# Shutdown
2017-01-06 22:00:08 -05:00
app . cleanup ( )
2017-05-14 20:21:13 -04:00
onion . cleanup ( )
2017-01-06 22:00:08 -05:00
2019-10-13 00:01:25 -04:00
if __name__ == " __main__ " :
2017-01-06 22:00:08 -05:00
main ( )