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 -*-
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 import SocketError
@ -94,6 +94,24 @@ class OnionShare(object):
hostname_file = '{0}/hostname'.format(hidserv_dir)
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():
# if running in Tails and as root, do only the things that require root
if helpers.get_platform() == 'Tails' and helpers.is_root():
@ -165,17 +183,30 @@ def main():
except TailsError as e:
sys.exit(e.args[0])
# startup
# prepare files to share
web.set_file_info(filenames)
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 'http://{0}/{1}'.format(app.onion_host, web.slug)
print ''
print strings._("ctrlc_to_stop")
# start the web server
web.start(app.port, app.stay_open)
print '\n'
# wait for app to close
running = True
while running:
try:
time.sleep(0.5)
except KeyboardInterrupt:
running = False
web.stop()
# shutdown
app.cleanup()

View File

@ -2,6 +2,10 @@
"calculating_sha1": "Calculating SHA1 checksum.",
"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?",
"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:",
"ctrlc_to_stop": "Press Ctrl-C to stop server",
"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
import strings, helpers
@ -187,3 +187,9 @@ def start(port, stay_open=False, gui_mode=False):
set_gui_mode(gui_mode)
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
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
import common
@ -101,11 +101,7 @@ class OnionShareGui(QtGui.QWidget):
t.start()
def stop_server(self):
# 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(self.app.port, web.shutdown_slug)).read()
except:
pass
web.stop()
self.app.cleanup()
self.stop_server_finished.emit()

View File

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