From a97b43e1c4e32ba4078b863dd9b1a1f22db16ba8 Mon Sep 17 00:00:00 2001 From: mufeng Date: Sat, 4 Jan 2025 08:29:19 +0800 Subject: [PATCH] Allow send custom location from server --- src/lib/detect.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib/detect.ts b/src/lib/detect.ts index c3ce6feed..bc31b262d 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -81,6 +81,23 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) { return; } + const customHeader = String(process.env.CLIENT_IPCOUNTRY_HEADER).toLowerCase(); + + if (customHeader !== 'undefined' && req.headers[customHeader]) { + const reginCodeHeader = String(process.env.CLIENT_REGION_CODE_HEADER).toLowerCase(); + const cityHeader = String(process.env.CLIENT_CITY_HEADER).toLowerCase(); + + const country = req.headers[customHeader]; + const subdivision1 = req.headers[reginCodeHeader]; + const city = req.headers[cityHeader]; + + return { + country, + subdivision1: getRegionCode(country, subdivision1), + city, + }; + } + // Cloudflare headers if (req.headers['cf-ipcountry']) { const country = safeDecodeCfHeader(req.headers['cf-ipcountry']);