This repository has been archived on 2026-11-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
WGTools-Extended/scripts/install.sh
T
asminme affe28ad71
Wydanie WGTools-Extended / release (push) Failing after 16s
1.0.0
2026-10-30 00:35:08 +01:00

239 lines
9.4 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
APP_DIR=/opt/wgtools
# Keep custom settings when upgrading an existing native installation.
# Avoid overriding environment explicitly supplied for a fresh install.
if [[ -r /etc/wgtools/wgtools.env ]]; then
# shellcheck disable=SC1091
source /etc/wgtools/wgtools.env
fi
WG_INTERFACE="${WG_INTERFACE:-${WGTOOLS_INTERFACE:-wg0}}"
WEB_PORT="${WGTOOLS_PORT:-8341}"
SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
if [[ ${EUID} -ne 0 ]]; then
echo "Run this installer as root." >&2
exit 1
fi
if [[ ! "$WG_INTERFACE" =~ ^[A-Za-z0-9_.-]{1,15}$ ]]; then
echo "Invalid WireGuard interface: $WG_INTERFACE" >&2
exit 1
fi
echo "[1/8] Installing WGTools runtime packages..."
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-venv python3-pip iproute2 iputils-ping traceroute curl openssh-client sshpass openssl
echo "[2/8] Preparing directories..."
install -d -o root -g root -m 0755 /var/lib/wgtools /etc/wgtools /usr/local/libexec /usr/local/bin
if [[ ! -s /var/lib/wgtools/session.key ]]; then
python3 - <<'PYSECRET'
from pathlib import Path
import secrets
p=Path('/var/lib/wgtools/session.key')
p.write_text(secrets.token_hex(32), encoding='utf-8')
p.chmod(0o600)
PYSECRET
fi
touch /var/lib/wgtools/wgtools.db
chmod 0600 /var/lib/wgtools/session.key /var/lib/wgtools/wgtools.db
echo "[3/8] Installing application..."
# Stop only the WGTools web service while replacing its files. WireGuard itself is not touched.
if systemctl is-active --quiet wgtools-telegram.service 2>/dev/null; then
systemctl stop wgtools-telegram.service
fi
if systemctl is-active --quiet wgtools-expiry.timer 2>/dev/null; then
systemctl stop wgtools-expiry.timer
fi
if systemctl is-active --quiet wgtools-certbot-renew.timer 2>/dev/null; then
systemctl stop wgtools-certbot-renew.timer
fi
if systemctl is-active --quiet wgtools.service 2>/dev/null; then
systemctl stop wgtools.service
fi
rm -rf "$APP_DIR"
mkdir -p "$APP_DIR"
cp -a "$SOURCE_DIR"/. "$APP_DIR"/
rm -rf "$APP_DIR/.git" "$APP_DIR/.github" "$APP_DIR/.venv" "$APP_DIR/.test-deps" "$APP_DIR/tests"
find "$APP_DIR" -type d -name __pycache__ -prune -exec rm -rf {} + 2>/dev/null || true
chown -R root:root "$APP_DIR"
# Normalize executable bits after archive extraction or Windows/Git workflows.
chmod 0755 "$APP_DIR/scripts/install.sh" "$APP_DIR/scripts/uninstall.sh" "$APP_DIR/scripts/full-clean.sh" "$APP_DIR/scripts/wgtools-web" "$APP_DIR/scripts/wgtools-cli" "$APP_DIR/scripts/wgtools-certbot-reload" "$APP_DIR/scripts/wgtools-helper"
python3 -m venv "$APP_DIR/.venv"
"$APP_DIR/.venv/bin/pip" install --upgrade pip >/dev/null
"$APP_DIR/.venv/bin/pip" install -r "$APP_DIR/requirements.txt"
echo "[4/8] Installing root helper..."
install -o root -g root -m 0755 "$APP_DIR/scripts/wgtools-helper" /usr/local/libexec/wgtools-helper
install -o root -g root -m 0755 "$APP_DIR/scripts/wgtools-certbot-reload" /usr/local/libexec/wgtools-certbot-reload
install -o root -g root -m 0755 "$APP_DIR/scripts/wgtools-cli" /usr/local/bin/wgtools
if [[ ! -f /etc/wgtools/wgtools.env ]]; then
cat > /etc/wgtools/wgtools.env <<ENV
WGTOOLS_INTERFACE=$WG_INTERFACE
WGTOOLS_PORT=$WEB_PORT
WGTOOLS_HELPER=/usr/local/libexec/wgtools-helper
WGTOOLS_REFRESH_SECONDS=5
WGTOOLS_ONLINE_THRESHOLD_SECONDS=180
WGTOOLS_DATABASE=/var/lib/wgtools/wgtools.db
WGTOOLS_SESSION_KEY_FILE=/var/lib/wgtools/session.key
WGTOOLS_SESSION_MAX_AGE_SECONDS=2592000
ENV
chmod 0644 /etc/wgtools/wgtools.env
else
echo "Preserving existing /etc/wgtools/wgtools.env"
fi
rm -f /etc/sudoers.d/wgtools
echo "[5/8] Installing systemd services (run as root)..."
cp "$APP_DIR/systemd/wgtools.service" /etc/systemd/system/wgtools.service
cp "$APP_DIR/systemd/wgtools-telegram.service" /etc/systemd/system/wgtools-telegram.service
cp "$APP_DIR/systemd/wgtools-expiry.service" /etc/systemd/system/wgtools-expiry.service
cp "$APP_DIR/systemd/wgtools-expiry.timer" /etc/systemd/system/wgtools-expiry.timer
cp "$APP_DIR/systemd/wgtools-certbot-renew.service" /etc/systemd/system/wgtools-certbot-renew.service
cp "$APP_DIR/systemd/wgtools-certbot-renew.timer" /etc/systemd/system/wgtools-certbot-renew.timer
systemctl daemon-reload
systemctl enable wgtools.service >/dev/null
systemctl enable wgtools-telegram.service >/dev/null
systemctl enable wgtools-expiry.timer >/dev/null
if [[ -r /etc/wgtools/https.env ]] && grep -q '^WGTOOLS_HTTPS_ENABLED=1$' /etc/wgtools/https.env; then
systemctl enable wgtools-certbot-renew.timer >/dev/null
systemctl restart wgtools-certbot-renew.timer
fi
systemctl restart wgtools.service
systemctl restart wgtools-expiry.timer
echo "[6/8] Waiting for web service..."
READY=0
HTTPS_ENABLED=0
HTTPS_PORT=443
WGTOOLS_ACCESS_PATH=""
if [[ -r /etc/wgtools/https.env ]]; then
# shellcheck disable=SC1091
source /etc/wgtools/https.env
HTTPS_ENABLED="${WGTOOLS_HTTPS_ENABLED:-0}"
HTTPS_PORT="${WGTOOLS_HTTPS_PORT:-443}"
WGTOOLS_ACCESS_PATH="${WGTOOLS_ACCESS_PATH:-}"
fi
for _ in $(seq 1 25); do
if [[ "$HTTPS_ENABLED" == "1" ]]; then
if curl -kfsS "https://127.0.0.1:${HTTPS_PORT}${WGTOOLS_ACCESS_PATH%/}/healthz" >/dev/null 2>&1; then
READY=1
break
fi
elif curl -fsS "http://127.0.0.1:${WEB_PORT}/healthz" >/dev/null 2>&1; then
READY=1
break
fi
sleep 1
done
if [[ "$READY" -ne 1 ]]; then
echo "WGTools did not become ready. Recent log:" >&2
journalctl -u wgtools.service -n 60 --no-pager >&2 || true
exit 1
fi
echo "[7/8] Starting integrated Telegram bot service..."
systemctl restart wgtools-telegram.service
sleep 1
if ! systemctl is-active --quiet wgtools-telegram.service; then
echo "WGTools Telegram service did not start. Recent log:" >&2
journalctl -u wgtools-telegram.service -n 40 --no-pager >&2 || true
exit 1
fi
echo "Telegram service OK (it stays idle until a BotFather token is enabled in Settings)."
echo "[8/8] Running helper self-test..."
printf '%s\n' '{"view":"server"}' | timeout --foreground 20s /usr/local/libexec/wgtools-helper snapshot "$WG_INTERFACE" >/tmp/wgtools-selftest.json
python3 - <<'PY'
import json
with open('/tmp/wgtools-selftest.json', encoding='utf-8') as f:
data=json.load(f)
assert data.get('ok') is True, data
wg=data.get('wireguard', {})
assert isinstance(data.get('route_items', []), list), data
assert isinstance(data.get('firewall_items', []), list), data
assert isinstance(data.get('client_groups', []), list), data
print(f"Helper OK: state={wg.get('state')} interface={data.get('interface')} routes={len(data.get('route_items', []))} firewall={len(data.get('firewall_items', []))}")
PY
rm -f /tmp/wgtools-selftest.json
# Create and immediately remove a full migration backup as a non-destructive smoke test.
timeout --foreground 60s /usr/local/libexec/wgtools-helper migration_backup_create "$WG_INTERFACE" </dev/null >/tmp/wgtools-migration-selftest.json
python3 - <<'PY'
import json
from pathlib import Path
with open('/tmp/wgtools-migration-selftest.json', encoding='utf-8') as f:
data=json.load(f)
assert data.get('ok') is True, data
p=Path(data.get('path',''))
assert p.is_file() and p.stat().st_size > 0, data
print(f"Migration backup helper OK: {p.name} ({p.stat().st_size} bytes)")
p.unlink()
PY
rm -f /tmp/wgtools-migration-selftest.json
# Smoke-test the raw configuration helper when a WireGuard config already exists.
# The HTTP API is intentionally protected by authentication from v0.6 onward.
if [[ -f "/etc/wireguard/${WG_INTERFACE}.conf" ]]; then
timeout --foreground 20s /usr/local/libexec/wgtools-helper raw_config_get "$WG_INTERFACE" </dev/null >/tmp/wgtools-raw-config-selftest.json
python3 - <<'PY'
import json
with open('/tmp/wgtools-raw-config-selftest.json', encoding='utf-8') as f:
data=json.load(f)
assert data.get('ok') is True, data
assert isinstance(data.get('content'), str) and '[Interface]' in data.get('content', ''), data
print('Raw config helper OK')
PY
rm -f /tmp/wgtools-raw-config-selftest.json
fi
echo
echo "WGTools v$(cat "$APP_DIR/VERSION") is installed."
echo "Service account: root (no additional Linux user is created)."
echo "WireGuard is NOT installed by the WGTools deploy script on a clean VM."
echo "If it is missing, install it later from Server -> Install WireGuard."
if [[ "$HTTPS_ENABLED" == "1" ]]; then
HTTPS_OPEN_HOST=""
if [[ -r /etc/wgtools/https.json ]]; then
HTTPS_OPEN_HOST="$(python3 - <<'PYHTTPSHOST'
import json
from pathlib import Path
try:
data=json.loads(Path('/etc/wgtools/https.json').read_text(encoding='utf-8'))
print(str(data.get('target') or '').strip())
except Exception:
pass
PYHTTPSHOST
)"
fi
if [[ -z "$HTTPS_OPEN_HOST" ]]; then
HTTPS_OPEN_HOST="$(hostname -I | awk '{print $1}')"
fi
if [[ "$HTTPS_OPEN_HOST" == *:* && "$HTTPS_OPEN_HOST" != \[*\] ]]; then
HTTPS_OPEN_HOST="[$HTTPS_OPEN_HOST]"
fi
HTTPS_OPEN_PATH="${WGTOOLS_ACCESS_PATH:-}"
if [[ -n "$HTTPS_OPEN_PATH" && "$HTTPS_OPEN_PATH" != */ ]]; then
HTTPS_OPEN_PATH="${HTTPS_OPEN_PATH}/"
fi
if [[ "$HTTPS_PORT" == "443" ]]; then
echo "Open: https://${HTTPS_OPEN_HOST}${HTTPS_OPEN_PATH}"
else
echo "Open: https://${HTTPS_OPEN_HOST}:${HTTPS_PORT}${HTTPS_OPEN_PATH}"
fi
else
echo "Open: http://$(hostname -I | awk '{print $1}'):${WEB_PORT}"
fi
echo "Web: systemctl status wgtools --no-pager"
echo "Telegram: systemctl status wgtools-telegram --no-pager"
echo "Access: systemctl status wgtools-expiry.timer --no-pager"
echo "Web logs: journalctl -u wgtools -f"
echo "Bot logs: journalctl -u wgtools-telegram -f"
echo "Console: wgtools"