web search done right
make sure correct address is used when frontend is behind nginx
Mikkel Denker0f3de6c0dc50
diff --git a/frontend/src/routes/search/+page.server.ts b/frontend/src/routes/search/+page.server.tsindex f71be3c4..0dba4fc3 100644--- a/frontend/src/routes/search/+page.server.ts+++ b/frontend/src/routes/search/+page.server.ts@@ -3,15 +3,17 @@ import { extractSearchParams, type SearchParams } from '$lib/search';import { redirect } from '@sveltejs/kit';import type { Actions, PageServerLoadEvent } from './$types';-export const load = async ({ locals, getClientAddress, url }: PageServerLoadEvent) => {+export const load = async ({ locals, getClientAddress, url, request }: PageServerLoadEvent) => {const searchParams: SearchParams | undefined =(locals['form'] && extractSearchParams(locals['form'])) || undefined;- if (await shouldShowCaptcha(getClientAddress())) {+ const clientAddress = request.headers.get('x-real-ip') || getClientAddress();++ if (await shouldShowCaptcha(clientAddress)) {return redirect(302, `/sorry?redirectTo=${encodeURIComponent(url.toString())}`);}- return { form: searchParams, clientAddress: getClientAddress() };+ return { form: searchParams, clientAddress };};export const actions: Actions = {diff --git a/frontend/src/routes/sorry/+page.server.ts b/frontend/src/routes/sorry/+page.server.tsindex 867dfeaa..1694f405 100644--- a/frontend/src/routes/sorry/+page.server.ts+++ b/frontend/src/routes/sorry/+page.server.ts@@ -16,9 +16,10 @@ const patchSelections = (searchParams: URLSearchParams): number[] => {return res;};-export const load = async ({ url, getClientAddress }: PageServerLoadEvent) => {+export const load = async ({ url, getClientAddress, request }: PageServerLoadEvent) => {const challenge = patchSelections(url.searchParams);- await checkCaptcha(url.searchParams, challenge, getClientAddress);+ const clientAddress = request.headers.get('x-real-ip') || getClientAddress();+ await checkCaptcha(url.searchParams, challenge, clientAddress);const captcha = await generateImage();const imgBase64 = await captcha.image.getBase64Async('image/png');diff --git a/frontend/src/routes/sorry/audio/+page.server.ts b/frontend/src/routes/sorry/audio/+page.server.tsindex 55b22f82..8e973815 100644--- a/frontend/src/routes/sorry/audio/+page.server.ts+++ b/frontend/src/routes/sorry/audio/+page.server.ts@@ -13,10 +13,11 @@ const extractChallenge = (searchParams: URLSearchParams): number[] => {return challenge;};-export const load = async ({ url, getClientAddress }: PageServerLoadEvent) => {+export const load = async ({ url, getClientAddress, request }: PageServerLoadEvent) => {const challenge = extractChallenge(url.searchParams);- await checkCaptcha(url.searchParams, challenge, getClientAddress);+ const clientAddress = request.headers.get('x-real-ip') || getClientAddress();+ await checkCaptcha(url.searchParams, challenge, clientAddress);const captcha = await generateAudio();diff --git a/frontend/src/routes/sorry/common.ts b/frontend/src/routes/sorry/common.tsindex 0b5639a3..09b9cebc 100644--- a/frontend/src/routes/sorry/common.ts+++ b/frontend/src/routes/sorry/common.ts@@ -5,13 +5,13 @@ import { verifyDigest } from '$lib/captcha/lib';export const checkCaptcha = async (searchParams: URLSearchParams,challenge: number[],- getClientAddress: () => string,+ clientAddress: string,) => {const digest = searchParams.get('digest');if (digest) {if (await verifyDigest(digest, challenge)) {- await allow(getClientAddress());+ await allow(clientAddress);let redirectTo = '/';try {const redirectToUrl = new URL(searchParams.get('redirectTo') || '/');