Skip to content

Commit

Permalink
improve dnswanip error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
infothrill committed Feb 21, 2021
1 parent 4110863 commit 539eb97
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Expand Up @@ -4,7 +4,7 @@ Release history
---------------
0.6.x (unreleased)
++++++++++++++++++
TBD
- improved: dnswanip error reporting

0.6.0 (February 21st 2021)
++++++++++++++++++++++++++
Expand Down
21 changes: 13 additions & 8 deletions dyndnsc/detector/dnswanip.py
Expand Up @@ -11,12 +11,11 @@
import dns.resolver

from .base import IPDetector, AF_INET, AF_INET6
from .dns import resolve

LOG = logging.getLogger(__name__)


def find_ip(family=AF_INET, flavour="opendns"):
def find_ip(family=AF_INET, provider="opendns"):
"""Find the publicly visible IP address of the current system.
This uses public DNS infrastructure that implement a special DNS "hack" to
Expand All @@ -25,7 +24,7 @@ def find_ip(family=AF_INET, flavour="opendns"):
:param family: address family, optional, default AF_INET (ipv4)
:param flavour: selector for public infrastructure provider, optional
"""
flavours = {
dnswanipproviders = {
"opendns": {
AF_INET: {
"@": ("resolver1.opendns.com", "resolver2.opendns.com"),
Expand All @@ -40,12 +39,18 @@ def find_ip(family=AF_INET, flavour="opendns"):
},
}

flavour = flavours["opendns"]
resolver = dns.resolver.Resolver()
# specify the custom nameservers to be used (as IPs):
resolver.nameservers = [next(iter(resolve(h, family=family))) for h in flavour[family]["@"]]
dnswanipprovider = dnswanipproviders[provider] # only option as of now

answers = resolver.query(qname=flavour[family]["qname"], rdtype=flavour[family]["rdtype"])
resolver = dns.resolver.Resolver()
# first, get the IPs of the DNS servers:
nameservers = []
for dnsservername in dnswanipprovider[family]["@"]:
_answers = resolver.query(qname=dnsservername, rdtype=dnswanipprovider[family]["rdtype"])
nameservers.extend([rdata.address for rdata in _answers])
# specify the nameservers to be used:
resolver.nameservers = nameservers
# finally, attempt to discover our WAN IP by querying the DNS:
answers = resolver.query(qname=dnswanipprovider[family]["qname"], rdtype=dnswanipprovider[family]["rdtype"])
for rdata in answers:
return rdata.address
return None
Expand Down

0 comments on commit 539eb97

Please sign in to comment.