Merge pull request #3834 from colinjtsen2010/feat/domain-monitoring
feat: add domain registration expiry monitoring
This commit is contained in:
13 files changed
+465
-30
No files matched your search
@@ -2,7 +2,10 @@ import { AppError } from "@/utils/AppError.js";
|
||||
import { Monitor, MonitorTypes, type MonitorType } from "@/domain/monitors/monitor.type.js";
|
||||
import { UserRole } from "@/domain/users/user.type.js";
|
||||
import sslChecker, { SSLDetails } from "ssl-checker";
|
||||
import * as whoiser from "whoiser";
|
||||
import { parse as parseDomain } from "tldts";
|
||||
type SSLCheckerType = typeof sslChecker;
|
||||
type WhoisModule = typeof whoiser;
|
||||
|
||||
export const fetchMonitorCertificate = async (checker: SSLCheckerType, monitor: Monitor): Promise<SSLDetails> => {
|
||||
const monitorUrl = new URL(monitor.url);
|
||||
@@ -13,6 +16,114 @@ export const fetchMonitorCertificate = async (checker: SSLCheckerType, monitor:
|
||||
}
|
||||
return cert;
|
||||
};
|
||||
|
||||
const WHOIS_TIMEOUT_MS = 10_000;
|
||||
const DOMAIN_EXPIRY_CACHE_POSITIVE_TTL_MS = 60 * 60 * 1000; // 1 hour
|
||||
const DOMAIN_EXPIRY_CACHE_NEGATIVE_TTL_MS = 10 * 60 * 1000; // 10 minutes
|
||||
const EXPIRY_DATE_KEY_PATTERN = /(expiry date|expiration|paid-?till|registration expiration)/i;
|
||||
|
||||
export interface DomainExpiryResult {
|
||||
domain: string | null;
|
||||
expiryDate: string | null;
|
||||
}
|
||||
|
||||
interface DomainExpiryCacheEntry extends DomainExpiryResult {
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
interface DomainExpiryCache {
|
||||
get(domain: string): DomainExpiryResult | undefined;
|
||||
set(domain: string, result: DomainExpiryResult): void;
|
||||
}
|
||||
|
||||
export const createDomainExpiryCache = (positiveTtlMs: number, negativeTtlMs: number): DomainExpiryCache => {
|
||||
const entries = new Map<string, DomainExpiryCacheEntry>();
|
||||
|
||||
return {
|
||||
get(domain: string): DomainExpiryResult | undefined {
|
||||
const entry = entries.get(domain);
|
||||
if (entry === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (entry.expiresAt <= Date.now()) {
|
||||
entries.delete(domain);
|
||||
return undefined;
|
||||
}
|
||||
return { domain: entry.domain, expiryDate: entry.expiryDate };
|
||||
},
|
||||
set(domain: string, result: DomainExpiryResult): void {
|
||||
entries.set(domain, {
|
||||
...result,
|
||||
expiresAt: Date.now() + (result.expiryDate === null ? negativeTtlMs : positiveTtlMs),
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const domainExpiryCache = createDomainExpiryCache(DOMAIN_EXPIRY_CACHE_POSITIVE_TTL_MS, DOMAIN_EXPIRY_CACHE_NEGATIVE_TTL_MS);
|
||||
|
||||
export const extractDomainExpiryDate = (whoisData: object): string | null => {
|
||||
const matchingKey = Object.keys(whoisData).find((key) => EXPIRY_DATE_KEY_PATTERN.test(key));
|
||||
if (!matchingKey) {
|
||||
return null;
|
||||
}
|
||||
const raw = extractString((whoisData as Record<string, unknown>)[matchingKey] ?? null);
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = parseDomainExpiryValue(raw);
|
||||
return parsed === null ? null : parsed.toISOString();
|
||||
};
|
||||
|
||||
const parseDomainExpiryValue = (raw: string): Date | null => {
|
||||
// Some registries (e.g. Nominet for .uk) return "14-Feb-2027" style values.
|
||||
const monthMatch = /^(\d{1,2})-([A-Za-z]{3})-(\d{4})$/.exec(raw.trim());
|
||||
if (monthMatch) {
|
||||
const months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
|
||||
const monthIndex = months.indexOf((monthMatch[2] ?? "").toLowerCase());
|
||||
if (monthIndex !== -1) {
|
||||
const parsed = new Date(Date.UTC(Number(monthMatch[3]), monthIndex, Number(monthMatch[1])));
|
||||
if (!Number.isNaN(parsed.getTime())) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const date = new Date(raw);
|
||||
if (!Number.isNaN(date.getTime())) {
|
||||
return date;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const fetchMonitorDomain = async (
|
||||
client: WhoisModule,
|
||||
monitor: Monitor,
|
||||
cache: DomainExpiryCache = domainExpiryCache
|
||||
): Promise<DomainExpiryResult> => {
|
||||
const monitorUrl = new URL(monitor.url);
|
||||
const registrableDomain = parseDomain(monitorUrl.hostname).domain;
|
||||
if (registrableDomain === null) {
|
||||
// Bare IP addresses, localhost, and other non-domain hosts have no WHOIS expiry.
|
||||
return { domain: null, expiryDate: null };
|
||||
}
|
||||
|
||||
const cached = cache.get(registrableDomain);
|
||||
if (cached !== undefined) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const result = await client.domain(registrableDomain, { timeout: WHOIS_TIMEOUT_MS });
|
||||
const firstServerData = Object.values(result)[0];
|
||||
const expiryDate = extractDomainExpiryDate(
|
||||
typeof firstServerData === "object" && firstServerData !== null && !Array.isArray(firstServerData) ? firstServerData : {}
|
||||
);
|
||||
const domainExpiry: DomainExpiryResult = { domain: registrableDomain, expiryDate };
|
||||
cache.set(registrableDomain, domainExpiry);
|
||||
return domainExpiry;
|
||||
};
|
||||
|
||||
export const requireString = (value: unknown, fieldName: string): string => {
|
||||
if (typeof value === "string" && value.trim().length > 0) {
|
||||
return value;
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
editMonitorBodyValidation,
|
||||
pauseMonitorParamValidation,
|
||||
getCertificateParamValidation,
|
||||
getDomainParamValidation,
|
||||
getHardwareDetailsByIdParamValidation,
|
||||
getHardwareDetailsByIdQueryValidation,
|
||||
getUptimeDetailsByIdParamValidation,
|
||||
@@ -18,13 +19,15 @@ import {
|
||||
bulkPauseMonitorBodyValidation,
|
||||
} from "@/api/validation/monitorValidation.js";
|
||||
import sslChecker from "ssl-checker";
|
||||
import { fetchMonitorCertificate, requireTeamId, requireUserId } from "@/api/controllers/controllerUtils.js";
|
||||
import * as whoiser from "whoiser";
|
||||
import { fetchMonitorCertificate, fetchMonitorDomain, requireTeamId, requireUserId } from "@/api/controllers/controllerUtils.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
import { IMonitorService } from "@/domain/monitors/monitor.service.js";
|
||||
import { INotificationsService } from "@/domain/notifications/notification.service.js";
|
||||
|
||||
export interface IMonitorController {
|
||||
getMonitorCertificate: RequestHandler;
|
||||
getMonitorDomain: RequestHandler;
|
||||
getUptimeDetailsById: RequestHandler;
|
||||
getHardwareDetailsById: RequestHandler;
|
||||
getPageSpeedDetailsById: RequestHandler;
|
||||
@@ -68,6 +71,22 @@ class MonitorController implements IMonitorController {
|
||||
});
|
||||
});
|
||||
|
||||
getMonitorDomain = catchAsync(async (req: Request, res: Response) => {
|
||||
const validatedParams = getDomainParamValidation.parse(req.params);
|
||||
const teamId = requireTeamId(req.user?.teamId);
|
||||
const monitor = await this.monitorService.getMonitorById({ teamId, monitorId: validatedParams.monitorId });
|
||||
const domainExpiry = await fetchMonitorDomain(whoiser, monitor);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
msg: "Domain expiry retrieved successfully",
|
||||
data: {
|
||||
domain: domainExpiry.domain,
|
||||
expiryDate: domainExpiry.expiryDate === null ? null : new Date(domainExpiry.expiryDate),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
getUptimeDetailsById = catchAsync(async (req: Request, res: Response) => {
|
||||
const validatedParams = getUptimeDetailsByIdParamValidation.parse(req.params);
|
||||
const validatedQuery = getUptimeDetailsByIdQueryValidation.parse(req.query);
|
||||
|
||||
@@ -29,6 +29,9 @@ export const createMonitorRoutes = (monitorController: IMonitorController): Rout
|
||||
router.get("/certificate/:monitorId", (req, res, next) => {
|
||||
monitorController.getMonitorCertificate(req, res, next);
|
||||
});
|
||||
router.get("/domain/:monitorId", (req, res, next) => {
|
||||
monitorController.getMonitorDomain(req, res, next);
|
||||
});
|
||||
|
||||
// General monitor CRUD routes
|
||||
router.patch("/notifications", isAllowed(["admin", "superadmin"]), monitorController.updateNotifications);
|
||||
|
||||
@@ -52,6 +52,10 @@ export const getCertificateParamValidation = z.object({
|
||||
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||
});
|
||||
|
||||
export const getDomainParamValidation = z.object({
|
||||
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||
});
|
||||
|
||||
const refineDnsHostname = (body: { type?: string; url?: string }, ctx: z.RefinementCtx) => {
|
||||
if (body.type === "dns" && body.url && !dnsHostnameRegex.test(body.url)) {
|
||||
ctx.addIssue({
|
||||
|
||||
Reference in new issue
Block a user