-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.php
56 lines (38 loc) · 1.65 KB
/
cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**********
** LOOKS FOR IP CHANGES AND UPDATES CLOUDFLARE DNS RECORD
** EXECUTE EVERY 5-10 MINUTES
***********/
require_once('vendor/autoload.php');
$config = require_once(__DIR__ . '/config.php');
$jsonPath = __DIR__ . '/ip.json';
if (!file_exists($jsonPath)) {
$success = file_put_contents($jsonPath, '{"ip":null}');
if ($success === false) {
throw new \Exception("Could not create the required json file: " . $jsonPath);
}
}
$lastIp = json_decode(file_get_contents($jsonPath));
$currentIp = json_decode(file_get_contents('https://api.ipify.org?format=json'));
if ($currentIp->ip == $lastIp->ip) {
exit;
}
// ip changed, time to notify cloudflare
$key = new \Cloudflare\API\Auth\APIKey($config['cloudflare']['email'], $config['cloudflare']['api_key']);
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$zones = new \Cloudflare\API\Endpoints\Zones($adapter);
$zoneID = $zones->getZoneID($config['cloudflare']['domain']);
$dns = new \Cloudflare\API\Endpoints\DNS($adapter);
// stupid cloudflare that doesnt have getRecordID (at the time of writting this code) https://github.com/cloudflare/cloudflare-php/pull/53
$records = $dns->listRecords($zoneID, 'A', $config['cloudflare']['subdomain'] . '.' . $config['cloudflare']['domain']);
if ($dns->updateRecordDetails($zoneID, $records->result[0]->id, [
'type' => $records->result[0]->type,
'name' => $records->result[0]->name,
'content' => $currentIp->ip,
'ttl' => $records->result[0]->ttl,
'proxied' => $records->result[0]->proxied
]) === true) {
echo "DNS record updated.". PHP_EOL;
}
$lastIp->ip = $currentIp->ip;
file_put_contents($jsonPath, json_encode($lastIp));