Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Support mmdb geolocation database for selfhosted

Blue Mouse518aba2e1cad
diff --git a/backend/.env.example b/backend/.env.example
index f39c59262..c10c42c52 100644
--- a/backend/.env.example
+++ b/backend/.env.example
@@ -46,6 +46,7 @@ [email protected]
PASSWORD=12345678
API_KEY=
DEBUG_MODE=
+IP_GEOLOCATION_DB_PATH=
# Swetrix CDN for storing files
CDN_URL=http://localhost:5006
diff --git a/backend/apps/production/src/common/utils.ts b/backend/apps/production/src/common/utils.ts
index 673e73b68..3ca386bb3 100644
--- a/backend/apps/production/src/common/utils.ts
+++ b/backend/apps/production/src/common/utils.ts
@@ -190,7 +190,11 @@ const dummyLookup = () => ({
],
})
-const GEOIP_DB_PATH = path.join(__dirname, '../../../..', 'dbip-city-lite.mmdb')
+const DEVELOPMENT_GEOIP_DB_PATH = path.join(
+ __dirname,
+ '../../../..',
+ 'dbip-city-lite.mmdb',
+)
const PRODUCTION_GEOIP_DB_PATH = path.join(
__dirname,
'../..',
@@ -206,8 +210,8 @@ let lookup: Reader<CityResponse> = {
if (fs.existsSync(PRODUCTION_GEOIP_DB_PATH)) {
const buffer = fs.readFileSync(PRODUCTION_GEOIP_DB_PATH)
lookup = new Reader<CityResponse>(buffer)
-} else if (fs.existsSync(GEOIP_DB_PATH)) {
- const buffer = fs.readFileSync(GEOIP_DB_PATH)
+} else if (fs.existsSync(DEVELOPMENT_GEOIP_DB_PATH)) {
+ const buffer = fs.readFileSync(DEVELOPMENT_GEOIP_DB_PATH)
lookup = new Reader<CityResponse>(buffer)
}
diff --git a/backend/apps/selfhosted/src/common/constants.ts b/backend/apps/selfhosted/src/common/constants.ts
index e633276cd..11150f661 100644
--- a/backend/apps/selfhosted/src/common/constants.ts
+++ b/backend/apps/selfhosted/src/common/constants.ts
@@ -33,6 +33,7 @@ const SELFHOSTED_EMAIL = process.env.EMAIL
const SELFHOSTED_PASSWORD = process.env.PASSWORD
const SELFHOSTED_API_KEY = process.env.API_KEY
const SELFHOSTED_API_AUTH_ENABLED = !!SELFHOSTED_API_KEY
+const SELFHOSTED_GEOIP_DB_PATH = process.env.IP_GEOLOCATION_DB_PATH
const UUIDV5_NAMESPACE = '912c64c1-73fd-42b6-859f-785f839a9f68'
const DEFAULT_SELFHOSTED_UUID = 'deadbeef-dead-beef-dead-beefdeadbeef'
@@ -134,4 +135,5 @@ export {
ERROR_COLUMNS,
PID_REGEX,
ALL_COLUMNS,
+ SELFHOSTED_GEOIP_DB_PATH,
}
diff --git a/backend/apps/selfhosted/src/common/utils.ts b/backend/apps/selfhosted/src/common/utils.ts
index 7f2881ef1..1e1316d0f 100644
--- a/backend/apps/selfhosted/src/common/utils.ts
+++ b/backend/apps/selfhosted/src/common/utils.ts
@@ -1,5 +1,8 @@
import { NotFoundException, HttpException } from '@nestjs/common'
import { xxh3 } from '@node-rs/xxhash'
+import path from 'node:path'
+import fs from 'node:fs'
+import { CityResponse, Reader } from 'maxmind'
import timezones from 'countries-and-timezones'
import { v5 as uuidv5 } from 'uuid'
import _join from 'lodash/join'
@@ -25,6 +28,7 @@ import {
UUIDV5_NAMESPACE,
isDevelopment,
isProxiedByCloudflare,
+ SELFHOSTED_GEOIP_DB_PATH,
} from './constants'
import { clickhouse } from './integrations/clickhouse'
import { DEFAULT_TIMEZONE, TimeFormat } from '../user/entities/user.entity'
@@ -691,6 +695,54 @@ const getSelfhostedUUID = (): string => {
}
}
+const dummyLookup = () => ({
+ country: {
+ names: {
+ en: null,
+ },
+ },
+ city: {
+ names: {
+ en: null,
+ },
+ },
+ subdivisions: [
+ {
+ names: {
+ en: null,
+ },
+ },
+ ],
+})
+
+const DEVELOPMENT_GEOIP_DB_PATH = path.join(
+ __dirname,
+ '../../../..',
+ 'dbip-city-lite.mmdb',
+)
+const PRODUCTION_GEOIP_DB_PATH = path.join(
+ __dirname,
+ '../..',
+ 'dbip-city-lite.mmdb',
+)
+
+// eslint-disable-next-line
+let lookup: Reader<CityResponse> = {
+ // @ts-ignore
+ get: dummyLookup,
+}
+
+if (SELFHOSTED_GEOIP_DB_PATH && fs.existsSync(SELFHOSTED_GEOIP_DB_PATH)) {
+ const buffer = fs.readFileSync(SELFHOSTED_GEOIP_DB_PATH)
+ lookup = new Reader<CityResponse>(buffer)
+} else if (fs.existsSync(PRODUCTION_GEOIP_DB_PATH)) {
+ const buffer = fs.readFileSync(PRODUCTION_GEOIP_DB_PATH)
+ lookup = new Reader<CityResponse>(buffer)
+} else if (fs.existsSync(DEVELOPMENT_GEOIP_DB_PATH)) {
+ const buffer = fs.readFileSync(DEVELOPMENT_GEOIP_DB_PATH)
+ lookup = new Reader<CityResponse>(buffer)
+}
+
interface IPGeoDetails {
country: string | null
region: string | null
@@ -703,7 +755,20 @@ const getGeoDetails = (
headers?: unknown,
): IPGeoDetails => {
// Stage 1: Using IP address based geo lookup
- // TODO: Add support for DBIP for self-hosted
+ const data = lookup.get(ip)
+
+ const country = data?.country?.iso_code || null
+ // TODO: Add city overrides, for example, Colinton -> Edinburgh, etc.
+ const city = data?.city?.names?.en || null
+ const region = data?.subdivisions?.[0]?.names?.en || null
+
+ if (country) {
+ return {
+ country,
+ city,
+ region,
+ }
+ }
// Stage 2: If Cloudflare is enabled, use their headers
if (
diff --git a/backend/docker-compose-prod.yml b/backend/docker-compose-prod.yml
deleted file mode 100644
index 8993e4f9d..000000000
--- a/backend/docker-compose-prod.yml
+++ /dev/null
@@ -1,95 +0,0 @@
-version: '3.9'
-services:
- # This is the frontend, you can remove it if you want
- swetrix:
- image: swetrix/swetrix-fe:2.0.0
- restart: always
- depends_on:
- - swetrix-api
- ports:
- - '80:80'
- environment:
- - API_URL=http://localhost:8080/
- - REACT_APP_AIAPI_URL=https://aiapi.swetrix.com/
-
- # This is the analytics API
- swetrix-api:
- image: swetrix/swetrix-api:latest
- container_name: swetrix-api
- restart: always
- ports:
- - '8080:5005'
- environment:
- - TWO_FACTOR_AUTHENTICATION_APP_NAME=Swetrix
- - CLIENT_URL=https://swetrix.com
- - JWT_ACCESS_TOKEN_SECRET=jwt-access-token-secret
- - JWT_REFRESH_TOKEN_SECRET=jwt-refresh-token-secret
- - REDIS_HOST=redis
- - MYSQL_HOST=mariadb
- - CLICKHOUSE_HOST=http://clickhouse
- - API_ORIGINS=
- - SMTP_HOST=localhost
- - SMTP_PORT=1025
- - SMTP_USER=smtp_user_name
- - SMTP_PASSWORD=smtp_password
- - CDN_URL=http://localhost:5006
- - CDN_ACCESS_TOKEN=SOME_SECRET_TOKEN
- - CAPTCHA_SALT=
- - GOOGLE_OAUTH2_CLIENT_ID=
- - GOOGLE_OAUTH2_CLIENT_SECRET=
- - GITHUB_OAUTH2_CLIENT_ID=
- - GITHUB_OAUTH2_CLIENT_SECRET=
- - ENABLE_INTEGRATIONS=true
- - ENABLE_TELEGRAM_INTEGRATION=true
- - TELEGRAM_BOT_TOKEN=
- - SENTRY_ENABLED=true
- - IS_PRIMARY_NODE=true
- - SENTRY_DSN=
- links:
- - 'redis'
- - 'mariadb'
- - 'clickhouse'
- depends_on:
- - 'redis'
- - 'mariadb'
- - 'clickhouse'
- redis:
- image: redis:7.2-alpine
- restart: always
- environment:
- - REDIS_PORT=6379
- - REDIS_USER=default
- - REDIS_PASSWORD=
- # volumes:
- # - '/opt/redis-volume:/data'
- mariadb:
- image: linuxserver/mariadb:alpine
- restart: always
- environment:
- - MYSQL_ROOT_PASSWORD=password
- - MYSQL_DATABASE=analytics
- - MYSQL_USER=swetrix
- - MYSQL_PASSWORD=password
- volumes:
- - swetrix-users-data:/var/lib/mysql
- clickhouse:
- image: clickhouse/clickhouse-server:23.8-alpine
- container_name: clickhouse
- environment:
- - CLICKHOUSE_DATABASE=analytics
- - CLICKHOUSE_USER=default
- - CLICKHOUSE_PORT=8123
- - CLICKHOUSE_PASSWORD=password
- restart: always
- volumes:
- - swetrix-events-data:/var/lib/clickhouse
- ulimits:
- nofile:
- soft: 262144
- hard: 262144
-volumes:
- swetrix-events-data:
- driver: local
- swetrix-users-data:
- driver: local
diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml
deleted file mode 100644
index adbcaffba..000000000
--- a/backend/docker-compose.yml
+++ /dev/null
@@ -1,68 +0,0 @@
-version: '3.9'
-services:
- # This is the frontend, you can remove it if you want
- swetrix:
- image: swetrix/swetrix-fe:2.0.0
- restart: always
- depends_on:
- - swetrix-api
- ports:
- - '80:80'
- environment:
- - API_URL=http://localhost:8080/
- - REACT_APP_AIAPI_URL=https://aiapi.swetrix.com/
- - SELFHOSTED=true
-
- # This is the analytics API
- swetrix-api:
- image: swetrix/swetrix-api:2.0.0
- restart: always
- container_name: swetrix-api
- ports:
- - '8080:5005'
- environment:
- - JWT_ACCESS_TOKEN_SECRET=jwt-access-token-secret
- - JWT_REFRESH_TOKEN_SECRET=jwt-refresh-token-secret
- - REDIS_HOST=redis
- - CLICKHOUSE_HOST=http://clickhouse
- - API_ORIGINS=
- - PASSWORD=12345678
- - CLOUDFLARE_PROXY_ENABLED=false
- links:
- - 'redis'
- - 'clickhouse'
- depends_on:
- - 'redis'
- - 'clickhouse'
- redis:
- image: redis:7.2-alpine
- restart: always
- environment:
- - REDIS_PORT=6379
- - REDIS_USER=default
- - REDIS_PASSWORD=
- # ports:
- # - 6379:6379
- # volumes:
- # - '/opt/redis-volume:/data'
- clickhouse:
- image: clickhouse/clickhouse-server:23.8-alpine
- container_name: clickhouse
- environment:
- - CLICKHOUSE_DATABASE=analytics
- - CLICKHOUSE_USER=default
- - CLICKHOUSE_PORT=8123
- - CLICKHOUSE_PASSWORD=
- # ports:
- # - 8123:8123
- restart: always
- volumes:
- - swetrix-events-data:/var/lib/clickhouse
- ulimits:
- nofile:
- soft: 262144
- hard: 262144
-volumes:
- swetrix-events-data:
- driver: local