-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodns.py
executable file
·91 lines (67 loc) · 2.37 KB
/
dodns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
from os import path
from datetime import datetime
from configparser import ConfigParser
try:
from requests import put, get
except:
print("Seems you forgot to install python modules.")
print("Run: `pip install -r requirements.txt`")
raise
API_URL = "https://api.digitalocean.com/v2/domains/"
CHECK_IP = "https://api.ipify.org"
def main():
config = ConfigParser()
config.read(path.join(path.dirname(__file__), 'config.ini'))
token = config['settings']['token']
domains = [domain.strip() for domain in config['settings']['domains'].split(',')]
try:
for domain in domains:
now = datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')
ip = update_ip(token, domain)
if ip:
print(now, domain + ": from " + ip['domain'] + " to " + ip['device'])
except:
now = datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')
print(now)
raise
def get_record_id(token, domain):
subdomain = None
if len(domain.split('>')) > 1:
subdomain = domain.split('>')[0]
domain = domain.split('>')[1]
url = API_URL + "%s/records" % domain
headers = {
"content-type": "application/json",
"authorization": "Bearer %s" % token
}
response = get(url, headers=headers).json()
if subdomain is None:
record = [record for record in response['domain_records']
if record['type'] == "A" and record['name'] == "@"][0]
else:
record = [record for record in response['domain_records']
if record['type'] == "A" and record['name'] == subdomain][0]
return record
def update_ip(token, domain):
record = get_record_id(token, domain)
ip = {
"domain": record['data'],
"device": get(CHECK_IP).text
}
response = None
domain = domain.split('>')[1] if len(domain.split('>')) > 1 else domain
if ip['domain'] != ip['device']:
record_id = str(record['id'])
url = API_URL + "%s/records/%s" % (domain, record_id)
payload = '{"data": "%s"}' % ip['device']
headers = {
"content-type": "application/json",
"authorization": "Bearer %s" % token
}
response = put(url, data=payload, headers=headers).status_code
if response == 200:
return ip
return False
if __name__ == "__main__":
main()