Last active
September 21, 2019 04:12
-
-
Save Choate/af519e86e7cfe68970b4 to your computer and use it in GitHub Desktop.
CloudXNS DDNS API PHP Script
This file contains 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 | |
<?php | |
$host = 'https://www.cloudxns.net/api2'; | |
$apiKey = ''; | |
$secretKey = ''; | |
$date = date('r'); | |
$ipFile = '/tmp/.current_ip'; | |
$ddnsLock = '/tmp/.ddns_lock'; | |
$logFile = '/opt/update.ddns.log'; | |
if (is_readable($ddnsLock)) { | |
exit; | |
} else { | |
touch($ddnsLock); | |
} | |
register_shutdown_function(function() use ($ddnsLock){ | |
unlink($ddnsLock); | |
}); | |
$ip = @json_decode(file_get_contents('http://ip.taobao.com/service/getIpInfo2.php?ip=myip'), true); | |
if (!isset($ip['data']['ip'])) { | |
exit; | |
} | |
$ip = $ip['data']['ip']; | |
if (is_readable($ipFile) && $ip == file_get_contents('/tmp/.current_ip')) { | |
exit; | |
} | |
function doHash($apiKey, $uri, $data, $date, $secretKey) { | |
return md5($apiKey . $uri . $data . $date . $secretKey); | |
} | |
function getRecordItem(array $record, $method, $type, $host, $apiKey, $secretKey) { | |
$mh = curl_multi_init(); | |
$mhItem = []; | |
foreach ($record as $key => $re) { | |
if (is_numeric($re)) { | |
$data = ''; | |
$id = $re; | |
} else { | |
$data = json_encode($re['data']); | |
$id = $re['id']; | |
} | |
$uri = "$host/$method/$id"; | |
$date = date('r'); | |
$hmac = doHash($apiKey, $uri, $data, $date, $secretKey); | |
$ch = curl_init($uri); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
"API-KEY: $apiKey", | |
"API-REQUEST-DATE: $date", | |
"API-HMAC: $hmac", | |
"API-FORMAT: json", | |
]); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type); | |
if ($data) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
} | |
$mhItem[$key] = $ch; | |
curl_multi_add_handle($mh, $ch); | |
} | |
$active = null; | |
do { | |
$mrc = curl_multi_exec($mh, $active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
while ($active && $mrc == CURLM_OK) { | |
if (curl_multi_select($mh) != -1) { | |
do { | |
$mrc = curl_multi_exec($mh, $active); | |
} while ($mrc == CURLM_CALL_MULTI_PERFORM); | |
} | |
} | |
$res = []; | |
foreach ($mhItem as $key => $ch) { | |
$info = curl_getinfo($ch); | |
$content = @json_decode(curl_multi_getcontent($ch), true); | |
if ($info['http_code'] == 200 && $content['message'] == 'success') { | |
$res[$key] = $content; | |
} | |
curl_multi_remove_handle($mh, $ch); | |
} | |
curl_multi_close($mh); | |
return $res; | |
} | |
$uri = "$host/domain"; | |
$data = ''; | |
$hmac = doHash($apiKey, $uri, $data, $date, $secretKey); | |
$curl = curl_init($uri); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, [ | |
"API-KEY: $apiKey", | |
"API-REQUEST-DATE: $date", | |
"API-HMAC: $hmac", | |
"API-FORMAT: json", | |
]); | |
$domain = curl_exec($curl); | |
$info = curl_getinfo($curl); | |
curl_close($curl); | |
$domain = @json_decode($domain, true); | |
if ($info['http_code'] == 200 && $domain['message'] == 'success') { | |
$domainItem = []; | |
foreach ($domain['data'] as $do) { | |
if ($do['status'] == 'ok' && $do['take_over_status'] == 'ok') { | |
$domainItem[$do['domain']] = $do['id']; | |
} | |
continue; | |
} | |
$recordItem = getRecordItem($domainItem, 'record', 'GET',$host, $apiKey, $secretKey); | |
if (count($recordItem) != count($domainItem)) {exit;} | |
$updateDomainItem = []; | |
foreach ($domainItem as $key => $do) { | |
foreach ($recordItem[$key]['data'] as $re) { | |
$updateDomainItem[$re['record_id']] = ['id' => $re['record_id'], | |
'data' => [ | |
'domain_id' => $do, | |
'host' => $re['host'], | |
'value' => $ip, | |
], | |
]; | |
} | |
} | |
$successItem = getRecordItem($updateDomainItem, 'record', 'PUT', $host, $apiKey, $secretKey); | |
if (count($successItem) != count($updateDomainItem)) { exit; } | |
foreach ($successItem as $suc) { | |
file_put_contents($logFile, "{$suc['data']['domain_name']}:{$suc['data']['value']}:[{$date}]\n", FILE_APPEND); | |
} | |
file_put_contents($ipFile, $ip); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment