Allow for arbitrary order of service open/close

d'Oh
This commit is contained in:
The Dod 2014-10-21 06:08:31 +07:00
parent 1786b5a891
commit 0b50829695

View File

@ -33,7 +33,7 @@ def hsdic2list(dic):
return [ return [
pair for pairs in [ pair for pairs in [
[('HiddenServiceDir',vals[0]),('HiddenServicePort',vals[1])] [('HiddenServiceDir',vals[0]),('HiddenServicePort',vals[1])]
for vals in zip(dic['HiddenServiceDir'],dic['HiddenServicePort']) for vals in zip(dic.get('HiddenServiceDir',[]),dic.get('HiddenServicePort',[]))
] for pair in pairs ] for pair in pairs
] ]
@ -41,7 +41,7 @@ class OnionShare(object):
def __init__(self, debug=False, local_only=False, stay_open=False): def __init__(self, debug=False, local_only=False, stay_open=False):
self.port = None self.port = None
self.controller = None self.controller = None
self.original_hs = None self.hidserv_dir = None
# debug mode # debug mode
if debug: if debug:
@ -59,9 +59,17 @@ class OnionShare(object):
def cleanup(self): def cleanup(self):
if self.controller: if self.controller:
if self.original_hs: # Get fresh hidden services (maybe changed since last time)
# This doesn't seem to remove soon-to-be-stale hs. Why??? # and remove ourselves
self.controller.set_options(hsdic2list(self.original_hs)) hsdic = self.controller.get_conf_map('HiddenServiceOptions') or {
'HiddenServiceDir': [], 'HiddenServicePort': []
}
if self.hidserv_dir and self.hidserv_dir in hsdic.get('HiddenServiceDir',[]):
dropme = hsdic['HiddenServiceDir'].index(self.hidserv_dir)
del hsdic['HiddenServiceDir'][dropme]
del hsdic['HiddenServicePort'][dropme]
self.controller.set_options(hsdic2list(hsdic))
# Politely close the controller
self.controller.close() self.controller.close()
for filename in self.cleanup_filenames: for filename in self.cleanup_filenames:
if os.path.isfile(filename): if os.path.isfile(filename):
@ -104,8 +112,8 @@ class OnionShare(object):
else: else:
# come up with a hidden service directory name # come up with a hidden service directory name
hidserv_dir = '{0}/onionshare_{1}'.format(helpers.get_tmp_dir(), helpers.random_string(8)) self.hidserv_dir = '{0}/onionshare_{1}'.format(helpers.get_tmp_dir(), helpers.random_string(8))
self.cleanup_filenames.append(hidserv_dir) self.cleanup_filenames.append(self.hidserv_dir)
# connect to the tor controlport # connect to the tor controlport
self.controller = None self.controller = None
@ -121,21 +129,22 @@ class OnionShare(object):
self.controller.authenticate() self.controller.authenticate()
# set up hidden service # set up hidden service
self.original_hs = self.controller.get_conf_map('HiddenServiceOptions') or { hsdic = self.controller.get_conf_map('HiddenServiceOptions') or {
'HiddenServiceDir': [], 'HiddenServicePort': [] 'HiddenServiceDir': [], 'HiddenServicePort': []
} }
new_hs = self.original_hs.copy() if self.hidserv_dir in hsdic.get('HiddenServiceDir',[]):
if not hidserv_dir in new_hs['HiddenServiceDir']: # Maybe a stale service with the wrong local port
# Unless another instance has opened dropme = hsdic['HiddenServiceDir'].index(self.hidserv_dir)
# the same persistent_hs_dir already del hsdic['HiddenServiceDir'][dropme]
new_hs['HiddenServiceDir'].append(hidserv_dir) del hsdic['HiddenServicePort'][dropme]
new_hs['HiddenServicePort'].append( hsdic['HiddenServiceDir'] = hsdic.get('HiddenServiceDir',[])+[self.hidserv_dir]
'80 127.0.0.1:{0}'.format(self.port)) hsdic['HiddenServicePort'] = hsdic.get('HiddenServicePort',[])+[
'80 127.0.0.1:{0}'.format(self.port) ]
self.controller.set_options(hsdic2list(new_hs)) self.controller.set_options(hsdic2list(hsdic))
# figure out the .onion hostname # figure out the .onion hostname
hostname_file = '{0}/hostname'.format(hidserv_dir) hostname_file = '{0}/hostname'.format(self.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): def wait_for_hs(self):