Skip to content
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

Add subdomain support #52

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ if (config.ARNS_ROOT_HOST !== undefined) {

app.use(
createSandboxMiddleware({
rootHost: config.ARNS_ROOT_HOST,
sandboxProtocol: config.SANDBOX_PROTOCOL,
}),
);
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export const ANS104_INDEX_FILTER = createFilter(
JSON.parse(ANS104_INDEX_FILTER_STRING),
);
export const ARNS_ROOT_HOST = env.varOrUndefined('ARNS_ROOT_HOST');
export const ROOT_HOST_SUBDOMAIN_LENGTH =
ARNS_ROOT_HOST !== undefined ? ARNS_ROOT_HOST.split('.').length - 2 : 0;
export const SANDBOX_PROTOCOL = env.varOrUndefined('SANDBOX_PROTOCOL');
export const START_WRITERS =
env.varOrDefault('START_WRITERS', 'true') === 'true';
39 changes: 23 additions & 16 deletions src/middleware/arns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { Handler } from 'express';
import { asyncMiddleware } from 'middleware-async';

import * as config from '../config.js';
import { sendNotFound } from '../routes/data.js';
import { NameResolver } from '../types.js';

Expand All @@ -32,26 +33,32 @@ export const createArnsMiddleware = ({
}): Handler =>
asyncMiddleware(async (req, res, next) => {
if (
Array.isArray(req.subdomains) &&
req.subdomains.length === 1 &&
!EXCLUDED_SUBDOMAINS.has(req.subdomains[0]) &&
// Ignore subdomains that are part of the ArNS root hostname.
!Array.isArray(req.subdomains) ||
req.subdomains.length === config.ROOT_HOST_SUBDOMAIN_LENGTH
) {
next();
return;
}
const arnsSubdomain = req.subdomains[config.ROOT_HOST_SUBDOMAIN_LENGTH - 1];
if (
EXCLUDED_SUBDOMAINS.has(arnsSubdomain) ||
// Avoid collisions with sandbox URLs by ensuring the subdomain length
// is below the mininimum length of a sandbox subdomain. Undernames are
// are an exception because they can be longer and '_' cannot appear in
// base32.
(req.subdomains[0].length <= 48 || req.subdomains[0].match(/_/))
(arnsSubdomain.length > 48 && !arnsSubdomain.match(/_/))
djwhitt marked this conversation as resolved.
Show resolved Hide resolved
) {
const { resolvedId, ttl } = await nameResolver.resolve(req.subdomains[0]);
if (resolvedId !== undefined) {
res.header('X-ArNS-Resolved-Id', resolvedId);
res.header('X-ArNS-TTL-Seconds', ttl.toString());
res.header('Cache-Control', `public, max-age=${ttl}`);
dataHandler(req, res, next);
return;
} else {
sendNotFound(res);
return;
}
next();
return;
}
const { resolvedId, ttl } = await nameResolver.resolve(arnsSubdomain);
if (resolvedId === undefined) {
sendNotFound(res);
return;
}
next();
res.header('X-ArNS-Resolved-Id', resolvedId);
res.header('X-ArNS-TTL-Seconds', ttl.toString());
res.header('Cache-Control', `public, max-age=${ttl}`);
dataHandler(req, res, next);
});
11 changes: 5 additions & 6 deletions src/middleware/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import { Handler, Request } from 'express';
import url from 'node:url';
import { base32 } from 'rfc4648';

import * as config from '../config.js';
import { fromB64Url } from '../lib/encoding.js';

function getRequestSandbox(req: Request): string | undefined {
if (req.subdomains.length === 1) {
return req.subdomains[0];
if (req.subdomains.length > config.ROOT_HOST_SUBDOMAIN_LENGTH) {
return req.subdomains[req.subdomains.length - 1];
}
return undefined;
}
Expand All @@ -37,14 +38,12 @@ function sandboxFromId(id: string): string {
}

export function createSandboxMiddleware({
rootHost,
sandboxProtocol,
}: {
rootHost?: string;
sandboxProtocol?: string;
}): Handler {
return (req, res, next) => {
if (rootHost === undefined) {
if (config.ARNS_ROOT_HOST === undefined) {
next();
return;
}
Expand All @@ -63,7 +62,7 @@ export function createSandboxMiddleware({
const protocol = sandboxProtocol ?? (req.secure ? 'https' : 'http');
return res.redirect(
302,
`${protocol}://${idSandbox}.${rootHost}${path}?${queryString}`,
`${protocol}://${idSandbox}.${config.ARNS_ROOT_HOST}${path}?${queryString}`,
);
}

Expand Down