🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Implement site ID resolution and URL rewriting
Bill Yangf4fdd16b4ad5
diff --git a/server/src/index.ts b/server/src/index.tsindex 6ebd35bb3..44e7df9ad 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -81,7 +81,7 @@ import { handleIdentify } from "./services/tracker/identifyService.js";// need to import telemetry service here to start itimport { telemetryService } from "./services/telemetryService.js";import { weeklyReportService } from "./services/weekyReports/weeklyReportService.js";-import { extractSiteId } from "./utils.js";+import { extractSiteId, replacePathSiteId, resolveNumericSiteId } from "./utils.js";import { getTrackingConfig } from "./api/sites/getTrackingConfig.js";import { updateSitePrivateLinkConfig } from "./api/sites/updateSitePrivateLinkConfig.js";import { getSitePrivateLinkConfig } from "./api/sites/getSitePrivateLinkConfig.js";@@ -281,8 +281,24 @@ server.addHook("onRequest", async (request, reply) => {const siteId = extractSiteId(processedUrl);if (siteId) {+ // Convert string ID to numeric ID if needed+ let resolvedSiteId = siteId;+ if (!/^\d+$/.test(siteId)) {+ const numericSiteId = await resolveNumericSiteId(siteId);+ if (numericSiteId) {+ // Rewrite the URL with the numeric ID+ const newUrl = replacePathSiteId(processedUrl, numericSiteId);+ request.raw.url = newUrl;+ processedUrl = newUrl;+ resolvedSiteId = String(numericSiteId);+ } else {+ // String ID not found in database+ return reply.status(404).send({ error: "Site not found" });+ }+ }+// Check all access methods: direct access, public site, or valid private key- const hasAccess = await getUserHasAccessToSitePublic(request, siteId);+ const hasAccess = await getUserHasAccessToSitePublic(request, resolvedSiteId);if (hasAccess) {// User has access via: direct access, public site, or valid private keydiff --git a/server/src/utils.ts b/server/src/utils.tsindex eefffaae3..192183d46 100644--- a/server/src/utils.ts+++ b/server/src/utils.ts@@ -1,5 +1,8 @@+import { eq } from "drizzle-orm";import { FastifyRequest } from "fastify";import * as psl from "psl";+import { db } from "./db/postgres/postgres.js";+import { sites } from "./db/postgres/schema.js";const desktopOS = new Set(["AIX",@@ -136,6 +139,56 @@ export const extractSiteId = (path: string) => {return null;};+// Cache for string ID to numeric ID lookups to avoid repeated DB queries+const siteIdCache = new Map<string, number>();++// Resolve a site identifier (string ID or numeric ID) to its numeric siteId+// Returns the numeric siteId or null if not found+export const resolveNumericSiteId = async (siteIdentifier: string): Promise<number | null> => {+ // If it's already numeric, return it+ if (/^\d+$/.test(siteIdentifier)) {+ return parseInt(siteIdentifier, 10);+ }++ // Check cache first+ if (siteIdCache.has(siteIdentifier)) {+ return siteIdCache.get(siteIdentifier)!;+ }++ // Look up the string ID in the database+ try {+ const site = await db+ .select({ siteId: sites.siteId })+ .from(sites)+ .where(eq(sites.id, siteIdentifier))+ .limit(1);++ if (site.length > 0) {+ const numericId = site[0].siteId;+ // Cache the result+ siteIdCache.set(siteIdentifier, numericId);+ return numericId;+ }+ } catch (error) {+ console.error("Error resolving site ID:", error);+ }++ return null;+};++// Replace site ID in URL path with numeric ID+export const replacePathSiteId = (path: string, numericId: number): string => {+ const [pathPart, queryPart] = path.split("?");+ const segments = pathPart.split("/");++ // Replace the last segment (which is the site ID)+ if (segments.length >= 2) {+ segments[segments.length - 1] = String(numericId);+ }++ return queryPart ? `${segments.join("/")}?${queryPart}` : segments.join("/");+};+// Normalizes a domain/hostname by removing all subdomain prefixes.// Accepts either a full URL or just a hostname.export const normalizeOrigin = (input: string): string => {