-
Notifications
You must be signed in to change notification settings - Fork 3k
Domain Connect #3877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pepeladeira
wants to merge
11
commits into
main
Choose a base branch
from
domain-connect-auto-configure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Domain Connect #3877
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
13503e0
domain-connect initial structure
pepeladeira 7e2ebd6
Merge branch 'main' into domain-connect-auto-configure
pepeladeira 339c2fc
simplify envs
pepeladeira e080edb
code improvements
pepeladeira 50887cf
code improvements
pepeladeira 6e8dd39
code improvements
pepeladeira fa0fd48
code improvements
pepeladeira 8e0b610
code improvements
pepeladeira afc0150
Merge branch 'main' into domain-connect-auto-configure
pepeladeira d3cb60c
Merge branch 'main' into domain-connect-auto-configure
pepeladeira aab041c
Merge branch 'main' into domain-connect-auto-configure
pepeladeira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
apps/web/app/api/domains/[domain]/domain-connect/apply/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { getConfigResponse } from "@/lib/api/domains/get-config-response"; | ||
| import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw"; | ||
| import { getDomainResponse } from "@/lib/api/domains/get-domain-response"; | ||
| import { DubApiError } from "@/lib/api/errors"; | ||
| import { withWorkspace } from "@/lib/auth"; | ||
| import { | ||
| DEFAULT_DC_SERVICE_APEX, | ||
| DEFAULT_DC_SERVICE_SUBDOMAIN, | ||
| } from "@/lib/domain-connect/constants"; | ||
| import { discoverDomainConnect } from "@/lib/domain-connect/discover"; | ||
| import { buildSignedApplyUrl } from "@/lib/domain-connect/sign-apply-url"; | ||
| import { APP_DOMAIN, getApexDomain, getSubdomain } from "@dub/utils"; | ||
| import { NextResponse } from "next/server"; | ||
| import * as z from "zod/v4"; | ||
|
|
||
| const bodySchema = z.object({ | ||
| recordType: z.enum(["A", "CNAME"]), | ||
| returnTo: z.string().max(512).optional(), | ||
| }); | ||
|
|
||
| // POST /api/domains/[domain]/domain-connect/apply | ||
| export const POST = withWorkspace( | ||
| async ({ req, workspace, params }) => { | ||
| const privateKeyPem = | ||
| process.env.DOMAIN_CONNECT_PRIVATE_KEY?.trim().replace(/\\n/g, "\n") || | ||
| null; | ||
| const keyHost = process.env.DOMAIN_CONNECT_KEY_HOST?.trim() || null; | ||
|
|
||
| if (!privateKeyPem || !keyHost) { | ||
| throw new DubApiError({ | ||
| code: "internal_server_error", | ||
| message: "Domain Connect signing is not configured.", | ||
| }); | ||
| } | ||
|
|
||
| const { slug: domain } = await getDomainOrThrow({ | ||
| workspace, | ||
| domain: params.domain, | ||
| dubDomainChecks: true, | ||
| }); | ||
|
|
||
| const body = bodySchema.parse(await req.json()); | ||
|
|
||
| const [domainJson, configJson] = await Promise.all([ | ||
| getDomainResponse(domain), | ||
| getConfigResponse(domain), | ||
| ]); | ||
|
|
||
| if (domainJson?.error?.code === "not_found" || domainJson?.error) { | ||
| throw new DubApiError({ | ||
| code: "bad_request", | ||
| message: "Domain is not available for configuration.", | ||
| }); | ||
| } | ||
|
|
||
| if (configJson?.conflicts?.length) { | ||
| throw new DubApiError({ | ||
| code: "bad_request", | ||
| message: "Remove conflicting DNS records first, then retry.", | ||
| }); | ||
| } | ||
|
|
||
| if (domainJson.verified && !configJson.misconfigured) { | ||
| throw new DubApiError({ | ||
| code: "bad_request", | ||
| message: "This domain is already configured correctly.", | ||
| }); | ||
| } | ||
|
|
||
| const apex = getApexDomain(`https://${domain}`); | ||
| const discovery = await discoverDomainConnect(apex); | ||
| if (!discovery) { | ||
| throw new DubApiError({ | ||
| code: "bad_request", | ||
| message: | ||
| "Auto configure is only available for Vercel or Cloudflare DNS zones.", | ||
| }); | ||
| } | ||
|
|
||
| const allowedSyncUXOrigins = [ | ||
| "https://vercel.com", | ||
| "https://dash.cloudflare.com", | ||
| ]; | ||
| const syncUXOrigin = new URL(discovery.urlSyncUX).origin; | ||
| if (!allowedSyncUXOrigins.includes(syncUXOrigin)) { | ||
| throw new DubApiError({ | ||
| code: "bad_request", | ||
| message: "Invalid Domain Connect provider URL.", | ||
| }); | ||
| } | ||
|
|
||
| const subdomain = getSubdomain( | ||
| domainJson.name?.toLowerCase() ?? domain, | ||
| domainJson.apexName?.toLowerCase() ?? apex, | ||
| ); | ||
| const isApex = body.recordType === "A" || !subdomain; | ||
| const serviceId = isApex | ||
| ? DEFAULT_DC_SERVICE_APEX | ||
| : DEFAULT_DC_SERVICE_SUBDOMAIN; | ||
|
|
||
| const returnPath = | ||
| body.returnTo && | ||
| body.returnTo.startsWith(`/${workspace.slug}/`) && | ||
| !body.returnTo.includes("://") | ||
| ? body.returnTo | ||
| : `/${workspace.slug}/settings/domains`; | ||
|
|
||
| const redirectUrl = new URL(returnPath, APP_DOMAIN); | ||
| redirectUrl.searchParams.set("domain_connect", "callback"); | ||
| const redirectUri = redirectUrl.toString(); | ||
|
|
||
| const queryParams: Record<string, string> = { | ||
| domain: apex, | ||
| groupId: "subdomain", | ||
| redirect_uri: redirectUri, | ||
| }; | ||
|
|
||
| if (isApex) { | ||
| queryParams.groupId = "apex"; | ||
| } else { | ||
| queryParams.groupId = "subdomain"; | ||
| queryParams.host = (subdomain ?? "www").toLowerCase(); | ||
| } | ||
|
|
||
| const txtVerification = domainJson.verification?.find( | ||
| (x: { type: string }) => x.type === "TXT", | ||
| ); | ||
| if (txtVerification) { | ||
| queryParams.groupId = queryParams.groupId + ",verification"; | ||
| const txtHostFqdn: string = txtVerification.domain?.toLowerCase() ?? ""; | ||
| const apexSuffix = `.${apex}`; | ||
| const txtHost = txtHostFqdn.endsWith(apexSuffix) | ||
| ? txtHostFqdn.slice(0, -apexSuffix.length) | ||
| : txtHostFqdn === apex | ||
| ? "@" | ||
| : txtHostFqdn; | ||
| queryParams.txtHost = txtHost; | ||
| queryParams.txtValue = txtVerification.value ?? ""; | ||
| } | ||
|
|
||
| const applyUrl = buildSignedApplyUrl({ | ||
| urlSyncUX: discovery.urlSyncUX, | ||
| serviceId, | ||
| privateKeyPem, | ||
| keyHost, | ||
| queryParams, | ||
| }); | ||
|
|
||
| return NextResponse.json({ applyUrl }); | ||
| }, | ||
| { | ||
| requiredPermissions: ["domains.write"], | ||
| }, | ||
| ); | ||
106 changes: 106 additions & 0 deletions
106
apps/web/app/api/domains/[domain]/forward-instructions/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw"; | ||
| import { getDomainResponse } from "@/lib/api/domains/get-domain-response"; | ||
| import { DubApiError } from "@/lib/api/errors"; | ||
| import { withWorkspace } from "@/lib/auth"; | ||
| import { | ||
| DUB_CUSTOM_DOMAIN_A_RECORD, | ||
| DUB_CUSTOM_DOMAIN_CNAME, | ||
| } from "@/lib/domain-connect/constants"; | ||
| import { ratelimit } from "@/lib/upstash"; | ||
| import { sendEmail } from "@dub/email"; | ||
| import DomainDnsInstructions from "@dub/email/templates/domain-dns-instructions"; | ||
| import { getApexDomain, getSubdomain } from "@dub/utils"; | ||
| import { NextResponse } from "next/server"; | ||
| import * as z from "zod/v4"; | ||
|
|
||
| const bodySchema = z.object({ | ||
| email: z.email(), | ||
| recordType: z.enum(["A", "CNAME"]), | ||
| }); | ||
|
|
||
| // POST /api/domains/[domain]/forward-instructions | ||
| export const POST = withWorkspace( | ||
| async ({ req, workspace, params, session }) => { | ||
| const { slug: domain } = await getDomainOrThrow({ | ||
| workspace, | ||
| domain: params.domain, | ||
| dubDomainChecks: true, | ||
| }); | ||
|
|
||
| const { email, recordType } = bodySchema.parse(await req.json()); | ||
|
|
||
| const { success } = await ratelimit(10, "1 h").limit( | ||
| `forward-dns-instructions:${workspace.id}`, | ||
| ); | ||
| if (!success) { | ||
| throw new DubApiError({ | ||
| code: "rate_limit_exceeded", | ||
| message: "Don't DDoS me pls 🥺", | ||
| }); | ||
| } | ||
|
|
||
| const records: { type: string; name: string; value: string }[] = []; | ||
|
|
||
| const domainJson = await getDomainResponse(domain); | ||
|
|
||
| if (domainJson?.error) { | ||
| throw new DubApiError({ | ||
| code: "bad_request", | ||
| message: "Could not retrieve DNS records for this domain.", | ||
| }); | ||
| } | ||
|
|
||
| const apex = getApexDomain(`https://${domain}`); | ||
| const subdomain = getSubdomain( | ||
| domainJson.name?.toLowerCase() ?? domain, | ||
| domainJson.apexName?.toLowerCase() ?? apex, | ||
| ); | ||
|
|
||
| if (recordType === "A") { | ||
| records.push({ | ||
| type: "A", | ||
| name: subdomain ?? "@", | ||
| value: DUB_CUSTOM_DOMAIN_A_RECORD, | ||
| }); | ||
| } else { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| records.push({ | ||
| type: "CNAME", | ||
| name: subdomain ?? "www", | ||
| value: DUB_CUSTOM_DOMAIN_CNAME, | ||
| }); | ||
| } | ||
|
|
||
| const txtVerification = domainJson.verification?.find( | ||
| (x: { type: string }) => x.type === "TXT", | ||
| ); | ||
| if (txtVerification) { | ||
| const txtHostFqdn: string = txtVerification.domain?.toLowerCase() ?? ""; | ||
| const apexSuffix = `.${apex}`; | ||
| const txtHost = txtHostFqdn.endsWith(apexSuffix) | ||
| ? txtHostFqdn.slice(0, -apexSuffix.length) | ||
| : txtHostFqdn === apex | ||
| ? "@" | ||
| : txtHostFqdn; | ||
| const txtValue = txtVerification.value?.trim() ?? ""; | ||
| if ((txtHost || txtHost === "@") && txtValue) { | ||
| records.push({ type: "TXT", name: txtHost, value: txtValue }); | ||
| } | ||
| } | ||
|
|
||
| await sendEmail({ | ||
| subject: `DNS instructions for ${domain}`, | ||
| to: email, | ||
| react: DomainDnsInstructions({ | ||
| email, | ||
| domain, | ||
| records, | ||
| senderEmail: session.user.email, | ||
| }), | ||
| }); | ||
|
|
||
| return NextResponse.json({ ok: true }); | ||
| }, | ||
| { | ||
| requiredPermissions: ["domains.read"], | ||
| }, | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.