diff --git a/nettests/blocking/dnstamper.py b/nettests/blocking/dnstamper.py index 7a60a653e..1ef2c8991 100644 --- a/nettests/blocking/dnstamper.py +++ b/nettests/blocking/dnstamper.py @@ -111,16 +111,16 @@ def test_a_lookup(self): return for test_resolver in self.test_resolvers: - log.msg("Testing %s test resolver" % test_resolver) + log.msg("Testing resolver: %s" % test_resolver) test_dns_server = (test_resolver, 53) experiment_answers = yield self.performALookup(hostname, test_dns_server) - log.debug("Got the following A lookup answers %s for %s" % (experiment_answers, test_resolver)) - if not experiment_answers: log.err("Got no response, perhaps the DNS resolver is down?") self.report['tampering'][test_resolver] = 'no_answer' continue + else: + log.debug("Got the following A lookup answers %s from %s" % (experiment_answers, test_resolver)) def lookup_details(): """ diff --git a/ooni/nettest.py b/ooni/nettest.py index 8374db111..39aba9852 100644 --- a/ooni/nettest.py +++ b/ooni/nettest.py @@ -17,8 +17,13 @@ from twisted.python import usage from twisted.internet.error import ConnectionRefusedError, DNSLookupError, TCPTimedOutError +from twisted.internet.defer import TimeoutError +from twisted.web._newclient import ResponseNeverReceived from ooni.utils import log +from ooni.utils.txagentwithsocks import SOCKSError + +from socket import gaierror def failureToString(failure): """ @@ -33,10 +38,15 @@ def failureToString(failure): A string representing the HTTP response error message. """ + string = None if isinstance(failure.value, ConnectionRefusedError): log.err("Connection refused. The backend may be down") string = 'connection_refused_error' + elif isinstance(failure.value, gaierror): + log.err("Address family for hostname not supported") + string = 'address_family_not_supported_error' + elif isinstance(failure.value, SOCKSError): log.err("Sock error. The SOCKS proxy may be down") string = 'socks_error' @@ -52,6 +62,13 @@ def failureToString(failure): elif isinstance(failure.value, ResponseNeverReceived): log.err("Response Never Received") string = 'response_never_received' + + elif isinstance(failure.value, TimeoutError): + log.err("Deferred Timed Out Error") + string = 'deferred_timed_out_error' + + else: + log.err("Unknown failure type: %s" % type(failure)) return string class NoPostProcessor(Exception): diff --git a/ooni/templates/dnst.py b/ooni/templates/dnst.py index 01678cbe4..ece1ff445 100644 --- a/ooni/templates/dnst.py +++ b/ooni/templates/dnst.py @@ -4,13 +4,15 @@ # :licence: see LICENSE from twisted.internet import defer +from twisted.internet.defer import TimeoutError from twisted.names import client, dns from twisted.names.client import Resolver from twisted.names.error import DNSQueryRefusedError from ooni.utils import log -from ooni.nettest import NetTestCase +from ooni.nettest import NetTestCase, failureToString +from socket import gaierror class DNSTest(NetTestCase): name = "Base DNS Test" @@ -39,23 +41,14 @@ def gotResponse(message): for answer in message.answers: if answer.type is 12: name = answer.payload.name - - result = {} - result['resolver'] = dns_server - result['query_type'] = 'PTR' - result['query'] = repr(query) - result['answers'] = answers - result['name'] = name - self.report['queries'].append(result) + self.addToReport(query, resolver=dns_server, + query_type = 'PTR', answers=answers, name=name) return name def gotError(failure): - log.exception(failure) - result = {} - result['resolver'] = dns_server - result['query_type'] = 'PTR' - result['query'] = repr(query) - result['error'] = str(failure) + failure.trap(gaierror, TimeoutError) + self.addToReport(query, resolver=dns_server, + query_type = 'PTR', failure=failure) return None resolver = Resolver(servers=[dns_server]) @@ -86,23 +79,15 @@ def gotResponse(message): # tuple r = (repr(answer), repr(answer.payload)) answers.append(r) - result = {} - result['resolver'] = dns_server - result['query_type'] = 'A' - result['query'] = repr(query) - result['answers'] = answers - result['addrs'] = addrs - self.report['queries'].append(result) + self.addToReport(query, resolver=dns_server, query_type='A', + answers=answers, addrs=addrs) return addrs def gotError(failure): - log.exception(failure) - result = {} - result['resolver'] = dns_server - result['query_type'] = 'A' - result['query'] = repr(query) - result['error'] = str(failure) - return None + failure.trap(gaierror, TimeoutError) + self.addToReport(query, resolver=dns_server, query_type='A', + failure=failure) + return failure resolver = Resolver(servers=[dns_server]) d = resolver.queryUDP(query, timeout=self.queryTimeout) @@ -110,3 +95,17 @@ def gotError(failure): d.addErrback(gotError) return d + def addToReport(self, query, resolver=None, query_type=None, + answers=None, name=None, addrs=None, failure=None): + log.debug("Adding %s to report)" % query) + result = {} + result['resolver'] = resolver + result['query_type'] = query_type + result['query'] = repr(query) + if failure: + result['failure'] = failureToString(failure) + if answers: + result['answers'] = answers + if name: result['name'] = name + if addrs: result['addrs'] = addrs + self.report['queries'].append(result)