-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONConfig.py
186 lines (156 loc) · 7.15 KB
/
JSONConfig.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
import json, shutil, os
from models.CloudflareDNSAccount import CloudflareDNSAccount
from models.CloudflareAuthenticationDetails import CloudflareAuthenticationDetails
from models.CloudflareDNSDetails import CloudflareDNSDetails
"""
Class for reading and loading the JSON config
"""
class JSONConfig:
__configJSON = {}
__jsonConfigPath = None
"""
Constructor for the JSON Config
jsonConfigPath: string - The path for the json config file
"""
def __init__(self, jsonConfigPath):
self.__jsonConfigPath = jsonConfigPath
self.__readConfigFile(jsonConfigPath)
"""
Gets a list of cloudflare accounts from the config file
Returns: An array of CloudFlareDNSAccounts if successful, an empty array otherwise
"""
def getCloudflareAccounts(self):
cloudflareAccounts = self.__getPropertyFromConfig(self.__configJSON, "cloudflareAccounts")
if (cloudflareAccounts == None):
return []
actualAccounts = []
# Add each account to the list
for account in cloudflareAccounts:
auth = self.__getAccountAuth(account)
dnsDetails = self.__getAccountDNSDetails(account)
zoneId = self.__getAccountZoneId(account)
# If any of the fields are None, ignore the account
if (auth == None or dnsDetails == None or zoneId == None):
continue
actualAccounts.append(CloudflareDNSAccount(authentication=auth, zoneId=zoneId, dnsDetails=dnsDetails))
return actualAccounts
"""
Set the id of a specific detail and save it to the file
zoneID: string - The zone id of the dns details to set
dnsDetail: CloudflareDNSDetail - The dns detail to set the id of
returns: bool - True if the update was successful, false otherwise
"""
def setIdFromDetails(self, zoneId, dnsDetail):
cloudflareAccounts = self.__getPropertyFromConfig(self.__configJSON, "cloudflareAccounts")
if (cloudflareAccounts == None):
return False
# Update the detail
for account in cloudflareAccounts:
for detail in self.__getPropertyFromConfig(account, "dnsDetails"):
if (dnsDetail == detail):
detail["id"] = dnsDetail.id
return self.saveAccounts(cloudflareAccounts)
"""
Saves the accounts in the config file
accounts: CloudflareDNSAccounts[] - The list of the accounts to be saved
Returns: bool - True if successful, false otherwise
"""
def saveAccounts(self, accounts):
self.__configJSON["cloudflareAccounts"] = accounts
try:
# Save to tmp file
f = open(self.__jsonConfigPath + "tmp", "w")
f.write(json.dumps(self.__configJSON, indent=4, sort_keys=True))
f.close()
# Overwrite json config
os.remove(self.__jsonConfigPath)
shutil.copyfile(self.__jsonConfigPath + "tmp", self.__jsonConfigPath)
os.remove(self.__jsonConfigPath + "tmp")
return True
except:
print("Could not update config file")
return False
"""
Get the number of minutes between checks for the DDNS checker
Returns: int - The number of minutes that should elapse between a DDNS check
"""
def getMinutesBetweenChecks(self):
return self.__getPropertyFromConfig(self.__configJSON, "minutesBetweenChecks")
"""
Get the authentication for a specific cloudflare account
account: jsonObject - The account to get the DNS details for
returns: CloudflareAuthenticationDetails - The authentication details for the cloudflare account
"""
def __getAccountAuth(self, account):
authenticationDetails = self.__getPropertyFromConfig(account, "authentication")
token = self.__getPropertyFromConfig(authenticationDetails, "token", noError=True)
key = self.__getPropertyFromConfig(authenticationDetails, "key", noError=True)
email = self.__getPropertyFromConfig(authenticationDetails, "email", noError=True)
# Check that either the token, or the key and email have been included, otherwise return None
if (not (token != None or (key != None and email != None))):
print("Account authentication details invalid, either enter a token or a key and email")
return None
return CloudflareAuthenticationDetails(token=token, key=key, email=email)
"""
Get the DNS details for a specific cloudflare account
account: jsonObject - The account to get the DNS details for
returns: CloudflareDNSDetails[] - A list of cloudflare dns details
"""
def __getAccountDNSDetails(self, account):
dnsDetails = self.__getPropertyFromConfig(account, "dnsDetails")
if (dnsDetails == None):
return None
details = []
for detail in dnsDetails:
id = self.__getPropertyFromConfig(detail, "id", noError=True)
dnsType = self.__getPropertyFromConfig(detail, "type")
name = self.__getPropertyFromConfig(detail, "name")
ttl = self.__getPropertyFromConfig(detail, "ttl", noError=True)
proxied = self.__getPropertyFromConfig(detail, "proxied", noError=True)
details.append(CloudflareDNSDetails(id=id, dnsType=dnsType, name=name, ttl=ttl, proxied=proxied))
return details
"""
Get the zoneID for a specific cloudflare account
account: jsonObject - The account to get the DNS details for
returns: string - The cloudflare zoneId
"""
def __getAccountZoneId(self, account):
return self.__getPropertyFromConfig(account, "zoneId")
"""
Get a specific property from the config file
json: jsonObject - The json object to get the value from the key
key: string - The key of the property to get
noError: bool (False) - Whether or not to show error messages
Returns: The value of the key if successful, None otherwise
"""
def __getPropertyFromConfig(self, json, key, noError=False):
try:
val = json[key]
if (val == ""):
if (not noError):
print("The key '" + str(key) + "' has not been assigned a value")
val = None
return val
except KeyError:
if (not noError):
print("The key '" + str(key) + "' could not be found in the config")
return None
"""
Read the contents of the config file
Returns true if the config was successfully read, and false otherwise
"""
def __readConfigFile(self, jsonConfigPath):
try:
# Read the file
f = open(jsonConfigPath)
self.__configJSON = json.load(f)
f.close()
# Handle file not openable error
except OSError:
print("The config file could not be opened. Does the file at '" + str(configFilePath) + "' exist?")
return False
# Handle any other errors
except:
print("An unknown error occurred")
return False
return True