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!

4.3 KB
Raw Blame
import Redis from 'ioredis'
import _toNumber from 'lodash/toNumber'
import crypto from 'crypto'

import 'dotenv/config'
import { deriveKey } from './utils'

const redis = new Redis(
  _toNumber(process.env.REDIS_PORT),
  process.env.REDIS_HOST,
  {
    password: process.env.REDIS_PASSWORD,
    username: process.env.REDIS_USER,
    family: 0,
  },
)

// Returns a 32-character hash of the provided string
const hash = (content: string): string => {
  return crypto
    .createHash('sha256')
    .update(content)
    .digest('hex')
    .substring(0, 32)
}

// CAPTCHA related constants
const CAPTCHA_TOKEN_LIFETIME = 300 * 1000 // milliseconds (5 minutes)
const CAPTCHA_SECRET_KEY_LENGTH = 50
const CAPTCHA_COLUMNS = [
  'cc',
  'br',
  'os',
  'dv',
  'captcha_event',
  'captcha_difficulty',
  'captcha_reason',
  'solve_time',
]

// redis keys for captcha
const getRedisCaptchaKey = (token: string) => `captcha_${hash(token)}`

export const JWT_ACCESS_TOKEN_SECRET = deriveKey('access-token')
export const JWT_REFRESH_TOKEN_SECRET = deriveKey('refresh-token')

const {
  // 30 days
  JWT_REFRESH_TOKEN_LIFETIME = 60 * 60 * 24 * 30,
  // 30 minutes
  JWT_ACCESS_TOKEN_LIFETIME = 60 * 30,
} = process.env
const isDevelopment = process.env.NODE_ENV === 'development'

export const IS_REGISTRATION_DISABLED =
  process.env.DISABLE_REGISTRATION === 'true' ||
  !process.env.DISABLE_REGISTRATION

const SELFHOSTED_GEOIP_DB_PATH = process.env.IP_GEOLOCATION_DB_PATH

// OIDC configuration
const TWO_FACTOR_AUTHENTICATION_APP_NAME =
  process.env.TWO_FACTOR_AUTHENTICATION_APP_NAME || 'Swetrix CE'

const OIDC_ENABLED = process.env.OIDC_ENABLED === 'true'
const OIDC_ONLY_AUTH = process.env.OIDC_ONLY_AUTH === 'true'

const ORIGINS_REGEX =
  /^(?=.{1,255}$)([0-9A-Za-z*:](?:(?:[0-9A-Za-z*:]|-){0,61}[0-9A-Za-z*:])?(?:\.[0-9A-Za-z*:](?:(?:[0-9A-Za-z*-]|-){0,61}[0-9A-Za-z*:])?)*)?$/
const PID_REGEX = /^(?!.*--)[a-zA-Z0-9-]{12}$/
const isValidPID = (pid: string) => PID_REGEX.test(pid)

// redis keys
const getRedisProjectKey = (pid: string) => `pid_${pid}`

const REDIS_OIDC_SESSION_KEY = 'oidc:session'

// 3600 sec -> 1 hour
const redisProjectCacheTimeout = 3600

// 30 minues -> the amount of time analytics requests within one session are counted as non-unique
const UNIQUE_SESSION_LIFE_TIME = 1800

const ONLINE_VISITORS_WINDOW_MINUTES = 5

// Funnels
const MIN_PAGES_IN_FUNNEL = 2
const MAX_PAGES_IN_FUNNEL = 10

const TRAFFIC_COLUMNS = [
  'cc',
  'rg',
  'ct',
  'isp',
  'og',
  'ut',
  'ctp',
  'host',
  'pg',
  'title',
  'lc',
  'br',
  'brv',
  'os',
  'osv',
  'dv',
  'ref',
  'so',
  'me',
  'ca',
  'te',
  'co',
]

const TRAFFIC_METAKEY_COLUMNS = ['tag:key', 'tag:value']

const ERROR_COLUMNS = [
  'host',
  'pg',
  'dv',
  'br',
  'brv',
  'os',
  'osv',
  'lc',
  'cc',
  'rg',
  'ct',
  'isp',
  'og',
  'ut',
  'ctp',
]

const ALL_COLUMNS = [...TRAFFIC_COLUMNS, 'ev', 'entryPage', 'exitPage']

const V2_VIEW_FILTER_DIMENSIONS = [
  'country',
  'region',
  'city',
  'page',
  'title',
  'host',
  'locale',
  'browser',
  'browser_version',
  'os',
  'os_version',
  'device',
  'referrer',
  'referrer_name',
  'utm_source',
  'utm_medium',
  'utm_campaign',
  'utm_term',
  'utm_content',
  'isp',
  'organization',
  'user_type',
  'connection_type',
  'entry_page',
  'exit_page',
  'event',
  'error_name',
  'error_message',
  'error_filename',
  'event_metadata',
  'page_property',
]

const PERFORMANCE_COLUMNS = [
  'cc',
  'rg',
  'ct',
  'isp',
  'og',
  'ut',
  'ctp',
  'host',
  'pg',
  'dv',
  'br',
  'brv',
]

const NUMBER_JWT_REFRESH_TOKEN_LIFETIME = Number(JWT_REFRESH_TOKEN_LIFETIME)
const NUMBER_JWT_ACCESS_TOKEN_LIFETIME = Number(JWT_ACCESS_TOKEN_LIFETIME)

export {
  redis,
  isValidPID,
  getRedisProjectKey,
  redisProjectCacheTimeout,
  UNIQUE_SESSION_LIFE_TIME,
  ONLINE_VISITORS_WINDOW_MINUTES,
  TRAFFIC_METAKEY_COLUMNS,
  ORIGINS_REGEX,
  isDevelopment,
  NUMBER_JWT_REFRESH_TOKEN_LIFETIME as JWT_REFRESH_TOKEN_LIFETIME,
  NUMBER_JWT_ACCESS_TOKEN_LIFETIME as JWT_ACCESS_TOKEN_LIFETIME,
  TRAFFIC_COLUMNS,
  PERFORMANCE_COLUMNS,
  MIN_PAGES_IN_FUNNEL,
  MAX_PAGES_IN_FUNNEL,
  ERROR_COLUMNS,
  PID_REGEX,
  ALL_COLUMNS,
  V2_VIEW_FILTER_DIMENSIONS,
  SELFHOSTED_GEOIP_DB_PATH,
  TWO_FACTOR_AUTHENTICATION_APP_NAME,
  OIDC_ENABLED,
  OIDC_ONLY_AUTH,
  REDIS_OIDC_SESSION_KEY,
  // CAPTCHA
  getRedisCaptchaKey,
  CAPTCHA_TOKEN_LIFETIME,
  CAPTCHA_SECRET_KEY_LENGTH,
  CAPTCHA_COLUMNS,
}