-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
329 lines (292 loc) · 11.7 KB
/
script.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#
# Steven MARTINS <steven.martins.fr@gmail.com>
# -*- encoding: utf-8 -*-
#
import logging, requests, re, json, sys, os, string, time, hashlib
try:
from ConfigParser import ConfigParser, Error, NoOptionError
except:
from configparser import ConfigParser, Error, NoOptionError
from requests.exceptions import RequestException
try:
from urllib import urlencode
except:
from urllib.parse import urlencode
logging.basicConfig(level=logging.INFO,format="%(asctime)-15s:%(levelname)s:%(threadName)s: %(message)s")
log = logging.getLogger("net")
class Conf:
def __init__(self, filename):
self._filename = filename
try:
self._config = ConfigParser(strict=False)
except:
self._config = ConfigParser()
try:
self._config.read(os.path.expanduser(filename))
except Exception as e:
logging.error("[Conf]" + self._filename + ": " + str(e))
raise Exception("Error during loading file " + self._filename)
def getSection(self, section):
data={}
try:
if section in self._config.sections():
for name, value in self._config.items(section):
data[name] = value
except Exception as e:
logging.error("[Conf]" + self._filename + ": " + str(e))
for key, value in data.items():
if ", " in value:
data[key] = value.split(", ")
return data
def get(self, section, option, default=""):
val = default
try:
val = self._config.get(section, option)
except:
val = default
if ", " in val:
return val.split(", ")
return default
def sections(self):
return self._config.sections()
def setSection(self, section, values):
if not self._config.has_section(section):
self._config.add_section(section)
for k, v in values.items():
self._config.set(section, k, v)
def setValue(self, section, option, value):
if not self._config.has_section(section):
self._config.add_section(section)
self._config.set(section, option, value)
def removeSection(self, section):
if self._config.has_section(section):
self._config.remove_section(section)
def removeValue(self, section, option):
if self._config.has_section(section) and self._config.has_option(section, option):
self._config.remove_option(section, option)
def save(self):
with open(self._filename, 'w') as f:
self._config.write(f)
def getAll(self):
data = {}
for section in self.sections():
data[section] = self.getSection(section)
return data
class net(object):
def __init__(self):
self._urls = [
("https://nsupdate.info/myip", "_simple"),
("http://jsonip.com/", "_json"),
("http://checkip.dns.he.net/", "_regex")
]
def _simple(self, text):
try:
return str(text.strip())
except ValueError as e:
log.warning("Unable to parse '%s'" % text)
return None
def _regex(self, text, pattern="[0-9]+(?:\.[0-9]+){3}"):
try:
ips = re.findall(pattern, text)
log.debug("_regex: %s" % str(ips))
except ValueError as e:
log.warning("Unable to parse '%s'" % text)
return None
def _json(self, text, field="ip"):
try:
j = json.loads(text)
return j.get(field)
except Exception as e:
log.warning("Unable to parse '%s'" % text)
return None
def _get(self, url, timeout=10):
log.debug("_get: %s" % url)
try:
r = requests.get(url, timeout=timeout)
except Exception as e:
log.warning("Unable to get '%s': %s" % (url, str(e)))
return None
if r.status_code == 200:
return (r.text)
log.warning("_get: bad status_code: %s" % (r.status_code))
return None
def getIP(self):
for (url, parser) in self._urls:
value = self._get(url)
if value:
try:
f = getattr(self, parser)
res = f(value)
log.debug("[%s] > %s" % (url, res))
return res
except Exception as e:
log.warning("Parse error '%s': %s" % (url, str(e)))
return None
class local(object):
def __init__(self, filepath="~", filename=".myip"):
self._filepath = os.path.expanduser(filepath)
self._filename = filename
def load(self):
datas = None
try:
with open(os.path.join(self._filepath, self._filename), "r") as f:
datas = json.loads(f.read())
except Exception as e:
log.warning("local: Unable to load file %s : %s" % (self._filename, str(e)))
return datas
def save(self, datas):
try:
datas = json.dumps(datas)
with open(os.path.join(self._filepath, self._filename), "w") as f:
f.write(datas)
except Exception as e:
log.warning("local: Unable to write file %s : %s" % (self._filename, str(e)))
class api(object):
def __init__(self):
self._end_point = "https://eu.api.ovh.com/1.0"
self._conf = None
for conffile in ("dynhost.conf","~/dynhost.conf"):
try:
self._conf = Conf(os.path.expanduser(conffile))
break
except Exception as e:
log.warning("Conf error: %s" % str(e))
pass
if not self._conf:
raise Exception("Unable to load dynhost.conf configuration file.")
self._session = requests.Session()
c = self._conf.getSection("credentials")
if not c or len(c) == 0:
raise Exception("No Credentials available on configuration file.")
self._application_key = c["application_key"] if "application_key" in c else None
self._application_secret = c["application_secret"] if "application_secret" in c else None
self._consumer_key = c["consumer_key"] if "consumer_key" in c else None
if not self._application_key:
raise Exception("No application key")
if not self._application_secret:
raise Exception("No application secret")
server_time = self.get('/auth/time', need_auth=False)
log.debug("server_time: %s" % str(server_time))
self._time_delta = server_time - int(time.time())
def _req(self, method, url, datas=None, need_auth=True):
headers = {
'X-Ovh-Application': self._application_key
}
_url = "%s%s" % (self._end_point, url)
body = ""
if datas:
headers['Content-type'] = 'application/json'
body = json.dumps(datas)
if need_auth:
if not self._consumer_key:
log.warning("auth needed without consumer key")
return None
now = str(int(time.time()) + self._time_delta)
sign = "+".join([
self._application_secret, self._consumer_key,
method.upper(), _url,
body,
now
]).encode('utf-8')
log.debug("sign: %s" % (sign))
signature = hashlib.sha1(sign)
headers['X-Ovh-Consumer'] = self._consumer_key
headers['X-Ovh-Timestamp'] = now
headers['X-Ovh-Signature'] = "$1$" + signature.hexdigest()
try:
result = self._session.request(method, _url, headers=headers,
data=body)
log.debug("%s %s > %s %s" % (method, _url, result.status_code, result.text))
log.debug("header: %s, datas: %s" % (headers, body))
except Exception as e:
log.warning("Request: %s, error: %s " % (_url, str(e)))
return None
if result.status_code == 200:
try:
return result.json()
except Exception as e:
log.warning("Unable to decode json from ovh api: %s" % str(e))
log.warning("Wrong status_code: %s %s" % (_url, result.status_code))
if result.status_code == 403:
raise Exception("Unauthorized %s" % str(_url))
if result.status_code == 400:
raise Exception("BadRequest %s" % str(_url))
return None
def get(self, url, datas=None, need_auth=True):
_url = url
if datas:
_url = "%s?%s" % (url, urlencode(datas))
return self._req("GET", _url, None, need_auth)
def post(self, url, datas={}, need_auth=True):
return self._req("POST", url, datas, need_auth)
def put(self, url, datas={}, need_auth=True):
return self._req("PUT", url, datas, need_auth)
def authenticate(self):
access_rules = [
{'method': 'GET', 'path': '/domain/*'},
{'method': 'PUT', 'path': '/domain/zone/*'}, # update a subdomain
{'method': 'POST', 'path': '/domain/zone/*'}, # create new subdomain
]
res = self.post('/auth/credential', need_auth=False,
datas={"accessRules":access_rules,"redirection":None})
self._consumer_key = res['consumerKey']
self._conf.setValue("credentials", "consumer_key", self._consumer_key)
self._conf.save()
return res
def updateHost(self, ip, create=True):
conf = self._conf.getSection("zone")
ttl = int(conf["ttl"]) if "ttl" in conf else 60
if not conf:
raise Exception("zone not configured")
if not "domain" in conf:
raise Exception("domain field missing in 'zone'")
domain = conf["domain"]
subd = conf["subdomain"] if "subdomain" in conf else ""
dataEdit = {"target":ip, "ttl":ttl}
dataFieldType = {"fieldType":"A" if ip.count('.') == 3 else "AAAA"}
dataSubDomain = {"subDomain":subd} if len(subd) > 0 else dict()
dataGet = dict()
dataGet.update(dataFieldType)
dataGet.update(dataSubDomain)
o = self.get("/domain/zone/%s/record" % (domain), dataGet)
if not o or len(o) == 0:
if create:
dataPost = dict()
dataPost.update(dataEdit)
dataPost.update(dataSubDomain)
dataPost.update(dataFieldType)
res = self.post("/domain/zone/%s/record" % (domain), dataPost)
else:
raise Exception("Unable to find %s.%s on Ovh's API" % (subd, domain))
else:
dataPut = dict()
dataPut.update(dataEdit)
dataPut.update(dataSubDomain)
res = self.put("/domain/zone/%s/record/%s" %(domain, o[0]), dataPut)
log.debug("updateHost: result: %s" % str(res))
def main():
n = net()
ip = n.getIP()
if not ip:
log.error("Unable to get your ip. Are you connected to internet ?")
sys.stderr.write("Unable to get your ip. Are you connected to internet ?\n")
l = local()
store = l.load()
if store and "ip" in store and store["ip"] == ip:
log.info("Your ip is the same since last check. Nothing to do.")
return
log.info("Your ip changed to %s" % (ip))
a = api()
if not a.get("/domain/zone"):
res = a.authenticate()
sys.stderr.write("Please visit this address to authenticate the script: %s\n" % str(res["validationUrl"]))
return
try:
a.updateHost(ip)
l.save({"ip": ip})
print("Update OK")
except Exception as e:
log.error("Update fail: %s" % (str(e)))
sys.stderr.write("Error: %s\n" % (str(e)))
if __name__=="__main__":
main()