-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-domains.js
More file actions
332 lines (292 loc) · 8.22 KB
/
Copy pathcheck-domains.js
File metadata and controls
332 lines (292 loc) · 8.22 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env node
/**
* Domain availability checker for multiple TLDs
* Uses Fastly Domain Research API (Domainr)
*
* Usage:
* echo "word1 word2 word3" | node check-domains.js
* node check-domains.js word1 word2 word3
* node check-domains.js --tld sh,dev word1 word2
* node check-domains.js --hack # check domain hacks only (e.g. publi.sh)
* node check-domains.js --full word1 # check full domain as-is (e.g. "publi.sh")
*
* Output: markdown table rows ready to paste into domain-search.md
*
* Maintains checked-domains.json as a dedup database.
*/
const https = require("node:https");
const fs = require("node:fs");
const path = require("node:path");
// Load .env file if it exists
const envPath = path.join(__dirname, ".env");
if (fs.existsSync(envPath)) {
for (const line of fs.readFileSync(envPath, "utf8").split("\n")) {
const [key, ...val] = line.split("=");
if (key && val.length) process.env[key.trim()] = val.join("=").trim();
}
}
const API_KEY = process.env.FASTLY_KEY;
if (!API_KEY) {
console.error(
"Error: FASTLY_KEY not set. Create a .env file with FASTLY_KEY=your_key",
);
process.exit(1);
}
const DB_FILE = path.join(__dirname, "checked-domains.json");
const DELAY_MS = 120; // polite delay between requests
// Default TLDs to check
const DEFAULT_TLDS = [
"sh",
"dev",
"io",
"is",
"ink",
"to",
"it",
"es",
"pub",
"page",
"press",
"run",
"app",
"ai",
"co",
];
// Load or create the checked domains database
function loadDb() {
try {
return JSON.parse(fs.readFileSync(DB_FILE, "utf8"));
} catch {
return {};
}
}
function saveDb(db) {
fs.writeFileSync(DB_FILE, JSON.stringify(db, null, 2));
}
// Check a single domain via Fastly API
function checkDomain(domain) {
return new Promise((resolve, reject) => {
const url = `https://api.fastly.com/domain-management/v1/tools/status?domain=${domain}`;
const req = https.request(
url,
{ headers: { "Fastly-Key": API_KEY } },
(res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
try {
resolve(JSON.parse(data));
} catch {
reject(
new Error(`Failed to parse response for ${domain}: ${data}`),
);
}
});
},
);
req.on("error", reject);
req.end();
});
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
// Parse status into human-readable form
function parseStatus(result) {
const s = result.status || "";
const offers = result.offers || [];
if (
s.includes("active") &&
!s.includes("undelegated") &&
!s.includes("inactive")
) {
if (offers.length > 0) {
const price = offers[0].price;
return {
status: "for sale",
price: `$${Number(price).toLocaleString()}`,
};
}
return { status: "taken", price: "—" };
}
if (s.includes("reserved")) {
return { status: "reserved", price: "contact registry" };
}
if (s.includes("inactive")) {
const isPremium = s.includes("premium");
return {
status: isPremium ? "available (premium)" : "available",
price: isPremium ? "TBD" : "—",
};
}
if (
s.includes("undelegated") &&
s.includes("active") &&
!s.includes("inactive")
) {
return { status: "unclear (verify)", price: "—" };
}
return { status: s, price: "—" };
}
// Format as markdown table row
function toMarkdownRow(domain, parsed) {
return `| [ ] | ${domain} | ${parsed.status} | ${parsed.price} | |`;
}
// Parse arguments
function parseArgs() {
const args = process.argv.slice(2);
let tlds = DEFAULT_TLDS;
let hackOnly = false;
let fullMode = false;
const words = [];
for (let i = 0; i < args.length; i++) {
if (args[i] === "--tld" && args[i + 1]) {
tlds = args[++i].split(",").map((t) => t.replace(/^\./, ""));
} else if (args[i] === "--hack") {
hackOnly = true;
} else if (args[i] === "--full") {
fullMode = true;
} else if (!args[i].startsWith("--")) {
words.push(args[i].trim().toLowerCase());
}
}
return { tlds, hackOnly, fullMode, words };
}
async function main() {
const db = loadDb();
let { tlds, hackOnly, fullMode, words } = parseArgs();
// Read from stdin if no words provided
if (words.length === 0) {
try {
const input = fs.readFileSync("/dev/stdin", "utf8");
words = input
.split(/[\s,\n]+/)
.map((w) => w.trim().toLowerCase())
.filter(Boolean);
} catch {
// no stdin
}
}
if (words.length === 0) {
console.error(
"Usage: node check-domains.js [--tld sh,dev,io] [--hack] [--full] word1 word2 ...",
);
console.error(" or: echo 'word1 word2' | node check-domains.js");
console.error("\nModes:");
console.error(" (default) Check word.tld for each TLD");
console.error(
" --hack Check domain hacks where TLD completes the word (e.g. publi.sh)",
);
console.error(
" --full Check exact domains as given (e.g. 'publi.sh getl.ink')",
);
console.error(" --tld Specify TLDs: --tld sh,dev,io");
process.exit(1);
}
// Build list of domains to check
let domains = [];
if (fullMode) {
// Check exact domains as provided
domains = words.map((w) => w);
} else if (hackOnly) {
// Only check hack domains (word already contains the dot, like publi.sh)
domains = words.map((w) => w);
} else {
// Standard mode: combine each word with each TLD
for (const word of words) {
for (const tld of tlds) {
domains.push(`${word}.${tld}`);
}
}
}
// Deduplicate against database
const newDomains = [];
const skipped = [];
for (const d of domains) {
if (db[d]) {
skipped.push(d);
} else {
newDomains.push(d);
}
}
if (skipped.length > 0) {
console.error(
`\n⏭ Skipped ${skipped.length} already-checked: ${skipped.join(", ")}`,
);
}
if (newDomains.length === 0) {
console.error("\nAll domains already checked. Nothing to do.");
process.exit(0);
}
console.error(`\nChecking ${newDomains.length} domains...\n`);
// Results buckets
const available = [];
const taken = [];
const forSale = [];
for (const domain of newDomains) {
try {
const result = await checkDomain(domain);
const parsed = parseStatus(result);
// Save to database
db[domain] = {
domain,
status: parsed.status,
price: parsed.price,
rawStatus: result.status,
checkedAt: new Date().toISOString().split("T")[0],
};
if (parsed.status.includes("available")) {
available.push({ domain, parsed });
} else if (parsed.status === "for sale") {
forSale.push({ domain, parsed });
} else {
taken.push({ domain, parsed });
}
process.stderr.write(`.`);
await sleep(DELAY_MS);
} catch (err) {
console.error(`\n Error checking ${domain}: ${err.message}`);
}
}
// Print available
if (available.length > 0) {
console.log("\n### Available\n");
console.log("| Fav | Domain | Status | Price | Notes |");
console.log("|---|---|---|---|---|");
for (const { domain, parsed } of available) {
console.log(toMarkdownRow(domain, parsed));
}
}
// Print for-sale
if (forSale.length > 0) {
console.log("\n### For Sale\n");
console.log("| Fav | Domain | Status | Price | Notes |");
console.log("|---|---|---|---|---|");
for (const { domain, parsed } of forSale) {
console.log(toMarkdownRow(domain, parsed));
}
}
// Print taken
if (taken.length > 0) {
console.log("\n### Taken\n");
console.log("| Domain | Status |");
console.log("|---|---|");
for (const { domain, parsed } of taken) {
console.log(`| ${domain} | ${parsed.status} |`);
}
}
// No results at all
if (available.length === 0 && forSale.length === 0 && taken.length === 0) {
console.log("\nNo results.");
}
// Save database
saveDb(db);
// Summary
console.error(
`\n\n✅ ${available.length} available, ${forSale.length} for sale, ${taken.length} taken`,
);
console.error(
`📁 Database: ${Object.keys(db).length} total domains in ${DB_FILE}`,
);
}
main().catch(console.error);