mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 07:37:11 +01:00
Merge pull request #3597 from badmike/feat/cloudfront-location-headers
feat: Add AWS CloudFront geolocation headers support
This commit is contained in:
commit
d972765760
1 changed files with 33 additions and 23 deletions
|
|
@ -15,6 +15,27 @@ import { safeDecodeURIComponent } from '@/lib/url';
|
||||||
|
|
||||||
const MAXMIND = 'maxmind';
|
const MAXMIND = 'maxmind';
|
||||||
|
|
||||||
|
const PROVIDER_HEADERS = [
|
||||||
|
// Cloudflare headers
|
||||||
|
{
|
||||||
|
countryHeader: 'cf-ipcountry',
|
||||||
|
regionHeader: 'cf-region-code',
|
||||||
|
cityHeader: 'cf-ipcity',
|
||||||
|
},
|
||||||
|
// Vercel headers
|
||||||
|
{
|
||||||
|
countryHeader: 'x-vercel-ip-country',
|
||||||
|
regionHeader: 'x-vercel-ip-country-region',
|
||||||
|
cityHeader: 'x-vercel-ip-city',
|
||||||
|
},
|
||||||
|
// CloudFront headers
|
||||||
|
{
|
||||||
|
countryHeader: 'cloudfront-viewer-country',
|
||||||
|
regionHeader: 'cloudfront-viewer-country-region',
|
||||||
|
cityHeader: 'cloudfront-viewer-city',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
export function getIpAddress(headers: Headers) {
|
export function getIpAddress(headers: Headers) {
|
||||||
const customHeader = process.env.CLIENT_IP_HEADER;
|
const customHeader = process.env.CLIENT_IP_HEADER;
|
||||||
|
|
||||||
|
|
@ -94,30 +115,19 @@ export async function getLocation(ip: string = '', headers: Headers, hasPayloadI
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasPayloadIP && !process.env.SKIP_LOCATION_HEADERS) {
|
if (!hasPayloadIP && !process.env.SKIP_LOCATION_HEADERS) {
|
||||||
// Cloudflare headers
|
for (const provider of PROVIDER_HEADERS) {
|
||||||
if (headers.get('cf-ipcountry')) {
|
const countryHeader = headers.get(provider.countryHeader);
|
||||||
const country = decodeHeader(headers.get('cf-ipcountry'));
|
if (countryHeader) {
|
||||||
const region = decodeHeader(headers.get('cf-region-code'));
|
const country = decodeHeader(countryHeader);
|
||||||
const city = decodeHeader(headers.get('cf-ipcity'));
|
const region = decodeHeader(headers.get(provider.regionHeader));
|
||||||
|
const city = decodeHeader(headers.get(provider.cityHeader));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
country,
|
country,
|
||||||
region: getRegionCode(country, region),
|
region: getRegionCode(country, region),
|
||||||
city,
|
city,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vercel headers
|
|
||||||
if (headers.get('x-vercel-ip-country')) {
|
|
||||||
const country = decodeHeader(headers.get('x-vercel-ip-country'));
|
|
||||||
const region = decodeHeader(headers.get('x-vercel-ip-country-region'));
|
|
||||||
const city = decodeHeader(headers.get('x-vercel-ip-city'));
|
|
||||||
|
|
||||||
return {
|
|
||||||
country,
|
|
||||||
region: getRegionCode(country, region),
|
|
||||||
city,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue