-
Notifications
You must be signed in to change notification settings - Fork 28
/
haproxy.sh
450 lines (384 loc) · 14.1 KB
/
haproxy.sh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/bin/bash
# Logo
show_logo() {
echo -e "${BLUE}"
cat << "EOF"
__ _____ ____
/ / / / | / __ \_________ _ ____ __
/ /_/ / /| | / /_/ / ___/ __ \| |/_/ / / /
/ __ / ___ |/ ____/ / / /_/ /> </ /_/ /
/_/ /_/_/ |_/_/ /_/ \____/_/|_|\__, /
/____/
github.com/Musixal v1.2
EOF
echo -e "${NC}"
}
# Check if the script is being run as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
sleep 2
exit 1
fi
# Function to install jq if not already installed
install_jq() {
if ! command -v jq &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
echo -e "${RED}jq is not installed. Installing...${NC}"
sleep 1
sudo apt-get update
sudo apt-get install -y jq
else
echo -e "${RED}Error: Unsupported package manager. Please install jq manually.${NC}\n"
read -p "Press any key to continue..."
exit 1
fi
fi
}
# Install jq
install_jq
install_haproxy() {
if ! command -v haproxy &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
echo -e "${RED}HAProxy is not installed. Installing...${NC}"
sleep 1
sudo apt-get update
sudo apt-get install -y haproxy
else
echo -e "${RED}Error: Unsupported package manager. Please install HAProxy manually.${NC}\n"
read -p "Press any key to continue..."
exit 1
fi
fi
}
#install HAProxy
install_haproxy
# Fetch server country
SERVER_COUNTRY=$(curl -sS "http://ipwhois.app/json/$SERVER_IP" | jq -r '.country')
# Fetch server isp
SERVER_ISP=$(curl -sS "http://ipwhois.app/json/$SERVER_IP" | jq -r '.isp')
# Function to display server location and IP
display_server_info() {
echo -e "${GREEN}Location:${NC} $SERVER_COUNTRY "
echo -e "${GREEN}Datacenter:${NC} $SERVER_ISP"
}
# Function to show HAProxy status
show_haproxy_status() {
# Check if HAProxy is installed
if ! command -v haproxy &>/dev/null; then
echo -e "${RED}HAProxy is not installed.${NC}"
return
fi
# Check the status of HAProxy service
systemctl is-active --quiet haproxy && echo -e "${GREEN}HAProxy is active${NC}" || echo -e "${RED}HAProxy is not active${NC}"
}
# Menu for multi-server configuration
multiple_server_menu() {
clear
echo -e "Select an option:"
echo ''
echo -e "${GREEN}1. New Configuration${NC}"
echo -e "${BLUE}2. Add a new config${NC}"
echo -e "${RED}3. Quit${NC}"
echo ''
read -p "Enter your choice: " choice
case $choice in
1) configure_new_tunnel ;;
2) add_new_server ;;
3) echo -e "${RED}3. Exiting...${NC}" && sleep 1 && return 0 ;;
*) echo -e "${RED}Invalid option!${NC}" && sleep 1 ;;
esac
}
# Define the default HAProxy configuration file path
haproxy_config_file="/etc/haproxy/haproxy.cfg"
# Function to configure tunnel
configure_new_tunnel() {
clear
# Prompt the user for confirmation
read -p "All your previous configs will be deleted, continue? (yes/no): " confirm
# Check user's response
if ! [[ $confirm == "yes" || $confirm == "Yes" || $confirm == "YES" ]]; then
echo -e "${RED}Operation cancelled by user.${NC}" && sleep 1
return 1
fi
echo
# Verify if the file exists, if not, create it
if [ ! -f "$haproxy_config_file" ]; then
touch "$haproxy_config_file"
fi
echo "# HAProxy configuration generated by script" > "$haproxy_config_file"
echo "global" >> "$haproxy_config_file"
echo " log /dev/log local0" >> "$haproxy_config_file"
echo " log /dev/log local1 notice" >> "$haproxy_config_file"
echo " chroot /var/lib/haproxy" >> "$haproxy_config_file"
echo " stats socket /run/haproxy/admin.sock mode 660 level admin" >> "$haproxy_config_file"
echo " stats timeout 30s" >> "$haproxy_config_file"
echo " user haproxy" >> "$haproxy_config_file"
echo " group haproxy" >> "$haproxy_config_file"
echo " daemon" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
echo "defaults" >> "$haproxy_config_file"
echo " log global" >> "$haproxy_config_file"
echo " mode tcp" >> "$haproxy_config_file"
echo " option tcplog" >> "$haproxy_config_file"
echo " option dontlognull" >> "$haproxy_config_file"
echo " timeout connect 5000ms" >> "$haproxy_config_file"
echo " timeout client 50000ms" >> "$haproxy_config_file"
echo " timeout server 50000ms" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
# Add multi-port support
read -p "1. Enter HAProxy bind ports (e.g., 443,8443,2096): " haproxy_bind_ports
read -p "2. Enter Destination ports (in the same order as HAProxy bind ports, e.g., 443,8443,2096): " destination_ports
read -p "3. Enter Destination (Kharej) IP address: " destination_ip
# Split the comma-separated ports into arrays
IFS=',' read -r -a haproxy_ports_array <<< "$haproxy_bind_ports"
IFS=',' read -r -a destination_ports_array <<< "$destination_ports"
# Check if both arrays have the same length
if [ "${#haproxy_ports_array[@]}" -ne "${#destination_ports_array[@]}" ]; then
echo -e "${RED}The number of HAProxy bind ports and Destination ports must match.${NC}"
read -p "Press Enter to continue..."
systemctl stop haproxy
rm -f "$haproxy_config_file"
return 1
fi
# Iterate over each port in the array
for i in "${!haproxy_ports_array[@]}"; do
haproxy_bind_port="${haproxy_ports_array[$i]}"
destination_port="${destination_ports_array[$i]}"
# Trim any leading or trailing whitespace from the ports
haproxy_bind_port=$(echo "$haproxy_bind_port" | xargs)
destination_port=$(echo "$destination_port" | xargs)
# Append frontend and backend configurations to HAProxy configuration file
echo "frontend frontend_$haproxy_bind_port" >> "$haproxy_config_file"
echo " bind *:$haproxy_bind_port" >> "$haproxy_config_file"
echo " default_backend backend_$haproxy_bind_port" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
echo "backend backend_$haproxy_bind_port" >> "$haproxy_config_file"
echo " server server_$haproxy_bind_port $destination_ip:$destination_port" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
done
echo
echo -e "${GREEN}Configuration updated successfully in $haproxy_config_file${NC}"
# Restart HAProxy service
systemctl restart haproxy
echo
read -p "Press Enter to continue..."
}
#Function to add a new server
add_new_server(){
# Verify if the file exists, if not, return
if [ ! -f "$haproxy_config_file" ]; then
echo ''
echo -e "${RED} There is no HAProxy config. First configurate a tunnel through option 1${NC}\n"
read -p "Press Enter to continue..."
return 1
fi
clear
while true; do
clear
read -p "Enter HAProxy bind port: " haproxy_bind_port
read -p "Enter Destination (Kharej) IP address: " destination_ip
read -p "Enter Destination port: " destination_port
# Append frontend and backend configurations to HAProxy configuration file
echo "frontend frontend_$haproxy_bind_port" >> "$haproxy_config_file"
echo " bind *:$haproxy_bind_port" >> "$haproxy_config_file"
echo " default_backend backend_$haproxy_bind_port" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
echo "backend backend_$haproxy_bind_port" >> "$haproxy_config_file"
echo " server server_$haproxy_bind_port $destination_ip:$destination_port" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
echo ''
read -p "Do you want to add another config? (yes/no): " add_another
echo ''
if [[ $add_another != "yes" ]]; then
break
fi
done
echo -e "${GREEN}Configuration updated successfully in $haproxy_config_file${NC}"
# Restart HAProxy service
systemctl restart haproxy
echo ''
read -p "Press Enter to continue..."
}
#Function for loadbalancing
load_balancing() {
clear
# Prompt the user for confirmation
read -p "All your previous configs will be deleted, continue? (yes/no): " confirm
echo ''
# Check user's response
if ! [[ $confirm == "yes" || $confirm == "Yes" || $confirm == "YES" ]]; then
echo -e "${RED}Operation cancelled by user.${NC}" && sleep 1
return 1
fi
# Verify if the file exists, if not, create it
if [ ! -f "$haproxy_config_file" ]; then
touch "$haproxy_config_file"
fi
echo "# HAProxy configuration generated by script" > "$haproxy_config_file"
echo "global" >> "$haproxy_config_file"
echo " log /dev/log local0" >> "$haproxy_config_file"
echo " log /dev/log local1 notice" >> "$haproxy_config_file"
echo " chroot /var/lib/haproxy" >> "$haproxy_config_file"
echo " stats socket /run/haproxy/admin.sock mode 660 level admin" >> "$haproxy_config_file"
echo " stats timeout 30s" >> "$haproxy_config_file"
echo " user haproxy" >> "$haproxy_config_file"
echo " group haproxy" >> "$haproxy_config_file"
echo " daemon" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
echo "defaults" >> "$haproxy_config_file"
echo " log global" >> "$haproxy_config_file"
echo " mode tcp" >> "$haproxy_config_file"
echo " option tcplog" >> "$haproxy_config_file"
echo " option dontlognull" >> "$haproxy_config_file"
echo " timeout connect 5000ms" >> "$haproxy_config_file"
echo " timeout client 50000ms" >> "$haproxy_config_file"
echo " timeout server 50000ms" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
# to display load balancing options
echo -e "${BLUE}Load balancing options:${NC}"
echo "1. Round Robin"
echo "2. Least Connections"
echo "3. Source IP Hash"
echo "4. URI Hash"
read -p "Select the desired load balancing algorithm: " choice
case $choice in
1) lb_algorithm="roundrobin";;
2) lb_algorithm="leastconn";;
3) lb_algorithm="source";;
4) lb_algorithm="uri";;
*) echo "Invalid input. Using default load balancing algorithm (roundrobin)."
lb_algorithm="roundrobin";;
esac
#Prompt the user for HAProxy bind port
echo ''
read -p "*. Enter HAProxy bind port for load balancing: " haproxy_bind_port
echo "frontend tcp_frontend" >> "$haproxy_config_file"
echo " bind *:${haproxy_bind_port}" >> "$haproxy_config_file"
echo " mode tcp" >> "$haproxy_config_file"
echo " default_backend tcp_backend" >> "$haproxy_config_file"
echo "" >> "$haproxy_config_file"
echo "backend tcp_backend" >> "$haproxy_config_file"
echo " mode tcp" >> "$haproxy_config_file"
echo " balance $lb_algorithm" >> "$haproxy_config_file"
clear
# Prompt the user for corresponding Destination IP and port
server=1
while true; do
echo ''
read -p "1. Enter Destination IP address for loadbalancing: " destination_ip
read -p "2. Enter Destination port for loadbalancing: " destination_port
echo " server server${server} ${destination_ip}:${destination_port} check" >> "$haproxy_config_file"
echo ''
read -p "Do you want to add another server for load balancing? (yes/no): " add_another
if [[ $add_another != "yes" ]]; then
break
fi
server=$((server + 1))
clear
done
echo ''
echo -e "${GREEN}Configuration updated successfully in $haproxy_config_file${NC}"
# Restart HAProxy service
systemctl restart haproxy
echo ''
read -p "Press Enter to continue..."
}
# Function to destroy tunnel
destroy_tunnel() {
echo ''
# Check if HAProxy service is running
if systemctl is-active --quiet haproxy; then
echo -e "${RED}Stopping HAProxy service...${NC}\n"
sleep 1
# Stop HAProxy service
systemctl stop haproxy
echo -e "${GREEN}HAProxy service stopped.${NC}"
else
echo -e "${YELLOW}HAProxy service is not running.${NC}"
fi
echo ''
# Check if the file exists
if [ -f "$haproxy_config_file" ]; then
echo -e "${RED}Removing ${haproxy_config_file}...${NC}\n"
sleep 1
# Remove the file
rm "$haproxy_config_file"
echo -e "${GREEN}$haproxy_config_file removed.${NC}"
else
echo -e "${YELLOW}$haproxy_config_file does not exist.${NC}"
fi
echo ''
read -p "Press Enter to continue..."
}
# Function to reset service
reset_service() {
echo -e "\n${YELLOW}Restarting HAProxy service...${NC}\n"
sleep 1
# Restart HAProxy
echo "Restarting HAProxy..."
echo ''
if systemctl restart haproxy ; then
echo -e "${GREEN}HAProxy restarted successfully.${NC}"
else
echo -e "${RED}Error: Failed to restart HAProxy.${NC}"
fi
echo ''
read -p "Press Enter to continue..."
}
view_haproxy_log_realtime() {
clear
# Define HAProxy log file path
haproxy_log_file="/var/log/haproxy.log"
# Check if HAProxy log file exists
if [ ! -f "$haproxy_log_file" ]; then
echo "HAProxy log file not found at $haproxy_log_file"
return 1
fi
# Display HAProxy log in real-time
echo "Displaying real-time HAProxy log ($haproxy_log_file):"
tail -f "$haproxy_log_file"
}
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[36m'
NC='\033[0m' # No Color
# Function to display menu
display_menu(){
clear
show_logo
display_server_info
echo "-------------------------------"
show_haproxy_status
echo "-------------------------------"
echo "Menu:"
echo -e "${GREEN}1. Configure Tunnel (IPv4/v6)${NC}"
echo -e "${BLUE}2. Configure Loadbalancer (IPv4/v6)${NC}"
echo -e "${RED}3. Stop HAProxy service and remove configs${NC}"
echo -e "${YELLOW}4. Restart HAProxy Service${NC}"
echo -e "5. View HAProxy real-time logs"
echo "6. Exit"
echo "-------------------------------"
}
# Function to read user input
read_option(){
read -p "Enter your choice: " choice
case $choice in
1) multiple_server_menu ;;
2) load_balancing ;;
3) destroy_tunnel ;;
4) reset_service ;;
5) view_haproxy_log_realtime ;;
6) echo "Exiting..." && exit ;;
*) echo -e "${RED}Invalid option!${NC}" && sleep 1 ;;
esac
}
# Main loop
while true
do
display_menu
read_option
done