📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
(improvement) Drop Cloudflare (#168)
Andrii Romasiunfa772ce1a939
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.tsindex 0a1ecd236..4c22b5783 100644--- a/apps/production/src/analytics/analytics.controller.ts+++ b/apps/production/src/analytics/analytics.controller.ts@@ -59,6 +59,7 @@ import {REDIS_SESSION_SALT_KEY,clickhouse,} from '../common/constants'+import { getGeoDetails } from '../common/utils'import { BotDetection } from '../common/decorators/bot-detection.decorator'import { BotDetectionGuard } from '../common/guards/bot-detection.guard'import { GetCustomEventsDto } from './dto/get-custom-events.dto'@@ -872,8 +873,7 @@ export class AnalyticsController {): Promise<any> {const { 'user-agent': userAgent, origin } = headers- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)@@ -899,18 +899,13 @@ export class AnalyticsController {city = 'NULL',region = 'NULL',country = 'NULL',- } = this.analyticsService.getGeoDetails(ip, eventsDTO.tz)+ } = getGeoDetails(ip, eventsDTO.tz)const ua = UAParser(userAgent)const dv = ua.device.type || 'desktop'const br = ua.browser.nameconst os = ua.os.name- // Using cf-ipcountry as a fallback. This is temporary until we stop using Cloudflare- const cc =- country ||- (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])-const dto = customLogDTO(eventsDTO.pid,eventsDTO.ev,@@ -923,7 +918,7 @@ export class AnalyticsController {eventsDTO.so,eventsDTO.me,eventsDTO.ca,- cc,+ country,region,city,)@@ -950,8 +945,7 @@ export class AnalyticsController {): Promise<any> {const { 'user-agent': userAgent } = headersconst { pid } = logDTO- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''const sessionID = await this.analyticsService.getSessionHash(pid,@@ -980,8 +974,7 @@ export class AnalyticsController {): Promise<any> {const { 'user-agent': userAgent, origin } = headers- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await this.analyticsService.validate(logDTO, origin, 'log', ip)@@ -1001,15 +994,11 @@ export class AnalyticsController {city = 'NULL',region = 'NULL',country = 'NULL',- } = this.analyticsService.getGeoDetails(ip, logDTO.tz)+ } = getGeoDetails(ip, logDTO.tz)const ua = UAParser(userAgent)const dv = ua.device.type || 'desktop'const br = ua.browser.nameconst os = ua.os.name- // Using cf-ipcountry as a fallback. This is temporary until we stop using Cloudflare- const cc =- country ||- (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])const dto = analyticsDTO(sessionHash,@@ -1024,7 +1013,7 @@ export class AnalyticsController {logDTO.so,logDTO.me,logDTO.ca,- cc,+ country,region,city,0,@@ -1050,7 +1039,7 @@ export class AnalyticsController {logDTO.pg,dv,br,- cc,+ country,region,city,dns,@@ -1105,8 +1094,7 @@ export class AnalyticsController {await this.analyticsService.validate(logDTO, origin)- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''const salt = await redis.get(REDIS_SESSION_SALT_KEY)const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)const unique = await this.analyticsService.isUnique(sessionHash)@@ -1117,17 +1105,12 @@ export class AnalyticsController {city = 'NULL',region = 'NULL',country = 'NULL',- } = this.analyticsService.getGeoDetails(ip)+ } = getGeoDetails(ip)const ua = UAParser(userAgent)const dv = ua.device.type || 'desktop'const br = ua.browser.nameconst os = ua.os.name- // Using cf-ipcountry as a fallback. This is temporary until we stop using Cloudflare- const cc =- country ||- (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])-const dto = analyticsDTO(sessionHash,logDTO.pid,@@ -1141,7 +1124,7 @@ export class AnalyticsController {'NULL','NULL','NULL',- cc,+ country,region,city,0,diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.tsindex 0821739a4..48a46b370 100644--- a/apps/production/src/analytics/analytics.service.ts+++ b/apps/production/src/analytics/analytics.service.ts@@ -15,7 +15,6 @@ import * as _now from 'lodash/now'import * as _values from 'lodash/values'import * as _round from 'lodash/round'import * as _filter from 'lodash/filter'-import timezones from 'countries-and-timezones'import * as dayjs from 'dayjs'import * as utc from 'dayjs/plugin/utc'import * as dayjsTimezone from 'dayjs/plugin/timezone'@@ -50,7 +49,6 @@ import {import {calculateRelativePercentage,millisecondsToSeconds,- lookup,} from '../common/utils'import { PageviewsDTO } from './dto/pageviews.dto'import { EventsDTO } from './dto/events.dto'@@ -258,12 +256,6 @@ const isValidOrigin = (origins: string[], origin: string) => {return false}-interface IPGeoDetails {- country?: string- region?: string- city?: string-}-@Injectable()export class AnalyticsService {constructor(private readonly projectService: ProjectService) {}@@ -934,33 +926,6 @@ export class AnalyticsService {return result}- getGeoDetails(ip: string, tz?: string): IPGeoDetails {- // Stage 1: Using IP address based geo lookup- const data = lookup.get(ip)-- const country = data?.country?.iso_code- // TODO: Add city overrides, for example, Colinton -> Edinburgh, etc.- const city = data?.city?.names?.en- const region = data?.subdivisions?.[0]?.names?.en-- if (country) {- return {- country,- city,- region,- }- }-- // Stage 2: Using timezone based geo lookup as a fallback- const tzCountry = timezones.getCountryForTimezone(tz)?.id || null-- return {- country: tzCountry,- city: null,- region: null,- }- }-async getFilters(pid: string, type: string): Promise<Array<string>> {if (!_includes(TRAFFIC_COLUMNS, type)) {throw new UnprocessableEntityException(diff --git a/apps/production/src/auth/auth.controller.ts b/apps/production/src/auth/auth.controller.tsindex 440ebd7d1..377e3138a 100644--- a/apps/production/src/auth/auth.controller.ts+++ b/apps/production/src/auth/auth.controller.ts@@ -85,8 +85,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() requestIp: string,): Promise<RegisterResponseDto> {- const ip =- headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp+ const ip = headers['x-forwarded-for'] || requestIp || ''await checkRateLimit(ip, 'register', 5)@@ -135,8 +134,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() requestIp: string,): Promise<LoginResponseDto> {- const ip =- headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp+ const ip = headers['x-forwarded-for'] || requestIp || ''await checkRateLimit(ip, 'login', 10, 1800)@@ -198,8 +196,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() requestIp: string,): Promise<void> {- const ip =- headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp+ const ip = headers['x-forwarded-for'] || requestIp || ''await checkRateLimit(ip, 'reset-password')await checkRateLimit(body.email, 'reset-password')@@ -447,8 +444,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() reqIP: string,): Promise<any> {- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, 'sso-generate', OAUTH_RATE_LIMIT, 1800)@@ -473,8 +469,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() reqIP: string,): Promise<any> {- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, 'sso-process', OAUTH_RATE_LIMIT, 1800)@@ -492,8 +487,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() reqIP: string,): Promise<any> {- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''const { hash, provider } = bodydiff --git a/apps/production/src/auth/auth.module.ts b/apps/production/src/auth/auth.module.tsindex 94adb1615..ce4477b9c 100644--- a/apps/production/src/auth/auth.module.ts+++ b/apps/production/src/auth/auth.module.ts@@ -1,12 +1,12 @@import { forwardRef, Module } from '@nestjs/common'import { JwtModule } from '@nestjs/jwt'import { PassportModule } from '@nestjs/passport'-import { ActionTokensModule } from 'src/action-tokens/action-tokens.module'-import { MailerModule } from 'src/mailer/mailer.module'-import { UserModule } from 'src/user/user.module'-import { ProjectModule } from 'src/project/project.module'-import { TelegramService } from 'src/integrations/telegram/telegram.service'import { TypeOrmModule } from '@nestjs/typeorm'+import { ActionTokensModule } from '../action-tokens/action-tokens.module'+import { MailerModule } from '../mailer/mailer.module'+import { UserModule } from '../user/user.module'+import { ProjectModule } from '../project/project.module'+import { TelegramService } from '../integrations/telegram/telegram.service'import { AuthController } from './auth.controller'import { AuthService } from './auth.service'import {diff --git a/apps/production/src/auth/auth.service.ts b/apps/production/src/auth/auth.service.tsindex 4c440895b..21b4a3455 100644--- a/apps/production/src/auth/auth.service.ts+++ b/apps/production/src/auth/auth.service.ts@@ -40,6 +40,7 @@ import {isDevelopment,JWT_ACCESS_TOKEN_SECRET,} from '../common/constants'+import { getGeoDetails } from '../common/utils'import { TelegramService } from '../integrations/telegram/telegram.service'import { SSOProviders } from './dtos'import { UserGoogleDTO } from '../user/dto/user-google.dto'@@ -227,7 +228,7 @@ export class AuthService {return}- const headersInfo = await this.getHeadersInfo(headers)+ const headersInfo = await this.getHeadersInfo(headers, ip)const loginDate = dayjs().utc().format('YYYY-MM-DD HH:mm:ss')const message ='🚨 *Someone has logged into your account!*\n\n' +@@ -245,20 +246,17 @@ export class AuthService {}}- private async getHeadersInfo(headers: unknown) {+ private async getHeadersInfo(headers: unknown, ip: string) {const ua = UAParser(headers['user-agent'])const browser = ua.browser.name || 'Unknown'const device = ua.device.type || 'Desktop'const os = ua.os.name || 'Unknown'- let country = 'Unknown'- const cfCountry = headers['cf-ipcountry']+ let { country } = getGeoDetails(ip)- if (cfCountry === 'T1') {- country = 'Unknown (Tor)'- }-- if (cfCountry) {- country = getCountry(cfCountry)?.name+ if (country) {+ country = getCountry(country)?.name+ } else {+ country = 'Unknown'}return {diff --git a/apps/production/src/captcha/captcha.controller.ts b/apps/production/src/captcha/captcha.controller.tsindex c4a17475c..948e47027 100644--- a/apps/production/src/captcha/captcha.controller.ts+++ b/apps/production/src/captcha/captcha.controller.ts@@ -10,6 +10,7 @@ import {Req,Response,Res,+ Ip,HttpCode,} from '@nestjs/common'import * as dayjs from 'dayjs'@@ -60,6 +61,7 @@ export class CaptchaController {@Req() request: Request,@Headers() headers,@Res({ passthrough: true }) response: Response,+ @Ip() reqIP,): Promise<any> {this.logger.log({ manualDTO }, 'POST /captcha/verify-manual')@@ -120,12 +122,14 @@ export class CaptchaController {)this.captchaService.setTokenCookie(response, newTokenCookie)+ const ip = headers['x-forwarded-for'] || reqIP || ''+await this.captchaService.logCaptchaPass(pid,userAgent,- headers,timestamp,true,+ ip,)return {@@ -146,6 +150,7 @@ export class CaptchaController {@Req() request: Request,@Headers() headers,@Res({ passthrough: true }) response: Response,+ @Ip() reqIP,): Promise<any> {this.logger.log(automaticDTO, 'POST /captcha/verify')@@ -228,12 +233,14 @@ export class CaptchaController {this.captchaService.setTokenCookie(response, newTokenCookie)+ const ip = headers['x-forwarded-for'] || reqIP || ''+await this.captchaService.logCaptchaPass(pid,userAgent,- headers,timestamp,false,+ ip,)return {diff --git a/apps/production/src/captcha/captcha.service.ts b/apps/production/src/captcha/captcha.service.tsindex 332cce0b0..eae407edf 100644--- a/apps/production/src/captcha/captcha.service.ts+++ b/apps/production/src/captcha/captcha.service.ts@@ -23,6 +23,7 @@ import {CAPTCHA_COOKIE_KEY,CAPTCHA_TOKEN_LIFETIME,} from '../common/constants'+import { getGeoDetails } from '../common/utils'import { getElValue } from '../analytics/analytics.controller'import { GeneratedCaptcha } from './interfaces/generated-captcha'import { TokenCaptcha } from './interfaces/token-captcha'@@ -135,19 +136,17 @@ export class CaptchaService {async logCaptchaPass(pid: string,userAgent: string,- headers: any,timestamp: number,isManual: boolean,+ ip: string,): Promise<void> {const ua = UAParser(userAgent)const dv = ua.device.type || 'desktop'const br = ua.browser.nameconst os = ua.os.name- const cc =- headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry']-- const dto = captchaDTO(pid, dv, br, os, cc, isManual, timestamp)+ const { country = 'NULL' } = getGeoDetails(ip)+ const dto = captchaDTO(pid, dv, br, os, country, isManual, timestamp)const values = `(${_map(dto, getElValue).join(',')})`try {diff --git a/apps/production/src/common/utils.ts b/apps/production/src/common/utils.tsindex bd605fb3d..c79c6050d 100644--- a/apps/production/src/common/utils.ts+++ b/apps/production/src/common/utils.ts@@ -3,6 +3,7 @@ import * as path from 'path'import * as fs from 'fs'import { HttpException } from '@nestjs/common'import { hash } from 'blake3'+import timezones from 'countries-and-timezones'import * as randomstring from 'randomstring'import * as _sample from 'lodash/sample'import * as _toNumber from 'lodash/toNumber'@@ -174,6 +175,39 @@ if (fs.existsSync(PRODUCTION_GEOIP_DB_PATH)) {lookup = new Reader<CityResponse>(buffer)}+interface IPGeoDetails {+ country?: string+ region?: string+ city?: string+}++const getGeoDetails = (ip: string, tz?: string): IPGeoDetails => {+ // Stage 1: Using IP address based geo lookup+ const data = lookup.get(ip)++ const country = data?.country?.iso_code+ // TODO: Add city overrides, for example, Colinton -> Edinburgh, etc.+ const city = data?.city?.names?.en+ const region = data?.subdivisions?.[0]?.names?.en++ if (country) {+ return {+ country,+ city,+ region,+ }+ }++ // Stage 2: Using timezone based geo lookup as a fallback+ const tzCountry = timezones.getCountryForTimezone(tz)?.id || null++ return {+ country: tzCountry,+ city: null,+ region: null,+ }+}+export {getRandomTip,checkRateLimit,@@ -183,4 +217,5 @@ export {generateRandomString,nFormatter,lookup,+ getGeoDetails,}diff --git a/apps/production/src/twoFactorAuth/twoFactorAuth.controller.ts b/apps/production/src/twoFactorAuth/twoFactorAuth.controller.tsindex af880e89a..cde1bed2a 100644--- a/apps/production/src/twoFactorAuth/twoFactorAuth.controller.ts+++ b/apps/production/src/twoFactorAuth/twoFactorAuth.controller.ts@@ -10,7 +10,7 @@ import {} from '@nestjs/common'import { ApiTags } from '@nestjs/swagger'-import { JwtAccessTokenGuard } from 'src/auth/guards'+import { JwtAccessTokenGuard } from '../auth/guards'import { TwoFactorAuthService } from './twoFactorAuth.service'import { UserService } from '../user/user.service'import { AuthService } from '../auth/auth.service'@@ -56,8 +56,7 @@ export class TwoFactorAuthController {) {this.logger.log({ body }, 'POST /2fa/enable')- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, '2fa-enable', 10, 1800)const user = await this.userService.findOneWhere({ id })@@ -104,8 +103,7 @@ export class TwoFactorAuthController {) {this.logger.log({ body }, 'POST /2fa/disable')- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, '2fa-disable', 10, 1800)const user = await this.userService.findOneWhere({ id })@@ -144,8 +142,7 @@ export class TwoFactorAuthController {) {this.logger.log({ body }, 'POST /2fa/authenticate')- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, '2fa-auth', 10, 1800)const user = await this.userService.findOneWhere({ id })diff --git a/apps/production/src/user/user.controller.ts b/apps/production/src/user/user.controller.tsindex 1789351a9..2e17f064a 100644--- a/apps/production/src/user/user.controller.ts+++ b/apps/production/src/user/user.controller.ts@@ -63,7 +63,7 @@ import { AuthService } from '../auth/auth.service'import { LetterTemplate } from '../mailer/letter'import { AppLoggerService } from '../logger/logger.service'import { UserProfileDTO } from './dto/user.dto'-import { checkRateLimit } from '../common/utils'+import { checkRateLimit, getGeoDetails } from '../common/utils'import { IUsageInfo, IMetaInfo } from './interfaces'dayjs.extend(utc)@@ -204,8 +204,7 @@ export class UserController {}> {this.logger.log({ userId }, 'POST /user/api-key')- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, 'generate-api-key', 5, 3600)@@ -652,11 +651,15 @@ export class UserController {@Get('metainfo')@Public()- async getMetaInfo(@Headers() headers): Promise<IMetaInfo> {- const country = headers['cf-ipcountry'] || 'XX'+ async getMetaInfo(+ @Headers() headers,+ @Ip() reqIP: string,+ ): Promise<IMetaInfo> {+ const ip = headers['x-forwarded-for'] || reqIP || ''+ const { country } = getGeoDetails(ip)return {- country: country === 'XX' || country === 'T1' ? null : country,+ country,...this.userService.getCurrencyByCountry(country),}}diff --git a/apps/production/src/webhook/webhook.controller.ts b/apps/production/src/webhook/webhook.controller.tsindex 5446da927..7d6775191 100644--- a/apps/production/src/webhook/webhook.controller.ts+++ b/apps/production/src/webhook/webhook.controller.ts@@ -40,8 +40,7 @@ export class WebhookController {@Headers() headers,@Ip() reqIP,): Promise<any> {- const ip =- headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip = headers['x-forwarded-for'] || reqIP || ''this.webhookService.verifyIP(ip)this.webhookService.validateWebhook(body)diff --git a/meta/nginx/example-config b/meta/nginx/example-confignew file mode 100644index 000000000..54f62da82--- /dev/null+++ b/meta/nginx/example-config@@ -0,0 +1,20 @@+server {+ server_name api.swetrix.com;++ location / {+ proxy_pass http://localhost:5005;+ }++ listen 443 ssl; # managed by Certbot+ ssl_certificate /etc/letsencrypt/live/api.swetrix.com/fullchain.pem; # managed by Certbot+ ssl_certificate_key /etc/letsencrypt/live/api.swetrix.com/privkey.pem; # managed by Certbot+ include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot+ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot+}++server {+ listen 80 default_server;+ listen [::]:80 default_server;+ server_name "";+ return 404;+}diff --git a/meta/nginx/swetrix b/meta/nginx/swetrixdeleted file mode 100644index 43ecb370a..000000000--- a/meta/nginx/swetrix+++ /dev/null@@ -1,50 +0,0 @@-server {- server_name swetrix.com-- root /var/www/html;-- location / {- try_files $uri $uri/ =404;- }-- listen 443 ssl; # managed by Certbot- ssl_certificate /etc/letsencrypt/live/swetrix.com/fullchain.pem; # managed by Certbot- ssl_certificate_key /etc/letsencrypt/live/swetrix.com/privkey.pem; # managed by Certbot- include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot- ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot-}--server {- server_name api.swetrix.com;-- location / {- proxy_pass http://localhost:5005;- }-- listen 443 ssl; # managed by Certbot- ssl_certificate /etc/letsencrypt/live/api.swetrix.com/fullchain.pem; # managed by Certbot- ssl_certificate_key /etc/letsencrypt/live/api.swetrix.com/privkey.pem; # managed by Certbot- include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot- ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot-}--server {- server_name chat.swetrix.com;-- location / {- proxy_pass http://localhost:3002;- }-- listen 443 ssl; # managed by Certbot- ssl_certificate /etc/letsencrypt/live/swetrix.com/fullchain.pem; # managed $- ssl_certificate_key /etc/letsencrypt/live/swetrix.com/privkey.pem; # manage$- include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot- ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot-}--server {- listen 80 default_server;- listen [::]:80 default_server;- server_name "";- return 404;-}