init cronjob
This commit is contained in:
75
jobs/ddns.php
Normal file
75
jobs/ddns.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__ . '/../init.php';
|
||||
|
||||
global $globalConfig;
|
||||
|
||||
$CloudFlareConfig = $globalConfig['cloudflare'];
|
||||
|
||||
try {
|
||||
$key = new Cloudflare\API\Auth\APIKey($CloudFlareConfig['email'], $CloudFlareConfig['api_keys']);
|
||||
|
||||
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
|
||||
|
||||
$zones = new Cloudflare\API\Endpoints\Zones($adapter);
|
||||
|
||||
$zoneID = $zones->getZoneID($CloudFlareConfig['base_domain']);
|
||||
|
||||
$dns = new Cloudflare\API\Endpoints\DNS($adapter);
|
||||
|
||||
$records = $dns->listRecords($zoneID, 'A', $CloudFlareConfig['secondary_domain']);
|
||||
|
||||
$recordId = $records->result[0]->id;
|
||||
|
||||
$recordIp = $records->result[0]->content;
|
||||
|
||||
} catch (Exception $e) {
|
||||
logMessage('ERROR', $e->getMessage());
|
||||
die;
|
||||
}
|
||||
|
||||
|
||||
$res = shell_exec('curl -s https://ip.cn');
|
||||
|
||||
$res = @json_decode($res, true);
|
||||
|
||||
if (!empty($res['ip'])) {
|
||||
$realIp = $res['ip'];
|
||||
|
||||
if ($realIp !== $recordIp) {
|
||||
$msg = 'ip changed from ' . $recordIp . ' to ' . $realIp;
|
||||
|
||||
$details = [
|
||||
'type' => 'A',
|
||||
'name' => $CloudFlareConfig['secondary_domain'],
|
||||
'content' => $realIp
|
||||
];
|
||||
|
||||
try {
|
||||
$dns->updateRecordDetails($zoneID, $recordId, $details);
|
||||
|
||||
$cmd = <<< CMD
|
||||
curl -s 'https://oapi.dingtalk.com/robot/send?access_token={$globalConfig['dingtalk_token']}' -H 'Content-Type: application/json' -d "{\"msgtype\": \"text\",\"text\": {\"content\": \"$msg\"}}"
|
||||
CMD;
|
||||
shell_exec($cmd);
|
||||
|
||||
logMessage('SUCCESS', $msg);
|
||||
} catch (Exception $e) {
|
||||
logMessage('ERROR', $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
logMessage('VOID', 'ip same');
|
||||
}
|
||||
} else {
|
||||
logMessage('ERROR', 'get real ip fail');
|
||||
}
|
||||
|
||||
|
||||
|
||||
function logMessage($type, $msg = '')
|
||||
{
|
||||
echo date('Y-m-d H:i:s') . ' ' . $type . ': ' . $msg . PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user