From 4a752a834b349b4aa8841cc5b3cab09854a5dca3 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 27 Jan 2026 10:42:54 -0800 Subject: forward ip to backend --- src/lib/utils.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/lib/utils.ts') diff --git a/src/lib/utils.ts b/src/lib/utils.ts index f220ed45..1b2bdfba 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -21,11 +21,23 @@ export function scroll(section: any) { const base = browser ? "" : PUBLIC_COINOS_URL; +let ipStore: any; +if (!browser) { + import("$lib/server/ip").then((m) => { + ipStore = m.ipStore; + }); +} + +const ipHeaders = (): Record => { + const ip = ipStore?.getStore(); + return ip ? { "cf-connecting-ip": ip } : {}; +}; + export const punk = (k: string) => `${Math.floor((parseInt(k.slice(-2), 16) / 256) * 64) + 1}.webp`; export const g = (url: string, fetch: any, headers: object) => - fetch(base + url, { headers }) + fetch(base + url, { headers: { ...ipHeaders(), ...headers } }) .then((r: Response) => r.text()) .then((body: string) => { try { @@ -36,7 +48,7 @@ export const g = (url: string, fetch: any, headers: object) => }); export const get = (url: string, headers = {}) => { - return fetch(base + url, { headers }) + return fetch(base + url, { headers: { ...ipHeaders(), ...headers } }) .then(async (r) => { if (r.ok) return r.text(); throw new Error(await r.text()); @@ -58,6 +70,7 @@ export const post = async ( headers = { "content-type": "application/json", accept: "application/json", + ...ipHeaders(), ...headers, }; -- cgit v1.2.3