/**
* Multi-Source Subdomain Finder Scraper
* Discover active subdomains for any target domain
*
* @author Vamz Spectre (https://vamxapi.my.id)
*
* Download:
* curl -sL "https://code.vamxapi.my.id/snippet/raw/subdomain-finder" -o subdomain-finder.js
*
* Usage:
* node subdomain-finder.js "bmkg.go.id"
* node subdomain-finder.js "google.com"
*/
async function fetchSubdomainCenter(domain) {
try {
const res = await fetch(`https://api.subdomain.center/?domain=${encodeURIComponent(domain)}`, {
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" },
signal: AbortSignal.timeout(3500),
});
if (!res.ok) return [];
const data = await res.json();
return Array.isArray(data) ? data : [];
} catch {
return [];
}
}
async function fetchCertspotter(domain) {
try {
const res = await fetch(
`https://api.certspotter.com/v1/issuances?domain=${encodeURIComponent(domain)}&include_subdomains=true&expand=dns_names`,
{
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" },
signal: AbortSignal.timeout(3500),
}
);
if (!res.ok) return [];
const data = await res.json();
if (!Array.isArray(data)) return [];
const names = [];
for (const item of data) {
if (Array.isArray(item.dns_names)) {
for (const name of item.dns_names) {
if (typeof name === "string") names.push(name);
}
}
}
return names;
} catch {
return [];
}
}
async function fetchRapidDns(domain) {
try {
const res = await fetch(`https://rapiddns.io/subdomain/${encodeURIComponent(domain)}?full=1#result`, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
},
signal: AbortSignal.timeout(3000),
});
if (!res.ok) return [];
const html = await res.text();
const regex = new RegExp(`
([a-zA-Z0-9.-]+\\.${domain.replace(/\\./g, "\\\\.")})<\\/td>`, "g");
const matches = html.match(regex);
if (matches) {
return matches.map((m) => m.replace(/<\/?td>/g, ""));
}
} catch {
return [];
}
return [];
}
async function fetchCrtSh(domain) {
try {
const res = await fetch(`https://crt.sh/?q=%25.${encodeURIComponent(domain)}&output=json`, {
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" },
signal: AbortSignal.timeout(3000),
});
if (!res.ok) return [];
const data = await res.json();
if (!Array.isArray(data)) return [];
const names = [];
for (const item of data) {
if (typeof item.name_value === "string") {
const split = item.name_value.split("\n");
for (const s of split) {
if (s.trim()) names.push(s.trim());
}
}
}
return names;
} catch {
return [];
}
}
async function subdomainFinder(targetDomain) {
if (!targetDomain || typeof targetDomain !== "string") {
throw new Error("Target domain wajib disertakan. Contoh: 'bmkg.go.id'");
}
let cleanDomain = targetDomain.trim().toLowerCase();
cleanDomain = cleanDomain.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
cleanDomain = cleanDomain.split("/")[0].split(":")[0].trim();
if (!cleanDomain || !/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/i.test(cleanDomain)) {
throw new Error("Format domain tidak valid.");
}
const startTime = Date.now();
const results = await Promise.allSettled([
fetchSubdomainCenter(cleanDomain),
fetchCertspotter(cleanDomain),
fetchRapidDns(cleanDomain),
fetchCrtSh(cleanDomain),
]);
const subSet = new Set();
const domainSuffix = `.${cleanDomain}`;
for (const res of results) {
if (res.status === "fulfilled" && Array.isArray(res.value)) {
for (const item of res.value) {
if (!item || typeof item !== "string") continue;
let s = item.toLowerCase().trim();
s = s.replace(/^\*\./, "").trim();
if ((s.endsWith(domainSuffix) || s === cleanDomain) && /^[a-z0-9.-]+$/.test(s)) {
subSet.add(s);
}
}
}
}
const subdomains = Array.from(subSet).sort((a, b) => {
if (a === cleanDomain) return -1;
if (b === cleanDomain) return 1;
return a.localeCompare(b);
});
return {
status: true,
domain: cleanDomain,
total: subdomains.length,
elapsed_ms: Date.now() - startTime,
subdomains,
};
}
module.exports = { subdomainFinder };
// CLI Runner
if (require.main === module || process.argv[1]?.endsWith("subdomain-finder.js")) {
const domain = process.argv[2] || "bmkg.go.id";
subdomainFinder(domain)
.then((res) => {
console.log(JSON.stringify(res, null, 2));
})
.catch((err) => {
console.error(JSON.stringify({ status: false, error: err.message }, null, 2));
process.exit(1);
});
}
|