[tor-commits] [oonib/master] Do some refactoring and cleaning up of the various handlers.

art at torproject.org art at torproject.org
Wed Sep 11 09:13:52 UTC 2013


commit a4f09650c55aec62b0de0e319b3336547b5f2a90
Author: Arturo Filastò <art at fuffa.org>
Date:   Fri Aug 16 19:17:14 2013 +0200

    Do some refactoring and cleaning up of the various handlers.
    
    @aagbsn you should take a look at this.
---
 oonib.conf.example               |    5 ++++-
 oonib/bouncer/api.py             |    6 ++----
 oonib/bouncer/handlers.py        |   10 ----------
 oonib/deck/handlers.py           |    2 ++
 oonib/input/api.py               |   10 ----------
 oonib/input/handlers.py          |   35 -----------------------------------
 oonib/inputs/api.py              |   10 ++++++++++
 oonib/inputs/handlers.py         |   35 +++++++++++++++++++++++++++++++++++
 oonib/policy/api.py              |    2 --
 oonib/report/api.py              |   11 +----------
 oonib/runner.py                  |   22 ++++++++++++++++++----
 oonib/testhelpers/dns_helpers.py |    1 -
 12 files changed, 72 insertions(+), 77 deletions(-)

diff --git a/oonib.conf.example b/oonib.conf.example
index 1b51621..60e177f 100644
--- a/oonib.conf.example
+++ b/oonib.conf.example
@@ -1,8 +1,11 @@
 main:
     report_dir: Null
     archive_dir: Null
-    input_dir: Null
+    inputs_dir: Null
     deck_dir: Null
+    policy_file: Null
+    bouncer_file: Null
+
     logfile: Null
     tor_datadir: Null
     database_uri: 'sqlite://oonib_test_db.db'
diff --git a/oonib/bouncer/api.py b/oonib/bouncer/api.py
index 21f2be4..42f53e1 100644
--- a/oonib/bouncer/api.py
+++ b/oonib/bouncer/api.py
@@ -1,8 +1,6 @@
-from oonib.bouncer.handlers import *
+from oonib.bouncer import handlers
 #XXX: if bouncer is configured
 bouncerAPI = [
         #return a collector and helper of the requested type
-        (r"/bouncer", BouncerQueryHandler),
-        #XXX: register a collector or helper
-        (r"/register", CollectorRegisterHandler),
+        (r"/bouncer", handlers.BouncerQueryHandler),
 ]
diff --git a/oonib/bouncer/handlers.py b/oonib/bouncer/handlers.py
index 74d828a..f2249dd 100644
--- a/oonib/bouncer/handlers.py
+++ b/oonib/bouncer/handlers.py
@@ -7,13 +7,3 @@ class BouncerQueryHandler(web.RequestHandler):
         pass
         # request.get?
         #'collector': {'foo.onion': {'helper-name': 'helper-address'}, 'bar.onion': {'helper-name': 'helper-address'}} 
-
-class CollectorRegisterHandler(self):
-    def post(self):
-        #XXX: update the list of collectors to query
-        pass
-
-    def get(self):
-        pass
-        #XXX unused
-
diff --git a/oonib/deck/handlers.py b/oonib/deck/handlers.py
index f37609a..511b6b1 100644
--- a/oonib/deck/handlers.py
+++ b/oonib/deck/handlers.py
@@ -1,3 +1,5 @@
+from cyclone import web
+
 class DeckDescHandler(web.RequestHandler):
     def get(self, deckID):
         bn = os.path.basename(deckID)
