#!/bin/sh # Teldrive v2 launcher. Configuration comes ONLY from environment variables # (no config file is written, so no secret is ever stored on disk). # Put ALL secrets in the host's "Secrets" (env vars), NEVER in git. set -eu # ---- Existing v1 secrets (kept unchanged, re-mapped to v2 names) ----------- : "${DB_DATA_SOURCE:?Missing DB_DATA_SOURCE (existing PostgreSQL URL; use session mode, port 5432)}" : "${JWT_SECRET:?Missing JWT_SECRET (reused as v2 security.signing-key, min 32 chars)}" : "${TG_UPLOADS_ENCRYPTION_KEY:?Missing TG_UPLOADS_ENCRYPTION_KEY (kept as v2 content-encryption key version 1)}" : "${ALLOWED_USERS:?Missing ALLOWED_USERS (telegram username without @, comma separated)}" # ---- NEW in v2: required, must be created once and then NEVER changed ------ # Base64 that decodes to exactly 32 bytes: openssl rand -base64 32 : "${TELDRIVE_SECURITY_DATA_KEY:?Missing TELDRIVE_SECURITY_DATA_KEY (new v2 secret; back it up)}" # ---- Sanity checks (never print secret values) ----------------------------- if [ "$(printf '%s' "$JWT_SECRET" | wc -c)" -lt 32 ]; then echo "ERROR: JWT_SECRET is shorter than 32 chars; v2 signing-key needs >= 32. Aborting." >&2 exit 1 fi if [ "$(printf '%s' "$TELDRIVE_SECURITY_DATA_KEY" | base64 -d 2>/dev/null | wc -c)" -ne 32 ]; then echo "ERROR: TELDRIVE_SECURITY_DATA_KEY must be Base64 of exactly 32 bytes. Aborting." >&2 exit 1 fi case "$TG_UPLOADS_ENCRYPTION_KEY" in *:*) echo "ERROR: TG_UPLOADS_ENCRYPTION_KEY contains ':' which breaks the v2 'version:key' format. Aborting." >&2; exit 1 ;; esac # ---- Map to Teldrive v2 settings ------------------------------------------- export TELDRIVE_DATABASE_URL="$DB_DATA_SOURCE" export TELDRIVE_SECURITY_SIGNING_KEY="$JWT_SECRET" export TELDRIVE_SECURITY_ALLOWED_USERS="$ALLOWED_USERS" export TELDRIVE_ENCRYPTION_ACTIVE_KEY_VERSION="${TELDRIVE_ENCRYPTION_ACTIVE_KEY_VERSION:-1}" export TELDRIVE_ENCRYPTION_KEYS="1:${TG_UPLOADS_ENCRYPTION_KEY}" export TELDRIVE_HTTP_ADDRESS="${TELDRIVE_HTTP_ADDRESS:-0.0.0.0:8080}" # SAFETY: the one-way v1 -> v2 database migration is DISABLED by default. # With "false", a v1 database makes Teldrive refuse to start (nothing is modified). # Set TELDRIVE_DATABASE_AUTO_MIGRATE_LEGACY=true only after a verified backup # and explicit approval. export TELDRIVE_DATABASE_AUTO_MIGRATE_LEGACY="${TELDRIVE_DATABASE_AUTO_MIGRATE_LEGACY:-false}" # TELDRIVE_MODE: "run" (default, start server) or "check" (validate config/DB init). MODE="${TELDRIVE_MODE:-run}" case "$MODE" in run|check) ;; *) echo "ERROR: TELDRIVE_MODE must be 'run' or 'check'." >&2; exit 1 ;; esac exec /usr/bin/teldrive "$MODE"