-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpyflare.py
62 lines (54 loc) · 2.56 KB
/
pyflare.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
#! /usr/bin/python
import json, os
import requests
class Cloudflare:
def __init__(self, email, key="", token=""):
self.endpoint = "https://api.cloudflare.com/client/v4"
if len(token)!=0:
bearer = str("Bearer " + token)
self.headers = {'Authorization': bearer, 'Content-Type': 'application/json'}
elif len(key)!=0 :
self.headers = {'X-Auth-Email': email, 'X-Auth-Key': key, 'Content-Type': 'application/json'}
def getmyip(self):
r = requests.get("https://api.simonpainter.com/ip/")
return r.text
def user(self):
r = requests.get(self.endpoint + "/user", headers=self.headers)
return r.json()
def zones(self, zone):
payload = {'name': zone}
r = requests.get(self.endpoint + "/zones", headers=self.headers, params=payload)
return r.json()
def dns_records(self, zone_id, record):
payload = {'name': record}
r = requests.get(self.endpoint + "/zones/" + zone_id + "/dns_records", headers=self.headers, params=payload)
return r.json()
def update_record(self, zone_id, record_id, record, ttl, ip_address, proxied):
payload = {'type': 'A', 'name': record,'ttl': int(ttl), 'content': ip_address, 'proxied': bool(proxied)}
r = requests.put(self.endpoint + "/zones/" + zone_id + "/dns_records/" + record_id, headers=self.headers, data=json.dumps(payload))
return r.json()
def __call__(self,zone,record,ttl,proxied):
zone_id = cf.zones(zone)['result'][0]['id']
record_id = cf.dns_records(zone_id, record)['result'][0]['id']
ip_address = cf.getmyip()
if ip_address != cf.dns_records(zone_id, record)['result'][0]['content']:
return cf.update_record(zone_id, record_id, record, ttl, ip_address, proxied)
else:
return "Record is up-to-date"
if __name__ == '__main__':
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
try:
with open(os.path.join(__location__,'config.json')) as json_data_file:
config = json.load(json_data_file)
for item in config['items']:
email = item['email']
key = item['key']
token = item['token']
zone = item['zone']
record = item['record']
ttl = item['ttl']
proxied = item['proxied']
cf = Cloudflare(email, key=key,token=token)
print(cf(zone,record,ttl, proxied))
except IOError:
print("Unable to find config file.")