forked from shuuzhoou/doubi
-
Notifications
You must be signed in to change notification settings - Fork 10
/
haproxy.sh
277 lines (271 loc) · 8.86 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
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================
# System Required: CentOS/Debian/Ubuntu
# Description: HaProxy
# Version: 1.0.7
# Author: Toyo
# Blog: https://doub.io/wlzy-19/
#=================================================
HaProxy_file="/etc/haproxy"
HaProxy_cfg_file="/etc/haproxy/haproxy.cfg"
#检查是否安装HaProxy
check_HaProxy(){
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} = "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 没有安装HaProxy,请检查 !" && exit 1
fi
}
#检查系统
check_sys(){
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -q -E -i "debian"; then
release="debian"
elif cat /etc/issue | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -q -E -i "debian"; then
release="debian"
elif cat /proc/version | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
fi
#bit=`uname -m`
}
# 设置 防火墙规则
Save_iptables(){
if [[ ${release} == "centos" ]]; then
service iptables save
else
iptables-save > /etc/iptables.up.rules
fi
}
Set_iptables(){
if [[ ${release} == "centos" ]]; then
service iptables save
chkconfig --level 2345 iptables on
elif [[ ${release} == "debian" ]]; then
iptables-save > /etc/iptables.up.rules
echo -e '#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules' > /etc/network/if-pre-up.d/iptables
chmod +x /etc/network/if-pre-up.d/iptables
elif [[ ${release} == "ubuntu" ]]; then
iptables-save > /etc/iptables.up.rules
echo -e '\npre-up iptables-restore < /etc/iptables.up.rules\npost-down iptables-save > /etc/iptables.up.rules' >> /etc/network/interfaces
chmod +x /etc/network/interfaces
fi
}
# 安装HaProxy
installHaProxy(){
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} != "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 已经安装HaProxy,请检查 !" && exit 1
fi
if [[ ${release} == "centos" ]]; then
yum update && yum install -y vim haproxy
else
apt-get update && apt-get install -y vim haproxy
fi
chmod +x /etc/rc.local
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} = "" ]]; then
echo -e "\033[41;37m [错误] \033[0m 安装HaProxy失败,请检查 !" && exit 1
else
Set_iptables
if [[ ${release} == "centos" ]]; then
cat /etc/redhat-release |grep 7\..*|grep -i centos>/dev/null
if [[ $? = 0 ]]; then
systemctl enable haproxy.service
else
chmod +x /etc/init.d/haproxy
chkconfig --add haproxy
chkconfig haproxy on
fi
else
chmod +x /etc/init.d/haproxy
update-rc.d -f haproxy defaults
fi
setHaProxy
fi
}
setHaProxy(){
check_HaProxy
stty erase '^H' && read -p "请输入 HaProxy 的 本地监听端口(转发端口) [1-65535](支持端口段: 2333-6666): " HaProxyport
[[ -z "${HaProxyport}" ]] && echo "取消..." && exit 1
stty erase '^H' && read -p "请输入 HaProxy 欲转发的 IP:" HaProxyip
[[ -z "${HaProxyip}" ]] && echo "取消..." && exit 1
echo
echo "——————————————————————————————"
echo " 请检查 HaProxy 配置是否有误 !"
echo
echo -e " 本地监听端口 : \033[41;37m ${HaProxyport} \033[0m"
echo -e " 欲转发 IP : \033[41;37m ${HaProxyip} \033[0m"
echo "——————————————————————————————"
echo
stty erase '^H' && read -p "请按任意键继续,如有配置错误请使用 Ctrl+C 退出。" var
HaProxy_port_1=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23 | grep "-"`
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23`
if [[ ${HaProxy_port_1} = "" ]]; then
iptables -D INPUT -p tcp --dport ${HaProxy_port} -j ACCEPT
else
HaProxy_port_1=`echo ${HaProxy_port_1} | sed 's/-/:/g'`
iptables -D INPUT -p tcp --dport ${HaProxy_port_1} -j ACCEPT
fi
cat > ${HaProxy_cfg_file}<<-EOF
global
defaults
log global
mode tcp
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend ss-in1
bind *:${HaProxyport}
default_backend ss-out1
backend ss-out1
server server1 ${HaProxyip} maxconn 20480
EOF
restartHaProxy
}
# 查看HaProxy列表
viewHaProxy(){
check_HaProxy
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23`
HaProxy_ip=`cat ${HaProxy_cfg_file} | sed -n "16p" | awk '{print $3}'`
ip=`wget -qO- -t1 -T2 ipinfo.io/ip`
[[ -z $ip ]] && ip="VPS_IP"
echo
echo "——————————————————————————————"
echo " HaProxy 配置信息: "
echo
echo -e " 本地 IP : \033[41;37m ${ip} \033[0m"
echo -e " 本地监听端口 : \033[41;37m ${HaProxy_port} \033[0m"
echo
echo -e " 欲转发 IP : \033[41;37m ${HaProxy_ip} \033[0m"
echo -e " 欲转发端口 : \033[41;37m ${HaProxy_port} \033[0m"
echo "——————————————————————————————"
echo
}
# 启动aProxy
startHaProxy(){
check_HaProxy
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
[[ ! -z $PID ]] && echo -e "\033[41;37m [错误] \033[0m 发现 HaProxy 正在运行,请检查 !" && exit 1
if [[ ${release} == "centos" ]]; then
cat /etc/redhat-release |grep 7\..*|grep -i centos>/dev/null
if [[ $? = 0 ]]; then
systemctl start haproxy.service
else
/etc/init.d/haproxy start
fi
else
/etc/init.d/haproxy start
fi
sleep 2s
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
[[ -z $PID ]] && echo -e "\033[41;37m [错误] \033[0m HaProxy 启动失败 !" && exit 1
HaProxy_port_1=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23 | grep "-"`
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23`
if [[ ${HaProxy_port_1} = "" ]]; then
iptables -I INPUT -p tcp --dport ${HaProxy_port} -j ACCEPT
else
HaProxy_port_1=`echo ${HaProxy_port_1} | sed 's/-/:/g'`
iptables -I INPUT -p tcp --dport ${HaProxy_port_1} -j ACCEPT
fi
echo && echo "——————————————————————————————" && echo
echo " HaProxy 已启动 !"
Save_iptables
viewHaProxy
}
# 停止aProxy
stopHaProxy(){
check_HaProxy
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
[[ -z $PID ]] && echo -e "\033[41;37m [错误] \033[0m 发现 HaProxy 没有运行,请检查 !" && exit 1
if [[ ${release} == "centos" ]]; then
cat /etc/redhat-release |grep 7\..*|grep -i centos>/dev/null
if [[ $? = 0 ]]; then
systemctl stop haproxy.service
else
/etc/init.d/haproxy stop
fi
else
/etc/init.d/haproxy stop
fi
HaProxy_port_1=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23 | grep "-"`
HaProxy_port=`cat ${HaProxy_cfg_file} | sed -n "12p" | cut -c 12-23`
if [[ ${HaProxy_port_1} = "" ]]; then
iptables -D INPUT -p tcp --dport ${HaProxy_port} -j ACCEPT
else
HaProxy_port_1=`echo ${HaProxy_port_1} | sed 's/-/:/g'`
iptables -D INPUT -p tcp --dport ${HaProxy_port_1} -j ACCEPT
fi
sleep 2s
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
echo -e "\033[41;37m [错误] \033[0m HaProxy 停止失败 !" && exit 1
else
Save_iptables
echo " HaProxy 已停止 !"
fi
}
restartHaProxy(){
# 检查是否安装
check_HaProxy
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
stopHaProxy
fi
startHaProxy
}
statusHaProxy(){
check_HaProxy
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
echo -e "\033[42;37m [信息] \033[0m HaProxy 正在运行,PID: ${PID} !"
else
echo -e "\033[42;37m [信息] \033[0m HaProxy 没有运行 !"
fi
}
uninstallHaProxy(){
check_HaProxy
echo "确定要卸载 HaProxy ? [y/N]"
stty erase '^H' && read -p "(默认: n):" unyn
[[ -z ${unyn} ]] && unyn="n"
if [[ ${unyn} == [Yy] ]]; then
PID=`ps -ef | grep "haproxy" | grep -v grep | grep -v "haproxy.sh" | awk '{print $2}'`
if [[ ! -z $PID ]]; then
stopHaProxy
fi
if [[ ${release} == "centos" ]]; then
yum remove haproxy -y
else
apt-get remove haproxy -y
apt-get autoremove
fi
rm -rf ${HaProxy_file}
HaProxy_exist=`haproxy -v`
if [[ ${HaProxy_exist} != "" ]]; then
echo -e "\033[41;37m [错误] \033[0m HaProxy卸载失败,请检查 !" && exit 1
fi
echo && echo " HaProxy 已卸载 !" && echo
else
echo && echo "卸载已取消..." && echo
fi
}
check_sys
action=$1
[[ -z $1 ]] && action=install
case "$action" in
install|set|view|start|stop|restart|status|uninstall)
${action}HaProxy
;;
*)
echo "输入错误 !"
echo "用法: { install | view | set | start | stop | restart | status | uninstall }"
;;
esac