-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: improve www detection #5314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
interface DNSList { | ||
description: string; | ||
publication: string; | ||
services: [string[], string[]][]; | ||
version: string; | ||
} | ||
|
||
// Cache map to store TLD (top level domain) → RDAP server URL mappings | ||
const dnsCache = new Map<string, string>(); | ||
|
||
/** | ||
* Fetch the IANA DNS RDAP bootstrap JSON. | ||
*/ | ||
const fetchDnsList = async (): Promise<undefined | DNSList> => { | ||
try { | ||
const response = await fetch("https://data.iana.org/rdap/dns.json", { | ||
headers: { | ||
accept: "application/json,application/rdap+json", | ||
}, | ||
}); | ||
if (response.ok) { | ||
return response.json(); | ||
} | ||
} catch { | ||
// empty block | ||
} | ||
}; | ||
|
||
/** | ||
* Find the RDAP server URL for a given top-level domain (TLD). | ||
*/ | ||
const findRdapServer = async (topLevelDomain: string) => { | ||
if (dnsCache.size === 0) { | ||
const dns = await fetchDnsList(); | ||
if (dns) { | ||
for (const [domains, [server]] of dns.services) { | ||
for (const domain of domains) { | ||
dnsCache.set(domain, server); | ||
} | ||
} | ||
} | ||
} | ||
return dnsCache.get(topLevelDomain); | ||
}; | ||
|
||
/** | ||
* Extract the top-level domain from a full domain string. | ||
* Unicode is converted to ascii | ||
*/ | ||
const getTopLevelDomain = (domain: string) => { | ||
try { | ||
return new URL(`https://${domain}`).hostname.split(".").at(-1); | ||
} catch { | ||
// invalid domain | ||
} | ||
}; | ||
|
||
const fetchRdap = async ( | ||
rdapServer: string, | ||
domain: string | ||
): Promise<undefined | string> => { | ||
try { | ||
const response = await fetch(`${rdapServer}domain/${domain}`, { | ||
headers: { | ||
accept: "application/json,application/rdap+json", | ||
}, | ||
}); | ||
if (response.ok) { | ||
return response.text(); | ||
} | ||
} catch { | ||
// empty block | ||
} | ||
}; | ||
|
||
/** | ||
* Determine whether a domain is using Cloudflare nameservers. | ||
* 1. Parse TLD from domain. | ||
* 2. Lookup RDAP server for that TLD. | ||
* 3. Fetch RDAP data for the domain. | ||
* 4. Search the raw response for ".ns.cloudflare.com". | ||
*/ | ||
export const isDomainUsingCloudflareNameservers = async (domain: string) => { | ||
const topLevelDomain = getTopLevelDomain(domain); | ||
if (!topLevelDomain) { | ||
throw new Error("Could not parse the top level domain."); | ||
} | ||
|
||
const rdapServer = await findRdapServer(topLevelDomain); | ||
if (!rdapServer) { | ||
throw new Error( | ||
"RDAP Server for the given top level domain could not be found." | ||
); | ||
} | ||
|
||
if (rdapServer) { | ||
const data = await fetchRdap(rdapServer, domain); | ||
if (data) { | ||
// detect by nameservers rather than registrar url | ||
// sometimes stored as *.NS.CLOUDFLARE.COM | ||
return data.toLowerCase().includes(".ns.cloudflare.com"); | ||
} | ||
} | ||
return false; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.