From a98f2d9c7cfb2c09c8e3c1c02581d11dabd7fd4e Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 11 Oct 2022 23:10:47 +0100 Subject: [PATCH] feat: use api route and redis cache for finding tenant --- src/middleware.ts | 17 ++++++---- src/pages/api/host2handle.ts | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 src/pages/api/host2handle.ts diff --git a/src/middleware.ts b/src/middleware.ts index ad68c15b..5c2f7fe8 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -15,11 +15,6 @@ const ALWAYS_REPLAY_ROUTES = [ export default async function middleware(req: NextRequest) { const { pathname } = req.nextUrl - console.log( - `x-forwarded-proto: ${req.headers.get( - "x-forwarded-proto", - )}, cf-visitor: ${req.headers.get("cf-visitor")}`, - ) if (IS_PROD && req.headers.get("x-forwarded-proto") !== "https") { let cfHttps = false try { @@ -50,7 +45,17 @@ export default async function middleware(req: NextRequest) { return NextResponse.next() } - const tenant = await getTenant(req, req.nextUrl.searchParams) + let tenant: { + subdomain?: string + redirect?: string + } = {} + try { + tenant = await ( + await fetch( + new URL(`/api/host2handle?host=${req.headers.get("host")}`, req.url), + ) + ).json() + } catch (error) {} if (tenant?.redirect && IS_PROD) { return NextResponse.redirect( diff --git a/src/pages/api/host2handle.ts b/src/pages/api/host2handle.ts new file mode 100644 index 00000000..bf556567 --- /dev/null +++ b/src/pages/api/host2handle.ts @@ -0,0 +1,63 @@ +import { NextApiRequest, NextApiResponse } from "next" +import { OUR_DOMAIN } from "~/lib/env" +import { cacheGet } from "~/lib/redis.server" + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse, +) { + let { host } = req.query + + let realHost: string + if (Array.isArray(host)) { + realHost = host[0] + } else { + realHost = host || "" + } + let result = {} + + const OUR_DOMAIN_SUFFIX = `.${OUR_DOMAIN}` + if (realHost && realHost !== OUR_DOMAIN) { + result = await cacheGet(["host2handle", realHost], async () => { + if (realHost.endsWith(OUR_DOMAIN_SUFFIX)) { + const subdomain = realHost.replace(OUR_DOMAIN_SUFFIX, "") + const res = await fetch( + `https://indexer.crossbell.io/v1/handles/${subdomain}/character`, + ) + const char = await res.json() + const customDomain = + char?.metadata?.content?.attributes?.find( + (a: any) => a.trait_type === "xlog_custom_domain", + )?.value || "" + if (customDomain) { + return { + redirect: /^https?:\/\//.test(customDomain) + ? customDomain + : `https://${customDomain}`, + subdomain: subdomain, + } + } else { + return { + subdomain: subdomain, + } + } + } else { + const res = await fetch( + `https://cloudflare-dns.com/dns-query?name=_xlog-challenge.${realHost}&type=TXT`, + { + headers: { + accept: "application/dns-json", + }, + }, + ) + const txt = await res.json() + const tenant = txt?.Answer?.[0]?.data.replace(/^"|"$/g, "") + return { + subdomain: tenant, + } + } + }) + } + + res.status(200).json(result) +}