[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Implement first iteration of Tor Hidden Service support for ooniprobe and oonib



commit d0d9662b4cafe11979e8173e6cc03c548f99ecf0
Author: Arturo Filastò <art@xxxxxxxxx>
Date:   Wed Nov 21 14:49:29 2012 +0100

    Implement first iteration of Tor Hidden Service support for ooniprobe and oonib
    * Also with better error handling for reports
---
 ooni/reporter.py     |   16 +++++++++++---
 oonib/oonibackend.py |   17 ++++-----------
 oonib/runner.py      |   51 +++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 67 insertions(+), 17 deletions(-)

diff --git a/ooni/reporter.py b/ooni/reporter.py
index 0110b02..0635d2f 100644
--- a/ooni/reporter.py
+++ b/ooni/reporter.py
@@ -188,9 +188,12 @@ class OONIBReporter(OReporter):
     def __init__(self, backend_url):
         from ooni.lib.txagentwithsocks import Agent
         from twisted.internet import reactor
+        try:
+            self.agent = Agent(reactor, sockshost="127.0.0.1",
+                socksport=int(config.advanced.tor_socksport))
+        except Exception, e:
+            log.exception(e)
 
-        self.agent = Agent(reactor, sockshost="127.0.0.1",
-                socksport=config.advanced.socksport)
         self.backend_url = backend_url
 
     @defer.inlineCallbacks
@@ -257,7 +260,7 @@ class OONIBReporter(OReporter):
         bodyProducer = StringProducer(json.dumps(request))
 
         try:
-            response = yield self.agent.request("POST", url, 
+            response = yield self.agent.request("POST", url,
                                 bodyProducer=bodyProducer)
         except ConnectionRefusedError:
             log.err("Connection to reporting backend failed (ConnectionRefusedError)")
@@ -273,7 +276,12 @@ class OONIBReporter(OReporter):
 
         backend_response = yield response_body
 
-        parsed_response = json.loads(backend_response)
+        try:
+            parsed_response = json.loads(backend_response)
+        except Exception, e:
+            log.exception(e)
+            raise OONIBReportCreationFailed
+
         self.report_id = parsed_response['report_id']
         self.backend_version = parsed_response['backend_version']
         log.debug("Created report with id %s" % parsed_response['report_id'])
diff --git a/oonib/oonibackend.py b/oonib/oonibackend.py
index dd25c8c..c712bc4 100644
--- a/oonib/oonibackend.py
+++ b/oonib/oonibackend.py
@@ -13,19 +13,17 @@ from twisted.application import internet, service
 from twisted.application.service import Application
 from twisted.names import dns
 
-from ooni.utils import log
+from cyclone import web
 
-from oonib.report.api import reportingBackend
-
-from oonib import config
+import txtorcon
 
 from oonib.testhelpers import dns_helpers, ssl_helpers
 from oonib.testhelpers import http_helpers, tcp_helpers
 
-#from oonib.testhelpers.daphn3 import Daphn3Server
-from oonib import db_threadpool
+from ooni.utils import log
 
-from cyclone import web
+from oonib import db_threadpool
+from oonib import config
 
 application = service.Application('oonibackend')
 serviceCollection = service.IServiceCollection(application)
@@ -59,11 +57,6 @@ if config.helpers.daphn3.port:
                             tcp_helpers.Daphn3Server())
     daphn3_helper.setServiceParent(serviceCollection)
 
-if config.main.collector_port:
-    print "Starting Collector on %s" % config.main.collector_port
-    collector = internet.TCPServer(int(config.main.collector_port),
-                       reportingBackend)
-    collector.setServiceParent(serviceCollection)
 
 if config.helpers.tcp_echo.port:
     print "Starting TCP echo helper on %s" % config.helpers.tcp_echo.port
diff --git a/oonib/runner.py b/oonib/runner.py
index febef46..2538710 100644
--- a/oonib/runner.py
+++ b/oonib/runner.py
@@ -4,11 +4,45 @@ We are just extending the
 
 """
 
+from twisted.internet import reactor
 from twisted.application import service, internet, app
 from twisted.python.runtime import platformType
 
+import txtorcon
+
+from oonib.report.api import reportingBackend
+
+from oonib import config
 from ooni.utils import log
 
+def txSetupFailed(failure):
+    log.err("Setup failed")
+    log.exception(failure)
+
+def setupCollector(tor_process_protocol):
+    def setup_complete(port):
+        print "Exposed collector Tor hidden service on httpo://%s" % port.onion_uri
+
+    torconfig = txtorcon.TorConfig(tor_process_protocol.tor_protocol)
+    public_port = 80
+    hs_endpoint = txtorcon.TCPHiddenServiceEndpoint(reactor, torconfig,
+            public_port)
+    hidden_service = hs_endpoint.listen(reportingBackend)
+    hidden_service.addCallback(setup_complete)
+    hidden_service.addErrback(txSetupFailed)
+
+def startTor():
+    def updates(prog, tag, summary):
+        print "%d%%: %s" % (prog, summary)
+
+    torconfig = txtorcon.TorConfig()
+    torconfig.SocksPort = 9055
+    torconfig.save()
+    d = txtorcon.launch_tor(torconfig, reactor,
+            progress_updates=updates)
+    d.addCallback(setupCollector)
+    d.addErrback(txSetupFailed)
+
 class OBaseRunner():
     pass
 
@@ -17,9 +51,24 @@ if platformType == "win32":
                                 WindowsApplicationRunner
 
     OBaseRunner = WindowsApplicationRunner
+
 else:
     from twisted.scripts._twistd_unix import ServerOptions, \
                                 UnixApplicationRunner
-    OBaseRunner = UnixApplicationRunner
+    class OBaseRunner(UnixApplicationRunner):
+        def postApplication(self):
+            """
+            To be called after the application is created: start the
+            application and run the reactor. After the reactor stops,
+            clean up PID files and such.
+            """
+            self.startApplication(self.application)
+            # This is our addition. The rest is taken from
+            # twisted/scripts/_twistd_unix.py 12.2.0
+            startTor()
+            self.startReactor(None, self.oldstdout, self.oldstderr)
+            self.removePID(self.config['pidfile'])
 
 OBaseRunner.loggerFactory = log.LoggerFactory
+
+



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits