-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_rdap_servers.php
More file actions
executable file
·290 lines (246 loc) · 8.93 KB
/
update_rdap_servers.php
File metadata and controls
executable file
·290 lines (246 loc) · 8.93 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env php
<?php
/* Poweradmin, a friendly web-based admin tool for PowerDNS.
* See <https://www.poweradmin.org> for more details.
*
* Copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
* Copyright 2010-2025 Poweradmin Development Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Script to update RDAP servers from IANA bootstrap file
*
* This script fetches the official RDAP bootstrap data from IANA
* and updates the rdap_servers.json file.
*
* IANA RDAP Bootstrap: https://data.iana.org/rdap/dns.json
*
* Usage:
* php scripts/update_rdap_servers.php [options]
*
* Options:
* --dry-run Show changes without writing to file
* --verbose Show detailed progress
* --help Show this help message
*/
define('IANA_RDAP_BOOTSTRAP_URL', 'https://data.iana.org/rdap/dns.json');
define('RDAP_SERVERS_FILE', __DIR__ . '/../lib/Module/Rdap/data/rdap_servers.php');
// Parse command line options
$options = getopt('', ['dry-run', 'verbose', 'help']);
$dryRun = isset($options['dry-run']);
$verbose = isset($options['verbose']);
$showHelp = isset($options['help']);
if ($showHelp) {
echo <<<HELP
Update RDAP servers from IANA bootstrap file
Usage:
php scripts/update_rdap_servers.php [options]
Options:
--dry-run Show changes without writing to file
--verbose Show detailed progress
--help Show this help message
Examples:
php scripts/update_rdap_servers.php --dry-run --verbose
php scripts/update_rdap_servers.php
HELP;
exit(0);
}
/**
* Fetch URL content using curl
*/
function fetchUrl(string $url): string
{
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Poweradmin RDAP Updater/1.0)',
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($response === false || $httpCode !== 200) {
throw new RuntimeException("HTTP request failed: $error (HTTP $httpCode)");
}
return $response;
}
/**
* Normalize RDAP URL (ensure consistent format)
*/
function normalizeRdapUrl(string $url): string
{
$url = trim($url);
// Ensure HTTPS
if (strpos($url, 'http://') === 0) {
$url = 'https://' . substr($url, 7);
}
return $url;
}
/**
* Convert punycode TLD to display form for output
*/
function formatTldForDisplay(string $tld): string
{
if (strpos($tld, 'xn--') === 0 && function_exists('idn_to_utf8')) {
$unicode = idn_to_utf8($tld, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
if ($unicode !== false && $unicode !== $tld) {
return "$tld ($unicode)";
}
}
return $tld;
}
// Main execution
try {
echo "=== RDAP Servers Update Script ===\n\n";
if ($dryRun) {
echo "Running in DRY-RUN mode - no changes will be written\n\n";
}
// Load existing RDAP servers
if (!file_exists(RDAP_SERVERS_FILE)) {
throw new RuntimeException("RDAP servers file not found: " . RDAP_SERVERS_FILE);
}
$existingServers = include RDAP_SERVERS_FILE;
if (!is_array($existingServers)) {
throw new RuntimeException("Failed to parse existing RDAP servers file");
}
echo "Loaded " . count($existingServers) . " existing RDAP server entries\n\n";
// Fetch IANA RDAP bootstrap data
echo "Fetching RDAP bootstrap data from IANA...\n";
$bootstrapJson = fetchUrl(IANA_RDAP_BOOTSTRAP_URL);
$bootstrapData = json_decode($bootstrapJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Failed to parse IANA RDAP bootstrap data");
}
if (!isset($bootstrapData['services']) || !is_array($bootstrapData['services'])) {
throw new RuntimeException("Invalid IANA RDAP bootstrap data format");
}
// Display bootstrap metadata
if (isset($bootstrapData['version'])) {
echo "Bootstrap version: " . $bootstrapData['version'] . "\n";
}
if (isset($bootstrapData['publication'])) {
echo "Publication date: " . $bootstrapData['publication'] . "\n";
}
// Parse bootstrap data into TLD -> URL mapping
// IANA format: services is array of [TLDs array, URLs array]
$ianaServers = [];
foreach ($bootstrapData['services'] as $service) {
if (!is_array($service) || count($service) < 2) {
continue;
}
$tlds = $service[0];
$urls = $service[1];
if (!is_array($tlds) || !is_array($urls) || empty($urls)) {
continue;
}
// Use the first URL as the primary RDAP server
$primaryUrl = normalizeRdapUrl($urls[0]);
foreach ($tlds as $tld) {
$tld = strtolower(trim($tld));
if (!empty($tld)) {
$ianaServers[$tld] = $primaryUrl;
}
}
}
echo "Found " . count($ianaServers) . " TLDs in IANA bootstrap data\n\n";
// Track changes
$added = [];
$updated = [];
$removed = [];
$unchanged = 0;
// Process IANA servers - add/update
foreach ($ianaServers as $tld => $ianaUrl) {
$existingUrl = $existingServers[$tld] ?? null;
if ($existingUrl === null) {
// New TLD
$added[$tld] = $ianaUrl;
$existingServers[$tld] = $ianaUrl;
if ($verbose) {
echo "ADDED: " . formatTldForDisplay($tld) . " -> $ianaUrl\n";
}
} elseif (normalizeRdapUrl($existingUrl) !== $ianaUrl) {
// Updated URL
$updated[$tld] = ['old' => $existingUrl, 'new' => $ianaUrl];
$existingServers[$tld] = $ianaUrl;
if ($verbose) {
echo "UPDATED: " . formatTldForDisplay($tld) . ": $existingUrl -> $ianaUrl\n";
}
} else {
$unchanged++;
if ($verbose) {
echo "unchanged: " . formatTldForDisplay($tld) . "\n";
}
}
}
// Find removed TLDs (in existing but not in IANA)
foreach ($existingServers as $tld => $url) {
if (!isset($ianaServers[$tld])) {
$removed[$tld] = $url;
unset($existingServers[$tld]);
if ($verbose) {
echo "REMOVED: " . formatTldForDisplay($tld) . " (was $url)\n";
}
}
}
// Sort the servers alphabetically
ksort($existingServers);
// Print summary
echo "\n=== Summary ===\n";
echo "Total TLDs in IANA bootstrap: " . count($ianaServers) . "\n";
echo "Unchanged: $unchanged\n";
echo "Added: " . count($added) . "\n";
echo "Updated: " . count($updated) . "\n";
echo "Removed: " . count($removed) . "\n";
if (count($added) > 0) {
echo "\n--- Added TLDs ---\n";
foreach ($added as $tld => $url) {
echo " " . formatTldForDisplay($tld) . ": $url\n";
}
}
if (count($updated) > 0) {
echo "\n--- Updated TLDs ---\n";
foreach ($updated as $tld => $change) {
echo " " . formatTldForDisplay($tld) . ": {$change['old']} -> {$change['new']}\n";
}
}
if (count($removed) > 0) {
echo "\n--- Removed TLDs ---\n";
foreach ($removed as $tld => $oldUrl) {
echo " " . formatTldForDisplay($tld) . ": was $oldUrl\n";
}
}
// Write updated file
if (!$dryRun && (count($added) > 0 || count($updated) > 0 || count($removed) > 0)) {
echo "\nWriting updated RDAP servers file...\n";
$date = date('Y-m-d');
$count = count($existingServers);
$phpContent = "<?php\n\n/**\n * RDAP servers list\n *\n * Updated on $date - $count entries\n * Source: IANA RDAP Bootstrap (https://data.iana.org/rdap/dns.json)\n *\n * Do not edit manually - use scripts/update_rdap_servers.php to regenerate\n */\n\nreturn " . var_export($existingServers, true) . ";\n";
if (file_put_contents(RDAP_SERVERS_FILE, $phpContent) === false) {
throw new RuntimeException("Failed to write RDAP servers file");
}
echo "Done! Updated " . RDAP_SERVERS_FILE . "\n";
} elseif ($dryRun) {
echo "\nDry-run complete. No changes written.\n";
} else {
echo "\nNo changes needed.\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}