forked from lyoshenka/cloudflare-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddns.php
executable file
·108 lines (91 loc) · 2.63 KB
/
ddns.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env php
<?php
require 'Cloudflare.php';
if (!file_exists('config.php'))
{
echo "Please copy config.php.skel to config.php and fill out the values therein.\n";
return 1;
}
$config = require 'config.php';
foreach(array('cloudflare_email','cloudflare_api_key','domain','record_name','ttl','cloudflare_active') as $key)
{
if (!isset($config[$key]) || $config[$key] === '')
{
echo "config.php is missing the '$key' config value\n";
return 1;
}
}
$api = new Cloudflare($config['cloudflare_email'], $config['cloudflare_api_key']);
$domain = $config['domain'];
$recordName = $config['record_name'];
$ip = getIP();
$verbose = !isset($argv[1]) || $argv[1] != '-s';
try
{
$record = getExistingRecord($api, $domain, $recordName, $verbose);
if (!$record)
{
if ($verbose) echo "No existing record found. Creating a new one\n";
$ret = $api->rec_new($domain, 'A', $recordName, $ip, $config['ttl'], $config['cloudflare_active']);
throwExceptionIfError($ret);
}
elseif($record['type'] != 'A')
{
if ($verbose) echo "Record exists but is not an A record. Fixing that.\n";
$ret = $api->rec_delete($domain, $record['rec_id']);
throwExceptionIfError($ret);
$ret = $api->rec_new($domain, 'A', $recordName, $ip, $config['ttl'], $config['cloudflare_active']);
throwExceptionIfError($ret);
}
elseif($record['content'] != $ip)
{
if ($verbose) echo "Record exists. Updating IP.\n";
$ret = $api->rec_edit('grin.io', 'A', $record['rec_id'], $ip, $config['ttl'], $config['cloudflare_active']);
throwExceptionIfError($ret);
}
else
{
if ($verbose) echo "Record OK. No need to update.\n";
}
return 0;
}
catch (Exception $e)
{
echo "Error: " . $e->message . "\n";
return 1;
}
// http://stackoverflow.com/questions/3097589/getting-my-public-ip-via-api
function getIP()
{
return trim(file_get_contents('http://ipv4.icanhazip.com/'));
}
function getExistingRecord($api, $domain, $recordName, $verbose)
{
if ($verbose) echo "Getting existing record.\n";
$retry = false;
$count = 0;
do {
$count++;
$data = $api->rec_load_all($domain);
$retry = isset($data['error']) && $data['error'] == 'Operation timed out after 5001 milliseconds with 0 bytes received';
if ($retry && $count > 5)
{
throw new Exception('Could not get data from Cloudflare after 5 retries. Aborting.');
}
} while ($retry);
foreach($data['response']['recs']['objs'] as $record)
{
if ($record['name'] == $recordName)
{
return $record;
}
}
return null;
}
function throwExceptionIfError($data)
{
if (isset($data['error']))
{
throw new Exception($data['error']);
}
}