commit 575f0aa5a44d1f851fb3d4fbb1bcf1fbf36a4f2d Author: Joe Landers joe@joelanders.net Date: Sun May 15 18:25:41 2016 +0200
keep old config behavior with new endpoint code --- oonib/onion.py | 36 ++++++++++++++++++++++++++++++++---- oonib/oonibackend.py | 42 +++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 27 deletions(-)
diff --git a/oonib/onion.py b/oonib/onion.py index 262e672..8abefe5 100644 --- a/oonib/onion.py +++ b/oonib/onion.py @@ -1,7 +1,7 @@ import tempfile from oonib import log from oonib.config import config -from twisted.internet import reactor, endpoints +from twisted.internet import reactor, endpoints, defer import os
from random import randint @@ -53,9 +53,8 @@ def txSetupFailed(failure): log.err("Setup failed") log.exception(failure)
-def configTor(torconfig): - def updates(prog, tag, summary): - print("%d%%: %s" % (prog, summary)) +def _configTor(): + torconfig = TorConfig()
if config.main.socks_port: torconfig.SocksPort = config.main.socks_port @@ -89,3 +88,32 @@ def configTor(torconfig): config.main.socks_port = socks_port
torconfig.save() + return torconfig + +# get_global_tor is a near-rip of that from txtorcon (so you can have some +# confidence in the logic of it), but we use our own _configTor() while +# the txtorcon function hardcodes some default values we don't want. +_global_tor_config = None +_global_tor_lock = defer.DeferredLock() +# we need the lock because we (potentially) yield several times while +# "creating" the TorConfig instance + +@defer.inlineCallbacks +def get_global_tor(reactor): + global _global_tor_config + global _global_tor_lock + yield _global_tor_lock.acquire() + + try: + if _global_tor_config is None: + _global_tor_config = config = _configTor() + + # start Tor launching + def updates(prog, tag, summary): + print("%d%%: %s" % (prog, summary)) + yield launch_tor(config, reactor, progress_updates=updates) + yield config.post_bootstrap + + defer.returnValue(_global_tor_config) + finally: + _global_tor_lock.release() diff --git a/oonib/oonibackend.py b/oonib/oonibackend.py index 337b797..18bacf0 100644 --- a/oonib/oonibackend.py +++ b/oonib/oonibackend.py @@ -11,14 +11,14 @@ from distutils.version import LooseVersion
from oonib.api import ooniBackend, ooniBouncer from oonib.config import config -from oonib.onion import configTor +from oonib.onion import get_global_tor from oonib.testhelpers import dns_helpers, ssl_helpers from oonib.testhelpers import http_helpers, tcp_helpers
import os
from twisted.application import internet, service -from twisted.internet import reactor, endpoints, ssl +from twisted.internet import reactor, endpoints, ssl, defer from twisted.names import dns
from txtorcon import TCPHiddenServiceEndpoint, TorConfig @@ -103,15 +103,17 @@ if config.helpers['http-return-json-headers'].port: http_return_request_helper.startService()
def getHSEndpoint(endpoint_config): - if torconfig is None: - raise Exception("you probably need to set tor_hidden_service: true") - hsdir = os.path.join(torconfig.DataDirectory, endpoint_config['hsdir']) + hsdir = endpoint_config['hsdir'] + hsdir = os.path.expanduser(hsdir) + hsdir = os.path.realpath(hsdir) if LooseVersion(txtorcon_version) >= LooseVersion('0.10.0'): - return TCPHiddenServiceEndpoint.global_tor(reactor, + return TCPHiddenServiceEndpoint(reactor, + get_global_tor(reactor), 80, hidden_service_dir=hsdir) else: - return TCPHiddenServiceEndpoint.global_tor(reactor, + return TCPHiddenServiceEndpoint(reactor, + get_global_tor(reactor), 80, data_dir=hsdir)
@@ -151,27 +153,21 @@ def createService(endpoint, role, endpoint_config): multiService.addService(service) service.startService()
-torconfig = None -if config.main.tor_hidden_service: - torconfig = TorConfig() - configTor(torconfig) - # this is to ensure same behaviour with an old config file -if config.main.bouncer_endpoints is None and config.main.tor_hidden_service: - config.main.bouncer_endpoints = [ {'type': 'onion', 'hsdir': 'bouncer'} ] - -if config.main.collector_endpoints is None and config.main.tor_hidden_service: - config.main.collector_endpoints = [ {'type': 'onion', 'hsdir': 'collector'} ] - -config.main.bouncer_endpoints = config.main.get('bouncer_endpoints', []) -config.main.collector_endpoints = config.main.get('collector_endpoints', []) - -for endpoint_config in config.main.bouncer_endpoints: +if config.main.tor_hidden_service and \ + config.main.bouncer_endpoints is None and \ + config.main.collector_endpoints is None: + bouncer_hsdir = os.path.join(config.main.tor_datadir, 'bouncer') + collector_hsdir = os.path.join(config.main.tor_datadir, 'collector') + config.main.bouncer_endpoints = [ {'type': 'onion', 'hsdir': bouncer_hsdir} ] + config.main.collector_endpoints = [ {'type': 'onion', 'hsdir': collector_hsdir} ] + +for endpoint_config in config.main.get('bouncer_endpoints'): print "Starting bouncer with config %s" % endpoint_config endpoint = getEndpoint(endpoint_config) createService(endpoint, 'bouncer', endpoint_config)
-for endpoint_config in config.main.collector_endpoints: +for endpoint_config in config.main.get('collector_endpoints'): print "Starting collector with config %s" % endpoint_config endpoint = getEndpoint(endpoint_config) createService(endpoint, 'collector', endpoint_config)
tor-commits@lists.torproject.org