From 4868a9155b174e2d21c910fdad209b11823d984b Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Fri, 30 May 2014 21:25:26 -0400 Subject: [PATCH] started web server in separate thread. organized file info, so it can be set from both cli and gui. --- onionshare/onionshare.py | 14 +++++++++++--- onionshare_gui/onionshare_gui.py | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/onionshare/onionshare.py b/onionshare/onionshare.py index 40a96c81..4e72271d 100644 --- a/onionshare/onionshare.py +++ b/onionshare/onionshare.py @@ -26,12 +26,20 @@ strings = {} # generate an unguessable string slug = os.urandom(16).encode('hex') -# file information -filename = filehash = filesize = '' +# information about the file +filename = filesize = filehash = None +def set_file_info(new_filename, new_filesize, new_filehash): + global filename, filesize, filehash + filename = new_filename + filesize = new_filesize + filehash = new_filehash @app.route("/{0}".format(slug)) def index(): global filename, filesize, filehash, slug, strings + print 'filename: {0}'.format(filename) + print 'filehash: {0}'.format(filehash) + print 'filesize: {0}'.format(filesize) return render_template_string(open('{0}/index.html'.format(os.path.dirname(__file__))).read(), slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize, strings=strings) @@ -125,7 +133,6 @@ def start_hidden_service(port): return onion_host def main(): - global filename, filehash, filesize load_strings() # try starting hidden service @@ -148,6 +155,7 @@ def main(): # startup print strings["calculating_sha1"] filehash, filesize = file_crunching(filename) + set_file_info(filename, filehash, filesize) tails_open_port(port) print '\n' + strings["give_this_url"] print 'http://{0}/{1}'.format(onion_host, slug) diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 8adead85..492f2ad8 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import onionshare, webgui -import os, sys, time, json, gtk +import os, sys, time, json, gtk, thread class Global(object): quit = False @@ -75,14 +75,14 @@ def main(): web_send("init('{0}', {1});".format(basename, json.dumps(strings))) web_send("update('{0}')".format(strings['calculating_sha1'])) filehash, filesize = onionshare.file_crunching(filename) + onionshare.set_file_info(filename, filehash, filesize) 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)); # start the web server - # todo: start this in another thread, and send output using web_send - #onionshare.app.run(port=port) + web_thread = thread.start_new_thread(onionshare.app.run, (), {"port": port}) # main loop last_second = time.time()