diff --git a/oonib/input/api.py b/oonib/input/api.py
deleted file mode 100644
index 4c397ab..0000000
--- a/oonib/input/api.py
+++ /dev/null
@@ -1,10 +0,0 @@
-from cyclone import web
-from oonib.input import handlers
-from oonib import config
-
-inputAPI = [
-    (r"/input", handlers.InputListHandler),
-    (r"/input/([a-z0-9]{40})", handlers.InputDescHandler),
-    (r"/input/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path":
-        config.main.input_dir}),
-]
diff --git a/oonib/input/handlers.py b/oonib/input/handlers.py
deleted file mode 100644
index ed8008e..0000000
--- a/oonib/input/handlers.py
+++ /dev/null
@@ -1,35 +0,0 @@
-from oonib import config
-from cyclone import web
-import json
-import os
-import yaml
-
-class InputDescHandler(web.RequestHandler):
-    def get(self, inputID):
-        #XXX return the input descriptor
-        # see oonib.md in ooni-spec
-        bn = os.path.basename(inputID) + ".desc"
-        try:
-            f = open(os.path.join(config.main.input_dir, bn))
-            a = {}
-            inputDesc = yaml.safe_load(f)
-            a['id'] = inputID
-            a['name'] = inputDesc['name']
-            a['description'] = inputDesc['description']
-            self.write(json.dumps(a))
-        except Exception:
-            log.err("No Input Descriptor found for id %s" % inputID) 
-
-class InputListHandler(web.RequestHandler):
-    def get(self):
-        if not config.main.input_dir: return
-        path = os.path.abspath(config.main.input_dir) + "/*"
-        inputnames = map(os.path.basename, glob.iglob(path))
-        inputList = []
-        for inputname in inputnames:
-            f = open(os.path.join(config.main.input_dir, deckname))
-            d = yaml.safe_load(f)
-            inputList.append({'id': inputname,'name': d['name'],
-                'description': d['description']})
-            f.close()
-        self.write(json.dumps(inputList))
diff --git a/oonib/inputs/api.py b/oonib/inputs/api.py
new file mode 100644
index 0000000..53342ae
--- /dev/null
+++ b/oonib/inputs/api.py
@@ -0,0 +1,10 @@
+from cyclone import web
+from oonib.input import handlers
+from oonib import config
+
+inputsAPI = [
+    (r"/inputs", handlers.InputsListHandler),
+    (r"/inputs/([a-z0-9]{40})", handlers.InputsDescHandler),
+    (r"/inputs/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path":
+        config.main.inputs_dir}),
+]
diff --git a/oonib/inputs/handlers.py b/oonib/inputs/handlers.py
new file mode 100644
index 0000000..722a757
--- /dev/null
+++ b/oonib/inputs/handlers.py
@@ -0,0 +1,35 @@
+from oonib import config
+from cyclone import web
+import json
+import os
+import yaml
+
+class InputsDescHandler(web.RequestHandler):
+    def get(self, inputID):
+        #XXX return the input descriptor
+        # see oonib.md in ooni-spec
+        bn = os.path.basename(inputID) + ".desc"
+        try:
+            f = open(os.path.join(config.main.input_dir, bn))
+            a = {}
+            inputDesc = yaml.safe_load(f)
+            a['id'] = inputID
+            a['name'] = inputDesc['name']
+            a['description'] = inputDesc['description']
+            self.write(json.dumps(a))
+        except Exception:
+            log.err("No Input Descriptor found for id %s" % inputID) 
+
+class InputsListHandler(web.RequestHandler):
+    def get(self):
+        if not config.main.input_dir: return
+        path = os.path.abspath(config.main.input_dir) + "/*"
+        inputnames = map(os.path.basename, glob.iglob(path))
+        inputList = []
+        for inputname in inputnames:
+            f = open(os.path.join(config.main.input_dir, deckname))
+            d = yaml.safe_load(f)
+            inputList.append({'id': inputname, 'name': d['name'],
+                'description': d['description']})
+            f.close()
+        self.write(json.dumps(inputList))
diff --git a/oonib/policy/api.py b/oonib/policy/api.py
index 0f6f023..4cacea2 100644
--- a/oonib/policy/api.py
+++ b/oonib/policy/api.py
@@ -1,6 +1,4 @@
-from cyclone import web
 from oonib.policy import handlers
-from oonib import config
 
 #XXX: if policy is configured
 policyAPI = [
diff --git a/oonib/report/api.py b/oonib/report/api.py
index c707291..e8a4c0c 100644
--- a/oonib/report/api.py
+++ b/oonib/report/api.py
@@ -1,16 +1,7 @@
-"""
-/report
-
-/pcap
-
-This is the async pcap reporting system. It requires the client to have created
-a report already, but can work independently from test progress.
-
-"""
 from oonib.report import handlers
 
 reportAPI = [
     (r"/report/([a-zA-Z0-9_\-]+)/close", handlers.CloseReportHandlerFile),
     (r"/report", handlers.NewReportHandlerFile),
-    (r"/pcap", file_collector.PCAPReportHandler),
+    (r"/pcap", handlers.PCAPReportHandler),
 ]
diff --git a/oonib/runner.py b/oonib/runner.py
index cfc0563..8e89f32 100644
--- a/oonib/runner.py
+++ b/oonib/runner.py
@@ -22,8 +22,9 @@ from txtorcon import launch_tor
 
 from oonib.report.api import reportAPI 
 from oonib.deck.api import deckAPI
-from oonib.input.api import inputAPI
+from oonib.inputs.api import inputsAPI
 from oonib.policy.api import policyAPI
+from oonib.bouncer.api import bouncerAPI
 
 from oonib import oonibackend
 from oonib import config
@@ -60,9 +61,22 @@ def setupCollector(tor_process_protocol):
     #XXX: also set up a separate keyed hidden service for collectors to push their status to, if the bouncer is enabled
     hs_endpoint = TCPHiddenServiceEndpoint(reactor, torconfig, public_port,
                                            data_dir=datadir)
-    EnabledAPIs = []
-    EnabledAPIs.append(reportAPI)
-    hidden_service = hs_endpoint.listen(EnabledAPIs)
+    enabledAPIs = []
+    enabledAPIs += reportAPI
+
+    if config.inputs_dir:
+        enabledAPIs += inputsAPI
+
+    if config.deck_dir:
+        enabledAPIs += deckAPI
+
+    if config.policy_file:
+        enabledAPIs += policyAPI
+
+    if config.bouncer_file:
+        enabledAPIs += bouncerAPI
+
+    hidden_service = hs_endpoint.listen(enabledAPIs)
     hidden_service.addCallback(setup_complete)
     hidden_service.addErrback(txSetupFailed)
 
diff --git a/oonib/testhelpers/dns_helpers.py b/oonib/testhelpers/dns_helpers.py
index cb4ff9f..dc9c772 100644
--- a/oonib/testhelpers/dns_helpers.py
+++ b/oonib/testhelpers/dns_helpers.py
@@ -12,5 +12,4 @@ class DNSTestHelper(server.DNSServerFactory):
                                          caches = caches, clients = [resolver],
                                          verbose = verbose)
     def handleQuery(self, message, protocol, address):
-        print message, protocol, address
         server.DNSServerFactory.handleQuery(self, message, protocol, address)





More information about the tor-commits mailing list