Skip to content

Commit

Permalink
added: more comments, release v2
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiIgna committed May 22, 2024
1 parent e3148a5 commit cf4054d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 22 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/publish-jsr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Publish to jsr.io
on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4
- name: Publish package
run: npx jsr publish
2 changes: 1 addition & 1 deletion .github/workflows/test-on-push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test
name: Run tests

on: [push, pull_request]

Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ Uses Cloudflare or Google DNS, has a built-in list of subdomains to test for and
* Detects wildcard `*` records
* Option to specify extra subdomains to check for
* Provides results in common format, see `DnsRecord`
* Works in all JavaScript runtimes: NodeJS (uses `dig`), CloudFlare Workers, Browsers, Deno, etc

## v2 (in dev now) breaking changes
- ESM only
- `getAllRecords()` renamed to `getAllDnsRecords()`
- `getNameServers` is removed, use `getDnsRecords(name: string, type = 'NS')`
* Works in all JavaScript runtimes: Browsers, NodeJS, CloudFlare Workers, Deno, Bun, etc

## Getting Started

#### Requirements

- `fetch` as a global
- `dig` command for DNS lookups, if using `node-dig` as resolver. https://linux.die.net/man/1/dig
- `dig` command for DNS lookups, if using `node-dig` resolver. https://linux.die.net/man/1/dig

#### Installation

Expand Down
9 changes: 5 additions & 4 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/** DNS Record object */
/** DNS Record object, with type, ttl and value */
export interface DnsRecord {
/** Fully qualified domain name */
/** Fully qualified domain name (example.com, mail.google.com, analytics.x.com) */
name: string;
/** Record type: A, AAAA, CNAME, MX, TXT, etc. */
type: string;
/** Time to live in seconds */
/** Time to live (in seconds) for this record */
ttl: number;
/** Record data */
/** Record data: IP for A or AAAA, fqdn for CNAME, etc */
data: string;
}
/**
Expand Down Expand Up @@ -81,5 +81,6 @@ export declare function parseDnsRecord(record: string | Uint8Array): DnsRecord;
* @param domain Domain name.
* @param records Array of DNS records.
* @param percent Percentage of records with the same data to consider a wildcard.
* @returns Array of DNS records with wildcard records grouped as `*.domain`.
*/
export declare function detectWildcardRecords(domain: string, records: DnsRecord[], percent?: number): DnsRecord[];
12 changes: 11 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ export function getAllDnsRecordsStream(domain, options = {}) {
const reqDone = () => {
// if we have all the records, check for subdomains
if (--runningChecks === 0) {
// check for A,AAAA,CNAME subdomains
while (subdomainsExtra.length) {
const subdomain = subdomainsExtra.shift();
//console.log('sub', subdomain, !subdomainsChecked.includes(subdomain))
if (subdomain && !subdomainsChecked.includes(subdomain)) {
runningChecks++;
subdomainsChecked.push(subdomain);
getDnsRecords(`${subdomain}.${domain}`, 'A', options.resolver).then(sendRecords);
}
}
//todo check for txt records for subdomains
}
if (runningChecks === 0) {
writer.close();
Expand Down Expand Up @@ -205,11 +206,18 @@ export function getAllDnsRecordsStream(domain, options = {}) {
getDnsRecords(domain, 'TXT', options.resolver).then(records => {
records.forEach(r => {
// extract subdomains from SPF records
// https://datatracker.ietf.org/doc/html/rfc7208
if (r.data.includes('v=spf1') && r.data.includes(domain)) {
r.data.split(' ').forEach(spf => {
if (spf.startsWith('include:') && spf.endsWith(domain)) {
addSubdomain(spf.replace('include:', ''));
}
else if (spf.startsWith('a:') && spf.endsWith(domain)) {
addSubdomain(spf.replace('a:', ''));
}
else if (spf.startsWith('mx:') && spf.endsWith(domain)) {
addSubdomain(spf.replace('mx:', ''));
}
});
}
});
Expand Down Expand Up @@ -282,6 +290,7 @@ export function parseDnsRecord(record) {
* @param domain Domain name.
* @param records Array of DNS records.
* @param percent Percentage of records with the same data to consider a wildcard.
* @returns Array of DNS records with wildcard records grouped as `*.domain`.
*/
export function detectWildcardRecords(domain, records, percent = 0.15) {
const sameDataGroup = {};
Expand All @@ -299,6 +308,7 @@ export function detectWildcardRecords(domain, records, percent = 0.15) {
const key = `${record.type}-${record.data}`;
const sameData = sameDataGroup[key] || 0;
const recordTypeLength = records.filter(r => r.type === record.type).length;
// ?? make the formula easier to understand, already don't know how it works
if (sameData / recordTypeLength < percent || recordTypeLength < subdomainsRecords.length / 2) {
recordsWithWildcard.push(record);
}
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@layered/dns-records",
"version": "2.0.2",
"version": "2.0.4",
"exports": "./src/index.ts"
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@layered/dns-records",
"version": "2.0.0-beta.10",
"version": "2.0.0",
"description": "Discover publicly available DNS Records for a domain",
"type": "module",
"keywords": [
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { toASCII } from 'punycode'
import { subdomainsRecords } from './subdomains.js'

/** DNS Record object */
/** DNS Record object, with type, ttl and value */
export interface DnsRecord {
/** Fully qualified domain name */
/** Fully qualified domain name (example.com, mail.google.com, analytics.x.com) */
name: string
/** Record type: A, AAAA, CNAME, MX, TXT, etc. */
type: string
/** Time to live in seconds */
/** Time to live (in seconds) for this record */
ttl: number
/** Record data */
/** Record data: IP for A or AAAA, fqdn for CNAME, etc */
data: string
}

Expand Down Expand Up @@ -210,16 +210,19 @@ export function getAllDnsRecordsStream(domain: string, options: Partial<GetAllDn
const reqDone = () => {
// if we have all the records, check for subdomains
if (--runningChecks === 0) {

// check for A,AAAA,CNAME subdomains
while (subdomainsExtra.length) {
const subdomain = subdomainsExtra.shift()
//console.log('sub', subdomain, !subdomainsChecked.includes(subdomain))

if (subdomain && !subdomainsChecked.includes(subdomain)) {
runningChecks++
subdomainsChecked.push(subdomain)
getDnsRecords(`${subdomain}.${domain}`, 'A', options.resolver).then(sendRecords)
}
}

//todo check for txt records for subdomains
}

if (runningChecks === 0) {
Expand Down Expand Up @@ -362,6 +365,7 @@ export function parseDnsRecord(record: string|Uint8Array): DnsRecord {
* @param domain Domain name.
* @param records Array of DNS records.
* @param percent Percentage of records with the same data to consider a wildcard.
* @returns Array of DNS records with wildcard records grouped as `*.domain`.
*/
export function detectWildcardRecords(domain: string, records: DnsRecord[], percent = 0.15): DnsRecord[] {
const sameDataGroup: { [key: string]: number } = {}
Expand All @@ -383,6 +387,7 @@ export function detectWildcardRecords(domain: string, records: DnsRecord[], perc
const sameData = sameDataGroup[key] || 0
const recordTypeLength = records.filter(r => r.type === record.type).length

// ?? make the formula easier to understand, already don't know how it works
if (sameData / recordTypeLength < percent || recordTypeLength < subdomainsRecords.length / 2) {
recordsWithWildcard.push(record)
} else if (!wildcardsFound.includes(key)) {
Expand Down

0 comments on commit cf4054d

Please sign in to comment.