Support Cloudflare headers for city and region.

This commit is contained in:
Mike Cao 2023-08-06 22:52:58 -07:00
parent f48720c915
commit f57fbe6ba1
3 changed files with 12 additions and 13 deletions

View file

@ -3,6 +3,7 @@ import { getClientIp } from 'request-ip';
import { browserName, detectOS } from 'detect-browser';
import isLocalhost from 'is-localhost-ip';
import maxmind from 'maxmind';
import { safeDecodeURIComponent } from 'next-basics';
import {
DESKTOP_OS,
@ -65,20 +66,18 @@ export async function getLocation(ip, req) {
// Cloudflare headers
if (req.headers['cf-ipcountry']) {
return {
country: req.headers['cf-ipcountry'],
country: safeDecodeURIComponent(req.headers['cf-ipcountry']),
subdivision1: safeDecodeURIComponent(req.headers['cf-region-code']),
city: safeDecodeURIComponent(req.headers['cf-ipcity']),
};
}
// Vercel headers
if (req.headers['x-vercel-ip-country']) {
const country = req.headers['x-vercel-ip-country'];
const region = req.headers['x-vercel-ip-country-region'];
const city = req.headers['x-vercel-ip-city'];
return {
country,
subdivision1: region,
city: city ? decodeURIComponent(city) : undefined,
country: safeDecodeURIComponent(req.headers['x-vercel-ip-country']),
subdivision1: safeDecodeURIComponent(req.headers['x-vercel-ip-country-region']),
city: safeDecodeURIComponent(req.headers['x-vercel-ip-city']),
};
}