Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Add x-umami-client-* headers as highest priority source for client detection.

Mike Caofbd056413323
diff --git a/src/lib/detect.ts b/src/lib/detect.ts
index 68cb66726..6af61b78f 100644
--- a/src/lib/detect.ts
+++ b/src/lib/detect.ts
@@ -10,6 +10,12 @@ import { safeDecodeURIComponent } from '@/lib/url';
const MAXMIND = 'maxmind';
const PROVIDER_HEADERS = [
+ // Umami custom headers
+ {
+ countryHeader: 'x-umami-client-country',
+ regionHeader: 'x-umami-client-region',
+ cityHeader: 'x-umami-client-city',
+ },
// Cloudflare headers
{
countryHeader: 'cf-ipcountry',
@@ -60,13 +66,13 @@ function decodeHeader(s: string | undefined | null): string | undefined | null {
return Buffer.from(s, 'latin1').toString('utf-8');
}
-export async function getLocation(ip: string = '', headers: Headers, hasPayloadIP: boolean) {
+export async function getLocation(ip: string = '', headers: Headers, skipHeaders: boolean) {
// Ignore local ips
if (!ip || (await isLocalhost(ip))) {
return null;
}
- if (!hasPayloadIP && !process.env.SKIP_LOCATION_HEADERS) {
+ if (!skipHeaders && !process.env.SKIP_LOCATION_HEADERS) {
for (const provider of PROVIDER_HEADERS) {
const countryHeader = headers.get(provider.countryHeader);
if (countryHeader) {
diff --git a/src/lib/ip.ts b/src/lib/ip.ts
index 5cd775746..b4c465736 100644
--- a/src/lib/ip.ts
+++ b/src/lib/ip.ts
@@ -1,4 +1,5 @@
export const IP_ADDRESS_HEADERS = [
+ 'x-umami-client-ip', // Umami custom header
'true-client-ip', // CDN
'cf-connecting-ip', // Cloudflare
'fastly-client-ip', // Fastly
@@ -31,7 +32,7 @@ export function getIpAddress(headers: Headers) {
}
if (header === 'forwarded') {
- const match = ip.match(/for=(\[?[0-9a-fA-F:.]+\]?)/);
+ const match = ip.match(/for=(\[?[0-9a-fA-F:.]+]?)/);
if (match) {
return match[1];