Last active
July 25, 2019 10:45
-
-
Save seb3point0/d5bd437d7d198b10e3139cb49176ed52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php -d open_basedir=/usr/syno/bin/ddns | |
| <?php | |
| $account = (string)$argv[1]; | |
| $pwd = (string)$argv[2]; | |
| $hostnames = (string)$argv[3]; | |
| $ip = (string)$argv[4]; | |
| // settings | |
| $proxied = true; // set to false to disable cloudflare proxy | |
| $ttl = 1; | |
| // debug | |
| $debug = true; // set to true to enable debug output | |
| /* debug */ | |
| if ($debug) { echo "account: $account" . PHP_EOL; } | |
| if ($debug) { echo "pwd: $pwd" . PHP_EOL; } | |
| if ($debug) { echo "hostname: $hostname" . PHP_EOL; } | |
| if ($debug) { echo "ip: $ip" . PHP_EOL; } | |
| /* /debug */ | |
| // define data and headers | |
| $headers = [ | |
| 'X-Auth-Email: ' . $account, | |
| 'X-Auth-Key: ' . $pwd, | |
| 'Content-Type: application/json' | |
| ]; | |
| /* debug */ | |
| if ($debug) { echo "headers: " ; print_r($headers) . PHP_EOL; } | |
| /* /debug */ | |
| // only for IPv4 format | |
| if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | |
| echo "badparam"; | |
| exit(); | |
| } | |
| // split hostnames | |
| $hostnames = explode(',', $hostnames); | |
| // run update script for each name | |
| foreach ($hostnames as $hostname) { | |
| $hostname = trim($hostname); | |
| /* debug */ | |
| if ($debug) { echo "hostname: $hostname" . PHP_EOL; } | |
| /* /debug */ | |
| // check the hostname contains '.' | |
| if (strpos($hostname, '.') === false) { | |
| echo "badparam"; | |
| exit(); | |
| } | |
| // extract domain | |
| $hostnameParts = explode('.', $hostname); | |
| $arrayCount = count($hostnameParts); | |
| if ($arrayCount > 2) { | |
| $domain = implode('.', array_slice($hostnameParts, $arrayCount-2, 2)); | |
| } else { | |
| $domain = implode('.', $hostnameParts); | |
| } | |
| /* debug */ | |
| if ($debug) { echo "domain: $domain" . PHP_EOL; } | |
| /* /debug */ | |
| $data = [ | |
| 'type' => 'A', | |
| 'name' => $hostname, | |
| 'content' => $ip, | |
| 'ttl' => $ttl, | |
| 'proxied' => $proxied | |
| ]; | |
| /* debug */ | |
| if ($debug) { echo "data: " ; print_r($data) . PHP_EOL; } | |
| /* /debug */ | |
| // get zone id | |
| $ch = curl_init(); | |
| $zoneUrl = "https://api.cloudflare.com/client/v4/zones?name=$domain"; | |
| curl_setopt($ch, CURLOPT_URL, $zoneUrl); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $result = curl_exec($ch); | |
| if (curl_errno($ch)) { | |
| echo "no"; | |
| exit('Error: ' . curl_error($ch)); | |
| } | |
| curl_close ($ch); | |
| $json = json_decode($result, true); | |
| $ZoneID = $json['result']['0']['id']; | |
| /* debug */ | |
| if ($debug) { echo "zone url: $zoneUrl" . PHP_EOL; } | |
| if ($debug) { echo "zone id: $ZoneID" . PHP_EOL; } | |
| if ($debug) { echo "zone data: " ; print_r($json) . PHP_EOL; } | |
| /* /debug */ | |
| // get record id | |
| $ch = curl_init(); | |
| $recordUrl = "https://api.cloudflare.com/client/v4/zones/$ZoneID/dns_records?name=$hostname"; | |
| curl_setopt($ch, CURLOPT_URL, $recordUrl); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $result = curl_exec($ch); | |
| if (curl_errno($ch)) { | |
| echo "no"; | |
| exit('Error: ' . curl_error($ch)); | |
| } | |
| curl_close ($ch); | |
| $json = json_decode($result, true); | |
| $DNSID = $json['result']['0']['id']; | |
| /* debug */ | |
| if ($debug) { echo "record url: $recordUrl" . PHP_EOL; } | |
| if ($debug) { echo "record id: $DNSID" . PHP_EOL; } | |
| if ($debug) { echo "record data: " ; print_r($json) . PHP_EOL; } | |
| /* /debug */ | |
| // check if update is needed | |
| $old_ip = $json['result']['0']['content']; | |
| if ($old_ip != $ip) { | |
| if ($debug) { echo "CloudFlare IP: $old_ip" . PHP_EOL; } | |
| if ($debug) { echo "Current IP: $ip" . PHP_EOL; } | |
| $ch = curl_init(); | |
| $updateUrl = "https://api.cloudflare.com/client/v4/zones/$ZoneID/dns_records/$DNSID"; | |
| curl_setopt($ch, CURLOPT_URL, $updateUrl); | |
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $result = curl_exec($ch); | |
| if (curl_errno($ch)) { | |
| echo "no"; | |
| exit('Error: ' . curl_error($ch)); | |
| } | |
| curl_close ($ch); | |
| $json = json_decode($result, true); | |
| /* debug */ | |
| if ($debug) { echo "update url: $updateUrl" . PHP_EOL; } | |
| if ($debug) { echo "update data: " ; print_r($json) . PHP_EOL; } | |
| if ($debug) { echo "The IP has changed from $old_ip to $ip!" . PHP_EOL; } | |
| /* /debug */ | |
| } else { | |
| if ($debug) { echo "CloudFlare IP: $old_ip" . PHP_EOL; } | |
| if ($debug) { echo "Current IP: $ip" . PHP_EOL; } | |
| if ($debug) { echo "The IP doesn't have to be changed!" . PHP_EOL; } | |
| } | |
| echo "good"; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment