-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathhaproxy.sh
More file actions
120 lines (103 loc) · 3.17 KB
/
Copy pathhaproxy.sh
File metadata and controls
120 lines (103 loc) · 3.17 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
#!/bin/bash
CONFIG_FILE="/etc/haproxy/haproxy.cfg"
BACKUP_FILE="/etc/haproxy/haproxy.cfg.bak"
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
}
install_haproxy() {
echo "Installing HAProxy..."
apt-get update && apt-get install -y haproxy
echo "HAProxy installed."
set_default_config
}
set_default_config() {
cat <<EOL > "$CONFIG_FILE"
global
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
mode tcp
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
EOL
}
generate_haproxy_config() {
local -a ports target_ips
IFS=',' read -ra ports <<< "$1"
IFS=',' read -ra target_ips <<< "$2"
echo "Generating HAProxy configuration..."
for port in "${ports[@]}"; do
cat <<EOL >> "$CONFIG_FILE"
frontend frontend_$port
bind *:$port
default_backend backend_$port
backend backend_$port
EOL
for i in "${!target_ips[@]}"; do
local mode=""
[ $i -ne 0 ] && mode=" backup"
echo " server server$((i+1)) ${target_ips[$i]}:$port check$mode" >> "$CONFIG_FILE"
done
done
echo "HAProxy configuration updated."
}
add_ip_ports() {
read -p "Enter target IP: " user_ips
read -p "Enter ports (comma-separated): " user_ports
generate_haproxy_config "$user_ports" "$user_ips"
if haproxy -c -f "$CONFIG_FILE"; then
echo "Restarting HAProxy..."
systemctl restart haproxy
echo "HAProxy restarted successfully."
else
echo "Invalid HAProxy configuration!"
fi
}
clear_configs() {
echo "Backing up current configuration..."
cp "$CONFIG_FILE" "$BACKUP_FILE" || { echo "Backup failed!"; return; }
echo "Clearing existing IP and port configurations..."
awk '!/^frontend frontend_/ && !/^backend backend_/' "$BACKUP_FILE" > "$CONFIG_FILE"
echo "Stopping HAProxy..."
systemctl stop haproxy && echo "HAProxy stopped." || echo "Failed to stop HAProxy."
echo "Configuration cleared."
}
remove_haproxy() {
echo "Removing HAProxy..."
apt-get remove --purge -y haproxy && apt-get autoremove -y
echo "HAProxy removed."
}
check_root
while true; do
clear
cat <<MENU
+------------------------------------------------------------+
| HAProxy Management Menu (Dev.MrAmini) |
+------------------------------------------------------------+
| 1) Install HAProxy |
| 2) Add IPs and Ports |
| 3) Clear Configurations |
| 4) Remove HAProxy |
| 9) Exit |
+------------------------------------------------------------+
MENU
read -p "Select an option: " choice
case $choice in
1) install_haproxy ;;
2) add_ip_ports ;;
3) clear_configs ;;
4) remove_haproxy ;;
9) echo "Exiting..."; break ;;
*) echo "Invalid option. Try again." ;;
esac
sleep 1.5
done