From 1a4cb26f5d751441ffdf6d5adef10900e9c4765e Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 8 Sep 2015 17:42:08 -0700 Subject: [PATCH] Move wait_for_hs() function into the HS class --- onionshare/hs.py | 47 +++++++++++++++++++++++++++++++- onionshare/onionshare.py | 59 ++++------------------------------------ 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/onionshare/hs.py b/onionshare/hs.py index b1636338..9a0a5b7f 100644 --- a/onionshare/hs.py +++ b/onionshare/hs.py @@ -19,7 +19,8 @@ along with this program. If not, see . """ from stem.control import Controller -import os, tempfile, shutil +import os, sys, tempfile, shutil, urllib2, httplib +import socks import helpers, strings @@ -100,6 +101,50 @@ class HS(object): onion_host = open(hostname_file, 'r').read().strip() return onion_host + def wait_for_hs(self, onion_host, transparent_torification): + # legacy only, this function is no longer required with ephemeral hidden services + print strings._('wait_for_hs') + + ready = False + while not ready: + try: + sys.stdout.write('{0:s} '.format(strings._('wait_for_hs_trying'))) + sys.stdout.flush() + + if transparent_torification: + # no need to set the socks5 proxy + urllib2.urlopen('http://{0:s}'.format(onion_host)) + else: + tor_exists = False + ports = [9050, 9150] + for port in ports: + try: + s = socks.socksocket() + s.setproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', port) + s.connect((onion_host, 80)) + s.close() + tor_exists = True + break + except socks.ProxyConnectionError: + pass + if not tor_exists: + raise NoTor(strings._("cant_connect_socksport").format(tor_socks_ports)) + ready = True + + sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_yup'))) + except socks.SOCKS5Error: + sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_nope'))) + sys.stdout.flush() + except urllib2.HTTPError: # torification error + sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_nope'))) + sys.stdout.flush() + except httplib.BadStatusLine: # torification (with bridge) error + sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_nope'))) + sys.stdout.flush() + except KeyboardInterrupt: + return False + return True + def cleanup(self): if self.supports_ephemeral: # todo: cleanup the ephemeral hidden service diff --git a/onionshare/onionshare.py b/onionshare/onionshare.py index 527eab09..68340df5 100644 --- a/onionshare/onionshare.py +++ b/onionshare/onionshare.py @@ -17,9 +17,8 @@ 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 . """ -import os, sys, subprocess, time, argparse, inspect, shutil, socket, threading, urllib2, httplib -import socks +import os, sys, subprocess, time, argparse, inspect, shutil, socket, threading import strings, helpers, web, hs class OnionShare(object): @@ -64,53 +63,6 @@ class OnionShare(object): self.onion_host = self.hs.start(self.port) - def wait_for_hs(self): - # legacy only, this function is no longer required with ephemeral hidden services - if self.local_only: - return True - - print strings._('wait_for_hs') - - ready = False - while not ready: - try: - sys.stdout.write('{0:s} '.format(strings._('wait_for_hs_trying'))) - sys.stdout.flush() - - if self.transparent_torification: - # no need to set the socks5 proxy - urllib2.urlopen('http://{0:s}'.format(self.onion_host)) - else: - tor_exists = False - ports = [9050, 9150] - for port in ports: - try: - s = socks.socksocket() - s.setproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', port) - s.connect((self.onion_host, 80)) - s.close() - tor_exists = True - break - except socks.ProxyConnectionError: - pass - if not tor_exists: - raise NoTor(strings._("cant_connect_socksport").format(tor_socks_ports)) - ready = True - - sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_yup'))) - except socks.SOCKS5Error: - sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_nope'))) - sys.stdout.flush() - except urllib2.HTTPError: # torification error - sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_nope'))) - sys.stdout.flush() - except httplib.BadStatusLine: # torification (with bridge) error - sys.stdout.write('{0:s}\n'.format(strings._('wait_for_hs_nope'))) - sys.stdout.flush() - except KeyboardInterrupt: - return False - return True - def cleanup(self): # cleanup files for filename in self.cleanup_filenames: @@ -186,11 +138,12 @@ def main(cwd=None): t.start() try: # Trap Ctrl-C - # wait for hs + # wait for hs, only if using old version of tor if not app.hs.supports_ephemeral: - ready = app.wait_for_hs() - if not ready: - sys.exit() + if not app.local_only: + ready = app.hs.wait_for_hs(app.onion_host, app.transparent_torification) + if not ready: + sys.exit() print strings._("give_this_url") print 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug)