-
Notifications
You must be signed in to change notification settings - Fork 3
/
cfUpdater.py
242 lines (197 loc) · 8.18 KB
/
cfUpdater.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
import requests
import tkinter as tk
from tkinter import messagebox
import threading
import time
import configparser
import os
auto_update_flag = False
def get_public_ip():
try:
return requests.get("https://api.ipify.org").text
except requests.RequestException as e:
messagebox.showerror("Error", f"Failed to get public IP: {e}")
return None
# Save and Load config file
def save_config():
config = configparser.ConfigParser()
config['DEFAULT'] = {
'ApiKey': api_key_entry.get(),
'Email': email_entry.get(),
'ZoneId': zone_id_entry.get(),
'RecordNames': record_name_entry.get(),
'RecordType': record_type_entry.get(),
'Interval': interval_entry.get()
}
with open('config.ini', 'w') as configfile:
config.write(configfile)
messagebox.showinfo("Info", "Configuration saved successfully.")
def load_config():
config = configparser.ConfigParser()
config.read('config.ini')
if 'DEFAULT' in config:
api_key_entry.delete(0, tk.END)
api_key_entry.insert(0, config['DEFAULT'].get('ApiKey', ''))
email_entry.delete(0, tk.END)
email_entry.insert(0, config['DEFAULT'].get('Email', ''))
zone_id_entry.delete(0, tk.END)
zone_id_entry.insert(0, config['DEFAULT'].get('ZoneId', ''))
record_name_entry.delete(0, tk.END)
record_name_entry.insert(0, config['DEFAULT'].get('RecordNames', ''))
record_type_entry.delete(0, tk.END)
record_type_entry.insert(0, config['DEFAULT'].get('RecordType', ''))
interval_entry.delete(0, tk.END)
interval_entry.insert(0, config['DEFAULT'].get('Interval', ''))
def get_dns_record_id(api_key, email, zone_id, record_name, record_type):
headers = {
"X-Auth-Email": email,
"X-Auth-Key": api_key,
"Content-Type": "application/json"
}
try:
response = requests.get(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?type={record_type}&name={record_name}", headers=headers)
if response.status_code == 200:
records = response.json()["result"]
if records:
return records[0]["id"]
else:
messagebox.showinfo("Info", "No matching DNS record found")
return None
else:
messagebox.showerror("Error", f"Failed to fetch DNS records: {response.text}")
return None
except requests.RequestException as e:
messagebox.showerror("Error", f"API request failed: {e}")
return None
def check_dns_record(api_key, email, zone_id, record_name, record_type):
headers = {
"X-Auth-Email": email,
"X-Auth-Key": api_key,
"Content-Type": "application/json"
}
response = requests.get(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?type={record_type}&name={record_name}", headers=headers)
if response.status_code == 200:
records = response.json()["result"]
if records:
return records[0]['content'] # Return the IP address of the DNS record
return None
def update_dns_record():
api_key = api_key_entry.get()
email = email_entry.get()
zone_id = zone_id_entry.get()
record_names = record_name_entry.get().split(",") # Splitting by comma for multiple record names
record_type = record_type_entry.get()
content = ip_label.cget("text")
for record_name in record_names:
record_name = record_name.strip() # Removing leading/trailing whitespace
dns_record_ip = check_dns_record(api_key, email, zone_id, record_name, record_type)
if dns_record_ip == content:
result_text.insert(tk.END, f"Info: The IP address already matches the A record for {record_name}.\n")
continue
elif dns_record_ip is None:
result_text.insert(tk.END, f"Error: Could not retrieve the DNS record for {record_name}.\n")
continue
record_id = get_dns_record_id(api_key, email, zone_id, record_name, record_type)
if not record_id:
continue
headers = {
"X-Auth-Email": email,
"X-Auth-Key": api_key,
"Content-Type": "application/json"
}
data = {
"type": record_type,
"name": record_name,
"content": content,
"ttl": 1,
"proxied": False
}
try:
response = requests.put(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}", json=data, headers=headers)
if response.status_code == 200:
result_text.insert(tk.END, f"Success: DNS record for {record_name} updated successfully.\n")
else:
result_text.insert(tk.END, f"Error: Failed to update DNS record for {record_name}: {response.text}\n")
except requests.RequestException as e:
result_text.insert(tk.END, f"API request failed for {record_name}: {e}\n")
def auto_update():
global auto_update_flag
auto_update_flag = True
interval = float(interval_entry.get()) * 60 # Convert minutes to seconds
while auto_update_flag:
for remaining in range(int(interval), 0, -1):
mins, secs = divmod(remaining, 60)
countdown_label.config(text=f"Next check in: {mins:02d}:{secs:02d}")
root.update()
time.sleep(1)
if not auto_update_flag:
break
if auto_update_flag:
current_ip = get_public_ip()
ip_label.config(text=current_ip) # Update the IP label with the current IP
update_performed = False
for record_name in record_name_entry.get().split(","):
record_name = record_name.strip()
dns_record_ip = check_dns_record(api_key_entry.get(), email_entry.get(), zone_id_entry.get(), record_name, record_type_entry.get())
if current_ip != dns_record_ip:
update_dns_record()
update_performed = True
if not update_performed:
result_text.insert(tk.END, f"No update necessary at {time.strftime('%Y-%m-%d %H:%M:%S')}.\n")
countdown_label.config(text="Update check completed.")
def stop_auto_update():
global auto_update_flag
auto_update_flag = False
# GUI Setup
root = tk.Tk()
root.title("DNS Updater")
# Public IP Display
tk.Label(root, text="Your Public IP:").pack()
ip_label = tk.Label(root, text=get_public_ip())
ip_label.pack()
# Save and Load Buttons
save_config_button = tk.Button(root, text="Save Config", command=save_config)
save_config_button.pack()
load_config_button = tk.Button(root, text="Load Config", command=load_config)
load_config_button.pack()
# Input Fields with Labels
tk.Label(root, text="API Key:").pack()
api_key_entry = tk.Entry(root)
api_key_entry.pack()
tk.Label(root, text="Email:").pack()
email_entry = tk.Entry(root)
email_entry.pack()
tk.Label(root, text="Zone ID:").pack()
zone_id_entry = tk.Entry(root)
zone_id_entry.pack()
tk.Label(root, text="Record Name(s):").pack()
record_name_entry = tk.Entry(root)
record_name_entry.pack()
tk.Label(root, text="Record Type:").pack()
record_type_entry = tk.Entry(root)
record_type_entry.pack()
# Update Button
update_button = tk.Button(root, text="Update DNS Record", command=update_dns_record)
update_button.pack()
# Interval Input Field
tk.Label(root, text="Update Interval (minutes):").pack()
interval_entry = tk.Entry(root)
interval_entry.pack()
# Start and Stop Auto-Update Buttons
start_auto_update_button = tk.Button(root, text="Start Auto Update", command=lambda: threading.Thread(target=auto_update, daemon=True).start())
start_auto_update_button.pack()
stop_auto_update_button = tk.Button(root, text="Stop Auto Update", command=stop_auto_update)
stop_auto_update_button.pack()
# Result Text Widget
result_text = tk.Text(root, height=5, width=50)
result_text.pack()
# Countdown timer
countdown_label = tk.Label(root, text="Next check in: --:--")
countdown_label.pack()
def start_auto_update_thread():
threading.Thread(target=auto_update, daemon=True).start()
# Check for config.ini and load if exists
if os.path.exists('config.ini'):
load_config()
start_auto_update_thread()
root.mainloop()