├── main.sh └── readme.md /main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | config_file="/etc/haproxy/haproxy.cfg" 4 | backup_file="/etc/haproxy/haproxy.cfg.bak" 5 | 6 | check_root() { 7 | if [ "$EUID" -ne 0 ]; then 8 | echo "Please run as root" 9 | exit 1 10 | fi 11 | } 12 | 13 | install_haproxy() { 14 | echo "Installing HAProxy..." 15 | sudo apt-get update 16 | sudo apt-get install -y haproxy 17 | echo "HAProxy installed." 18 | default_config 19 | } 20 | 21 | default_config() { 22 | cat < $config_file 23 | global 24 | # log /dev/log local0 25 | # log /dev/log local1 notice 26 | chroot /var/lib/haproxy 27 | stats socket /run/haproxy/admin.sock mode 660 level admin 28 | stats timeout 30s 29 | user haproxy 30 | group haproxy 31 | daemon 32 | 33 | defaults 34 | # log global 35 | mode tcp 36 | # option tcplog 37 | option dontlognull 38 | timeout connect 5000 39 | timeout client 50000 40 | timeout server 50000 41 | EOL 42 | } 43 | 44 | generate_haproxy_config() { 45 | local ports=($1) 46 | local target_ips=($2) 47 | local config_file="/etc/haproxy/haproxy.cfg" 48 | 49 | echo "Generating HAProxy configuration..." 50 | 51 | for port in "${ports[@]}"; do 52 | cat <> $config_file 53 | 54 | frontend frontend_$port 55 | bind *:$port 56 | default_backend backend_$port 57 | 58 | backend backend_$port 59 | EOL 60 | for i in "${!target_ips[@]}"; do 61 | if [ $i -eq 0 ]; then 62 | cat <> $config_file 63 | server server$(($i+1)) ${target_ips[$i]}:$port check 64 | EOL 65 | else 66 | cat <> $config_file 67 | server server$(($i+1)) ${target_ips[$i]}:$port check backup 68 | EOL 69 | fi 70 | done 71 | done 72 | 73 | echo "HAProxy configuration generated at $config_file" 74 | } 75 | 76 | add_ip_ports() { 77 | read -p "Enter the IPs to forward to (use comma , to separate multiple IPs): " user_ips 78 | IFS=',' read -r -a ips_array <<< "$user_ips" 79 | read -p "Enter the ports (use comma , to separate): " user_ports 80 | IFS=',' read -r -a ports_array <<< "$user_ports" 81 | generate_haproxy_config "${ports_array[*]}" "${ips_array[*]}" 82 | 83 | if haproxy -c -f /etc/haproxy/haproxy.cfg; then 84 | echo "Restarting HAProxy service..." 85 | service haproxy restart 86 | echo "HAProxy configuration updated and service restarted." 87 | else 88 | echo "HAProxy configuration is invalid. Please check the configuration file." 89 | fi 90 | } 91 | 92 | clear_configs() { 93 | echo "Creating a backup of the HAProxy configuration..." 94 | cp $config_file $backup_file 95 | 96 | if [ $? -ne 0 ]; then 97 | echo "Failed to create a backup. Aborting." 98 | return 99 | fi 100 | 101 | echo "Clearing IP and port configurations from HAProxy configuration..." 102 | 103 | awk ' 104 | /^frontend frontend_/ {skip = 1} 105 | /^backend backend_/ {skip = 1} 106 | skip {if (/^$/) {skip = 0}; next} 107 | {print} 108 | ' $backup_file > $config_file 109 | 110 | echo "Clearing IP and port configurations from $config_file." 111 | 112 | echo "Stopping HAProxy service..." 113 | sudo service haproxy stop 114 | 115 | if [ $? -eq 0 ]; then 116 | echo "HAProxy service stopped." 117 | else 118 | echo "Failed to stop HAProxy service." 119 | fi 120 | 121 | echo "Done!" 122 | } 123 | 124 | remove_haproxy() { 125 | echo "Removing HAProxy..." 126 | sudo apt-get remove --purge -y haproxy 127 | sudo apt-get autoremove -y 128 | echo "HAProxy removed." 129 | } 130 | 131 | check_root 132 | 133 | while true; do 134 | sleep 1.5 135 | clear 136 | echo "+----------------------------------------------------------------------------+" 137 | echo "| ## ## ### ###### ###### ##### ### ## ### ### |" 138 | echo "| ## ## ## ## ## ## ## ## ### ### ### ## ## ## |" 139 | echo "| ## ## ## ## ## ## ## ## ## ## ##### #### |" 140 | echo "| ###### ## ## ##### ##### ## ## ### ## |" 141 | echo "| ## ## ####### ## ## ## ## ## ##### ## TG CHANNEL |" 142 | echo "| ## ## ## ## ## ## ## ### ### ## ### ## @DVHOST_CLOUD |" 143 | echo "| ## ## ## ## #### #### ## ##### ## ### #### |" 144 | echo "+----------------------------------------------------------------------------+" 145 | echo "|Select an option:" 146 | echo "|1) Install HAProxy" 147 | echo "|2) Add IPs and Ports to Forward" 148 | echo "|3) Clear Configurations" 149 | echo "|4) Remove HAProxy Completely" 150 | echo "|9) Back" 151 | echo "+----------------------------------------------------------------------------+" 152 | 153 | read -p "Select a Number : " choice 154 | 155 | case $choice in 156 | 1) 157 | install_haproxy 158 | ;; 159 | 2) 160 | add_ip_ports 161 | ;; 162 | 3) 163 | clear_configs 164 | ;; 165 | 4) 166 | remove_haproxy 167 | ;; 168 | 9) 169 | echo "Exiting..." 170 | break 171 | ;; 172 | *) 173 | echo "Invalid option. Please try again." 174 | ;; 175 | esac 176 | done -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

⚡️ Welcome to HAProxy ⚡️

2 | 3 | > **Disclaimer:** HAProxy is a free and open source software that provides a high availability load balancer and Proxy for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient 4 | 5 | ## Install & Upgrade 6 | 7 | ``` 8 | bash <(curl -Ls https://raw.githubusercontent.com/dev-ir/HAProxy/master/main.sh) 9 | ``` 10 | 11 | ## 🪚 Preview 12 |

13 | Image 14 |

15 | 16 | ## Languages 17 | - English 18 | 19 | ## 🙏 Support with Crypto 20 | **We don't need financial support, only Star (⭐) is enough, thank you.** 21 | - USDT (TRC20): `TVUqVMoCEe5DVUoxmPg8MwmgcHvZLqLjr4` 22 | 23 | ## 📧 Join Telegram Channel 24 | 25 | TG : https://t.me/+EpErnDsDPhw3ZThk 26 | --------------------------------------------------------------------------------