-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-iana.js
More file actions
executable file
·266 lines (222 loc) · 7.73 KB
/
Copy pathsync-iana.js
File metadata and controls
executable file
·266 lines (222 loc) · 7.73 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
#!/usr/bin/env node
/**
* IANA WHOIS Server Sync Script
*
* Syncs WHOIS servers from the official IANA Root Zone Database.
* Updates whois_servers.json and README.md with latest data.
*
* Data sources:
* - TLD List: https://data.iana.org/TLD/tlds-alpha-by-domain.txt
* - WHOIS Servers: https://www.iana.org/domains/root/db/{tld}.html
*
* Usage:
* node sync-iana.js
* node sync-iana.js --dry-run
*/
import fetch from 'node-fetch';
import fs from 'fs/promises';
const DRY_RUN = process.argv.includes('--dry-run');
const BATCH_SIZE = 10;
const DELAY_MS = 500;
const colors = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
gray: '\x1b[90m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function formatDate() {
return new Date().toISOString().split('T')[0];
}
async function fetchTLDList() {
log('\n📥 Fetching TLD list from IANA...', 'cyan');
const response = await fetch('https://data.iana.org/TLD/tlds-alpha-by-domain.txt');
const text = await response.text();
const tlds = text
.split('\n')
.filter(line => line && !line.startsWith('#'))
.map(tld => tld.toLowerCase().trim())
.filter(tld => tld);
log(`✓ Found ${tlds.length} TLDs`, 'green');
return tlds;
}
async function fetchWhoisServer(tld) {
try {
const url = `https://www.iana.org/domains/root/db/${tld}.html`;
const response = await fetch(url);
if (!response.ok) {
return { tld, server: null, error: `HTTP ${response.status}` };
}
const html = await response.text();
const whoisMatch = html.match(/<b>WHOIS Server:<\/b>\s*([^\s<]+)/i);
if (whoisMatch) {
return { tld, server: whoisMatch[1].trim(), error: null };
}
return { tld, server: null, error: 'No WHOIS server listed' };
} catch (error) {
return { tld, server: null, error: error.message };
}
}
async function processBatch(tlds, startIdx, currentDict) {
const batch = tlds.slice(startIdx, startIdx + BATCH_SIZE);
const results = await Promise.all(batch.map(tld => fetchWhoisServer(tld)));
for (const result of results) {
if (result.server) {
const existing = currentDict[result.tld];
if (!existing) {
process.stdout.write(colors.green + '+' + colors.reset);
} else if (existing !== result.server) {
process.stdout.write(colors.yellow + '~' + colors.reset);
} else {
process.stdout.write(colors.gray + '.' + colors.reset);
}
} else {
process.stdout.write(colors.gray + '-' + colors.reset);
}
}
return results;
}
async function updateReadmeBadges(totalTlds, lastUpdated) {
let readme = await fs.readFile('README.md', 'utf8');
// Update Last Updated badge
readme = readme.replace(
/!\[Last Updated\]\(https:\/\/img\.shields\.io\/badge\/Last%20Updated-\d{4}--\d{2}--\d{2}-blue\)/,
`}-blue)`
);
// Update TLDs badge
readme = readme.replace(
/!\[TLDs\]\(https:\/\/img\.shields\.io\/badge\/TLDs-\d+\+-green\)/,
``
);
await fs.writeFile('README.md', readme);
}
async function main() {
log('\n🔄 WHOIS Server List - IANA Sync', 'blue');
log('━'.repeat(50), 'gray');
if (DRY_RUN) {
log('🔍 DRY RUN MODE - No changes will be made', 'yellow');
}
// Load current data
let currentDict = {};
try {
const content = await fs.readFile('whois_servers.json', 'utf8');
currentDict = JSON.parse(content);
log(`📖 Current list: ${Object.keys(currentDict).length} TLDs`, 'gray');
} catch (error) {
log('⚠️ No existing list found, creating new one', 'yellow');
}
// Fetch TLD list
const tlds = await fetchTLDList();
log(`\n🔍 Fetching WHOIS servers for ${tlds.length} TLDs...`, 'cyan');
log('Legend: ' +
colors.green + '+' + colors.reset + ' new, ' +
colors.yellow + '~' + colors.reset + ' changed, ' +
colors.gray + '.' + colors.reset + ' unchanged, ' +
colors.gray + '-' + colors.reset + ' no server\n', 'gray');
// Process in batches
const allResults = [];
for (let i = 0; i < tlds.length; i += BATCH_SIZE) {
const results = await processBatch(tlds, i, currentDict);
allResults.push(...results);
if (i + BATCH_SIZE < tlds.length) {
await new Promise(resolve => setTimeout(resolve, DELAY_MS));
}
}
console.log('\n');
// Build new dictionary
const newDict = {};
const stats = {
total: tlds.length,
withWhois: 0,
withoutWhois: 0,
new: 0,
changed: 0,
unchanged: 0
};
const changes = [];
for (const result of allResults) {
if (result.server) {
newDict[result.tld] = result.server;
stats.withWhois++;
const existing = currentDict[result.tld];
if (!existing) {
stats.new++;
changes.push({ tld: result.tld, type: 'new', server: result.server });
} else if (existing !== result.server) {
stats.changed++;
changes.push({ tld: result.tld, type: 'changed', old: existing, new: result.server });
} else {
stats.unchanged++;
}
} else {
stats.withoutWhois++;
}
}
// Display summary
log('📊 Sync Summary', 'blue');
log('━'.repeat(50), 'gray');
log(`Total TLDs in IANA: ${stats.total}`, 'gray');
log(`TLDs with WHOIS: ${stats.withWhois}`, 'green');
log(`TLDs without WHOIS: ${stats.withoutWhois} (brand TLDs, reserved, etc.)`, 'gray');
log('');
log(`New TLDs: ${stats.new}`, stats.new > 0 ? 'green' : 'gray');
log(`Changed servers: ${stats.changed}`, stats.changed > 0 ? 'yellow' : 'gray');
log(`Unchanged: ${stats.unchanged}`, 'gray');
// Show changes
if (changes.length > 0) {
log(`\n📝 Changes (${changes.length})`, 'yellow');
log('━'.repeat(50), 'gray');
for (const change of changes.slice(0, 20)) {
if (change.type === 'new') {
log(` .${change.tld} → ${change.server}`, 'green');
} else {
log(` .${change.tld}`, 'yellow');
log(` ${change.old} → ${change.new}`, 'gray');
}
}
if (changes.length > 20) {
log(` ... and ${changes.length - 20} more`, 'gray');
}
}
// Save results
if (!DRY_RUN && (stats.new > 0 || stats.changed > 0)) {
log('\n💾 Saving changes...', 'cyan');
// Backup current data
if (Object.keys(currentDict).length > 0) {
const backupPath = `whois_servers.json.backup.${formatDate()}`;
await fs.writeFile(backupPath, JSON.stringify(currentDict, null, 2));
log(`✓ Backup created: ${backupPath}`, 'gray');
}
// Save new data (sorted by key)
const sortedDict = Object.keys(newDict)
.sort()
.reduce((acc, key) => {
acc[key] = newDict[key];
return acc;
}, {});
await fs.writeFile('whois_servers.json', JSON.stringify(sortedDict, null, 2) + '\n');
log('✓ Updated whois_servers.json', 'green');
// Also update data.json (alternative endpoint)
await fs.writeFile('data.json', JSON.stringify(sortedDict, null, 2) + '\n');
log('✓ Updated data.json', 'green');
// Update README badges
await updateReadmeBadges(stats.withWhois, formatDate());
log('✓ Updated README.md badges', 'green');
log('\n✅ Sync complete!', 'green');
log(`\n📈 Stats: ${stats.withWhois} TLDs with WHOIS servers (${Math.round(stats.withWhois / stats.total * 100)}%)`, 'cyan');
} else if (DRY_RUN) {
log('\n🔍 Dry run complete - no changes made', 'yellow');
} else {
log('\n✅ List is up-to-date!', 'green');
}
}
main().catch(error => {
log(`\n❌ Sync failed: ${error.message}`, 'red');
console.error(error);
process.exit(1);
});