-
Notifications
You must be signed in to change notification settings - Fork 55
/
update.php
executable file
·232 lines (197 loc) · 8.93 KB
/
update.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env php
<?php
// Load necessary functions
require_once __DIR__ . '/functions.php';
outputStdout("=============================================");
outputStdout(sprintf("Running dynamic DNS client for netcup %s", VERSION));
outputStdout("This script is not affiliated with netcup.");
outputStdout("=============================================\n");
if (! _is_curl_installed()) {
outputStderr("cURL PHP extension is not installed. Please install the cURL PHP extension, otherwise the script will not work. Exiting.");
exit(1);
}
if (!defined('USE_IPV4')) {
outputWarning("USE_IPV4 not defined in config.php. Assuming that IPv4 should be used to support deprecated legacy configs. Please add USE_IPV4 to your config.php, as in config.dist.php");
define('USE_IPV4', true);
}
if (USE_IPV4 === false && USE_IPV6 === false) {
outputStderr("IPv4 as well as IPv6 is disabled in config.php. Please activate either IPv4 or IPv6 in config.php. I do not know what I am supposed to do. Exiting.");
exit(1);
}
if (!defined('IPV4_ADDRESS_URL')) {
define('IPV4_ADDRESS_URL', 'https://get-ipv4.steck.cc');
}
if (!defined('IPV4_ADDRESS_URL_FALLBACK')) {
define('IPV4_ADDRESS_URL_FALLBACK', 'https://ipv4.seeip.org');
}
if (!defined('IPV6_ADDRESS_URL')) {
define('IPV6_ADDRESS_URL', 'https://get-ipv6.steck.cc');
}
if (!defined('IPV6_ADDRESS_URL_FALLBACK')) {
define('IPV6_ADDRESS_URL_FALLBACK', 'https://v6.ident.me/');
}
if (USE_IPV4 === true) {
// Get current IPv4 address
if (!$publicIPv4 = getCurrentPublicIPv4()) {
outputStderr("Main API and fallback API didn't return a valid IPv4 address (Try 3 / 3). Exiting.");
exit(1);
}
}
if (USE_IPV6 === true) {
//Get current IPv6 address
if (!$publicIPv6 = getCurrentPublicIPv6()) {
outputStderr("Main API and fallback API didn't return a valid IPv6 address (Try 3 / 3). Do you have IPv6 connectivity? If not, please disable USE_IPV6 in config.php. Exiting.");
exit(1);
}
}
// Login
if ($apisessionid = login(CUSTOMERNR, APIKEY, APIPASSWORD)) {
outputStdout("Logged in successfully!");
} else {
exit(1);
}
// Get list of domains
$domains = getDomains();
foreach ($domains as $domain => $subdomains) {
outputStdout(sprintf('Beginning work on domain "%s"', $domain));
// Let's get infos about the DNS zone
if ($infoDnsZone = infoDnsZone($domain, CUSTOMERNR, APIKEY, $apisessionid)) {
outputStdout("Successfully received Domain info.");
} else {
exit(1);
}
//TTL Warning
if (CHANGE_TTL !== true && $infoDnsZone['responsedata']['ttl'] > 300) {
outputStdout("TTL is higher than 300 seconds - this is not optimal for dynamic DNS, since DNS updates will take a long time. Ideally, change TTL to lower value. You may set CHANGE_TTL to True in config.php, in which case TTL will be set to 300 seconds automatically.");
}
//If user wants it, then we lower TTL, in case it doesn't have correct value
if (CHANGE_TTL === true && $infoDnsZone['responsedata']['ttl'] !== "300") {
$infoDnsZone['responsedata']['ttl'] = 300;
if (updateDnsZone($domain, CUSTOMERNR, APIKEY, $apisessionid, $infoDnsZone['responsedata'])) {
outputStdout("Lowered TTL to 300 seconds successfully.");
} else {
outputStderr("Failed to set TTL... Continuing.");
}
}
//Let's get the DNS record data.
if ($infoDnsRecords = infoDnsRecords($domain, CUSTOMERNR, APIKEY, $apisessionid)) {
outputStdout("Successfully received DNS record data.");
} else {
exit(1);
}
foreach ($subdomains as $subdomain) {
outputStdout(sprintf('Updating DNS records for subdomain "%s" of domain "%s".', $subdomain, $domain));
if (USE_IPV4 === true) {
//Find the host defined in config.php
$foundHostsV4 = array();
foreach ($infoDnsRecords['responsedata']['dnsrecords'] as $record) {
if ($record['hostname'] === $subdomain && $record['type'] === "A") {
$foundHostsV4[] = array(
'id' => $record['id'],
'hostname' => $record['hostname'],
'type' => $record['type'],
'priority' => $record['priority'],
'destination' => $record['destination'],
'deleterecord' => $record['deleterecord'],
'state' => $record['state'],
);
}
}
//If we can't find the host, create it.
if (count($foundHostsV4) === 0) {
outputStdout(sprintf("A record for host %s doesn't exist, creating necessary DNS record.", $subdomain));
$foundHostsV4[] = array(
'hostname' => $subdomain,
'type' => 'A',
'destination' => 'newly created Record',
);
}
//If the host with A record exists more than one time...
if (count($foundHostsV4) > 1) {
outputStderr(sprintf("Found multiple A records for the host %s – Please specify a host for which only a single A record exists in config.php. Exiting.", $subdomain));
exit(1);
}
$ipv4change = false;
//Has the IP changed?
foreach ($foundHostsV4 as $record) {
if ($record['destination'] !== $publicIPv4) {
//Yes, it has changed.
$ipv4change = true;
outputStdout(sprintf("IPv4 address has changed. Before: %s; Now: %s", $record['destination'], $publicIPv4));
} else {
//No, it hasn't changed.
outputStdout("IPv4 address hasn't changed. Current IPv4 address: ".$publicIPv4);
}
}
//Yes, it has changed.
if ($ipv4change === true) {
$foundHostsV4[0]['destination'] = $publicIPv4;
//Update the record
if (updateDnsRecords($domain, CUSTOMERNR, APIKEY, $apisessionid, $foundHostsV4)) {
outputStdout("IPv4 address updated successfully!");
} else {
exit(1);
}
}
}
if (USE_IPV6 === true) {
//Find the host defined in config.php
$foundHostsV6 = array();
foreach ($infoDnsRecords['responsedata']['dnsrecords'] as $record) {
if ($record['hostname'] === $subdomain && $record['type'] === "AAAA") {
$foundHostsV6[] = array(
'id' => $record['id'],
'hostname' => $record['hostname'],
'type' => $record['type'],
'priority' => $record['priority'],
'destination' => $record['destination'],
'deleterecord' => $record['deleterecord'],
'state' => $record['state'],
);
}
}
//If we can't find the host, create it.
if (count($foundHostsV6) === 0) {
outputStdout(sprintf("AAAA record for host %s doesn't exist, creating necessary DNS record.", $subdomain));
$foundHostsV6[] = array(
'hostname' => $subdomain,
'type' => 'AAAA',
'destination' => 'newly created Record',
);
}
//If the host with AAAA record exists more than one time...
if (count($foundHostsV6) > 1) {
outputStderr(sprintf("Found multiple AAAA records for the host %s – Please specify a host for which only a single AAAA record exists in config.php. Exiting.", $subdomain));
exit(1);
}
$ipv6change = false;
//Has the IP changed?
foreach ($foundHostsV6 as $record) {
if ($record['destination'] !== $publicIPv6) {
//Yes, it has changed.
$ipv6change = true;
outputStdout(sprintf("IPv6 address has changed. Before: %s; Now: %s", $record['destination'], $publicIPv6));
} else {
//No, it hasn't changed.
outputStdout("IPv6 address hasn't changed. Current IPv6 address: ".$publicIPv6);
}
}
//Yes, it has changed.
if ($ipv6change === true) {
$foundHostsV6[0]['destination'] = $publicIPv6;
//Update the record
if (updateDnsRecords($domain, CUSTOMERNR, APIKEY, $apisessionid, $foundHostsV6)) {
outputStdout("IPv6 address updated successfully!");
} else {
exit(1);
}
}
}
}
}
//Logout
if (logout(CUSTOMERNR, APIKEY, $apisessionid)) {
outputStdout("Logged out successfully!");
} else {
exit(1);
}