Add support for new API tokens.

This commit is contained in:
Han Zhang 2020-07-27 15:27:55 -04:00
parent 66bbbb5295
commit 022da2958a
5 changed files with 59 additions and 25 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
config.json
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -8,13 +8,35 @@ This script was written for the Raspberry Pi platform to enable low cost, simple
## :vertical_traffic_light: Getting Started ## :vertical_traffic_light: Getting Started
Edit config.json and replace the values with your own. First copy the example configuration file into the real one.
Values explained: ``bash
cp config-example.json config.json
```
Edit `config.json` and replace the values with your own.
- Authentication methods:
You can choose to use either the newer API tokens, or the traditional API keys
To generate a new API tokens, go to https://dash.cloudflare.com/profile/api-tokens and create a token capable of **Edit DNS**. Then replace the value in
```json ```json
"authentication":
"api_token": "Your cloudflare API token, including the capability of **Edit DNS**"
```
Alternatively, you can use the traditional API keys by setting appropriate values for:
```json
"authentication":
"api_key":
"api_key": "Your cloudflare API Key", "api_key": "Your cloudflare API Key",
"account_email": "The email address you use to sign in to cloudflare", "account_email": "The email address you use to sign in to cloudflare",
```
- Other values explained:
```json
"zone_id": "The ID of the zone that will get the records. From your dashboard click into the zone. Under the overview tab, scroll down and the zone ID is listed in the right rail", "zone_id": "The ID of the zone that will get the records. From your dashboard click into the zone. Under the overview tab, scroll down and the zone ID is listed in the right rail",
"subdomains": "Array of subdomains you want to update the A & where applicable, AAAA records. IMPORTANT! Only write subdomain name. Do not include the base domain name. (e.g. foo or an empty string to update the base domain)", "subdomains": "Array of subdomains you want to update the A & where applicable, AAAA records. IMPORTANT! Only write subdomain name. Do not include the base domain name. (e.g. foo or an empty string to update the base domain)",
"proxied": false (defaults to false. Make it true if you want CDN/SSL benefits from cloudflare. This usually disables SSH) "proxied": false (defaults to false. Make it true if you want CDN/SSL benefits from cloudflare. This usually disables SSH)

View File

@ -83,11 +83,17 @@ def commitRecord(ip):
def cf_api(endpoint, method, config, headers={}, data=False): def cf_api(endpoint, method, config, headers={}, data=False):
api_token = config['authentication']['api_token']
if api_token != '' and api_token != 'api_token_here':
headers = { headers = {
"X-Auth-Email": config['account_email'], "Authorization": "Bearer " + config['authentication']['api_token'],
"X-Auth-Key": config['api_key'],
**headers **headers
} }
else:
headers = {
"X-Auth-Email": config['authentication']['api_key']['account_email'],
"X-Auth-Key": config['authentication']['api_key']['api_key'],
}
if(data == False): if(data == False):
response = requests.request( response = requests.request(
@ -98,7 +104,6 @@ def cf_api(endpoint, method, config, headers={}, data=False):
return response.json() return response.json()
for ip in getIPs(): for ip in getIPs():
print("Checking " + ip["type"] + " records") print("Checking " + ip["type"] + " records")
commitRecord(ip) commitRecord(ip)

19
config-example.json Executable file
View File

@ -0,0 +1,19 @@
{
"cloudflare": [
{
"authentication": {
"api_token": "api_token_here",
"api_key": {
"api_key": "api_key_here",
"account_email": "your_email_here"
}
},
"zone_id": "your_zone_id_here",
"subdomains": [
"",
"subdomain"
],
"proxied": false
}
]
}

View File

@ -1,14 +0,0 @@
{
"cloudflare": [
{
"api_key": "api_key_here",
"account_email": "your_email_here",
"zone_id": "your_zone_id_here",
"subdomains": [
"",
"subdomain"
],
"proxied": false
}
]
}