#!/usr/bin/env bash set -euo pipefail if [[ ${EUID:-$(id -u)} -ne 0 ]]; then echo "Run as root: sudo bash $0" >&2 exit 1 fi if [[ "${WGTOOLS_FULL_CLEAN_CONFIRMED:-0}" != "1" ]]; then cat <<'TXT' WARNING: FULL CLEANUP removes WGTools, all WireGuard interfaces/configs, local WGTools Relay state and the WGTools Let's Encrypt certificate from THIS host. Remote Relay Agents on other VPS hosts are not contacted. TXT printf '\nType DELETE ALL to continue: ' read -r answer [[ "$answer" == "DELETE ALL" ]] || { echo "Cancelled."; exit 0; } fi echo "[1/7] Stopping WGTools and Relay services..." for unit in \ wgtools.service wgtools-telegram.service \ wgtools-expiry.timer wgtools-expiry.service \ wgtools-certbot-renew.timer wgtools-certbot-renew.service \ wg-quick@wgnode0.service do systemctl disable --now "$unit" >/dev/null 2>&1 || true done mapfile -t relay_units < <( systemctl list-unit-files --type=service --no-legend 2>/dev/null \ | awk '$1 ~ /^wgtools-relay-(agent|transport-|home-)/ {print $1}' \ | sort -u ) for unit in "${relay_units[@]}"; do systemctl disable --now "$unit" >/dev/null 2>&1 || true done echo "[2/7] Bringing down WireGuard interfaces..." if [[ -d /etc/wireguard ]]; then shopt -s nullglob for conf in /etc/wireguard/*.conf; do iface="$(basename "$conf" .conf)" systemctl disable --now "wg-quick@${iface}.service" >/dev/null 2>&1 || true if command -v wg-quick >/dev/null 2>&1; then wg-quick down "$iface" >/dev/null 2>&1 || true fi done shopt -u nullglob fi # Also stop enabled wg-quick units whose config may already be missing. while read -r unit _; do [[ "$unit" == wg-quick@*.service ]] || continue systemctl disable --now "$unit" >/dev/null 2>&1 || true done < <(systemctl list-unit-files 'wg-quick@*.service' --no-legend 2>/dev/null || true) if command -v wg >/dev/null 2>&1; then for iface in $(wg show interfaces 2>/dev/null || true); do ip link delete "$iface" >/dev/null 2>&1 || true done fi echo "[3/7] Removing WGTools firewall/NAT leftovers..." if command -v iptables >/dev/null 2>&1; then # Known direct Nodes rules. while iptables -C FORWARD -i wgnode0 -j ACCEPT >/dev/null 2>&1; do iptables -D FORWARD -i wgnode0 -j ACCEPT || true; done while iptables -C FORWARD -o wgnode0 -j ACCEPT >/dev/null 2>&1; do iptables -D FORWARD -o wgnode0 -j ACCEPT || true; done # Remove every rule that jumps to a WGTOOLS-* custom chain (including # source/comment-qualified jumps), then remove the chains themselves. for table in filter nat mangle; do mapfile -t chains < <(iptables -t "$table" -S 2>/dev/null | awk '$1=="-N" && $2 ~ /^WGTOOLS-/ {print $2}') for chain in "${chains[@]}"; do mapfile -t jump_rules < <(iptables -t "$table" -S 2>/dev/null | grep -E "^-A .* -j ${chain}($| )" || true) for rule in "${jump_rules[@]}"; do eval "iptables -t '$table' ${rule/-A /-D }" >/dev/null 2>&1 || true done iptables -t "$table" -F "$chain" >/dev/null 2>&1 || true iptables -t "$table" -X "$chain" >/dev/null 2>&1 || true done done # Protection rules are direct FORWARD entries carrying a WGTools comment. mapfile -t protect_rules < <(iptables -S FORWARD 2>/dev/null | grep -- '--comment wgtools-' || true) for rule in "${protect_rules[@]}"; do # iptables -S output is generated by the local kernel/iptables, not user input. eval "iptables ${rule/-A FORWARD/-D FORWARD}" >/dev/null 2>&1 || true done fi echo "[4/7] Removing WGTools files and systemd units..." rm -f \ /etc/systemd/system/wgtools.service \ /etc/systemd/system/wgtools-telegram.service \ /etc/systemd/system/wgtools-expiry.service \ /etc/systemd/system/wgtools-expiry.timer \ /etc/systemd/system/wgtools-certbot-renew.service \ /etc/systemd/system/wgtools-certbot-renew.timer \ /etc/systemd/system/wgtools-relay-agent.service \ /etc/systemd/system/wgtools-relay-transport-*.service \ /etc/systemd/system/wgtools-relay-home-*.service \ /etc/sudoers.d/wgtools \ /usr/local/libexec/wgtools-helper \ /usr/local/libexec/wgtools-certbot-reload \ /usr/local/bin/wgtools \ /etc/sysctl.d/99-wgtools-wireguard.conf \ /var/lib/systemd/timers/stamp-wgtools-* rm -rf \ /opt/wgtools \ /etc/wgtools \ /var/lib/wgtools \ /opt/wgtools-relay \ /var/lib/wgtools-relay \ /etc/wireguard \ /tmp/wgtools-install \ /tmp/wgtools-update* \ /tmp/wgtools-cli-* \ /tmp/wgtools-* echo "[5/7] Removing WGTools Let's Encrypt certificate..." if command -v certbot >/dev/null 2>&1; then certbot delete --cert-name wgtools --non-interactive >/dev/null 2>&1 || true fi rm -rf /etc/letsencrypt/live/wgtools /etc/letsencrypt/archive/wgtools rm -f /etc/letsencrypt/renewal/wgtools.conf echo "[6/7] Removing WireGuard packages..." export DEBIAN_FRONTEND=noninteractive if command -v apt-get >/dev/null 2>&1; then apt-get purge -y wireguard wireguard-tools || true apt-get autoremove -y --purge || true fi echo "[7/7] Reloading system state..." systemctl daemon-reload systemctl reset-failed >/dev/null 2>&1 || true sysctl --system >/dev/null 2>&1 || true echo echo "Done: WGTools, local Relay state, all WireGuard configs/interfaces and WGTools-specific files were removed from this host." echo "Remote Relay Agents on other VPS hosts were not changed." echo "Shared OS tools (Python, iproute2, iptables, certbot, OpenSSH) were intentionally preserved."