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
|
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import https from 'https';
import http from 'http';
import yaml from 'js-yaml';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const NAMES_DIR = path.join(__dirname, '..', 'src', 'content', 'names');
const TIMEOUT = 5000; // 5 seconds timeout
function makeRequest(url, useHttps = true) {
return new Promise((resolve, reject) => {
const client = useHttps ? https : http;
const parsedUrl = new URL(url);
const options = {
hostname: parsedUrl.hostname,
port: parsedUrl.port || (useHttps ? 443 : 80),
path: parsedUrl.pathname + parsedUrl.search,
method: 'HEAD',
timeout: TIMEOUT,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
};
const req = client.request(options, (res) => {
resolve({
statusCode: res.statusCode,
success: res.statusCode >= 200 && res.statusCode < 400
});
});
req.on('error', (err) => {
reject(err);
});
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timeout'));
});
req.end();
});
}
async function checkDomain(domain, explicitUrl = null) {
const urlsToTry = [];
if (explicitUrl) {
urlsToTry.push(explicitUrl);
} else {
urlsToTry.push(`https://${domain}`);
urlsToTry.push(`http://${domain}`);
}
for (const url of urlsToTry) {
try {
const isHttps = url.startsWith('https://');
const result = await makeRequest(url, isHttps);
if (result.success) {
return { success: true, url, statusCode: result.statusCode };
}
} catch (error) {
// Continue to next URL
continue;
}
}
return { success: false, url: null, statusCode: null };
}
async function processYamlFile(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf8');
const data = yaml.load(content);
if (!data || !data.domain) {
console.log(`SKIP: ${path.basename(filePath)} - No domain field`);
return { updated: false };
}
const domain = data.domain;
const explicitUrl = data.url || null;
console.log(`CHECKING: ${domain}${explicitUrl ? ` (explicit URL: ${explicitUrl})` : ''}`);
const result = await checkDomain(domain, explicitUrl);
if (result.success) {
console.log(`✓ FOUND: ${domain} at ${result.url} (${result.statusCode})`);
// Remove invalid flag if it was previously set
if (data.invalid) {
delete data.invalid;
const updatedYaml = yaml.dump(data, {
lineWidth: -1,
noCompatMode: true,
quotingType: '"',
forceQuotes: false
});
fs.writeFileSync(filePath, updatedYaml);
return { updated: true };
}
return { updated: false };
} else {
console.log(`✗ NOT FOUND: ${domain}`);
// Mark as invalid if not already marked
if (!data.invalid) {
data.invalid = true;
const updatedYaml = yaml.dump(data, {
lineWidth: -1,
noCompatMode: true,
quotingType: '"',
forceQuotes: false
});
fs.writeFileSync(filePath, updatedYaml);
return { updated: true };
}
return { updated: false };
}
} catch (error) {
console.error(`ERROR processing ${filePath}: ${error.message}`);
return { updated: false };
}
}
async function main() {
try {
const files = fs.readdirSync(NAMES_DIR)
.filter(file => file.endsWith('.yml'))
.map(file => path.join(NAMES_DIR, file));
console.log(`Found ${files.length} YAML files to process\n`);
let processedCount = 0;
let updatedCount = 0;
let foundCount = 0;
let notFoundCount = 0;
for (const file of files) {
const result = await processYamlFile(file);
processedCount++;
if (result.updated) {
updatedCount++;
}
// Small delay to be respectful to servers
await new Promise(resolve => setTimeout(resolve, 100));
}
console.log(`\n--- SUMMARY ---`);
console.log(`Processed: ${processedCount} files`);
console.log(`Updated: ${updatedCount} files`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
main();
|