Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "media-streamer",
"version": "0.6.0",
"version": "0.6.1",
"private": true,
"type": "module",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/app/i/[code]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe('/i/<code>', () => {
);
});

it('answers with the public host, not the container address behind nginx', async () => {
const res = await GET(
new NextRequest('http://localhost:3000/i/ABCDEFGHJK', { headers: { host: 'bittorrented.com' } }),
{ params: Promise.resolve({ code: 'ABCDEFGHJK' }) }
);
expect(res.headers.get('location')).toBe('https://bittorrented.com/signup?invite=ABCDE-FGHJK');
});

it('sends a malformed code to the invite-only screen', async () => {
expect((await go('/i/nope')).location).toBe('https://bittorrented.com/invite-only');
});
Expand Down
9 changes: 8 additions & 1 deletion src/app/i/[code]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ import { formatInviteCode, isInviteCodeShape, normalizeInviteCode } from '@/lib/

export const dynamic = 'force-dynamic';

/** The public origin: behind nginx, request.url is the container's localhost:3000. */
function origin(request: NextRequest): string {
const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host') ?? request.nextUrl.host;
const proto = host.startsWith('localhost') || host.startsWith('127.') ? 'http' : 'https';
return `${proto}://${host}`;
}

export async function GET(request: NextRequest, { params }: { params: Promise<{ code: string }> }): Promise<NextResponse> {
const { code: raw } = await params;
const code = normalizeInviteCode(decodeURIComponent(raw ?? ''));
const target = new URL(isInviteCodeShape(code) ? '/signup' : '/invite-only', request.url);
const target = new URL(isInviteCodeShape(code) ? '/signup' : '/invite-only', origin(request));
if (isInviteCodeShape(code)) target.searchParams.set('invite', formatInviteCode(code));
const ref = request.nextUrl.searchParams.get('ref');
if (ref && /^[a-zA-Z0-9_-]{1,64}$/.test(ref)) target.searchParams.set('ref', ref);
Expand Down
Loading