2014-05-29 21:05:30 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import onionshare, webgui
|
2014-05-30 21:25:26 -04:00
|
|
|
import os, sys, time, json, gtk, thread
|
2014-05-29 21:05:30 -04:00
|
|
|
|
|
|
|
class Global(object):
|
|
|
|
quit = False
|
|
|
|
@classmethod
|
|
|
|
def set_quit(cls, *args, **kwargs):
|
|
|
|
cls.quit = True
|
|
|
|
|
2014-05-30 20:46:24 -04:00
|
|
|
def alert(msg, type=gtk.MESSAGE_INFO):
|
|
|
|
dialog = gtk.MessageDialog(
|
|
|
|
parent=None,
|
|
|
|
flags=gtk.DIALOG_MODAL,
|
|
|
|
type=type,
|
|
|
|
buttons=gtk.BUTTONS_OK,
|
|
|
|
message_format=msg)
|
|
|
|
response = dialog.run()
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
|
|
def select_file(strings):
|
|
|
|
# get filename, either from argument or file chooser dialog
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
filename = sys.argv[1]
|
|
|
|
else:
|
|
|
|
canceled = False
|
|
|
|
chooser = gtk.FileChooserDialog(
|
|
|
|
title="Choose a file to share",
|
|
|
|
action=gtk.FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
|
|
|
|
response = chooser.run()
|
|
|
|
if response == gtk.RESPONSE_OK:
|
|
|
|
filename = chooser.get_filename()
|
|
|
|
elif response == gtk.RESPONSE_CANCEL:
|
|
|
|
canceled = True
|
|
|
|
chooser.destroy()
|
|
|
|
|
|
|
|
if canceled:
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
# validate filename
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
alert(strings["not_a_file"].format(filename), gtk.MESSAGE_ERROR)
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
filename = os.path.abspath(filename)
|
|
|
|
basename = os.path.basename(filename)
|
|
|
|
return filename, basename
|
|
|
|
|
2014-05-29 21:05:30 -04:00
|
|
|
def main():
|
2014-05-30 20:46:24 -04:00
|
|
|
strings = onionshare.load_strings()
|
|
|
|
|
2014-05-30 21:03:53 -04:00
|
|
|
# try starting hidden service
|
|
|
|
port = onionshare.choose_port()
|
|
|
|
try:
|
|
|
|
onion_host = onionshare.start_hidden_service(port)
|
|
|
|
except onionshare.NoTor as e:
|
|
|
|
alert(e.args[0], gtk.MESSAGE_ERROR)
|
|
|
|
return
|
|
|
|
|
2014-05-30 20:46:24 -04:00
|
|
|
# select file to share
|
|
|
|
filename, basename = select_file(strings)
|
2014-05-29 22:31:28 -04:00
|
|
|
if not filename:
|
2014-05-29 21:05:30 -04:00
|
|
|
return
|
|
|
|
|
2014-05-29 22:31:28 -04:00
|
|
|
# open the window, launching webkit browser
|
2014-05-29 21:05:30 -04:00
|
|
|
webgui.start_gtk_thread()
|
2014-05-29 22:31:28 -04:00
|
|
|
browser, web_recv, web_send = webgui.sync_gtk_msg(webgui.launch_window)(
|
|
|
|
title="OnionShare | {0}".format(basename),
|
|
|
|
quit_function=Global.set_quit)
|
|
|
|
time.sleep(0.1)
|
2014-05-29 23:23:40 -04:00
|
|
|
|
2014-05-30 21:03:53 -04:00
|
|
|
# startup
|
2014-05-30 20:46:24 -04:00
|
|
|
web_send("init('{0}', {1});".format(basename, json.dumps(strings)))
|
2014-05-29 23:23:40 -04:00
|
|
|
web_send("update('{0}')".format(strings['calculating_sha1']))
|
|
|
|
filehash, filesize = onionshare.file_crunching(filename)
|
2014-05-30 21:25:26 -04:00
|
|
|
onionshare.set_file_info(filename, filehash, filesize)
|
2014-05-29 23:23:40 -04:00
|
|
|
onionshare.tails_open_port(port)
|
|
|
|
url = 'http://{0}/{1}'.format(onion_host, onionshare.slug)
|
|
|
|
web_send("update('{0}')".format('Secret URL is {0}'.format(url)))
|
|
|
|
web_send("set_url('{0}')".format(url));
|
2014-05-29 22:31:28 -04:00
|
|
|
|
2014-05-29 23:27:29 -04:00
|
|
|
# start the web server
|
2014-05-30 21:25:26 -04:00
|
|
|
web_thread = thread.start_new_thread(onionshare.app.run, (), {"port": port})
|
2014-05-29 23:27:29 -04:00
|
|
|
|
2014-05-29 22:31:28 -04:00
|
|
|
# main loop
|
2014-05-29 21:05:30 -04:00
|
|
|
last_second = time.time()
|
|
|
|
uptime_seconds = 1
|
|
|
|
clicks = 0
|
|
|
|
while not Global.quit:
|
|
|
|
|
|
|
|
current_time = time.time()
|
|
|
|
again = False
|
|
|
|
msg = web_recv()
|
|
|
|
if msg:
|
|
|
|
msg = json.loads(msg)
|
|
|
|
again = True
|
|
|
|
|
2014-05-29 22:31:28 -04:00
|
|
|
# check msg for messages from the browser
|
|
|
|
# use web_send() to send javascript to the browser
|
2014-05-29 22:53:35 -04:00
|
|
|
|
2014-05-29 22:31:28 -04:00
|
|
|
if not again:
|
2014-05-29 21:05:30 -04:00
|
|
|
time.sleep(0.1)
|
2014-05-30 20:46:24 -04:00
|
|
|
|
2014-05-29 23:27:29 -04:00
|
|
|
# shutdown
|
|
|
|
onionshare.tails_close_port(port)
|
2014-05-29 21:05:30 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|