forked from bensquire/Digital-Ocean-Dynamic-DNS-Updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.php
164 lines (141 loc) · 4.84 KB
/
updater.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /usr/bin/env php
<?php
/**
* Return the contents of a page (DO only uses GET requests for its API...)
*
* @param $url string
*
* @return mixed
*/
function getWebPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* Retrieve our external IP address
*
* @return mixed
*/
function getExternalIp()
{
$html = getWebPage(CHECK_IP);
if (preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $html, $matches) === 0) {
return false;
}
return $matches[0];
}
/**
* Given the domain ID, fetch all Records and return the one we want.
*
* @param null $page
* @return bool
*/
function getRecord($page = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, API_URL . 'domains/' . DOMAIN . '/records' . ($page != null ? '?page=' . $page : ''));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . ACCESS_TOKEN));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
$responseCode = $info['http_code'];
curl_close($ch);
if ($data === false || $responseCode !== 200) {
return false;
}
$dataJson = json_decode($data, true);
foreach ($dataJson['domain_records'] as $record) {
if ($record['name'] === RECORD && $record['type'] === 'A') {
return $record;
}
}
// Recursive call for pages results
if (isset($dataJson['links']['pages']['next']) && $dataJson['links']['pages']['next'] != '') {
preg_match('/page=(?<page_number>\d+)/i', $dataJson['links']['pages']['next'], $match);
if (isset($match['page_number']) && $match['page_number'] != '') {
return getRecord($match['page_number']);
}
}
return false;
}
/**
* Update the DO Domain IP address, providing the IPAddress, the Domain and Record ID.
*
* @param $record
* @param $ipAddress
*
* @return mixed|string
* @throws Exception
*/
function setRecordIP($record, $ipAddress)
{
$data = json_encode(array(
'data' => $ipAddress
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, API_URL . 'domains/' . DOMAIN . '/records/' . $record['id']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . ACCESS_TOKEN,
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
));
$data = curl_exec($ch);
curl_close($ch);
return ($data !== false);
}
try {
if (!isset($argv[1])) {
throw new Exception('1st parameter (Access Token) is missing.');
}
if (!isset($argv[2])) {
throw new Exception('2nd parameter (Domain) is missing.');
}
if (!isset($argv[3])) {
throw new Exception('3rd parameter (A Record) is missing.');
}
DEFINE('ACCESS_TOKEN', $argv[1]); //Digital Ocean Personal Access Tokens (read & write)
DEFINE('DOMAIN', $argv[2]); //joebloggs.co.uk
DEFINE('RECORD', $argv[3]); //home
DEFINE('CURL_TIMEOUT', 15);
DEFINE('CHECK_IP', "http://checkip.dyndns.org:8245");
DEFINE('API_URL', "https://api.digitalocean.com/v2/");
echo 'Updating ' . RECORD . '.' . DOMAIN . ': ' . date('Y-m-d H:i:s') . "\r\n";
echo 'Fetching external IP from: ' . CHECK_IP . "\r\n";
if (($ipAddress = getExternalIp()) === false) {
throw new Exception('Unable to extract external IP address');
}
echo 'Fetching Record ID for: ' . RECORD . "\r\n";
if (($record = getRecord()) === false) {
throw new Exception('Unable to find requested record in DO account');
}
echo 'Comparing ' . $record['data'] . ' to ' . $ipAddress . "\r\n";
if ($record['data'] === $ipAddress) {
throw new Exception('Record ' . RECORD . '.' . DOMAIN . ' already set to ' . $ipAddress);
}
echo 'Updating record ' . $record['name'] . '.' . DOMAIN . " to " . $ipAddress . "\r\n";
if (setRecordIP($record, $ipAddress) === false) {
throw new Exception('Unable to update IP address');
}
echo 'IP Address successfully updated.' . "\r\n";
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . "\r\n";
exit(1);
}
exit(0);