waits for hidden service before displaying URL. fixes #116

This commit is contained in:
Micah Lee 2014-08-29 14:32:19 -07:00
parent 36914114f9
commit 846b10b755
5 changed files with 50 additions and 13 deletions

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os, sys, subprocess, time, argparse, inspect, shutil, socket import os, sys, subprocess, time, argparse, inspect, shutil, socks, socket, threading
from stem.control import Controller from stem.control import Controller
from stem import SocketError from stem import SocketError
@ -94,6 +94,24 @@ class OnionShare(object):
hostname_file = '{0}/hostname'.format(hidserv_dir) hostname_file = '{0}/hostname'.format(hidserv_dir)
self.onion_host = open(hostname_file, 'r').read().strip() self.onion_host = open(hostname_file, 'r').read().strip()
def wait_for_hs(self):
print strings._('wait_for_hs')
ready = False
while not ready:
try:
sys.stdout.write('{0} '.format(strings._('wait_for_hs_trying')))
sys.stdout.flush()
s = socks.socksocket()
s.setproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 9150)
s.connect((self.onion_host, 80))
ready = True
sys.stdout.write('{0}\n'.format(strings._('wait_for_hs_yup')))
except TypeError as e:
sys.stdout.write('{0}\n'.format(strings._('wait_for_hs_nope')))
sys.stdout.flush()
def tails_root(): def tails_root():
# if running in Tails and as root, do only the things that require root # if running in Tails and as root, do only the things that require root
if helpers.get_platform() == 'Tails' and helpers.is_root(): if helpers.get_platform() == 'Tails' and helpers.is_root():
@ -165,17 +183,30 @@ def main():
except TailsError as e: except TailsError as e:
sys.exit(e.args[0]) sys.exit(e.args[0])
# startup # prepare files to share
web.set_file_info(filenames) web.set_file_info(filenames)
app.cleanup_filenames.append(web.zip_filename) app.cleanup_filenames.append(web.zip_filename)
# start onionshare service in new thread
t = threading.Thread(target=web.start, args=(app.port, app.stay_open))
t.daemon = True
t.start()
# wait for hs
app.wait_for_hs()
print strings._("give_this_url") print strings._("give_this_url")
print 'http://{0}/{1}'.format(app.onion_host, web.slug) print 'http://{0}/{1}'.format(app.onion_host, web.slug)
print '' print ''
print strings._("ctrlc_to_stop") print strings._("ctrlc_to_stop")
# start the web server # wait for app to close
web.start(app.port, app.stay_open) running = True
print '\n' while running:
try:
time.sleep(0.5)
except KeyboardInterrupt:
running = False
web.stop()
# shutdown # shutdown
app.cleanup() app.cleanup()

View file

@ -2,6 +2,10 @@
"calculating_sha1": "Calculating SHA1 checksum.", "calculating_sha1": "Calculating SHA1 checksum.",
"connecting_ctrlport": "Connecting to Tor control port to set up hidden service on port {0}.", "connecting_ctrlport": "Connecting to Tor control port to set up hidden service on port {0}.",
"cant_connect_ctrlport": "Cannot connect to Tor control port on port {0}. Is Tor running?", "cant_connect_ctrlport": "Cannot connect to Tor control port on port {0}. Is Tor running?",
"wait_for_hs": "Waiting for hidden service to be ready:",
"wait_for_hs_trying": "Trying...",
"wait_for_hs_nope": "Not ready yet.",
"wait_for_hs_yup": "Ready!",
"give_this_url": "Give this URL to the person you're sending the file to:", "give_this_url": "Give this URL to the person you're sending the file to:",
"ctrlc_to_stop": "Press Ctrl-C to stop server", "ctrlc_to_stop": "Press Ctrl-C to stop server",
"not_a_file": "{0} is not a file.", "not_a_file": "{0} is not a file.",

View file

@ -1,4 +1,4 @@
import Queue, mimetypes, platform, os, sys, zipfile import Queue, mimetypes, platform, os, sys, zipfile, urllib2
from flask import Flask, Response, request, render_template_string, abort from flask import Flask, Response, request, render_template_string, abort
import strings, helpers import strings, helpers
@ -187,3 +187,9 @@ def start(port, stay_open=False, gui_mode=False):
set_gui_mode(gui_mode) set_gui_mode(gui_mode)
app.run(port=port) app.run(port=port)
def stop():
# to stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown
try:
urllib2.urlopen('http://127.0.0.1:{0}/{1}/shutdown'.format(app.port, shutdown_slug)).read()
except:
pass

View file

@ -1,5 +1,5 @@
from __future__ import division from __future__ import division
import os, sys, subprocess, inspect, platform, argparse, threading, time, math, inspect, platform, urllib2 import os, sys, subprocess, inspect, platform, argparse, threading, time, math, inspect, platform
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
import common import common
@ -101,11 +101,7 @@ class OnionShareGui(QtGui.QWidget):
t.start() t.start()
def stop_server(self): def stop_server(self):
# to stop flask, load http://127.0.0.1:<port>/<shutdown_slug>/shutdown web.stop()
try:
urllib2.urlopen('http://127.0.0.1:{0}/{1}/shutdown'.format(self.app.port, web.shutdown_slug)).read()
except:
pass
self.app.cleanup() self.app.cleanup()
self.stop_server_finished.emit() self.stop_server_finished.emit()

View file

@ -1,4 +1,4 @@
[DEFAULT] [DEFAULT]
Package: onionshare Package: onionshare
Depends: python-flask, python-stem Depends: python-flask, python-stem, python-socksipy
Suite: trusty Suite: trusty