Skip to content

Commit

Permalink
keep old config behavior with new endpoint code
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanders committed May 15, 2016
1 parent 612eb5a commit c84c28d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
36 changes: 32 additions & 4 deletions oonib/onion.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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
import socket

from txtorcon import TCPHiddenServiceEndpoint, TorConfig
from txtorcon import launch_tor
from txtorcon.util import available_tcp_port

from txtorcon import __version__ as txtorcon_version
if tuple(map(int, txtorcon_version.split('.'))) < (0, 9, 0):
Expand Down Expand Up @@ -53,9 +54,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
Expand Down Expand Up @@ -89,3 +89,31 @@ def updates(prog, tag, summary):
config.main.socks_port = socks_port

torconfig.save()
return torconfig

# get_global_tor is a near-rip of that from txtorcon, but
# we use our own configTor() rather than txtorcon's defaults
_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()
45 changes: 25 additions & 20 deletions oonib/oonibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

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
Expand Down Expand Up @@ -102,18 +102,26 @@
multiService.addService(http_return_request_helper)
http_return_request_helper.startService()

@defer.inlineCallbacks
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'])
tor_config = yield get_global_tor(reactor)
if 'hsdir' not in endpoint_config:
hsdir = os.path.join(tor_config.DataDirectory,
endpoint_config['rel_hsdir'])
else:
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,
defer.returnValue(TCPHiddenServiceEndpoint(reactor,
tor_config,
80,
hidden_service_dir=hsdir)
hidden_service_dir=hsdir))
else:
return TCPHiddenServiceEndpoint.global_tor(reactor,
defer.returnValue(TCPHiddenServiceEndpoint(reactor,
tor_config,
80,
data_dir=hsdir)
data_dir=hsdir))

def getTCPEndpoint(endpoint_config):
return endpoints.TCP4ServerEndpoint(reactor, endpoint_config['port'])
Expand All @@ -136,6 +144,7 @@ def getEndpoint(endpoint_config):
else:
raise Exception("unknown endpoint type")

@defer.inlineCallbacks
def createService(endpoint, role, endpoint_config):
if role == 'bouncer':
factory = ooniBouncer
Expand All @@ -144,24 +153,20 @@ def createService(endpoint, role, endpoint_config):
else:
raise Exception("unknown service type")

res_endpoint = yield endpoint
service = internet.StreamServerEndpointService(
endpoint, factory
res_endpoint, factory
)
service.setName("-".join([endpoint_config['type'], role]))
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'} ]
if config.main.tor_hidden_service and \
config.main.bouncer_endpoints is None and \
config.main.collector_endpoints is None:
config.main.bouncer_endpoints = [ {'type': 'onion', 'rel_hsdir': 'bouncer'} ]
config.main.collector_endpoints = [ {'type': 'onion', 'rel_hsdir': 'collector'} ]

config.main.bouncer_endpoints = config.main.get('bouncer_endpoints', [])
config.main.collector_endpoints = config.main.get('collector_endpoints', [])
Expand Down

0 comments on commit c84c28d

Please sign in to comment.