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

[tor-commits] [ooni-probe/master] Fix up @torify decorator by making it a descriptor



commit 78e3bdd22288eed3ac5431f23060cb3b74f5f901
Author: Arturo Filastò <hellais@xxxxxxxxx>
Date:   Sat Feb 11 15:18:20 2012 +0100

    Fix up @torify decorator by making it a descriptor
---
 refactor/ooni-probe.conf  |    1 +
 refactor/plugoo.py        |   28 ++++++++++++++++++----------
 refactor/tests/bridget.py |   12 +++++++++++-
 3 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/refactor/ooni-probe.conf b/refactor/ooni-probe.conf
index 2e22961..e74b241 100644
--- a/refactor/ooni-probe.conf
+++ b/refactor/ooni-probe.conf
@@ -9,6 +9,7 @@ assetdir = assets/
 testdir = tests/
 
 loglevel = DEBUG
+proxyaddress = 127.0.0.1:9050
 
 # These are configurations specific to the tests that should be
 # run by ooni-probe
diff --git a/refactor/plugoo.py b/refactor/plugoo.py
index 20b418d..49b3e16 100644
--- a/refactor/plugoo.py
+++ b/refactor/plugoo.py
@@ -315,19 +315,27 @@ class Plugoo():
                     job.kill()
                 jobs = []
 
-def torify(socksaddr, modules=None):
+class torify(object):
     """This is the torify decorator. It should be used to
     decorate functions that should use to for connecting to
     the interwebz. The suggary syntax is the following:
-    @torify("127.0.0.1:9050", [urllib2])
+    @torify([urllib2])
     def myfunction():
         f = urllib2.urlopen('https://torproject.org/')
+    remember to set the proxyaddress in the config file.
     """
-    def decorator(target):
-        host, port = socksaddr.split(":")
-        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, host, int(port))
-        # Wrap the modules into socks
-        for module in modules:
-            socks.wrapmodule(module)
-        return target
-    return decorator
+    def __init__(self, f):
+        print f
+        self.f = f
+
+    def __get__(self, instance, owner):
+        self.modules = instance.modules
+        def decorator(*args):
+            print instance.config.main.proxyaddress
+            host, port = instance.config.main.proxyaddress.split(":")
+            socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, host, int(port))
+            # Wrap the modules into socks
+            for module in self.modules:
+                socks.wrapmodule(module)
+            return self.f(instance, *args)
+        return decorator
diff --git a/refactor/tests/bridget.py b/refactor/tests/bridget.py
index 2f6df2c..5ad16c0 100644
--- a/refactor/tests/bridget.py
+++ b/refactor/tests/bridget.py
@@ -23,7 +23,8 @@ import plugoo
 import gevent
 from gevent import socket
 import fcntl
-from plugoo import Plugoo, Asset
+from plugoo import Plugoo, Asset, torify
+import urllib2
 
 try:
     from TorCtl import TorCtl
@@ -38,6 +39,8 @@ class BridgeT(Plugoo):
     # This is the timeout value after which
     # we will give up
     timeout = 20
+    # These are the modules that should be torified
+    modules = [urllib2]
 
     def writetorrc(self, bridge):
         # register Tor to an ephemeral port
@@ -81,11 +84,18 @@ usemicrodescriptors 0
                 ret[cfield[0]] = ' '.join(cfield[1:])
         return ret
 
+    @torify
+    def download_file(self):
+        f = urllib2.urlopen('http://check.torproject.org')
+        print f.readlines()
+
+
     def connect(self, bridge, timeout=None):
         if not timeout:
             if self.config.tests.tor_bridges_timeout:
                 self.timeout = self.config.tests.tor_bridges_timeout
             timeout = self.timeout
+        self.download_file()
         torrc, tordir, controlport = self.writetorrc(bridge)
         cmd = ["tor", "-f", torrc]
 

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