oshipka/provision/auto_dns/check.py

174 lines
4.4 KiB
Python
Raw Permalink Normal View History

2020-06-21 11:17:34 +02:00
import csv
import ipaddress
import threading
from collections import defaultdict
from datetime import datetime
from time import sleep
import sys
import requests
from lexicon.client import Client
from lexicon.config import ConfigResolver
from oshipka.util.process import process_exp_backoff
2020-06-21 17:45:33 +02:00
from sensitive import CLOUDFLARE_USERNAME, CLOUDFLARE_AUTH_KEY
2020-06-21 11:17:34 +02:00
SLEEP_SECONDS = 60 * 30 # 30 min
2020-06-21 17:45:33 +02:00
DEFAULT_TTL = 5 * 60
2020-06-21 11:17:34 +02:00
ipv4_sites = [
"http://www.icanhazip.com",
"http://ipecho.net/plain",
"http://checkip.amazonaws.com",
"http://ipinfo.io/ip",
"http://ifconfig.me"
]
ipv6_sites = [
"https://diagnostic.opendns.com/myip",
"https://ifconfig.co/ip",
]
ip_checkers = ipv4_sites + ipv6_sites
2020-06-21 17:45:33 +02:00
dns_to_check = []
2020-06-21 11:17:34 +02:00
results = []
real = defaultdict(dict)
def test_ip(ip):
try:
ipaddress_info = ipaddress.ip_address(ip)
except ValueError as e:
print(e)
return None
if ipaddress_info.version == 4:
version = 'A'
elif ipaddress_info.version == 6:
version = 'AAAA'
else:
return None
return {
"addr": ipaddress_info.exploded,
"version": version,
}
def fetch_ip(url):
try:
resp = requests.get(url)
ip = resp.text.strip()
ip_info = test_ip(ip)
if ip_info:
results.append(ip_info)
except Exception as e:
print("Error fetching {}: {}".format(url, e))
tso, tsd = [], []
def check_observed():
for url in ip_checkers:
t = threading.Thread(target=fetch_ip, args=(url,))
tso.append(t)
t.start()
def check_dns():
for domain, ipv in dns_to_check:
t = threading.Thread(target=get_dns, args=(domain, ipv,))
tsd.append(t)
t.start()
2020-06-21 17:45:33 +02:00
def get_resolver_results(domain, ipv):
2020-06-21 11:17:34 +02:00
dns_config = ConfigResolver()
dns_config.with_dict({
2020-06-21 17:45:33 +02:00
'provider_name': 'cloudflare',
2020-06-21 11:17:34 +02:00
'action': 'list',
'type': ipv,
'domain': domain,
2020-06-21 17:45:33 +02:00
'cloudflare': {
'auth_username': CLOUDFLARE_USERNAME,
'auth_token': CLOUDFLARE_AUTH_KEY,
2020-06-21 11:17:34 +02:00
}
})
client = Client(dns_config)
return client.execute()
2020-06-21 17:45:33 +02:00
def set_resolver_results(domain, ipv, new):
2020-06-21 11:17:34 +02:00
dns_config = ConfigResolver()
dns_config.with_dict({
2020-06-21 17:45:33 +02:00
'provider_name': 'cloudflare',
'action': 'create',
2020-06-21 11:17:34 +02:00
'type': ipv,
'domain': domain,
'name': domain,
'content': new,
2020-06-21 17:45:33 +02:00
'ttl': DEFAULT_TTL,
'cloudflare': {
'auth_username': CLOUDFLARE_USERNAME,
'auth_token': CLOUDFLARE_AUTH_KEY,
2020-06-21 11:17:34 +02:00
}
})
client = Client(dns_config)
return client.execute()
def get_dns(domain, ipv):
2020-06-21 17:45:33 +02:00
results = process_exp_backoff(get_resolver_results, func_args=[domain, ipv])
2020-06-21 11:17:34 +02:00
res = [x.get('content') for x in results if x.get('name') == domain]
if len(res) == 1:
print("Real {}: {}".format(ipv, res[0]))
real[domain][ipv] = res[0]
def set_dns(domain, ipv, new):
2020-06-21 17:45:33 +02:00
return process_exp_backoff(set_resolver_results, func_args=[domain, ipv, new])
2020-06-21 11:17:34 +02:00
def get_uniq(_list):
if len(_list) > 1 and len(set(_list)) == 1:
return _list[0]
def main():
2020-06-21 17:45:33 +02:00
with open('dns_addresses.csv') as csvf:
csvreader = csv.reader(csvf)
for row in csvreader:
dns_to_check.append(row)
2020-06-21 11:17:34 +02:00
check_dns()
check_observed()
for t in tso:
t.join()
for t in tsd:
t.join()
for domain, version in dns_to_check:
ip_obs = get_uniq([x['addr'] for x in results if x['version'] == version])
print("{}/{}: {}".format(domain, version, ip_obs))
ip_real = real.get(domain).get(version)
sys.stdout.flush()
if ip_obs == ip_real:
print('{}/{} is same!'.format(domain, version))
else:
print("{}/{} diff on dns: real: {}, obs: {}".format(domain, version, ip_real, ip_obs))
if set_dns(domain, version, ip_real):
print("update successful {}/{} -> {}".format(domain, version, ip_real))
else:
print("update failed: {}/{} on dns is {}, but obs is {}".format(
domain, version, ip_real, ip_obs
))
if __name__ == "__main__":
while True:
print("{}: Waking up".format(datetime.utcnow()))
sys.stdout.flush()
main()
print("{}: Sleeping for {}".format(datetime.utcnow(), SLEEP_SECONDS))
sys.stdout.flush()
sleep(SLEEP_SECONDS)