2026-10-05 09:24:55 +03:00
#!/bin/sh
# Entrypoint of the all-in-one container (variant A).
#
# Order: environment -> TUN device -> workspace (easy-rsa) -> NAT -> nginx config ->
# DATABASE MIGRATION (explicit step, container does not start on failure) -> supervisord.
#
# Re-running (container restart) is safe: every step is idempotent.
set -eu
log() { echo "[ovpmon] $* " ; }
warn() { echo "[ovpmon] WARNING: $* " >& 2; }
die() { echo "[ovpmon] FATAL: $* " >& 2; exit 1; }
# --- 1. Secret ----------------------------------------------------------------------------
# Both APIs read OVPMON_API_SECRET_KEY; JWT_SECRET is accepted as an alias (same name as in .env).
if [ -z " ${ OVPMON_API_SECRET_KEY :- } " ] && [ -n " ${ JWT_SECRET :- } " ] ; then
OVPMON_API_SECRET_KEY = " $JWT_SECRET "
fi
[ -n " ${ OVPMON_API_SECRET_KEY :- } " ] || die "JWT_SECRET (or OVPMON_API_SECRET_KEY) is not set. Generate one: openssl rand -hex 32"
export OVPMON_API_SECRET_KEY
2026-10-05 20:30:45 +03:00
# Key rule (same as APP_CORE/secret_key.py; lib.sh cannot be sourced in the image): no placeholder, at least 32 characters
_key_lc = $( printf '%s' " $OVPMON_API_SECRET_KEY " | tr '[:upper:]' '[:lower:]' )
case " $_key_lc " in
*[ ![ :space:]] *) ;;
*) die "JWT_SECRET is blank. Generate one: openssl rand -hex 32" ;;
esac
case " $_key_lc " in
*change-me*| *changeme*) die "JWT_SECRET is a placeholder/default value: the API refuses to start with a weak signing key. Generate one: openssl rand -hex 32" ;;
esac
[ " ${# OVPMON_API_SECRET_KEY } " -ge 32 ] || die "JWT_SECRET is too short ( ${# OVPMON_API_SECRET_KEY } characters, at least 32 required). Generate one: openssl rand -hex 32"
unset _key_lc
2026-10-05 09:24:55 +03:00
# Database locations (same defaults as in the image and in docker-compose.allinone.yml)
export OVPMON_PROFILER_DB_PATH = " ${ OVPMON_PROFILER_DB_PATH :- /app/db/ovpn_profiler.db } "
export OVPMON_OPENVPN_MONITOR_DB_PATH = " ${ OVPMON_OPENVPN_MONITOR_DB_PATH :- /app/db/openvpn_monitor.db } "
# --- 2. TUN device ------------------------------------------------------------------------
if [ ! -c /dev/net/tun ] ; then
log "Creating /dev/net/tun"
mkdir -p /dev/net
mknod /dev/net/tun c 10 200 || warn "cannot create /dev/net/tun (add devices: /dev/net/tun to the container)"
chmod 600 /dev/net/tun 2>/dev/null || true
fi
# IP forwarding is set by the container runtime (sysctls: net.ipv4.ip_forward=1 in compose)
mkdir -p /run /run/nginx /app/db /app/client-config /etc/openvpn /var/log/openvpn
# --- 3. Easy-RSA workspace ----------------------------------------------------------------
# /app/easy-rsa is the PKI volume; the profiler (working directory /app) uses /app/easy-rsa/pki.
# Copy the easy-rsa scripts only if the volume does not have them yet (never touches pki/).
if [ ! -f /app/easy-rsa/easyrsa ] ; then
log "Initializing Easy-RSA workspace in /app/easy-rsa"
mkdir -p /app/easy-rsa
cp -r /usr/share/easy-rsa/* /app/easy-rsa/
fi
# --- 4. NAT and MSS clamping (as in APP_PROFILER/entrypoint.sh, without duplicates) --------
# The rules live in the container's network namespace and survive a plain container restart,
# so each rule is added only if it is not there yet (-C checks for it).
NAT_IFACE = " ${ OVPMON_NAT_IFACE :- eth0 } "
ensure_rule() {
table = " $1 " ; shift
if iptables -t " $table " -C " $@ " 2>/dev/null; then
return 0
fi
iptables -t " $table " -A " $@ " || warn "iptables rule not added: -t $table $* (NET_ADMIN missing? VPN traffic will not be routed)"
}
ensure_rule nat POSTROUTING -o " $NAT_IFACE " -j MASQUERADE
ensure_rule mangle FORWARD -o " $NAT_IFACE " -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
ensure_rule mangle FORWARD -i " $NAT_IFACE " -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
# --- 5. Nginx configuration ---------------------------------------------------------------
2026-10-07 13:28:30 +03:00
# The same template as the UI image of variant B; all upstreams are local. Only the OVP_* variables
# are substituted, nginx's own $variables stay intact.
# OVP_CSP_CONNECT_EXTRA (compose: OVPMON_CSP_CONNECT_EXTRA): extra connect-src origins of the CSP. It goes into
# a response header, so only origins are accepted (APP_UI/check-csp-connect-extra.sh, copied into the image):
# anything else could inject another CSP directive.
# Empty value: same origin only.
OVP_CSP_CONNECT_EXTRA = " ${ OVP_CSP_CONNECT_EXTRA :- } "
/usr/local/bin/ovpmon-check-csp.sh || die "invalid OVPMON_CSP_CONNECT_EXTRA (see above)"
# one space between origins (the template has the value after 'self' and a space)
OVP_CSP_CONNECT_EXTRA = $( printf '%s' " $OVP_CSP_CONNECT_EXTRA " | tr -s ' ' )
export OVP_CSP_CONNECT_EXTRA
2026-10-05 09:24:55 +03:00
export OVP_API_HOST = 127.0.0.1 OVP_API_PORT = 5001
export OVP_PROFILER_HOST = 127.0.0.1 OVP_PROFILER_PORT = 8000
2026-10-07 13:31:53 +03:00
export OVP_UI_PORT = 80
envsubst '${OVP_API_HOST} ${OVP_API_PORT} ${OVP_PROFILER_HOST} ${OVP_PROFILER_PORT} ${OVP_UI_PORT} ${OVP_CSP_CONNECT_EXTRA}' \
2026-10-05 09:24:55 +03:00
< /etc/ovpmon/default.conf.template > /etc/nginx/http.d/ovpmon.conf
nginx -t || die "generated nginx configuration is invalid (see above)"
# --- 6. Database migration (explicit step, before any service starts) ----------------------
# Same command as the update scripts run. Pending migrations are applied after a consistent
# backup (<db>.bak-<UTC timestamp> next to the database). A database that is newer than this
# image, or a failed migration, stops the container here: nothing is started on a bad database.
# See DOCS/General/Database_Migrations.md
run_migration() {
name = " $1 " ; dir = " $2 "
log "Database migration: $name "
if ! ( cd " $dir " && python migrate.py ) ; then
2026-10-05 20:30:45 +03:00
die "database migration of $name failed (see the report above). Nothing was started. Restore the volumes from the last backup or use the previous image; see DOCS/Deployment/Containers/Operations.md"
2026-10-05 09:24:55 +03:00
fi
}
run_migration "profiler" /app/APP_PROFILER
run_migration "monitoring" /app/APP_CORE
# The migration has been done: the services only verify the schema and never migrate on their own
# (two gunicorn workers and the gatherer must not race for it).
export OVPMON_AUTO_MIGRATE = 0
# --- 7. Services --------------------------------------------------------------------------
log "Starting services (supervisord)"
exec supervisord -n -c /etc/supervisord.conf