-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.py
190 lines (149 loc) · 3.93 KB
/
update.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
#!/bin/python3
# michaelpeterswa
# Route53 Updater, v1.0
# 11.14.20
import toml
import requests
import boto3
from tabulate import tabulate
from colorama import Fore, Style
from pyfiglet import Figlet
from datetime import datetime
AWS_IP_SERVICE = "http://checkip.amazonaws.com"
CONFIG = "domains.toml"
COLORS = True
LIVE = True
FANCY = True
VERBOSE = True
names = []
ids = []
records = []
matches = []
def title(time):
if FANCY:
f = Figlet(font="slant")
print(f.renderText("R53 U"), end="")
print(time)
else:
print("\nRoute53 Updater, by michaelpeterswa")
print(time)
def green(text):
if COLORS:
print(Fore.GREEN + text)
else:
print(text)
def yellow(text):
if COLORS:
print(Fore.YELLOW + text)
else:
print(text)
def red(text):
if COLORS:
print(Fore.RED + text)
else:
print(text)
def reset():
if COLORS:
print(Style.RESET_ALL)
else:
print()
def update_record(zone_id, name, ip):
subd = "*." + name
batch = {
"Comment": "Changed by update.py",
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": name,
"Type": "A",
"TTL": 300,
"ResourceRecords": [{"Value": ip}],
},
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": subd,
"Type": "A",
"TTL": 300,
"ResourceRecords": [{"Value": ip}],
},
},
],
}
response = client.change_resource_record_sets(
HostedZoneId=zone_id, ChangeBatch=batch
)
return response
def groupHostedZones():
for z in resp["HostedZones"]:
names.append(z["Name"][:-1]) # trim trailing .
ids.append(z["Id"])
records.append((z["Name"][:-1], z["Id"]))
def printTitle(time):
if COLORS:
print(Fore.YELLOW)
title(time)
def printRoute53Records():
yellow("")
print("Route53 Records:\n")
print(tabulate({"Name": names, "ID": ids}, headers="keys"))
reset()
def generateRoute53Matches():
for link, ID in records:
if link in data["domains"]:
matches.append((link, ID))
def printRoute53Matches():
for link, ID in records:
if link in data["domains"]:
green("✔ found domain match: " + link)
else:
red("✗ " + link + " was found on Route53 but not in domains")
reset()
def updateMatchesOnRoute53(ip):
for match in matches:
r53response = update_record(match[1], match[0], ip)
return r53response
def printUpdatesOnRoute53(r53response):
for match in matches:
if r53response["ChangeInfo"]["Status"] == "PENDING":
yellow("PENDING (" + ip + "): " + match[0])
else:
pass
reset()
def saveTOML(data, ip, time):
data["ip"] = ip
data["time"] = time
with open(CONFIG, "w") as f:
toml.dump(data, f)
def checkPrevIP(ip, prev_ip):
if ip == prev_ip:
return True
else:
return False
if __name__ == "__main__":
data = toml.load(CONFIG)
client = boto3.client("route53")
resp = client.list_hosted_zones()
ip = requests.get(AWS_IP_SERVICE).text.strip()
curr_time = datetime.now().isoformat()
prev_ip = data["ip"]
printTitle(curr_time)
if not checkPrevIP(ip, prev_ip):
groupHostedZones()
generateRoute53Matches()
if VERBOSE:
printRoute53Records()
printRoute53Matches()
else:
print()
saveTOML(data, ip, curr_time)
if LIVE:
printUpdatesOnRoute53(updateMatchesOnRoute53(ip), ip)
else:
yellow("testing mode")
reset()
else:
yellow("\nprevious IP match, SKIPPING")
reset()