-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyddns.py
133 lines (123 loc) · 5.53 KB
/
pyddns.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import re
from sys import exit
from urllib import request
from urllib.error import HTTPError
class GODADDY_DDNS():
CONFIG_FILE_PATH = './pyddns_config'
def __init__(self):
try:
CONFIG_FILE = open(self.CONFIG_FILE_PATH, 'r')
self.config = json.loads(CONFIG_FILE.read())
CONFIG_FILE.close()
KEY = self.config["user"]["key"]
SECRET = self.config["user"]["secret"]
self.HEADERS = {
"Accept": "application/json",
'Content-type': 'application/json',
'Authorization': 'sso-key {}:{}'.format(KEY, SECRET)
}
self.DOMAINS = self.config["user"]["domains"].split(",")
except FileNotFoundError as e:
print("Config file not found.")
fd = open(self.CONFIG_FILE_PATH,'w')
fd.write('''
{
"user": {
"key": "",
"secret": "",
"domains": ""
},
"ipv4": {
"enable": 0,
"checkurl": "https://myip4.ipip.net",
"pattern": "([0-9]{1,3}.){3}[0-9]{1,3}",
"type": "A",
"cache": {
"name.domain": "ip"
},
"names": "",
"TTL": 600
},
"ipv6": {
"enable": 0,
"checkurl": "https://myip6.ipip.net",
"pattern": "([0-9a-f]{0,4}:|::){1,7}[0-9a-f]{0,4}",
"type": "AAAA",
"cache": {
"name.domain": "ip"
},
"names": "",
"TTL": 600
}
}
''')
fd.close()
print("Config file be created.")
print("Run after edit config.")
exit(1)
def main(self):
for DOMAIN in self.DOMAINS:
if self.config["ipv4"]["enable"]:
self.iptype = "ipv4"
self.getpbip()
for NAME in self.config[self.iptype]["names"].split(","):
if DOMAIN == "" or NAME == "":
continue
self.reqget(DOMAIN, NAME)
if self.config["ipv6"]["enable"]:
self.iptype = "ipv6"
self.getpbip()
for NAME in self.config[self.iptype]["names"].split(","):
if DOMAIN == "" or NAME == "":
continue
self.reqget(DOMAIN, NAME)
def getpbip (self):
CHECK_URL = self.config[self.iptype]["checkurl"]
# Get Host Public IP
PUBLIC_IP = request.urlopen(CHECK_URL).read().decode('utf-8')
regex = self.config[self.iptype]["pattern"]
res = re.search(regex, PUBLIC_IP)
if res:
self.PUBLIC_IP = res.group()
print("Get Public IP: {}".format(self.PUBLIC_IP))
else:
print("Fail! Public IP: {}".format(self.PUBLIC_IP))
exit(1)
def reqget(self,DOMAIN,NAME):
AD = "%s.%s"%(NAME, DOMAIN)
GOD_ADDY_API_URL = "https://api.godaddy.com/v1/domains/{}/records/{}/{}".format(DOMAIN, self.config[self.iptype]["type"], NAME)
# Check if the IP needs to be updated
if AD not in self.config[self.iptype]["cache"]:
self.config[self.iptype]["cache"][AD] = ""
CACHED_IP = self.config[self.iptype]["cache"][AD]
if CACHED_IP != self.PUBLIC_IP:
req = request.Request(GOD_ADDY_API_URL, headers=self.HEADERS)
try:
with request.urlopen(req) as response:
data = json.loads(response.read().decode('utf-8'))
NAME_BIND_IP = data[0]["data"] if data else None
except HTTPError as e:
if e.code in (422, 404):
NAME_BIND_IP = None
if e.code == 401:
print("Authentication info not sent or invalid")
exit(1)
else:
print(e)
exit(1)
if NAME_BIND_IP == self.PUBLIC_IP:
print("unchanged! Current 'Public IP' matches 'GoDaddy' records. No update required!")
else:
print("changed! Updating '{}.{}', {} to {}".format(NAME, DOMAIN, NAME_BIND_IP, self.PUBLIC_IP))
data = json.dumps([{"data": self.PUBLIC_IP, "name": NAME, "ttl": self.config[self.iptype]["TTL"], "type": self.config[self.iptype]["type"]}]).encode('utf-8')
req = request.Request(GOD_ADDY_API_URL, data=data, headers=self.HEADERS, method='PUT')
with request.urlopen(req) as response:
print("Success!" if not response.read().decode('utf-8') else "Success!")
self.config[self.iptype]["cache"][AD] = self.PUBLIC_IP
CONFIG_FILE = open(self.CONFIG_FILE_PATH, mode="w", encoding="utf-8").write(json.dumps(self.config, sort_keys=True, indent=4, separators=(',', ': ')))
else:
print("Current 'Public IP' matches 'Cached IP' recorded. No update required!")
CONFIG_FILE = open(self.CONFIG_FILE_PATH, mode="w", encoding="utf-8").write(json.dumps(self.config, sort_keys=True, indent=4, separators=(',', ': ')))
ddns = GODADDY_DDNS()
ddns.main()