-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_bind.py
More file actions
102 lines (92 loc) · 2.04 KB
/
utils_bind.py
File metadata and controls
102 lines (92 loc) · 2.04 KB
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
import os
import time
from dnslib import RR, QTYPE, SOA, DNSLabel, CLASS
from utils_debugging import Echo
bind_path = os.environ['BIND_NAMED_PATH']
retry_sleep = 0.5
retry_times = 5
def HostConvert(r, domain):
label = DNSLabel(r.get_rname()).label
ret = ".".join([ s.decode("idna") for s in label ]) + "$"
idx = ret.rfind(domain + "$")
if idx != -1:
ret = ret[0:idx] + "$"
ret = ret[0:-1]
if len(ret) > 0 and ret[-1] == ".":
ret = ret[0:-1]
if ret == "":
return "@"
else:
return ret
def BINDPath(domain):
global bind_path
return bind_path + domain
def BINDZoneExists(domain):
return os.path.exists(BINDPath(domain))
def WaitForZoneChanges(domain):
"""
Returns
=======
0: NOT CHANGED
1: CHANGED
2: DELETED
"""
global retry_sleep
global retry_times
fileName = BINDPath(domain)
last_mtime = os.stat(fileName).st_mtime
current_time = time.time()
elapsed = current_time - last_mtime
if elapsed <= 1:
# under a second
return 2
for i in range(retry_times):
time.sleep(retry_sleep)
if not os.path.exists(fileName):
return 3
cur_mtime = os.stat(fileName).st_mtime
if cur_mtime != last_mtime:
return 1
return 0
def WaitForZoneCreated(domain):
global retry_sleep
global retry_times
file = BINDPath(domain)
for i in range(retry_times):
time.sleep(retry_sleep)
if os.path.exists(file):
return True
return False
def GetBINDRecords(domain):
file = BINDPath(domain)
f = open(file, "r")
body = f.read()
f.close()
records = RR.fromZone(body)
ss = ""
for r in records:
host = HostConvert(r, domain)
ttl = r.ttl
cl = CLASS.get(r.rclass)
qt = QTYPE.get(r.rtype)
record = r.rdata.toZone()
line = '%-23s %-7s %-7s %-7s %s' % (host, ttl, cl, qt, record)
ss += line + "\n"
return ss
def GetBINDRecordsAndCheck(domain):
bind = GetBINDRecords(domain)
if bind == "":
bind = GetBINDRecords(domain)
return bind
def GetBINDRecordsUntil(domain):
for i in range(retry_times):
try:
rs = GetBINDRecords(domain)
if rs == "":
pass
else:
return rs
except:
pass
time.sleep(retry_sleep)
return None