mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 14:47:14 +01:00
Added stripPort function. Reorganized constants.
Some checks are pending
Node.js CI / build (postgresql, 18.18) (push) Waiting to run
Some checks are pending
Node.js CI / build (postgresql, 18.18) (push) Waiting to run
This commit is contained in:
parent
04c06443a8
commit
a4a9d6c227
2 changed files with 66 additions and 56 deletions
|
|
@ -3,18 +3,57 @@ import { browserName, detectOS } from 'detect-browser';
|
|||
import isLocalhost from 'is-localhost-ip';
|
||||
import ipaddr from 'ipaddr.js';
|
||||
import maxmind from 'maxmind';
|
||||
import {
|
||||
DESKTOP_OS,
|
||||
DESKTOP_SCREEN_WIDTH,
|
||||
IP_ADDRESS_HEADERS,
|
||||
LAPTOP_SCREEN_WIDTH,
|
||||
MOBILE_OS,
|
||||
MOBILE_SCREEN_WIDTH,
|
||||
} from './constants';
|
||||
import { safeDecodeURIComponent } from '@/lib/url';
|
||||
|
||||
const MAXMIND = 'maxmind';
|
||||
|
||||
export const DESKTOP_OS = [
|
||||
'BeOS',
|
||||
'Chrome OS',
|
||||
'Linux',
|
||||
'Mac OS',
|
||||
'Open BSD',
|
||||
'OS/2',
|
||||
'QNX',
|
||||
'Sun OS',
|
||||
'Windows 10',
|
||||
'Windows 2000',
|
||||
'Windows 3.11',
|
||||
'Windows 7',
|
||||
'Windows 8',
|
||||
'Windows 8.1',
|
||||
'Windows 95',
|
||||
'Windows 98',
|
||||
'Windows ME',
|
||||
'Windows Server 2003',
|
||||
'Windows Vista',
|
||||
'Windows XP',
|
||||
];
|
||||
|
||||
export const MOBILE_OS = ['Amazon OS', 'Android OS', 'BlackBerry OS', 'iOS', 'Windows Mobile'];
|
||||
|
||||
export const DESKTOP_SCREEN_WIDTH = 1920;
|
||||
export const LAPTOP_SCREEN_WIDTH = 1024;
|
||||
export const MOBILE_SCREEN_WIDTH = 479;
|
||||
|
||||
// The order here is important and influences how IPs are detected by lib/detect.ts
|
||||
// Please do not change the order unless you know exactly what you're doing - read https://developers.cloudflare.com/fundamentals/reference/http-headers/
|
||||
export const IP_ADDRESS_HEADERS = [
|
||||
'x-client-ip',
|
||||
'x-forwarded-for',
|
||||
'cf-connecting-ip', // This should be *after* x-forwarded-for, so that x-forwarded-for is respected if present
|
||||
'do-connecting-ip',
|
||||
'fastly-client-ip',
|
||||
'true-client-ip',
|
||||
'x-real-ip',
|
||||
'x-cluster-client-ip',
|
||||
'x-forwarded',
|
||||
'forwarded',
|
||||
'x-appengine-user-ip',
|
||||
'x-nf-client-connection-ip',
|
||||
'x-real-ip',
|
||||
];
|
||||
|
||||
const PROVIDER_HEADERS = [
|
||||
// Cloudflare headers
|
||||
{
|
||||
|
|
@ -36,6 +75,24 @@ const PROVIDER_HEADERS = [
|
|||
},
|
||||
];
|
||||
|
||||
function stripPort(ip) {
|
||||
if (ip.startsWith('[')) {
|
||||
const endBracket = ip.indexOf(']');
|
||||
if (endBracket !== -1) {
|
||||
return ip.slice(0, endBracket + 1);
|
||||
}
|
||||
}
|
||||
|
||||
const idx = ip.lastIndexOf(':');
|
||||
if (idx !== -1) {
|
||||
if (ip.includes('.') || /^[a-zA-Z0-9.-]+$/.test(ip.slice(0, idx))) {
|
||||
return ip.slice(0, idx);
|
||||
}
|
||||
}
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
export function getIpAddress(headers: Headers) {
|
||||
const customHeader = process.env.CLIENT_IP_HEADER;
|
||||
|
||||
|
|
@ -140,7 +197,7 @@ export async function getLocation(ip: string = '', headers: Headers, hasPayloadI
|
|||
);
|
||||
}
|
||||
|
||||
const result = globalThis[MAXMIND]?.get(ip?.split(':')[0]);
|
||||
const result = globalThis[MAXMIND]?.get(stripPort(ip));
|
||||
|
||||
if (result) {
|
||||
const country = result.country?.iso_code ?? result?.registered_country?.iso_code;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue