Skip to content

Commit

Permalink
Do not query DNS if last_succes in the last minute (#9)
Browse files Browse the repository at this point in the history
Do not query DNS if last_succes in the last minute
  • Loading branch information
Andreea Dima authored Sep 7, 2018
1 parent a82b6c0 commit a89059c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions dyndns/domain_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ def main(domain, settings='settings.txt'):
return "Domain {} configured incorectly. Rerun setup.".format(domain)
print("Read {} config.".format(domain))

# read existing ip for domain
# read existing ip for domain from config || from DNS if last check was less than 60 sec ago
ip = None
try:
answers = dns.resolver.query(domain, 'A')
if not answers:
return "No A record found for domain {}".format(domain)
ip = answers[0]
print("IP {} found in A record".format(ip))
except Exception:
print("No A record found for domain {}".format(domain))
if 'last_success' in config[domain] and int(time.time()) - config[domain]['last_success'] < 60:
ip = config[domain]['ip']
print("Recently used IP {}".format(ip))
else:
try:
answers = dns.resolver.query(domain, 'A')
if not answers:
return "No A record found for domain {}".format(domain)
ip = answers[0]
print("IP {} found in A record".format(ip))
config[domain]['last_dns_check'] = int(time.time())
except Exception:
print("No A record found for domain {}".format(domain))

# get public ip
response = requests.get("https://api.ipify.org", params={'format': 'json'})
Expand All @@ -46,8 +51,6 @@ def main(domain, settings='settings.txt'):
if not public_ip:
return "Could not discover public IP."

config[domain]['last_dns_check'] = int(time.time())

print("New IP: {}".format(public_ip))
if ip and str(ip) == str(public_ip):
config[domain]['ip'] = public_ip
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name = 'domain-connect-dyndns',
version = '0.0.2',
version = '0.0.3',
description = 'Python client library for Dynamic DNS using Domain Connect',
license = 'MIT',
long_description=long_description,
Expand Down

0 comments on commit a89059c

Please sign in to comment.