init cronjob

master
tradewind 5 years ago
commit 67fae29fed
  1. 8
      .gitignore
  2. 12
      composer.json
  3. 6
      init.php
  4. 75
      jobs/ddns.php

8
.gitignore vendored

@ -0,0 +1,8 @@
etc/config.php
var
.idea
*.swp
.DS_Store
vendor
.vscode
composer.lock

@ -0,0 +1,12 @@
{
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
},
"require": {
"php": ">=7.1.0",
"cloudflare/sdk": "^1.1"
}
}

@ -0,0 +1,6 @@
<?php
require 'vendor/autoload.php';
require_once 'etc/config.php';

@ -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;
}
Loading…
Cancel
Save