-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.sh
More file actions
131 lines (111 loc) · 4.56 KB
/
uninstall.sh
File metadata and controls
131 lines (111 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
set -e # Exit on error
# --- Configuration ---
CORE_SERVICE_NAME="dynamic-port-guard"
CORE_BIN_PATH="/usr/local/bin/dynamic_port_guard.sh"
CORE_SERVICE_FILE="/etc/systemd/system/${CORE_SERVICE_NAME}.service"
CORE_CONFIG_FILE="/etc/dynamic-port-guard.conf"
WEBUI_SERVICE_NAME="dynamic-port-webui"
WEBUI_APP_DIR="/opt/${WEBUI_SERVICE_NAME}"
WEBUI_SERVICE_FILE="/etc/systemd/system/${WEBUI_SERVICE_NAME}.service"
REMOVE_CORE=true
REMOVE_WEBUI=true
REMOVE_CONFIG=false # Ask user about config
REINSTALL=false
# --- Helper Functions ---
log_info() { echo "[*] $1"; }
log_success() { echo "[✓] $1"; }
log_error() { echo "[!] $Error: $1" >&2; }
# --- Argument Parsing ---
while [[ "$#" -gt 0 ]]; do
case $1 in
--core-only) REMOVE_WEBUI=false ;;
--webui-only) REMOVE_CORE=false ;;
--remove-config) REMOVE_CONFIG=true ;; # Flag to force config removal
--reinstall)
REINSTALL=true
REMOVE_CONFIG=true
log_info "Option: Batch uninstall for reinstall"
;;
*)
log_error "Unknown parameter passed: $1"
exit 1
;;
esac
shift
done
# --- Root Check ---
if [ "$(id -u)" -ne 0 ]; then
log_error "This script must be run as root. Use sudo."
exit 1
fi
# --- Core Service Uninstallation ---
if $REMOVE_CORE; then
log_info "Uninstalling Dynamic Port Guard Core Service..."
# 1. Stop and disable service
log_info "Stopping and disabling ${CORE_SERVICE_NAME} service..."
systemctl stop "$CORE_SERVICE_NAME" || true # Ignore error if not running
systemctl disable "$CORE_SERVICE_NAME" || true # Ignore error if not enabled
# 2. Remove files
log_info "Removing script: ${CORE_BIN_PATH}"
rm -f "$CORE_BIN_PATH"
log_info "Removing service file: ${CORE_SERVICE_FILE}"
rm -f "$CORE_SERVICE_FILE"
# 3. Handle config file
if [ -f "$CORE_CONFIG_FILE" ]; then
if $REMOVE_CONFIG; then
log_info "Removing configuration file: ${CORE_CONFIG_FILE}"
rm -f "$CORE_CONFIG_FILE"
else
if ! $REINSTALL; then
read -p "Do you want to remove the configuration file ${CORE_CONFIG_FILE}? [y/N] " -r REPLY
else
REPLY=Y
fi
echo # Move to new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
log_info "Removing configuration file: ${CORE_CONFIG_FILE}"
rm -f "$CORE_CONFIG_FILE"
else
log_info "Keeping configuration file: ${CORE_CONFIG_FILE}"
fi
fi
fi
# 4. Run firewall cleanup manually (optional, systemd stop should trigger trap)
# log_info "Attempting to run firewall cleanup (best effort)..."
# if [ -f "$CORE_BIN_PATH" ]; then # If script was somehow kept, try running its cleanup
# "$CORE_BIN_PATH" cleanup # This needs the script to handle a 'cleanup' argument
# else # Manually remove chain if script is gone (assuming iptables and default name)
# log_info "Running manual iptables cleanup for chain PORTGUARD_ALLOW..."
# CHAIN_NAME=$(grep -Po 'IPTABLES_CHAIN="\K[^"]+' "$CORE_CONFIG_FILE" 2>/dev/null || echo "PORTGUARD_ALLOW")
# iptables -D INPUT -j "$CHAIN_NAME" 2>/dev/null || true
# ip6tables -D INPUT -j "$CHAIN_NAME" 2>/dev/null || true
# iptables -F "$CHAIN_NAME" 2>/dev/null || true
# ip6tables -F "$CHAIN_NAME" 2>/dev/null || true
# iptables -X "$CHAIN_NAME" 2>/dev/null || true
# ip6tables -X "$CHAIN_NAME" 2>/dev/null || true
# fi
log_success "Dynamic Port Guard Core Service uninstalled."
fi
# --- Web UI Uninstallation ---
if $REMOVE_WEBUI; then
log_info "Uninstalling Dynamic Port Guard Web UI..."
# 1. Stop and disable service
log_info "Stopping and disabling ${WEBUI_SERVICE_NAME} service..."
systemctl stop "$WEBUI_SERVICE_NAME" || true
systemctl disable "$WEBUI_SERVICE_NAME" || true
# 2. Remove files
log_info "Removing application directory: ${WEBUI_APP_DIR}"
# + # This removes the app code AND the venv directory inside it
rm -rf "$WEBUI_APP_DIR"
log_info "Removing service file: ${WEBUI_SERVICE_FILE}"
rm -f "$WEBUI_SERVICE_FILE"
# 3. Optional: Remove dependencies (tricky, might be used by others)
# log_info "Note: Python dependencies (Flask) are not automatically removed."
# log_info "You can remove them manually if desired (e.g., 'sudo apt-get remove python3-flask')."
log_success "Dynamic Port Guard Web UI uninstalled."
fi
# --- Final Steps ---
log_info "Reloading systemd daemon..."
systemctl daemon-reload
log_success "Uninstallation finished."