[tor-commits] [ooni-probe/master] Fix BridgeT(until we have torify working)

art at torproject.org art at torproject.org
Sun Feb 12 16:18:24 UTC 2012


commit 13db3d0e2b1898a788e3d52115b9da0d7952af27
Author: Sathyanarayanan Gunasekaran <gsathya.ceg at gmail.com>
Date:   Sun Feb 12 21:30:25 2012 +0530

    Fix BridgeT(until we have torify working)
---
 refactor/tests/bridget.py |   69 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/refactor/tests/bridget.py b/refactor/tests/bridget.py
index 10ba144..3e55eb0 100644
--- a/refactor/tests/bridget.py
+++ b/refactor/tests/bridget.py
@@ -15,22 +15,52 @@ import errno
 import time
 import random
 import re
+import glob
+import socks
 from shutil import rmtree
 from subprocess import Popen, PIPE
 from datetime import datetime
 
+import shutil
 import plugoo
 import gevent
 from gevent import socket
 import fcntl
 from plugoo import Plugoo, Asset, torify
 import urllib2
+import httplib
 
 try:
     from TorCtl import TorCtl
 except:
     print "Error TorCtl not installed!"
 
+class SocksiPyConnection(httplib.HTTPConnection):
+    def __init__(self, proxytype, proxyaddr, proxyport = None, rdns = True,
+                 username = None, password = None, *args, **kwargs):
+        self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
+        httplib.HTTPConnection.__init__(self, *args, **kwargs)
+
+    def connect(self):
+        self.sock = socks.socksocket()
+        self.sock.setproxy(*self.proxyargs)
+        if isinstance(self.timeout, float):
+            self.sock.settimeout(self.timeout)
+        self.sock.connect((self.host, self.port))
+            
+class SocksiPyHandler(urllib2.HTTPHandler):
+    def __init__(self, *args, **kwargs):
+        self.args = args
+        self.kw = kwargs
+        urllib2.HTTPHandler.__init__(self)
+
+    def http_open(self, req):
+        def build(host, port=None, strict=None, timeout=0):    
+            conn = SocksiPyConnection(*self.args, host=host, port=port,
+                                      strict=strict, timeout=timeout, **self.kw)
+            return conn
+        return self.do_open(build, req)
+    
 class BridgeTAsset(Asset):
     def __init__(self, file=None):
         self = Asset.__init__(self, file)
@@ -41,8 +71,10 @@ class BridgeT(Plugoo):
     timeout = 20
     # These are the modules that should be torified
     modules = [urllib2]
+    
 
     def writetorrc(self, bridge):
+        self.failures = []
         # register Tor to an ephemeral port
         socksport = random.randint(49152, 65535)
         controlport = random.randint(49152, 65535)
@@ -84,10 +116,11 @@ usemicrodescriptors 0
 
     #Can't use @torify as it doesn't support concurrency right now 
     def download_file(self, socksport):
-        socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", int(socksport))
-        socks.wrapmodule(urllib2)
         time_start=time.time()
-        f = urllib2.urlopen('http://check.torproject.org')
+
+        opener = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5,
+                                                      '127.0.0.1', int(socksport)))
+        f = opener.open('http://check.torproject.org')
         data= f.readlines()
         print data
         print len(data)
@@ -120,15 +153,9 @@ usemicrodescriptors 0
                     print "%s bridge works" % bridge
                     print "%s controlport" % controlport
                     c = TorCtl.connect('127.0.0.1', controlport)
-                    print c
-                    #c.set_event_handler(LogHandler())
-                    #c.set_events(["DEBUG", "INFO", "NOTICE", "WARN", "ERR"])
                     bridgeinfo = self.parsebridgeinfo(c.get_info('dir/server/all')['dir/server/all'])
-                    #circID = c.extend_circuit(0, ["bridge","serenity"])
                     bandwidth=self.download_file(socksport)
                     print bandwidth
-                    print c.get_info('stream-status')
-                    #c.signal("HALT")
                     c.close()
                     p.stdout.close()
                     os.unlink(os.path.join(os.getcwd(), torrc))
@@ -159,6 +186,7 @@ usemicrodescriptors 0
                 socket.wait_read(p.stdout.fileno(), timeout=ct)
             except:
                 print "%s bridge does not work (%s s timeout)" % (bridge, timeout)
+                self.failures.append(bridge)
                 p.stdout.close()
                 os.unlink(os.path.join(os.getcwd(), torrc))
                 rmtree(tordir)
@@ -178,21 +206,38 @@ usemicrodescriptors 0
 
         return self.connect(bridge)
 
+    def clean(self):
+        for infile in glob.glob('tor_*'):
+            os.remove(infile)
+
+    def print_failures(self):
+        if self.failures:
+            for item in self.failures:
+                print "Offline : %s" % item
+        else:
+            print "All online"
+
+    # For logging TorCtl event msgs
+    #class LogHandler:
+    #def msg(self, severity, message):
+    #   print "[%s] %s"%(severity, message)
+       
 def run(ooni):
     """
     Run the test
     """
+
     config = ooni.config
     urls = []
 
     bridges = BridgeTAsset(os.path.join(config.main.assetdir, \
-                                            config.tests.tor_bridges))
+                                        config.tests.tor_bridges))
 
     assets = [bridges]
 
     bridget = BridgeT(ooni)
     ooni.logger.info("Starting bridget test")
     bridget.run(assets)
+    bridget.print_failures()
+    bridget.clean()
     ooni.logger.info("Testing completed!")
-
-





More information about the tor-commits mailing list