commit b57552d009e6d8af08a66a88e01897faed5ba945 Author: Arun Pandian G arunpandianp@gmail.com Date: Tue Apr 8 03:12:12 2014 +0530
Store tor exit router's ip and name with every request --- ooni/templates/httpt.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-)
diff --git a/ooni/templates/httpt.py b/ooni/templates/httpt.py index 403e222..c9e42bf 100644 --- a/ooni/templates/httpt.py +++ b/ooni/templates/httpt.py @@ -2,10 +2,14 @@ import copy import random import struct
+from zope.interface import implements + from twisted.plugin import IPlugin from twisted.internet import protocol, defer from twisted.internet.ssl import ClientContextFactory
+from txtorcon.interface import IStreamListener + from twisted.internet import reactor from twisted.internet.error import ConnectionRefusedError, DNSLookupError, TCPTimedOutError from twisted.internet.endpoints import TCP4ClientEndpoint @@ -18,7 +22,6 @@ from ooni.utils import log from ooni.settings import config
from ooni.utils.net import BodyReceiver, StringProducer, userAgents - from ooni.utils.trueheaders import TrueHeaders from ooni.errors import handleAllFailures
@@ -26,6 +29,33 @@ from ooni.errors import handleAllFailures class InvalidSocksProxyOption(Exception): pass
+class StreamListener(object): + implements(IStreamListener) + + def __init__(self, request): + self.request = request + + def stream_new(self, stream): + pass + + def stream_succeeded(self, stream): + host=self.request['url'].split('/')[2] + try: + if stream.target_host == host and len(self.request['tor']) == 1: + self.request['tor']['exit_ip'] = stream.circuit.path[-1].ip + self.request['tor']['exit_name'] = stream.circuit.path[-1].name + except: + log.err("Tor Exit ip detection failed") + + def stream_attach(self, stream, circuit): + pass + + def stream_closed(self, stream,**k): + pass + + def stream_failed(self, stream, reason, remote_reason): + pass + class HTTPTest(NetTestCase): """ A utility class for dealing with HTTP based testing. It provides methods to @@ -228,7 +258,7 @@ class HTTPTest(NetTestCase): content_length = int(response.headers.getRawHeaders('content-length')[0]) except Exception: content_length = None - + finished = defer.Deferred() response.deliverBody(BodyReceiver(finished, content_length)) finished.addCallback(self._processResponseBody, request, @@ -285,9 +315,11 @@ class HTTPTest(NetTestCase): request['url'] = url request['headers'] = headers request['body'] = body - request['tor'] = False + request['tor'] = {} if use_tor: - request['tor'] = True + request['tor']['is_tor'] = True + else: + request['tor']['is_tor'] = False
if self.randomizeUA: log.debug("Randomizing user agent") @@ -306,7 +338,7 @@ class HTTPTest(NetTestCase): headers = TrueHeaders(request['headers'])
def errback(failure, request): - if request['tor']: + if request['tor']['is_tor']: log.err("Error performing torified request: %s" % request['url']) else: log.err("Error performing request: %s" % request['url']) @@ -314,6 +346,10 @@ class HTTPTest(NetTestCase): self.addToReport(request, failure_string=failure_string) return failure
+ if use_tor: + state = config.tor_state + state.add_stream_listener(StreamListener(request)) + d = agent.request(request['method'], request['url'], headers, body_producer) d.addErrback(errback, request)
tor-commits@lists.torproject.org