Skip to content

Commit

Permalink
feat: disable domain if DNS records not found (NXDOMAIN) (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle authored Aug 27, 2024
1 parent 36316c6 commit fb32c17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions apps/mail-bridge/postal-db/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export type GetDomainDNSRecordsOutput =
acceptable: string;
};
}
| { error: string };
| { error: string; errorCode: number };

export async function getDomainDNSRecords(
domainId: string,
Expand All @@ -179,7 +179,7 @@ export async function getDomainDNSRecords(
});

if (!domainInfo) {
return { error: 'Domain not found, Contact Support' };
return { error: 'Domain not found, Contact Support', errorCode: 3 };
}

const records: GetDomainDNSRecordsOutput = {
Expand Down Expand Up @@ -222,7 +222,8 @@ export async function getDomainDNSRecords(

if (txtRecords.success === false && txtRecords.code !== 0) {
return {
error: `${txtRecords.error} Please retry after sometime, if the problem persists contact Support`
error: `${txtRecords.error} Please retry after sometime, if the problem persists contact Support`,
errorCode: txtRecords.code
};
}

Expand Down
11 changes: 11 additions & 0 deletions apps/platform/utils/updateDnsRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ export async function updateDnsRecords(
});

if ('error' in currentDNSRecords) {
if (currentDNSRecords.errorCode === 3) {
// Domain does not exist, disable the domain
await db
.update(domains)
.set({
disabled: true,
disabledAt: new Date(),
lastDnsCheckAt: new Date()
})
.where(eq(domains.id, domainResponse.id));
}
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: currentDNSRecords.error
Expand Down
3 changes: 3 additions & 0 deletions apps/worker/services/dns-check-queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createQueue, createWorker } from '../utils/queue-helpers';
import { checkDns } from '../functions/check-dns';
import type { TypeId } from '@u22n/utils/typeid';
import { domains } from '@u22n/database/schema';
import { discord } from '@u22n/utils/discord';
import { eq } from '@u22n/database/orm';
import { db } from '@u22n/database';
import { CronJob } from 'cron';

Expand Down Expand Up @@ -33,6 +35,7 @@ export async function addImmediateDnsCheckJob(
export const masterCronJob = new CronJob('0 6,14,22 * * *', async () => {
await discord.info('Running Daily DNS Cron Job');
const activeDomains = await db.query.domains.findMany({
where: eq(domains.disabled, false),
columns: { publicId: true }
});
await dnsCheckQueue.addBulk(
Expand Down

0 comments on commit fb32c17

Please sign in to comment.