Skip to content

Commit f47ce8f

Browse files
authored
Merge pull request #9 from rfuehrer/feature/api-token
Use of individual API keys/tokens
2 parents 640b87e + 2bbd109 commit f47ce8f

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@ Before starting the installation process, make sure you have (and know) the foll
2727

2828
a. Know your Cloudflare account username (or [register for an account if you're new to Cloudflare](https://dash.cloudflare.com/sign-up)); and
2929
30-
b. Have your [Global API key](https://dash.cloudflare.com/profile/api-tokens) (More info: [Global API keys](https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys)).
30+
b. Have your [API key](https://dash.cloudflare.com/profile/api-tokens) - no need to use your Global API key! (More info: [API keys](https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys)).
31+
32+
![image](example4.png)
33+
34+
c. Create a API key with following (3) permissions:
3135
36+
**Zone** > **Zone.Settings** > **Read**
37+
**Zone** > **Zone** > **Read**
38+
**Zone** > **DNS** > **Edit**
39+
40+
The affected zone ressouces have to be (at least):
41+
42+
**Include** > **All zones from an account** > `<domain>`
43+
3244
2. *DNS settings:*
3345

3446
Ensure the DNS A record(s) for the domain/zone(s) you wish to update with this script have been created (More information: [Managing DNS records](https://support.cloudflare.com/hc/en-us/articles/360019093151-Managing-DNS-records-in-Cloudflare)).
@@ -91,8 +103,8 @@ Before starting the installation process, make sure you have (and know) the foll
91103
For a single domain: __mydomain.com__
92104
For multiple domains: __subdomain.mydomain.com---vpn.mydomain.com__
93105
(ensure each domain is seperated by three dashes: ---)
94-
* Username: The email address you use for logging in to Cloudflare.
95-
* Password: Your Cloudflare Global API Key
106+
* Username: The email address you use for logging in to Cloudflare (optional since the API key is sufficient)
107+
* Password: Your created Cloudflare API Key
96108

97109
![image](example3.png)
98110

@@ -136,4 +148,5 @@ Source [Identifying network ports compatible with Cloudflare's proxy](https://su
136148

137149
## Credits
138150

139-
<small><i><a href='http://ecotrust-canada.github.io/markdown-toc/'>Table of contents generated with markdown-toc</a></i></small>
151+
<small><i><a href='http://ecotrust-canada.github.io/markdown-toc/'>Table of contents generated with markdown-toc</a></i></small>
152+
<small><i><a href='https://www.youtube.com/watch?v=Nf7m3h11y-s'>DB Tech - creating API keys and using Cloudflare CNAME for single updates</a></i></small>

cloudflare.php

100644100755
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* DDNS auto updater for Synology NAS
1414
* Base on Cloudflare API v4
15-
* Supports multidomains and sundomains
15+
* Supports multidomains and sundomains
1616
*/
1717
class updateCFDDNS
1818
{
@@ -22,10 +22,9 @@ class updateCFDDNS
2222
function __construct($argv)
2323
{
2424
if (count($argv) != 5) {
25-
$this->badParam();
25+
$this->badParam('wrong parameter count');
2626
}
2727

28-
$this->account = (string) $argv[1];
2928
$this->apiKey = (string) $argv[2]; // CF Global API Key
3029
$hostname = (string) $argv[3]; // example: example.com.uk---sundomain.example1.com---example2.com
3130
$this->ip = (string) $argv[4];
@@ -37,7 +36,7 @@ function __construct($argv)
3736
$this->badParam('empty host list');
3837
}
3938

40-
foreach ($arHost as $value) {
39+
foreach ($arHost as $value) {
4140
$this->hostList[$value] = [
4241
'hostname' => '',
4342
'fullname' => $value,
@@ -52,14 +51,14 @@ function __construct($argv)
5251
$this->setRecord($arHost['fullname'], $arHost['zoneId']);
5352
}
5453
}
55-
54+
5655
/**
5756
* Update CF DNS records
5857
*/
5958
function makeUpdateDNS()
6059
{
6160
if(empty($this->hostList)) {
62-
$this->badParam();
61+
$this->badParam('empty host list');
6362
}
6463

6564
foreach ($this->hostList as $arHost) {
@@ -93,7 +92,7 @@ function validateIpV4($ip)
9392
}
9493
return true;
9594
}
96-
95+
9796
/**
9897
* Set ZoneID for each hosts
9998
*/
@@ -163,7 +162,7 @@ function setRecord($fullname, $zoneId)
163162
function callCFapi($method, $path, $data = []) {
164163
$options = [
165164
CURLOPT_URL => self::API_URL . '/' . $path,
166-
CURLOPT_HTTPHEADER => ["X-Auth-Email: $this->account", "X-Auth-Key: $this->apiKey", "Content-Type: application/json"],
165+
CURLOPT_HTTPHEADER => ["Authorization: Bearer $this->apiKey", "Content-Type: application/json"],
167166
CURLOPT_RETURNTRANSFER => true,
168167
CURLOPT_HEADER => false,
169168
CURLOPT_VERBOSE => false,
@@ -176,7 +175,7 @@ function callCFapi($method, $path, $data = []) {
176175
switch($method) {
177176
case "GET":
178177
$options[CURLOPT_HTTPGET] = true;
179-
break;
178+
break;
180179

181180
case "POST":
182181
$options[CURLOPT_POST] = true;

example4.png

63.2 KB
Loading

0 commit comments

Comments
 (0)