dns: implement querying directly the master server
This commit is contained in:
parent
0d628f35e0
commit
d71122aa1c
27
nsupdate/main/_tests/test_dnstools.py
Normal file
27
nsupdate/main/_tests/test_dnstools.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""
|
||||
Tests for dnstools module.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from nsupdate.main.dnstools import (query_ns, NONEXISTING_HOST, WWW_HOST, WWW_IPV4_HOST, WWW_IPV4_IP,
|
||||
WWW_IPV6_HOST, WWW_IPV6_IP, )
|
||||
|
||||
from dns.resolver import NXDOMAIN
|
||||
|
||||
|
||||
class Tests(object):
|
||||
def test_queries_ok(self):
|
||||
"""
|
||||
check some simple dns lookups
|
||||
"""
|
||||
assert query_ns(WWW_IPV4_HOST, 'A') == WWW_IPV4_IP # v4 ONLY
|
||||
assert query_ns(WWW_IPV6_HOST, 'AAAA') == WWW_IPV6_IP # v6 ONLY
|
||||
assert query_ns(WWW_HOST, 'A') == WWW_IPV4_IP # v4 and v6, query v4
|
||||
assert query_ns(WWW_HOST, 'AAAA') == WWW_IPV6_IP # v4 and v6, query v6
|
||||
|
||||
def test_queries_failing(self):
|
||||
with pytest.raises(NXDOMAIN):
|
||||
query_ns(NONEXISTING_HOST, 'A')
|
||||
with pytest.raises(NXDOMAIN):
|
||||
query_ns(NONEXISTING_HOST, 'AAAA')
|
34
nsupdate/main/dnstools.py
Normal file
34
nsupdate/main/dnstools.py
Normal file
@ -0,0 +1,34 @@
|
||||
"""
|
||||
Misc. DNS related code: query, dynamic update, etc.
|
||||
"""
|
||||
|
||||
SERVER = '85.10.192.104' # ns1.thinkmo.de (master / dynamic upd server for nsupdate.info)
|
||||
BASEDOMAIN = 'nsupdate.info'
|
||||
|
||||
NONEXISTING_HOST = 'nonexisting.' + BASEDOMAIN
|
||||
WWW_HOST = 'www.' + BASEDOMAIN
|
||||
WWW_IPV4_HOST = 'www.ipv4.' + BASEDOMAIN
|
||||
WWW_IPV6_HOST = 'www.ipv6.' + BASEDOMAIN
|
||||
WWW_IPV4_IP = '178.32.221.14'
|
||||
WWW_IPV6_IP = '2001:41d0:8:e00e::1'
|
||||
|
||||
import dns.name
|
||||
import dns.resolver
|
||||
|
||||
def query_ns(qname, rdtype):
|
||||
"""
|
||||
query a dns name from our master server
|
||||
|
||||
:param qname: the query name
|
||||
:type qname: dns.name.Name object or str
|
||||
:param rdtype: the query type
|
||||
:type rdtype: int or str
|
||||
:return: IP (as str)
|
||||
"""
|
||||
resolver = dns.resolver.Resolver(configure=False)
|
||||
# we do not configure it from resolv.conf, but patch in the values we
|
||||
# want into the documented attributes:
|
||||
resolver.nameservers = [SERVER, ]
|
||||
resolver.search = [dns.name.from_text(BASEDOMAIN), ]
|
||||
answer = resolver.query(qname, rdtype)
|
||||
return str(list(answer)[0])
|
Loading…
x
Reference in New Issue
Block a user