Files
OpenVPN-Monitoring-Simple/deploy/native/install.sh
T
ayurishchevandClaude Sonnet 5.5 f95a0b63e6 Native install: nftables script for VPN clients (masquerade, MSS clamp, TTL)
deploy/native/firewall.sh reads the server interfaces and applies, for traffic
from the VPN network to all other networks: masquerade, TCP MSS clamping
(path MTU tuning) and TTL tuning, in its own table `ip ovpmon`. Rules are
persisted in /etc/ovpmon/nftables.nft and loaded at boot by the ovpmon-nft
service (systemd and OpenRC). Opt-in: install.sh and update.sh do not call it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-05 14:48:39 +03:00

534 lines
26 KiB
Bash
Executable File

#!/bin/sh
# Install OpenVPN Monitor & Profiler as system services (no containers): Alpine (OpenRC), Debian, Ubuntu (systemd).
#
# deploy/native/install.sh [--prefix DIR] [--ui-dist FILE|DIR | --build-ui [--node-download] | --no-ui]
# [--https-port N] [--host-name NAME] [--admin-user NAME] [--force]
#
# --prefix install directory (APP_CORE, APP_PROFILER, venvs). REQUIRED: without the option the script
# asks for it (no default is assumed). It must be a new or empty directory, different from
# this repository checkout (the checkout is only the source) and not a system directory
# --ui-dist UI tarball made by deploy/native/build-ui.sh, or a directory with index.html (the host
# needs no Node). Highest priority
# --build-ui build the UI now from APP_UI (Docker when it works, otherwise Node >= 20.19 on this host);
# for a host where the committed build is stale or missing
# --node-download with --build-ui: fetch a temporary official Node 20 (checksum verified, glibc only,
# needs curl and xz) instead of the system Node; nothing is installed
# --no-ui install a placeholder page instead of the UI (on purpose)
# Without these options the committed build deploy/ui-dist of the repository is installed
# automatically (a warning is printed when it is older than APP_UI); when there is no UI
# source at all the install stops
# --https-port HTTPS port of the web UI (default 8088)
# --host-name DNS name or IP of this host: certificate name and CORS origin (default: detected address)
# --admin-user login of the initial administrator (default: admin); the password is generated
# --force continue on an untested OS version / Debian derivative
#
# Run as root from a checkout of the repository. Refuses when an installation exists (use update.sh).
# After a failure it is safe to run again: every step is idempotent and nothing is generated twice.
# It does NOT touch the firewall: NAT/forwarding commands are printed and documented.
# See DOCS/Deployment/Scenario2_System_Services.md
set -eu
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
. "$SCRIPT_DIR/lib.sh"
usage() {
sed -n '2,/^set -eu$/{/^set -eu$/!p}' "$0" | sed 's/^# \{0,1\}//'
}
PREFIX=""
UI_DIST=""
UI_BUILD=0
UI_NODE_DL=0
NO_UI=0
HTTPS_PORT=8088
HOST_NAME=""
ADMIN_USER="admin"
FORCE=0
CURRENT_PHASE="start"
while [ $# -gt 0 ]; do
case "$1" in
--prefix) [ $# -ge 2 ] || die "--prefix needs a value"; PREFIX="$2"; shift 2 ;;
--ui-dist) [ $# -ge 2 ] || die "--ui-dist needs a value"; UI_DIST="$2"; shift 2 ;;
--build-ui) UI_BUILD=1; shift ;;
--node-download) UI_NODE_DL=1; shift ;;
--no-ui) NO_UI=1; shift ;;
--https-port) [ $# -ge 2 ] || die "--https-port needs a value"; HTTPS_PORT="$2"; shift 2 ;;
--host-name) [ $# -ge 2 ] || die "--host-name needs a value"; HOST_NAME="$2"; shift 2 ;;
--admin-user) [ $# -ge 2 ] || die "--admin-user needs a value"; ADMIN_USER="$2"; shift 2 ;;
--force) FORCE=1; shift ;;
-h|--help) usage; exit 0 ;;
*) usage >&2; die "unknown option: $1" ;;
esac
done
SRC="$ROOT_DIR"
# prefix_problem DIR: prints why DIR cannot be the install directory (nothing = acceptable).
# The install directory gets mkdir/chmod 755 and root-owned code: it must be a dedicated place.
prefix_problem() {
local d n sabs
d="$1"
valid_prefix "$d" || { echo "not an absolute path of letters, digits, . _ - and /"; return; }
# normalise: no trailing slash, no double slash, no . or .. segments
case "$d" in *//*|*/./*|*/../*|*/.|*/..|*/) echo "write the path without //, ., .. or a trailing /"; return ;; esac
case "$d" in
/|/bin|/boot|/dev|/etc|/home|/lib|/lib64|/media|/mnt|/opt|/proc|/root|/run|/sbin|/srv|/sys|/tmp|/usr|/var|/usr/local|/usr/lib|/var/lib|/var/log|/var/www|/etc/openvpn|/etc/ovpmon)
echo "a system directory (the installer changes its permissions); choose a dedicated directory, e.g. $DEFAULT_PREFIX"; return ;;
esac
sabs=$(abs_dir "$SRC")
[ "$d" != "$sabs" ] || { echo "this is the repository checkout itself ($sabs): the checkout is only the source of the installation. Choose another directory, or move/clone the repository elsewhere"; return; }
case "$d/" in "$sabs"/*) echo "inside the repository checkout ($sabs): choose a directory outside of it"; return ;; esac
case "$sabs/" in "$d"/*) echo "the repository checkout ($sabs) is inside it: choose a directory that does not contain the checkout"; return ;; esac
if [ -e "$d" ] && [ ! -d "$d" ]; then echo "exists and is not a directory"; return; fi
if [ -d "$d" ] && [ ! -f "$INCOMPLETE_MARK" ]; then
n=$(ls -A "$d" 2>/dev/null | wc -l)
[ "$n" -eq 0 ] || { echo "already exists and is not empty (an installation, or other data). To upgrade an installation use update.sh; otherwise choose a new directory"; return; }
fi
}
# The install directory is always asked for: it decides where the code, the PKI and the client profiles
# live, and it is not something to guess. Without a terminal it must be given with --prefix.
ask_prefix() {
local ans tries p
[ -t 0 ] || die "--prefix DIR is required (no terminal to ask on). Example: --prefix $DEFAULT_PREFIX"
echo "Install directory: where APP_CORE, APP_PROFILER and their virtualenvs are installed."
echo "The PKI and the client profiles live below it. It must be a new or empty directory outside"
echo "this repository checkout ($ROOT_DIR). Suggestion: $DEFAULT_PREFIX"
tries=0
while [ "$tries" -lt 3 ]; do
printf 'Install directory (absolute path): '
read -r ans || die "no input: --prefix DIR is required"
if [ -z "$ans" ]; then
echo " an install directory is required (type the path; there is no default)"
else
p=$(prefix_problem "$ans")
if [ -z "$p" ]; then
PREFIX="$ans"
printf 'Install into %s ? [y/N] ' "$PREFIX"
read -r ans || die "no input"
case "$ans" in y|Y|yes|YES) return 0 ;; esac
echo " not confirmed"
else
echo " not accepted: $p"
fi
fi
tries=$((tries + 1))
done
die "no valid install directory given"
}
if [ -z "$PREFIX" ]; then
ask_prefix
else
_problem=$(prefix_problem "$PREFIX")
[ -z "$_problem" ] || die "invalid --prefix '$PREFIX': $_problem"
fi
valid_port "$HTTPS_PORT" || die "invalid --https-port '$HTTPS_PORT'"
case "$ADMIN_USER" in
""|*[!A-Za-z0-9._-]*) die "invalid --admin-user (letters, digits, . _ - only)" ;;
esac
case "$HOST_NAME" in
*[!A-Za-z0-9.-]*) die "invalid --host-name (letters, digits, . and - only)" ;;
esac
[ -z "$UI_DIST" ] || case "$UI_DIST" in /*) ;; *) UI_DIST="$PWD/$UI_DIST" ;; esac
[ "$UI_NODE_DL" = 0 ] || [ "$UI_BUILD" = 1 ] || die "--node-download only works together with --build-ui"
[ "$NO_UI" = 0 ] || { [ -z "$UI_DIST" ] && [ "$UI_BUILD" = 0 ]; } || die "--no-ui excludes --ui-dist and --build-ui"
[ -z "$UI_DIST" ] || [ "$UI_BUILD" = 0 ] || die "use either --ui-dist or --build-ui, not both"
on_exit() {
_rc=$?
set +e
trap '' INT TERM
if [ "$_rc" -ne 0 ]; then
case "$CURRENT_PHASE" in
start|0*) log "install stopped in phase '$CURRENT_PHASE': nothing was changed on this system" >&2 ;;
*) log "install stopped in phase '$CURRENT_PHASE'. Nothing is lost: fix the cause and run the script again with the same --prefix (it resumes; existing secrets, certificates and data are kept)" >&2 ;;
esac
fi
[ -z "${TMPD:-}" ] || rm -rf "$TMPD"
release_lock
exit "$_rc"
}
TMPD=$(mktemp -d /tmp/ovpmon-install.XXXXXX) || die "cannot create a temporary directory"
trap on_exit EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="0 preflight"
phase 0 "preflight"
need_root
check_os
require_init
acquire_lock
# An existing installation is updated, not installed over
if [ -f "$STATE_FILE" ]; then
die "already installed (state file $STATE_FILE). To upgrade use: $SCRIPT_DIR/update.sh"
fi
if [ ! -f "$INCOMPLETE_MARK" ]; then
for _p in "$PREFIX/APP_CORE" "$PREFIX/APP_PROFILER" "$ENV_FILE" "$(unit_file ovpmon-api)"; do
if [ -e "$_p" ]; then
die "an installation already exists ($_p). install.sh never installs over one. To upgrade or adopt it use: $SCRIPT_DIR/update.sh"
fi
done
fi
[ -f "$SRC/APP_CORE/openvpn_api_v3.py" ] && [ -f "$SRC/APP_PROFILER/main.py" ] \
|| die "run the script from a complete checkout of the repository (APP_CORE and APP_PROFILER not found in $SRC)"
# UI source order (ui_resolve, run below after the cheap checks): --ui-dist, --build-ui, the committed
# deploy/ui-dist, an archive next to the scripts. Nothing found: stop, unless --no-ui
UI_FALLBACK="$SCRIPT_DIR/ui-dist.tar.gz"
# ports: ours may already be bound when the script is resumed
if svc_active ovpmon-api 2>/dev/null; then
log "ovpmon services are already running (resumed install): application ports not checked"
else
check_port 5001 tcp "monitoring API"
check_port 8000 tcp "profiler API"
fi
if [ ! -f "$NGINX_SITE" ]; then
check_port "$HTTPS_PORT" tcp "HTTPS (UI)"
fi
if port_in_use 1194 udp; then log "note: 1194/udp is in use; OpenVPN uses the port set in the server settings of the UI"; fi
check_free "$PREFIX" "$MIN_FREE_PREFIX_MB" "the installation (code and virtualenvs)"
check_free /var/lib 256 "data"
if [ "$NO_UI" = 1 ]; then
warn "--no-ui: only a placeholder page is installed instead of the web UI (update.sh installs the UI from the repository later)"
else
ui_resolve "$SRC" "$TMPD"
if [ -z "$UI_SRC" ]; then
die "no UI source found: there is no --ui-dist, no committed build in $SRC/deploy/ui-dist and no archive $UI_FALLBACK. Choose one: --build-ui (builds from APP_UI with Docker or Node 20.19+, add --node-download for a temporary Node), --ui-dist FILE|DIR (built elsewhere with deploy/native/build-ui.sh), or --no-ui (placeholder page on purpose). A complete clone of the repository contains deploy/ui-dist"
fi
log "UI: $UI_ORIGIN"
fi
mkdir -p "$ETC_DIR"
chmod 755 "$ETC_DIR"
: > "$INCOMPLETE_MARK"
chmod 600 "$INCOMPLETE_MARK"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="1 packages"
phase 1 "packages"
log "installing: $PKG_LIST"
# shellcheck disable=SC2086
pkg_install $PKG_LIST
check_gnu_tar
python3 -c 'import sys, sqlite3, venv; sys.exit(0 if sys.version_info >= (3, 9) else 1)' \
|| die "Python 3.9 or newer with the sqlite3 and venv modules is required (found: $(python3 --version 2>&1))"
for _c in curl openssl nginx openvpn; do
command -v "$_c" >/dev/null 2>&1 || die "$_c is not available after the package installation"
done
[ -f "$EASYRSA_SRC/easyrsa" ] || die "$EASYRSA_SRC/easyrsa not found (package easy-rsa)"
check_nginx_include
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="2 user and directories"
phase 2 "service user and directories"
ensure_service_user
ensure_dir "$DATA_DIR" "$SERVICE_USER:$SERVICE_USER" 750
ensure_dir "$DATA_DIR/staging" "$SERVICE_USER:$SERVICE_USER" 750
ensure_dir "$APP_LOG_DIR" "$SERVICE_USER:$SERVICE_USER" 750
ensure_dir "$OVPN_LOG_DIR" root:root 755
# distribution-owned directories are only created when missing, never re-owned
for _d in /etc/openvpn "$(dirname "$OVPN_CONFIG")"; do
if [ ! -d "$_d" ]; then mkdir -p "$_d"; chmod 755 "$_d"; fi
done
ensure_dir /etc/openvpn/scripts root:root 755
# status log: written by OpenVPN (root), read by the gatherer (group ovpmon); the helper keeps these rights
if [ ! -e "$OVPN_LOG_DIR/openvpn-status.log" ]; then : > "$OVPN_LOG_DIR/openvpn-status.log"; fi
chown root:"$SERVICE_USER" "$OVPN_LOG_DIR/openvpn-status.log"
chmod 640 "$OVPN_LOG_DIR/openvpn-status.log"
log "directories ready: $DATA_DIR, $APP_LOG_DIR, $OVPN_LOG_DIR, /etc/openvpn/scripts, $ETC_DIR"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="3 code"
phase 3 "application code to $PREFIX"
mkdir -p "$PREFIX"
chmod 755 "$PREFIX"
copy_component "$SRC/APP_CORE" "$PREFIX/APP_CORE"
copy_component "$SRC/APP_PROFILER" "$PREFIX/APP_PROFILER"
sync_deploy_files "$SRC" "$PREFIX"
echo "$(version_of "$SRC")" > "$PREFIX/VERSION"
log "code installed (version $(cat "$PREFIX/VERSION")); the virtualenvs and data are not part of the copy"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="4 virtualenvs and easy-rsa"
phase 4 "Python virtualenvs and the easy-rsa workspace"
make_venv "$PREFIX/APP_CORE" gunicorn
make_venv "$PREFIX/APP_PROFILER"
smoke_component "$PREFIX/APP_CORE" core || die "the APP_CORE virtualenv cannot import the application's dependencies"
smoke_component "$PREFIX/APP_PROFILER" profiler || die "the APP_PROFILER virtualenv cannot import the application's dependencies"
log "dependencies import correctly"
# easy-rsa workspace: real directory owned by the service user (never a symlink); never overwritten
if [ ! -f "$PREFIX/APP_PROFILER/easy-rsa/easyrsa" ]; then
mkdir -p "$PREFIX/APP_PROFILER/easy-rsa"
cp -R "$EASYRSA_SRC"/. "$PREFIX/APP_PROFILER/easy-rsa/"
chown -R "$SERVICE_USER:$SERVICE_USER" "$PREFIX/APP_PROFILER/easy-rsa"
chmod 750 "$PREFIX/APP_PROFILER/easy-rsa"
log "easy-rsa workspace created from $EASYRSA_SRC"
else
log "easy-rsa workspace exists: kept"
fi
ensure_runtime_files "$PREFIX"
lock_down_code "$PREFIX/APP_CORE" "$PREFIX/APP_PROFILER"
compile_code "$PREFIX/APP_CORE"
compile_code "$PREFIX/APP_PROFILER"
log "code and virtualenvs are root-owned and read-only for the service user; easy-rsa, client-config and profiler.log belong to $SERVICE_USER"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="5 configuration"
phase 5 "configuration: environment file, root helper, doas, forwarding"
detect_ip() {
local ip
ip=$(ip -4 route get 1.1.1.1 2>/dev/null | sed -n 's/.* src \([0-9.]*\).*/\1/p' | head -n 1 || true)
[ -n "$ip" ] || ip=$(hostname -i 2>/dev/null | awk '{print $1}' || true)
case "$ip" in ""|127.*) ip="" ;; esac
printf '%s' "$ip"
}
HOST_IP=$(detect_ip)
CERT_HOST="${HOST_NAME:-${HOST_IP:-localhost}}"
SHOW_USER=""
SHOW_PASSWORD=""
if [ -f "$ENV_FILE" ]; then
log "$ENV_FILE exists and is kept as it is"
else
SECRET=$(openssl rand -hex 32 2>/dev/null || true)
[ "${#SECRET}" -eq 64 ] || SECRET=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')
[ "${#SECRET}" -eq 64 ] || die "cannot generate a random secret"
ADMIN_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 24)
[ "${#ADMIN_PASSWORD}" -eq 24 ] || die "cannot generate a random password"
(
umask 077
{
echo "# Created by deploy/native/install.sh on $(date -u '+%Y-%m-%d %H:%M:%S') UTC. root:root 600: keep it private, never commit it."
echo "# Read by the init system (systemd EnvironmentFile / OpenRC start_pre) as root; the services run as $SERVICE_USER."
echo "OVPMON_API_SECRET_KEY=$SECRET"
echo "OVPMON_API_HOST=127.0.0.1"
echo "OVPMON_API_PORT=5001"
echo "OVPMON_OPENVPN_MONITOR_DB_PATH=$DATA_DIR/openvpn_monitor.db"
echo "OVPMON_OPENVPN_MONITOR_LOG_PATH=$OVPN_LOG_DIR/openvpn-status.log"
echo "OVPMON_PROFILER_DB_PATH=$DATA_DIR/ovpn_profiler.db"
echo "OVPMON_LOGGING_LEVEL=INFO"
echo "OVPMON_CORS_ORIGINS=https://$CERT_HOST:$HTTPS_PORT"
echo "# This is a host install, never a container (matters when the host itself runs inside a container)"
echo "OVPMON_RUNTIME=host"
echo "OVPMON_OPENVPN_SERVICE=$OVPN_SERVICE"
echo "OVPMON_OPENVPN_CONFIG=$OVPN_CONFIG"
echo "# Initial administrator: used only while the users table is empty; install.sh removes these two lines after the first start"
echo "OVPMON_INITIAL_ADMIN_USER=$ADMIN_USER"
echo "OVPMON_INITIAL_ADMIN_PASSWORD=$ADMIN_PASSWORD"
} > "$ENV_FILE"
)
chown root:root "$ENV_FILE"
chmod 600 "$ENV_FILE"
unset SECRET ADMIN_PASSWORD
log "$ENV_FILE created (root:root 600, random secret)"
fi
# helper configuration (read by the root helper; strictly validated there)
_tmp="$TMPD/helper.conf"
helper_conf_text "$PREFIX" > "$_tmp"
if install_managed "$_tmp" "$HELPER_CONF" 644 "deploy/native/lib.sh helper_conf_text"; then log "$HELPER_CONF written (root:root 644)"; else log "$HELPER_CONF unchanged"; fi
[ -f "$HELPER_CONF" ] || die "$HELPER_CONF could not be written"
if install_helper "$SRC"; then log "helper installed: $HELPER_DEST (root:root 755)"; else log "helper unchanged: $HELPER_DEST"; fi
rc=0
install_doas "$SRC" || rc=$?
doas_target
case "$rc" in
0) log "doas rules installed ($DOAS_MODE: $DOAS_FILE)" ;;
1) log "doas rules unchanged ($DOAS_FILE)" ;;
2) warn "doas rules NOT installed: add the rules of DOCS/General/privilege-separation/doas-ovpmon.conf to $DOAS_FILE yourself" ;;
esac
if doas_selftest; then log "self-test: $SERVICE_USER can call the helper through doas"; else warn "the helper self-test failed: the profiler will not be able to configure or start OpenVPN until this is fixed"; fi
# IP forwarding (persistent) and the OpenVPN service prerequisites
printf '# %s\nnet.ipv4.ip_forward = 1\n' "$MARKER" > "$SYSCTL_FILE"
chmod 644 "$SYSCTL_FILE"
sysctl -w net.ipv4.ip_forward=1 >/dev/null 2>&1 || warn "could not enable net.ipv4.ip_forward now (it is set for the next boot); on a container the host must allow it"
if [ "$INIT" = openrc ]; then
rc-update add sysctl boot >/dev/null 2>&1 || true
fi
if [ "$OS_FAMILY" = alpine ]; then
# the OpenRC service reads /etc/openvpn/openvpn.conf: link it to server.conf (as the helper does)
if [ ! -e /etc/openvpn/openvpn.conf ] && [ ! -L /etc/openvpn/openvpn.conf ]; then
ln -s server.conf /etc/openvpn/openvpn.conf
log "/etc/openvpn/openvpn.conf -> server.conf"
fi
fi
svc_enable "$OVPN_SERVICE" || warn "could not enable the OpenVPN service $OVPN_SERVICE"
log "OpenVPN service $OVPN_SERVICE enabled, NOT started (no server.conf yet: generate it in the UI first)"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="6 services, UI, nginx"
phase 6 "service definitions, web UI, TLS certificate and nginx"
for s in $SERVICES; do
_tmp="$TMPD/$s"
render_unit "$SRC" "$s" "$PREFIX" "$_tmp"
if [ "$INIT" = systemd ]; then _mode=644; else _mode=755; fi
rc=0
install_managed "$_tmp" "$(unit_file "$s")" "$_mode" "$(unit_src "$SRC" "$s")" || rc=$?
case "$rc" in
0) log "installed $(unit_file "$s")" ;;
1) log "unchanged $(unit_file "$s")" ;;
2) warn "$(unit_file "$s") is not ours: left untouched" ;;
esac
done
svc_defs_reload
for s in $SERVICES; do svc_enable "$s" || warn "could not enable $s"; done
log "services enabled at boot: $SERVICES"
# UI files
mkdir -p "$(dirname "$WEB_ROOT")"
if [ -n "$UI_SRC" ]; then
stage_ui "$UI_SRC" "$WEB_ROOT.new"
swap_dir "$WEB_ROOT.new" "$WEB_ROOT" "$WEB_ROOT.old"
rm -rf "$WEB_ROOT.old"
log "UI files installed to $WEB_ROOT: $UI_ORIGIN"
elif [ ! -f "$WEB_ROOT/index.html" ]; then
mkdir -p "$WEB_ROOT"
cat > "$WEB_ROOT/index.html" <<'EOF'
<!doctype html><meta charset="utf-8"><title>OpenVPN Monitor</title>
<h1>OpenVPN Monitor</h1><p>The web UI is not installed (install.sh --no-ui). Run deploy/native/update.sh: it installs the UI from the repository; or pass --ui-dist FILE|DIR or --build-ui.</p>
EOF
chmod -R a+rX "$WEB_ROOT"
log "placeholder page written to $WEB_ROOT"
fi
# TLS certificate: self-signed, never regenerated
mkdir -p "$TLS_DIR"
chmod 750 "$TLS_DIR"
if [ -f "$TLS_DIR/ovpmon.crt" ] && [ -f "$TLS_DIR/ovpmon.key" ]; then
log "TLS certificate exists: kept ($TLS_DIR/ovpmon.crt)"
else
SAN="DNS:ovpmon,DNS:localhost,IP:127.0.0.1"
[ -z "$HOST_IP" ] || SAN="$SAN,IP:$HOST_IP"
if [ -n "$HOST_NAME" ] && [ "$HOST_NAME" != "$HOST_IP" ]; then
case "$HOST_NAME" in
*[!0-9.]*) SAN="$SAN,DNS:$HOST_NAME" ;;
*) SAN="$SAN,IP:$HOST_NAME" ;;
esac
fi
( umask 077
openssl req -x509 -nodes -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -days 825 \
-subj "/CN=$CERT_HOST" -addext "subjectAltName=$SAN" \
-keyout "$TLS_DIR/ovpmon.key" -out "$TLS_DIR/ovpmon.crt" >/dev/null 2>&1 ) \
|| die "cannot create the self-signed certificate"
chmod 600 "$TLS_DIR/ovpmon.key"
chmod 644 "$TLS_DIR/ovpmon.crt"
log "self-signed TLS certificate created for $SAN"
fi
# nginx site
_tmp="$TMPD/ovpmon.conf"
mkdir -p "$NGINX_CONF_DIR"
render_nginx "$SRC" "$HTTPS_PORT" "$_tmp"
rc=0
install_managed "$_tmp" "$NGINX_SITE" 644 "deploy/native/nginx/ovpmon.conf.template" || rc=$?
case "$rc" in
0) log "nginx site written: $NGINX_SITE (HTTPS on $HTTPS_PORT)" ;;
1) log "nginx site unchanged: $NGINX_SITE" ;;
2) die "$NGINX_SITE exists and is not ours; remove or rename it, then run the script again" ;;
esac
svc_enable nginx || warn "could not enable nginx at boot"
nginx_apply || die "nginx did not accept the configuration (see above)"
log "nginx running with the new site"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="7 start and health checks"
phase 7 "start the services and check them"
for s in $SERVICES; do
if svc_active "$s"; then
log "restarting $s (resumed install)"
svc_restart "$s" >/dev/null 2>&1 || { show_service_log "$s"; die "$s failed to restart"; }
else
log "starting $s"
svc_start "$s" >/dev/null 2>&1 || { show_service_log "$s"; die "$s failed to start"; }
fi
done
health_check "$HTTPS_PORT" || die "health checks failed (see above). Logs: $APP_LOG_DIR/ (OpenRC) or journalctl -u ovpmon-api -u ovpmon-gatherer -u ovpmon-profiler (systemd)"
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="8 initial administrator"
phase 8 "initial administrator"
resolve_db_paths "$PREFIX"
if [ -n "$(env_get OVPMON_INITIAL_ADMIN_USER)$(env_get OVPMON_INITIAL_ADMIN_PASSWORD)" ]; then
COUNT=$(users_count "$DB_MONITOR" || true)
case "$COUNT" in
""|*[!0-9]*)
warn "cannot read the users table (got '$COUNT'): OVPMON_INITIAL_ADMIN_* are LEFT in $ENV_FILE. Remove them by hand after you have logged in, then restart ovpmon-api"
;;
0)
warn "the users table is empty: the administrator was not created. OVPMON_INITIAL_ADMIN_* stay in $ENV_FILE. Check the log of ovpmon-api"
;;
*)
SHOW_USER=$(env_get OVPMON_INITIAL_ADMIN_USER)
SHOW_PASSWORD=$(env_get OVPMON_INITIAL_ADMIN_PASSWORD)
env_remove_admin
log "$COUNT user(s) exist: OVPMON_INITIAL_ADMIN_* removed from $ENV_FILE, restarting ovpmon-api without them"
if ! svc_restart ovpmon-api >/dev/null 2>&1 || ! wait_http "monitoring API" "http://127.0.0.1:5001/" '[1-5][0-9][0-9]'; then
# the seed is gone from the file: the one-time credentials must not be lost
printf '\n Administrator (shown ONCE): login %s, password %s\n' "$SHOW_USER" "$SHOW_PASSWORD"
die "ovpmon-api did not come back after removing the seed (see its log)"
fi
;;
esac
else
COUNT=$(users_count "$DB_MONITOR" || true)
case "$COUNT" in
0) warn "no users exist and $ENV_FILE has no OVPMON_INITIAL_ADMIN_USER / OVPMON_INITIAL_ADMIN_PASSWORD: add them and restart ovpmon-api" ;;
*) log "users already exist: nothing to seed" ;;
esac
fi
# ---------------------------------------------------------------------------------------------
CURRENT_PHASE="finish"
state_set OVPMON_PREFIX "$PREFIX"
state_set OVPMON_HTTPS_PORT "$HTTPS_PORT"
state_set OVPMON_OS "$OS_ID $OS_VERSION_ID"
state_set OVPMON_VERSION "$(cat "$PREFIX/VERSION")"
[ -n "$(state_get OVPMON_INSTALLED_AT)" ] || state_set OVPMON_INSTALLED_AT "$(now_iso)"
rm -f "$INCOMPLETE_MARK"
UPLINK=$(ip -4 route get 1.1.1.1 2>/dev/null | sed -n 's/.* dev \([^ ]*\).*/\1/p' | head -n 1 || true)
UPLINK="${UPLINK:-<uplink-interface>}"
FP=$(openssl x509 -noout -fingerprint -sha256 -in "$TLS_DIR/ovpmon.crt" 2>/dev/null | sed 's/^.*=//' || true)
printf '\n'
log "=== INSTALLED: $OS_ID $OS_VERSION_ID, $INIT, prefix $PREFIX ==="
printf ' Web UI: https://%s:%s/ (self-signed certificate; SHA-256 %s)\n' "${HOST_NAME:-${HOST_IP:-<server-address>}}" "$HTTPS_PORT" "${FP:-n/a}"
printf ' UI files: %s (%s)\n' "$WEB_ROOT" "${UI_ORIGIN:-placeholder page (--no-ui)}"
printf ' Config: %s (root:root 600), helper: %s, %s\n' "$ENV_FILE" "$HELPER_CONF" "$HELPER_DEST"
printf ' Data: %s (databases), %s/APP_PROFILER/easy-rsa (PKI), %s/APP_PROFILER/client-config\n' "$DATA_DIR" "$PREFIX" "$PREFIX"
printf ' Logs: %s, %s\n' "$APP_LOG_DIR" "$OVPN_LOG_DIR"
if [ -n "$SHOW_PASSWORD" ]; then
printf '\n Administrator (shown ONCE, it is not stored anywhere any more):\n'
printf ' login: %s\n' "$SHOW_USER"
printf ' password: %s\n' "$SHOW_PASSWORD"
fi
cat <<EOF
Next steps:
1. Open the Web UI and sign in; change the password and enable 2FA (Account).
2. PKI Configuration -> Initialize PKI (wait until it finishes).
3. Generate the server configuration and start the VPN server in the UI
(the profiler installs $OVPN_CONFIG through the root helper and controls $OVPN_SERVICE).
4. Firewall (NOT configured by this script): open ${HTTPS_PORT}/tcp and the VPN port (1194/udp by default),
and give the VPN clients internet access, for example (replace the subnet with the one of the server settings):
iptables -t nat -A POSTROUTING -s 172.20.1.0/24 -o $UPLINK -j MASQUERADE
iptables -A FORWARD -i tun+ -o $UPLINK -j ACCEPT
iptables -A FORWARD -i $UPLINK -o tun+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
net.ipv4.ip_forward is enabled ($SYSCTL_FILE). Persistence and nftables variants: see the guide.
With nftables: $SCRIPT_DIR/firewall.sh sets masquerade, TCP MSS clamping and TTL for the VPN clients
(read the interfaces, --dry-run first; after the VPN server configuration exists).
5. Replace the self-signed certificate ($TLS_DIR/ovpmon.crt and .key) with a real one when a domain is available.
Update later with: $SCRIPT_DIR/update.sh Backup: $SCRIPT_DIR/backup.sh
Log of this run: $DEPLOY_LOG
EOF