Move wait_for_hs() function into the HS class

This commit is contained in:
Micah Lee 2015-09-08 17:42:08 -07:00
parent 629a70c249
commit 1a4cb26f5d
2 changed files with 52 additions and 54 deletions

View file

@ -19,7 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from stem.control import Controller from stem.control import Controller
import os, tempfile, shutil import os, sys, tempfile, shutil, urllib2, httplib
import socks
import helpers, strings import helpers, strings
@ -100,6 +101,50 @@ class HS(object):
onion_host = open(hostname_file, 'r').read().strip() onion_host = open(hostname_file, 'r').read().strip()
return onion_host 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): def cleanup(self):
if self.supports_ephemeral: if self.supports_ephemeral:
# todo: cleanup the ephemeral hidden service # todo: cleanup the ephemeral hidden service

View file

@ -17,9 +17,8 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
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 import strings, helpers, web, hs
class OnionShare(object): class OnionShare(object):
@ -64,53 +63,6 @@ class OnionShare(object):
self.onion_host = self.hs.start(self.port) 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): def cleanup(self):
# cleanup files # cleanup files
for filename in self.cleanup_filenames: for filename in self.cleanup_filenames:
@ -186,11 +138,12 @@ def main(cwd=None):
t.start() t.start()
try: # Trap Ctrl-C try: # Trap Ctrl-C
# wait for hs # wait for hs, only if using old version of tor
if not app.hs.supports_ephemeral: if not app.hs.supports_ephemeral:
ready = app.wait_for_hs() if not app.local_only:
if not ready: ready = app.hs.wait_for_hs(app.onion_host, app.transparent_torification)
sys.exit() if not ready:
sys.exit()
print strings._("give_this_url") print strings._("give_this_url")
print 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug) print 'http://{0:s}/{1:s}'.format(app.onion_host, web.slug)