-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (44 loc) · 1.38 KB
/
main.py
File metadata and controls
56 lines (44 loc) · 1.38 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
import requests
URL_UPTIME_ROBOT_IPS = "https://uptimerobot.com/inc/files/ips/IPv4andIPv6.txt"
URL_CLOUDFLARE_ADD_IP = "https://api.cloudflare.com/client/" \
"v4/user/firewall/access_rules/rules"
class APIError(Exception):
pass
def load_ips():
r = requests.get(URL_UPTIME_ROBOT_IPS)
ips = []
for ip in r.text.split("\n"):
if ip.strip():
ips.append(ip.strip())
return ips
def add_ip_to_cloudflare(ip, email, secret):
headers = {
"Content-Type": "application/json",
"X-Auth-Email": email,
"X-Auth-Key": secret,
}
data = {
"mode": "whitelist",
"configuration": {
"target": "ip",
"value": ip,
},
"notes": "uptime_robot_ip",
}
r = requests.post(URL_CLOUDFLARE_ADD_IP, json=data, headers=headers)
if not (r.status_code == 200 and r.json().get("success", False)):
msg = "API returned error: {}"
raise APIError(msg.format(r.json().get("errors", [])))
def ask_for_auth():
input_function = None
try:
input_function = raw_input
except NameError:
input_function = input
email = input_function("Email: ").strip()
secret = input_function("API-Secret: ").strip()
return email, secret
if __name__ == "__main__":
auth = ask_for_auth()
for ip in load_ips():
add_ip_to_cloudflare(ip, *auth)