-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyndns.py
314 lines (239 loc) · 9.22 KB
/
dyndns.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/python
import requests
import json
from django.conf import settings
user_agent = 'WWW Security - Okerr.com Dynamic DNS client - 0.1'
class DDNSExc(Exception):
pass
class DDNSBase(object):
def __init__(self, hostname=None, domain=None, login=None, secret=None, cache=None): # HDLS
self.hostname = hostname
self.domain = domain
self.login = login
self.secret = secret
if domain:
if hostname:
self.fqdn = hostname + '.' + domain
else:
self.fqdn = domain
else:
self.fqdn = hostname
self.headers = dict()
self.headers['User-Agent'] = user_agent
self._cache = dict()
self.cache_updated = False
if cache:
self.cache = json.loads(cache)
def update_cache(self, key, value):
self._cache[key] = value
self.cache_updated = True
def get_cache(self):
return json.dumps(self._cache)
def set_record(self, value):
print("NOT IMPLEMENTED Set record ({}) {} = {}".format(self.method, self.hostname, value))
pass
class YaPDD(DDNSBase):
def __init__(self, hostname=None, domain=None, login=None, secret=None, cache=None):
super(YaPDD, self).__init__(hostname, domain, login, secret, cache)
# token url: https://pddimp.yandex.ru/api2/admin/get_token
self.yasrv = 'https://pddimp.yandex.ru/api2/admin/dns/{}'
self.headers['PddToken'] = secret
self.ttl = 120 # min: 90
def check_error(self, r):
if r.status_code != 200:
raise DDNSExc('HTTP code: {}: {}'.format(r.status_code, r.text))
rdata = json.loads(r.text)
if not 'success' in rdata or not rdata['success']=='ok':
raise DDNSExc(r.text)
def get_rid(self, host):
url = self.yasrv.format('list?domain={}'.format(self.domain))
r = requests.get(url, headers = self.headers)
self.check_error(r)
data = json.loads(r.text)
# print json.dumps(data, indent=4)
for r in data['records']:
if r['subdomain'] == host:
return r['record_id']
return None
def set_record(self, value):
rid = self.get_rid(self.hostname)
if rid:
# change
return self.change(rid, value)
else:
return self.add(self.hostname, value)
def change(self, rid, content):
data = {
'domain': self.domain,
'record_id': rid,
'content': content,
'ttl': self.ttl
}
url = self.yasrv.format('edit')
r = requests.post(
url,
data = data,
headers = self.headers)
if r.status_code != 200:
raise DDNSExc("HTTP code {}: {}", r.status_code, r.text)
rdata = json.loads(r.text)
if 'success' in rdata and rdata['success'] == 'ok':
return r.text
else:
raise DDNSExc(r.text)
def add(self, host, content):
data = {
'domain': self.domain,
'subdomain': host,
'type': 'A',
'ttl': self.ttl,
'content': content
}
url = self.yasrv.format('add')
r = requests.post(
url,
data = data,
headers = self.headers)
if r.status_code != 200:
raise DDNSExc("HTTP code {}: {}", r.status_code, r.text)
rdata = json.loads(r.text)
if 'success' in rdata and rdata['success'] == 'ok':
return r.text
else:
raise DDNSExc(r.text)
class OkerrYaPDD(YaPDD):
def __init__(self, hostname=None, domain=None, login=None, secret=None, cache=None):
super(OkerrYaPDD, self).__init__(hostname, domain, login, secret, cache)
# okerr.com: EV2TIQ744BM3W2MJYY3NXVPUKIHTQKTYDAW4HE5Z42SEZDVKVZJQ
# dyn1.okerr.com: 5PHGKYF25ASL62MXE4JHTWNRYFOWQWFBZM4SQPNAFROBGGXLNE5A
self.domain = 'dyn1.okerr.com'
#self.yasrv = 'https://pddimp.yandex.ru/api2/admin/dns/{}'
self.headers['PddToken'] = settings.OKERR_PDD_TOKEN
#self.ttl = 120 # min: 90
class CloudflareDNS(DDNSBase):
def __init__(self, hostname=None, domain=None, login=None, secret=None, cache=None):
super(CloudflareDNS, self).__init__(hostname, domain, login, secret, cache)
self.headers['X-Auth-Key'] = self.secret
self.headers['X-Auth-Email'] = self.login
self.ttl = 120 # min cf ttl
def get_zid(self):
if 'zid' in self._cache:
return self._cache['zid']
r = requests.get('https://api.cloudflare.com/client/v4/zones', headers = self.headers )
if r.status_code != 200:
raise DDNSExc('HTTP code: {}: {}'.format(r.status_code, r.text))
data = json.loads(r.text)
for z in data['result']:
if z['name'] == self.domain:
self.update_cache('zid', z['id'])
return self._cache['zid']
def get_records(self):
zid = self.get_zid()
r = requests.get('https://api.cloudflare.com/client/v4/zones/{}/dns_records'.format(zid), headers = self.headers )
if r.status_code != 200:
raise DDNSExc('HTTP code: {}: {}'.format(r.status_code, r.text))
data = json.loads(r.text)
return data
def get_rid(self):
if 'rid' in self._cache:
return self._cache['rid']
data = self.get_records()
#print json.dumps(data, indent=4)
for r in data['result']:
if r['type'] == 'A' and r['name'] == self.fqdn:
self.update_cache('rid', r['id'])
return r['id']
def check_error(self, r):
if r.status_code != 200:
raise DDNSExc('HTTP code: {}: {}'.format(r.status_code, r.text))
rdata = json.loads(r.text)
if not 'success' in rdata or not rdata['success']:
if rdata['errors']:
raise DDNSExc(str(rdata['errors']))
raise DDNSExc(r.text)
# cloudflaredns.create_record
def create_record(self, value):
zid = self.get_zid()
data = {
'type': 'A',
'name': self.fqdn,
'content': value,
'ttl': self.ttl
}
r = requests.post('https://api.cloudflare.com/client/v4/zones/{}/dns_records'.format(zid), json = data, headers = self.headers )
self.check_error(r)
return r.text
def update_record(self, rid, value):
zid = self.get_zid()
data = {
'type': 'A',
'name': self.fqdn,
'content': value,
'ttl': self.ttl
}
r = requests.put('https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}'.format(zid, rid), json = data, headers = self.headers )
self.check_error(r)
# print "code: {} text: {}".format(r.status_code, r.text)
return r.text
def set_record(self, value):
# print "set record for {} {} ({})".format(self.hostname, self.domain, self.fqdn)
rid = self.get_rid()
if rid:
return self.update_record(rid, value)
else:
return self.create_record(value)
class OkerrCloudflareDNS(CloudflareDNS):
def __init__(self, hostname=None, domain=None, login=None, secret=None, cache=None):
super(OkerrCloudflareDNS, self).__init__(hostname, domain,
settings.OKERR_CLOUDFLARE_USER,
settings.OKERR_CLOUDFLARE_SECRET,
cache)
class HeNetDNS(DDNSBase):
def set_record(self, value):
data = {
'hostname': self.fqdn,
'password': self.secret,
'myip': value
}
r = requests.post('https://dyn.dns.he.net/nic/update', data = data)
if r.status_code != 200:
raise DDNSExc('HTTP code {}: {}'.format(r.status_code, r.text))
return r.text
#
# Main unified DynDNS
#
class DynDNS:
def __init__(self, method=None, hostname=None, domain = None, login = None, secret = None, cache = None):
self.method = method
self.hostname = hostname
self.domain = domain
self.login = login
self.secret = secret
classes = {
'okerr/yapdd': OkerrYaPDD,
'yapdd': YaPDD,
'cloudflare': CloudflareDNS,
'okerr/cloudflare': OkerrCloudflareDNS,
'he.net': HeNetDNS,
}
try:
self.service = classes[method](
hostname = hostname,
domain = domain,
login = login,
secret = secret,
cache = cache)
except KeyError:
raise DDNSExc('Unknown method "{}"'.format(method))
# dyndns set_record
def set_record(self, value):
# print "Set record ({}) {} = {}".format(self.method, self.hostname, value)
if self.service:
try:
return self.service.set_record(value)
except requests.exceptions.RequestException as e:
raise DDNSExc("HTTP: {}".format(str(e)))
else:
raise DDNSExc('Service not set (method: {})!'.format(self.method))
if __name__ == '__main__':
pass