-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
cloudflare.php
executable file
·442 lines (402 loc) · 14.5 KB
/
cloudflare.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/php -d open_basedir=/usr/syno/bin/ddns
<?php
/**
* Cloudflare DDNS agent for Synology DSM
* @link https://github.com/mrikirill/SynologyDDNSCloudflareMultidomain
* @version 2.0
* @license MIT
* @author https://github.com/mrikirill
*/
/**
* Synology passes 5 arguments in order:
* 0 - not in use
* 1 - username - uses for domains: domain1.com|vpn.domain2.com
* 2 - password - Cloudflare API token
* 3 - hostname - the script doesn't use it die to input limits
* 4 - IPv4 - Synology provided IPv4
*/
if ($argc !== 5) {
echo SynologyOutput::BAD_PARAMS;
exit();
}
$cf = new SynologyCloudflareDDNSAgent($argv[2], $argv[1], $argv[4]);
$cf->setDnsRecords();
$cf->updateDnsRecords();
class SynologyOutput
{
const SUCCESS = 'good'; // Update successfully
const NO_HOSTNAME = 'nohost'; // The hostname specified does not exist in this user account
const HOSTNAME_INCORRECT = 'notfqdn'; // The hostname specified is not a fully-qualified domain name
const AUTH_FAILED = 'badauth'; // Authenticate failed
const DDNS_FAILED = '911'; // There is a problem or scheduled maintenance on provider side
const BAD_HTTP_REQUEST = 'badagent'; // HTTP method/parameters is not permitted
const BAD_PARAMS = 'badparam'; // Bad params
}
/**
* Cloudflare api client
* @link https://developers.cloudflare.com/api/
*/
class CloudflareAPI
{
const API_URL = 'https://api.cloudflare.com';
const ZONES_PER_PAGE = 50;
private $apiKey;
public function __construct($apiKey)
{
$this->apiKey = $apiKey;
}
/**
* Makes an API call to the specified Cloudflare endpoint.
*
* @param string $method The HTTP method to use (GET, POST, PUT, PATCH).
* @param string $path The API endpoint path to call.
* @param array $data Optional data to send with the request.
* @return array The JSON-decoded response from the API call.
* @throws Exception If an error occurs during the API call.
*/
private function call($method, $path, $data = [])
{
$options = [
CURLOPT_URL => self::API_URL . '/' . $path,
CURLOPT_HTTPHEADER => ["Authorization: Bearer $this->apiKey", "Content-Type: application/json"],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_VERBOSE => false,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
];
switch ($method) {
case "GET":
$options[CURLOPT_HTTPGET] = true;
break;
case "POST":
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = json_encode($data);
break;
case "PUT":
$options[CURLOPT_CUSTOMREQUEST] = "PUT";
$options[CURLOPT_POSTFIELDS] = json_encode($data);
break;
case "PATCH":
$options[CURLOPT_CUSTOMREQUEST] = "PATCH";
$options[CURLOPT_POSTFIELDS] = json_encode($data);
break;
default:
throw new Exception("Invalid HTTP method: $method");
}
$req = curl_init();
curl_setopt_array($req, $options);
$res = curl_exec($req);
if (curl_errno($req)) {
throw new Exception('Curl error: ' . curl_error($req));
}
curl_close($req);
$json = json_decode($res, true);
if (!$json['success']) {
throw new Exception('API call failed');
}
return $json;
}
/**
* @link https://developers.cloudflare.com/api/operations/user-api-tokens-verify-token
* @throws Exception
*/
public function verifyToken()
{
return $this->call("GET", "client/v4/user/tokens/verify");
}
/**
* Note: getting max 50 zones see the documentation
* @link https://developers.cloudflare.com/api/operations/zones-get
* @throws Exception
*/
public function getZones()
{
return $this->call("GET", "client/v4/zones?per_page=" . self::ZONES_PER_PAGE . "&status=active");
}
/**
* @link https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records
* @throws Exception
*/
public function getDnsRecords($zoneId, $type, $name)
{
return $this->call("GET", "client/v4/zones/$zoneId/dns_records?type=$type&name=$name");
}
/**
* @link https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-patch-dns-record
* @throws Exception
*/
public function updateDnsRecord($zoneId, $recordId, $body)
{
return $this->call("PATCH", "client/v4/zones/$zoneId/dns_records/$recordId", $body);
}
}
class Ipify
{
const API_URL = 'https://api6.ipify.org';
/**
* Return if external IPv6 address is available
* @link https://www.ipify.org
* @throws Exception
*/
public function tryGetIpv6()
{
$options = [
CURLOPT_URL => self::API_URL . "/?format=json",
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_VERBOSE => false,
CURLOPT_HTTPGET => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
];
$req = curl_init();
curl_setopt_array($req, $options);
$res = curl_exec($req);
if (curl_errno($req)) {
throw new Exception('Curl error: ' . curl_error($req));
}
curl_close($req);
$json = json_decode($res, true);
if (!$json['ip']) {
throw new Exception('API call failed: ' . json_encode($json));
}
return $json['ip'];
}
}
class DnsRecordEntity
{
public $id;
public $type;
public $hostname;
public $ip;
public $zoneId;
public $ttl;
public $proxied;
public function __construct($id, $type, $hostname, $ip, $zoneId, $ttl, $proxied)
{
$this->id = $id;
$this->type = $type;
$this->hostname = $hostname;
$this->ip = $ip;
$this->zoneId = $zoneId;
$this->ttl = $ttl;
$this->proxied = $proxied;
}
public function toArray()
{
return [
'id' => $this->id,
'type' => $this->type,
'name' => $this->hostname,
'content' => $this->ip,
'zoneId' => $this->zoneId,
'ttl' => $this->ttl,
'proxied' => $this->proxied
];
}
}
/**
* DDNS auto update agent for Synology DSM
* Supports multidomains and subdomains
*/
class SynologyCloudflareDDNSAgent
{
private $ipv4, $ipv6, $dnsRecordList = [];
private $cloudflareAPI;
private $ipify;
function __construct($apiKey, $hostname, $ipv4)
{
$this->cloudflareAPI = new CloudflareAPI($apiKey);
$this->ipify = new Ipify();
$this->ipv4 = $ipv4;
try {
$this->ipv6 = $this->ipify->tryGetIpv6();
} catch (Exception $e) {
// IPv6 not available
}
try {
if (!$this->isCFTokenValid()) {
$this->exitWithSynologyMsg(SynologyOutput::AUTH_FAILED);
}
} catch (Exception $e) {
$this->exitWithSynologyMsg();
}
$hostnameList = $this->extractHostnames($hostname);
if (empty($hostnameList)) {
$this->exitWithSynologyMsg(SynologyOutput::HOSTNAME_INCORRECT);
}
$this->matchHostnameWithZone($hostnameList);
}
/**
* Sets DNS A Records for each host in the DNS record list.
*
* Iterates over the dnsRecordList, retrieves existing DNS records
* from the Cloudflare API, and updates the records' IDs, TTL, and proxied status.
*
* If the dnsRecordList is empty, it exits with a NO_HOSTNAME message.
* If an API call fails, it exits with a DDNS_FAILED message.
*/
public function setDnsRecords()
{
if (empty($this->dnsRecordList)) {
$this->exitWithSynologyMsg(SynologyOutput::NO_HOSTNAME);
}
try {
foreach ($this->dnsRecordList as $key => $dnsRecord) {
$json = $this->cloudflareAPI->getDnsRecords($dnsRecord->zoneId, $dnsRecord->type, $dnsRecord->hostname);
if (isset($json['result']['0'])) {
// If the DNS record exists, update its ID, TTL, and proxied status
$this->dnsRecordList[$key]->id = $json['result']['0']['id'];
$this->dnsRecordList[$key]->ttl = $json['result']['0']['ttl'];
$this->dnsRecordList[$key]->proxied = $json['result']['0']['proxied'];
} else {
// If the DNS record does not exist, remove it from the list
unset($this->dnsRecordList[$key]);
}
}
} catch (Exception $e) {
$this->exitWithSynologyMsg(SynologyOutput::DDNS_FAILED);
}
}
/**
* Updates Cloudflare DNS records
*
* Verifies each DNS record in the list, attempts to update it via the Cloudflare API,
* and outputs 'SUCCESS' if all updates are completed without errors.
* If the DNS record list is empty, it exits with a 'NO_HOSTNAME' message.
* If an API call fails, it exits with a 'BAD_HTTP_REQUEST' message.
*/
function updateDnsRecords()
{
if (empty($this->dnsRecordList)) {
$this->exitWithSynologyMsg(SynologyOutput::NO_HOSTNAME);
}
foreach ($this->dnsRecordList as $dnsRecord) {
try {
$this->cloudflareAPI->updateDnsRecord($dnsRecord->zoneId, $dnsRecord->id, $dnsRecord->toArray());
} catch (Exception $e) {
$this->exitWithSynologyMsg(SynologyOutput::BAD_HTTP_REQUEST);
}
}
echo SynologyOutput::SUCCESS;
}
/**
* Matches hostnames with their corresponding Cloudflare zone.
*
* This method fetches the list of zones from the Cloudflare API,
* iterates over each hostname provided, and stores corresponding DNS records
* in the dnsRecordList property if a match is found.
*
* @param array $hostnameList List of hostnames to be matched with zones.
* @throws Exception If an error occurs during the API call,
* it outputs an appropriate Synology message and exits the script.
*/
private function matchHostnameWithZone($hostnameList = [])
{
try {
$zoneList = $this->cloudflareAPI->getZones();
$zoneList = $zoneList['result'];
foreach ($zoneList as $zone) {
$zoneId = $zone['id'];
$zoneName = $zone['name'];
foreach ($hostnameList as $hostname) {
if (strpos($hostname, $zoneName) !== false) {
// Add an IPv4 DNS record for each hostname that matches a zone
$this->dnsRecordList[] = new DnsRecordEntity(
'',
'A',
$hostname,
$this->ipv4,
$zoneId,
'',
''
);
if (isset($this->ipv6)) {
// Add an IPv6 DNS record if an IPv6 address is available
$this->dnsRecordList[] = new DnsRecordEntity(
'',
'AAAA',
$hostname,
$this->ipv6,
$zoneId,
'',
''
);
}
}
}
}
} catch (Exception $e) {
$this->exitWithSynologyMsg(SynologyOutput::NO_HOSTNAME);
}
}
/**
* Extracts valid hostnames from a given string of hostnames separated by pipes (|).
*
* @param string $hostnames A string of hostnames separated by pipes (|).
* @return array An array of validated and parsed hostnames.
*/
private function extractHostnames($hostnames)
{
$arHost = preg_split('/\|/', $hostnames, -1, PREG_SPLIT_NO_EMPTY);
$hostList = [];
foreach ($arHost as $value) {
if ($this->isValidHostname($value)) {
$hostList[] = $value;
}
}
return $hostList;
}
/**
* Validates whether a given value is a fully-qualified domain name (FQDN).
*
* Uses a regular expression pattern to check for valid FQDN structure.
* An FQDN must consist of at least one label, each label must be alphanumeric or hyphenated,
* but cannot begin or end with a hyphen, followed by a top-level domain (TLD) that is 2-6 characters long.
*
* @param string $value The input string to be validated as an FQDN.
* @return bool Returns true if the input string is a valid FQDN, false otherwise.
*/
private function isValidHostname($value)
{
$domainPattern = "/^(?!-)(?:\*\.)?(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/";
return preg_match($domainPattern, $value);
}
/**
* Checks CF API Token is valid
*
* This function verifies if the Cloudflare API token is valid by calling the verifyToken
* method of the CloudflareAPI class. If the token is valid, it returns true.
* If an exception occurs during the verification process, the function catches the exception
* and returns false, indicating that the token is not valid or an error has occurred.
*
* @return bool Returns true if the Cloudflare API token is valid, otherwise false.
*/
private function isCFTokenValid()
{
try {
$res = $this->cloudflareAPI->verifyToken();
return $res['success'];
} catch (Exception $e) {
return false;
}
}
/**
* Outputs a message and exits the script.
*
* This function is used to print a specified message and then terminate
* the execution of the script. It is primarily used for handling
* and responding to various error conditions during the DNS update process.
*
* @param string $msg The message to be output before exiting.
* If no message is specified, an empty string is printed.
*/
private function exitWithSynologyMsg($msg = '')
{
echo $msg;
exit();
}
}
?>