haproxy.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env bash
  2. #
  3. # System Required: CentOS, Debian, Ubuntu
  4. #
  5. # Description: Install haproxy for Shadowsocks server
  6. #
  7. # Author: Teddysun <i@teddysun.com>
  8. #
  9. # Intro: https://shadowsocks.be/10.html
  10. #
  11. cur_dir=`pwd`
  12. [[ $EUID -ne 0 ]] && echo "Error: This script must be run as root!" && exit 1
  13. clear
  14. echo
  15. echo "#############################################################"
  16. echo "# Install haproxy for Shadowsocks server #"
  17. echo "# Intro: https://shadowsocks.be/10.html #"
  18. echo "# Author: Teddysun <i@teddysun.com> #"
  19. echo "#############################################################"
  20. echo
  21. check_sys() {
  22. local checkType=$1
  23. local value=$2
  24. local release=''
  25. local systemPackage=''
  26. if [ -f /etc/redhat-release ]; then
  27. release="centos"
  28. systemPackage="yum"
  29. elif cat /etc/issue | grep -Eqi "debian"; then
  30. release="debian"
  31. systemPackage="apt"
  32. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  33. release="ubuntu"
  34. systemPackage="apt"
  35. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  36. release="centos"
  37. systemPackage="yum"
  38. elif cat /proc/version | grep -Eqi "debian"; then
  39. release="debian"
  40. systemPackage="apt"
  41. elif cat /proc/version | grep -Eqi "ubuntu"; then
  42. release="ubuntu"
  43. systemPackage="apt"
  44. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  45. release="centos"
  46. systemPackage="yum"
  47. fi
  48. if [ ${checkType} == "sysRelease" ]; then
  49. if [ "$value" == "$release" ]; then
  50. return 0
  51. else
  52. return 1
  53. fi
  54. elif [ ${checkType} == "packageManager" ]; then
  55. if [ "$value" == "$systemPackage" ]; then
  56. return 0
  57. else
  58. return 1
  59. fi
  60. fi
  61. }
  62. install_check() {
  63. if check_sys packageManager yum || check_sys packageManager apt; then
  64. return 0
  65. else
  66. return 1
  67. fi
  68. }
  69. disable_selinux(){
  70. if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
  71. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  72. setenforce 0
  73. fi
  74. }
  75. valid_ip(){
  76. local ip=$1
  77. local stat=1
  78. if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  79. OIFS=$IFS
  80. IFS='.'
  81. ip=($ip)
  82. IFS=$OIFS
  83. [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  84. stat=$?
  85. fi
  86. return ${stat}
  87. }
  88. get_ip(){
  89. local IP=$( ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1 )
  90. [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
  91. [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipinfo.io/ip )
  92. [ ! -z ${IP} ] && echo ${IP} || echo
  93. }
  94. get_char(){
  95. SAVEDSTTY=`stty -g`
  96. stty -echo
  97. stty cbreak
  98. dd if=/dev/tty bs=1 count=1 2> /dev/null
  99. stty -raw
  100. stty echo
  101. stty $SAVEDSTTY
  102. }
  103. # Pre-installation settings
  104. pre_install(){
  105. if ! install_check; then
  106. echo "Your OS is not supported to run it."
  107. echo "Please change to CentOS 6+/Debian 7+/Ubuntu 12+ and try again."
  108. exit 1
  109. fi
  110. # Set haproxy config port
  111. while true
  112. do
  113. echo -e "Please enter a port for haproxy and Shadowsocks server [1-65535]"
  114. read -p "(Default port: 8989):" haproxyport
  115. [ -z "${haproxyport}" ] && haproxyport="8989"
  116. expr ${haproxyport} + 0 &>/dev/null
  117. if [ $? -eq 0 ]; then
  118. if [ ${haproxyport} -ge 1 ] && [ ${haproxyport} -le 65535 ]; then
  119. echo
  120. echo "---------------------------"
  121. echo "port = ${haproxyport}"
  122. echo "---------------------------"
  123. echo
  124. break
  125. else
  126. echo "Enter error! Please enter a correct number."
  127. fi
  128. else
  129. echo "Enter error! Please enter a correct number."
  130. fi
  131. done
  132. # Set haproxy config IPv4 address
  133. while :
  134. do
  135. echo -e "Please enter your Shadowsocks server's IPv4 address for haproxy"
  136. read -p "(IPv4 is):" haproxyip
  137. valid_ip ${haproxyip}
  138. if [ $? -eq 0 ]; then
  139. echo
  140. echo "---------------------------"
  141. echo "IP = ${haproxyip}"
  142. echo "---------------------------"
  143. echo
  144. break
  145. else
  146. echo "Enter error! Please enter correct IPv4 address."
  147. fi
  148. done
  149. echo
  150. echo "Press any key to start...or Press Ctrl+C to cancel"
  151. char=`get_char`
  152. }
  153. # Config haproxy
  154. config_haproxy(){
  155. # Config DNS nameserver
  156. if ! grep -q "8.8.8.8" /etc/resolv.conf; then
  157. cp -p /etc/resolv.conf /etc/resolv.conf.bak
  158. echo "nameserver 8.8.8.8" > /etc/resolv.conf
  159. echo "nameserver 8.8.4.4" >> /etc/resolv.conf
  160. fi
  161. if [ -f /etc/haproxy/haproxy.cfg ]; then
  162. cp -p /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
  163. fi
  164. cat > /etc/haproxy/haproxy.cfg<<-EOF
  165. global
  166. ulimit-n 51200
  167. log 127.0.0.1 local2
  168. chroot /var/lib/haproxy
  169. pidfile /var/run/haproxy.pid
  170. user haproxy
  171. group haproxy
  172. daemon
  173. defaults
  174. mode tcp
  175. log global
  176. option dontlognull
  177. timeout connect 5s
  178. timeout client 1m
  179. timeout server 1m
  180. frontend ss-${haproxyport}
  181. bind *:${haproxyport}
  182. default_backend ss-${haproxyport}
  183. backend ss-${haproxyport}
  184. server server1 ${haproxyip}:${haproxyport} maxconn 20480
  185. EOF
  186. }
  187. install(){
  188. # Install haproxy
  189. if check_sys packageManager yum; then
  190. yum install -y haproxy
  191. elif check_sys packageManager apt; then
  192. apt-get -y update
  193. apt-get install -y haproxy
  194. fi
  195. if [ -d /etc/haproxy ]; then
  196. echo "haproxy install success."
  197. echo "Config haproxy start..."
  198. config_haproxy
  199. echo "Config haproxy completed..."
  200. if check_sys packageManager yum; then
  201. chkconfig --add haproxy
  202. chkconfig haproxy on
  203. elif check_sys packageManager apt; then
  204. update-rc.d haproxy defaults
  205. fi
  206. # Start haproxy
  207. service haproxy start
  208. if [ $? -eq 0 ]; then
  209. echo "haproxy start success..."
  210. else
  211. echo "haproxy start failure..."
  212. fi
  213. else
  214. echo
  215. echo "haproxy install failed."
  216. exit 1
  217. fi
  218. sleep 3
  219. # restart haproxy
  220. service haproxy restart
  221. # Active Internet connections confirm
  222. netstat -nxtlp
  223. echo
  224. echo "Congratulations, haproxy install completed."
  225. echo -e "Your haproxy Server IP: \033[41;37m $(get_ip) \033[0m"
  226. echo -e "Your haproxy Server port: \033[41;37m ${haproxyport} \033[0m"
  227. echo -e "Your Input Shadowsocks IP: \033[41;37m ${haproxyip} \033[0m"
  228. echo
  229. echo "Welcome to visit: https://shadowsocks.be/10.html"
  230. echo "Enjoy it."
  231. echo
  232. }
  233. # Install haproxy
  234. install_haproxy(){
  235. disable_selinux
  236. pre_install
  237. install
  238. }
  239. # Initialization step
  240. install_haproxy 2>&1 | tee ${cur_dir}/haproxy_for_shadowsocks.log