Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dnst catch exceptions #19

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nettests/blocking/dnstamper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down
17 changes: 17 additions & 0 deletions ooni/nettest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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'
Expand All @@ -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):
Expand Down
57 changes: 28 additions & 29 deletions ooni/templates/dnst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -86,27 +79,33 @@ 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)
d.addCallback(gotResponse)
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)