-
Notifications
You must be signed in to change notification settings - Fork 4
/
gratisdns.py
246 lines (216 loc) · 8.76 KB
/
gratisdns.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
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2007 Mads Sülau Jørgensen <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import re
from urllib import urlopen, urlencode
from BeautifulSoup import BeautifulSoup
__version__ = '$Id$'
__license__ = 'MIT'
__copyright__ = 'Mads Sülau Jørgensen <[email protected]>'
class GratisDNS(object): # {{{
BACKEND_URL = 'https://oldsystem.gratisdns.dk/editdomains4.phtml'
SUPPORTED_RECORDS = ('A', 'AAAA', 'CNAME', 'MX', 'TXT', 'SRV')
def __init__(self, username, password):
self.username = username
self.password = password
# }}}
def _get_domains(self, soup): # {{{
domains = set()
for domain in soup.findAll('input', {'name': 'user_domain', 'type': 'hidden'}):
domains.add(domain['value'])
return list(domains)
# }}}
def _get_records(self, soup): # {{{
records = []
siblings = soup.findAll('tr', {'class': re.compile('BODY[1-2]BG')})
for sibling in siblings:
type = sibling.parent.find('td').next.string
if type in self.SUPPORTED_RECORDS and sibling.find('input'):
record = {}
tds = sibling.findAll('td')
form = sibling.find('form')
record['type'] = type
record['recordid'] = int(sibling.find('input', {'name': 'recordid'})['value'])
record['domainid'] = int(sibling.find('input', {'name': 'domainid'})['value'])
record['host'] = tds[0].string
record['data'] = tds[1].string
if type == 'MX':
record['preference'] = int(tds[2].string)
record['ttl'] = int(tds[3].string)
elif type != 'TXT':
record['ttl'] = int(tds[2].string)
records.append(record)
return records
# }}}
def create_record(self, domain, host, type, data, preference=None, weight=None, port=None): # {{{
if type in self.SUPPORTED_RECORDS:
if host.find(domain) == -1:
if host == '':
host = domain
else:
host = "%s.%s" % (host, domain)
args = {
'action': 'add%srecord' % type.lower(),
'user_domain': domain,
}
if type in ('A', 'AAAA'):
args['host'] = host
args['ip'] = data
elif type == 'CNAME':
args['host'] = host
args['kname'] = data
elif type == 'MX':
args['host'] = host
args['preference'] = preference or 10
args['exchanger'] = data
elif type == 'TXT':
args['leftRR'] = host
args['rightRR'] = data
elif type == 'SRV':
args['host'] = host
args['exchanger'] = data
args['preference'] = preference or 10
args['weight'] = weight or 0
args['port'] = port or 0
soup = self._request(**args)
for record in self._get_records(soup):
if record['host'] == host:
return True
return False
else:
raise ValueError, 'Unsupported record type.'
# }}}
def update_record(self, domain, recordid, host, type, data, ttl): # {{{
if type in self.SUPPORTED_RECORDS:
if host.find(domain) == -1:
if host == '':
host = domain
else:
host = "%s.%s" % (host, domain)
soup = self._request(
action='makechangesnow',
recordid=recordid,
type=type,
user_domain=domain,
host=host,
new_data=data,
new_ttl=ttl,
)
for record in self._get_records(soup):
if record['host'] == host:
return True
return False
else:
raise ValueError, 'Unsupported record type.'
# }}}
def delete_record(self, domain, host, type=None, preference=None): # {{{
records = self.get_primary_domain_details(domain)
if host.find(domain) == -1:
if host == '':
host = domain
else:
host = "%s.%s" % (host, domain)
record = None
for record in records:
if record['host'] == host:
if not type or record['type'] == type:
if not preference or record['preference'] == preference:
break
if record:
soup = self._request(
action='delete%s' % record['type'].lower(),
recordid=record['recordid'],
domainid=record['domainid'],
type=record['type'],
)
for record in self._get_records(soup):
if record['host'] == host:
return False
return True
else:
raise ValueError, 'Host not found.'
# }}}
def get_primary_domains(self): # {{{
return self._get_domains(self._request(action='primarydns'))
# }}}
def get_secondary_domains(self): # {{{
return self._get_domains(self._request(action='secondarydns'))
# }}}
def get_primary_domain_details(self, domain): # {{{
soup = self._request(action='changeDNSsetup', user_domain=domain)
return self._get_records(soup)
# }}}
def create_primary_domain(self, domain): # {{{
soup = self._request(action='createprimaryandsecondarydnsforthisdomain', user_domain=domain)
return domain in self._get_domains(soup)
# }}}
def create_secondary_domain(self, domain, master, slave='xxx.xxx.xxx.xxx'): # {{{
soup = self._request(
action='createsecondarydnsforthisdomain',
user_domain=domain,
user_domain_ip=master,
user_domain_ip2=slave
)
return domain in self._get_domains(soup)
# }}}
def delete_primary_domain(self, domain): # {{{
soup = self._request(action="deleteprimarydnsnow", user_domain=domain)
return domain not in self._get_domains(soup)
# }}}
def delete_secondary_domain(self, domain): # {{{
soup = self._request(action="deletesecondarydns", user_domain=domain)
return domain not in self._get_domains(soup)
# }}}
def import_from_axfr(self, domain, slave="127.0.0.1"): # {{{
records = self.get_primary_domain_details(domain)
if len(records) > 0:
domainid = records[0]['domainid']
else:
return False
soup = self._request(
action='importdomainfromaxfrnow',
domainid=domainid,
ip=slave
)
system_messages = soup.findAll('td', {'class': 'systembesked'})
if len(system_messages) == 1:
message = system_messages[0].text
domains = system_messages[0].findAll('input', {'name': 'domain'})
if len(domains) == 1:
return domains[0]['value'] + ': ' + message
else:
return False
else:
return False
# }}}
def test_axfr(self, domain, master, slave=None): # {{{
raise NotImplementedError()
# }}}
def _request(self, **kwargs): # {{{
kwargs['user'] = self.username
kwargs['password'] = self.password
req = urlopen(self.BACKEND_URL, urlencode(kwargs))
return BeautifulSoup(req.read())
# }}}
if __name__ == '__main__':
# TODO: Add tests here.
pass