Skip to content

Commit b574500

Browse files
authored
Update cloudflare.php
isCFTokenValid method clean up
1 parent c59b843 commit b574500

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

cloudflare.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Output
2222
const DDNS_PROVIDER_DOWN = '911'; // [Server is broken][De DDNS-server is tijdelijk buiten dienst. Neem contact op met de Internet-provider.]
2323
const BAD_HTTP_REQUEST = 'badagent'; // [DDNS function needs to be modified, please contact synology support]
2424
const HOSTNAME_FORMAT_INCORRECT = 'badparam'; // [The format of hostname is not correct]
25-
25+
const BAD_PARAMS = 'badparam';
2626
// Not logged messages, didn't trigger/work while testing on DSM
2727
const PROVIDER_ADDRESS_NOT_RESOLVED = 'badresolv';
2828
const PROVIDER_TIMEOUT_CONNECTION = 'badconn';
@@ -53,12 +53,14 @@ function __construct($argv)
5353
if($this->ipv6)
5454
$this->validateIp((string) $this->ipv6); // Validates IPV6
5555

56-
// Test address to force-enable IPV6 manually to simulate ipv6 "found":
57-
//$this->ipv6 = "2222:7e01::f03c:91ff:fe99:b41d";
58-
5956
// Since DSM is only providing an IP(v4) address (DSM 6/7 doesn't deliver IPV6)
6057
// I override above IPV4 detection & rely on DSM instead for now
6158
$this->validateIp((string) $argv[4]);
59+
60+
// Before runs DNS update checks API token is valid or not
61+
if(!$this->isCFTokenValid()) {
62+
$this->badParam();
63+
}
6264

6365
// safer than explode: in case of wrong formatting with --- separations (empty elements removed automatically)
6466
$arHost = preg_split('/(---)/', $hostnames, -1, PREG_SPLIT_NO_EMPTY);
@@ -86,6 +88,19 @@ function __construct($argv)
8688
}
8789
}
8890
}
91+
92+
/**
93+
* Checks CF API Token is valid
94+
*
95+
* @return bool
96+
*/
97+
function isCFTokenValid() {
98+
$res = $this->callCFapi("GET", "client/v4/user/tokens/verify");
99+
if ($res['success']) {
100+
return true;
101+
}
102+
return false;
103+
}
89104

90105
/**
91106
* Update CF DNS records
@@ -103,7 +118,7 @@ function makeUpdateDNS()
103118
$json = $this->callCFapi("PATCH", "client/v4/zones/${zoneId}/dns_records/${recordId}", $dnsRecord);
104119

105120
if (!$json['success']) {
106-
echo 'Update Record failed';
121+
echo Output::BAD_HTTP_REQUEST
107122
exit();
108123
}
109124
}
@@ -113,7 +128,7 @@ function makeUpdateDNS()
113128

114129
function badParam($msg = '')
115130
{
116-
echo (strlen($msg) > 0) ? $msg : 'badparam';
131+
echo (strlen($msg) > 0) ? $msg : echo Output::BAD_PARAMS;
117132
exit();
118133
}
119134

@@ -285,4 +300,4 @@ function callCFapi($method, $path, $data = []) {
285300
return json_decode($res, true);
286301
}
287302
}
288-
?>
303+
?>

0 commit comments

Comments
 (0)