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

[tor-commits] [oonib/master] Add Handlers and API for policy



commit 5a349ee10d01b5cdb7c9eb256b58e78b310559ad
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Sun Jul 7 19:47:40 2013 +0200

    Add Handlers and API for policy
---
 oonib/policy/api.py      |   40 ++++++++++++++++++++++++
 oonib/policy/handlers.py |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+)

diff --git a/oonib/policy/api.py b/oonib/policy/api.py
new file mode 100644
index 0000000..577d988
--- /dev/null
+++ b/oonib/policy/api.py
@@ -0,0 +1,40 @@
+"""
+/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.
+
+"""
+import random
+import string
+import json
+import re
+import os
+
+from twisted.internet import reactor, defer
+
+from cyclone import web
+
+from oonib import otime
+from oonib import randomStr
+
+from oonib import config
+from oonib.policy import DeckListHandler, InputListHandler, NetTestListHandler
+
+#XXX: if policy is configured
+policyAPI = [
+    (r"/deck", DeckListHandler),
+    (r"/input", InputListHandler),
+    (r"/deck/([a-z0-9]{40})$", DeckDescHandler),
+    (r"/deck/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path":
+        config.main.deck_dir}),
+    (r"/input/([a-z0-9]{40})/file$", web.StaticFileHandler, {"path":
+        config.main.input_dir}),
+    (r"/input([a-z0-9]{40}$", InputDescHandler),
+    (r"/policy/nettest", NetTestListHandler),
+    (r"/policy/nettest/([a-z0-9]+/py$", web.StaticFileHandler, {"path":
+        config.main.nettest_dir}),
+    (r"/policy/input", InputListHandler
+]
diff --git a/oonib/policy/handlers.py b/oonib/policy/handlers.py
new file mode 100644
index 0000000..567da2c
--- /dev/null
+++ b/oonib/policy/handlers.py
@@ -0,0 +1,78 @@
+from cyclone import web
+from oonib import config
+import json
+import os
+import yaml
+
+class DeckDescHandler(web.RequestHandler):
+    def get(self, deckID):
+        bn = os.path.basename(deckID)
+        try:
+            f = open(os.path.join(config.main.deck_dir, bn))
+            a = {}
+            deckDesc = yaml.safe_load(f)
+            a['id'] = deckID
+            a['name'] = deckDesc['name']
+            a['description'] = deckDesc['description']
+            self.write(json.dumps(a))
+        except IOError:
+            log.err("Deck %s missing" % deckID)
+        except KeyError:
+            log.err("Deck %s missing required keys!" % deckID)
+
+class DeckListHandler(web.RequestHandler):
+    def get(self):
+        if not config.main.deck_dir: return
+        path = os.path.abspath(config.main.deck_dir) + "/*"
+        decknames = map(os.path.basename, glob.iglob(path))
+        deckList = []
+        for deckname in decknames:
+            f = open(os.path.join(config.main.deck_dir, deckname))
+            d = yaml.safe_load(f)
+            deckList.append({'id': deckname,'name': d['name'],
+                'description': d['description']})
+            f.close()
+        self.write(json.dumps(deckList))
+
+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))
+
+class NetTestListHandler(web.RequestHandler):
+    def get(self):
+        #XXX: returns a list of accepted NetTests
+        pass
+
+class HelperListHandler(web.RequestHandler):
+    def get(self):
+        #XXX: get the list of the running handlers
+        #'id'
+        #'description'
+        #'address'
+        pass



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