Skip to content

Commit

Permalink
Fix tests where domain name is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
juffalow committed Jul 28, 2024
1 parent 013c703 commit 53662c9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/dns/DMARC.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dns from 'dns';
import Test, { TestParameters, Result } from '../Test';
import logger from '../logger';
import getDomain from '../functions/getDomain';

class DMARC extends Test {
public name = 'DMARC';
Expand All @@ -9,7 +10,7 @@ class DMARC extends Test {
logger.info(`Starting ${this.constructor.name} test...`);

const response: any = await new Promise((resolve, reject) => {
dns.resolveTxt(`_dmarc.${(new URL(url).hostname)}`, (err, records) => {
dns.resolveTxt(`_dmarc.${getDomain(url)}`, (err, records) => {
if (err) {
reject(err);
}
Expand Down
3 changes: 2 additions & 1 deletion src/dns/NS.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import whois from 'whois';
import Test, { TestParameters, Result } from '../Test';
import logger from '../logger';
import getDomain from '../functions/getDomain';

class NS extends Test {
public name = 'NS';

public async test({ url }: TestParameters): Promise<Result> {
logger.info(`Starting ${this.constructor.name} test...`);

const nameServers = await this.getNameServers((new URL(url).hostname));
const nameServers = await this.getNameServers(getDomain(url));

return {
status: 'SUCCESS',
Expand Down
3 changes: 2 additions & 1 deletion src/dns/RegistrationDate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import whois from 'whois';
import Test, { TestParameters, Result } from '../Test';
import logger from '../logger';
import getDomain from '../functions/getDomain';

class RegistrationDate extends Test {
public name = 'RegistrationDate';

public async test({ url }: TestParameters): Promise<Result> {
logger.info(`Starting ${this.constructor.name} test...`);

const registrationDate = await this.getRegistrationDate((new URL(url).hostname));
const registrationDate = await this.getRegistrationDate(getDomain(url));

const diffInMs = (new Date(registrationDate)).getTime() - (new Date()).getTime();
const diffInDays = diffInMs / (1000 * 60 * 60 * 24);
Expand Down
16 changes: 16 additions & 0 deletions src/functions/getDomain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getDomain = (url: string): string => {
const hostname = (new URL(url)).hostname;
const parts = hostname.split('.');

if (parts.length === 2) {
return hostname;
}

if (parts.length === 3 && parts[1].length > parts[2].length) {
return hostname.replace(`${parts[0]}.`, '');
}

return hostname;
}

export default getDomain;

0 comments on commit 53662c9

Please sign in to comment.