-
Notifications
You must be signed in to change notification settings - Fork 138
/
lkl-haproxy-centos-nocheckvirt.sh
282 lines (235 loc) · 8.42 KB
/
lkl-haproxy-centos-nocheckvirt.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
#!/bin/bash
Green_font="\033[32m" && Yellow_font="\033[33m" && Red_font="\033[31m" && Font_suffix="\033[0m"
Info="${Green_font}[Info]${Font_suffix}"
Error="${Red_font}[Error]${Font_suffix}"
echo -e "
${Green_font}
#================================================
# Project: bbrplus lkl-haproxy
# Platform: --CentOS --nocheckvirt
# Version: 1.0.3
# Author: mzz2017
# Github: https://github.com/mzz2017/lkl-haproxy
#
# bash <(curl -Ls https://git.io/JJJEc)
#================================================
${Font_suffix}"
check_system(){
[[ -z "`cat /etc/redhat-release | grep -iE "CentOS"`" ]] && echo -e "${Error} only support CentOS !\nYou can find debian script at https://github.com/mzz2017/lkl-haproxy" && exit 1
[[ "`uname -m`" != "x86_64" ]] && echo -e "${Error} only support 64 bit !" && exit 1
}
check_root(){
[[ "`id -u`" != "0" ]] && echo -e "${Error} must be root user !" && exit 1
}
check_ovz(){
yum update && yum install -y virt-what
[[ "`virt-what`" != "openvz" ]] && echo -e "${Error} only support OpenVZ !" && exit 1
}
check_ldd(){
#ldd=`ldd --version | grep ldd | awk '{print $NF}'`
[[ "`ldd --version | grep ldd | awk '{print $NF}'`" < "2.14" ]] && echo -e "${Error} ldd version < 2.14, not support !" && exit 1
}
check_tuntap(){
echo -e "\n"
cat /dev/net/tun
echo -e "${Info} 请确认上一行的返回值是否为 'File descriptor in bad state'(文件描述符处于错误状态) ?"
echo -e "1.是\n2.否"
read -p "输入数字以选择:" tuntap
while [[ ! "${tuntap}" =~ ^[1-2]$ ]]
do
echo -e "${Error} 无效输入"
echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" tuntap
done
[[ -z "${tuntap}" || "${tuntap}" == "2" ]] && echo -e "${Error} 未开启 tun/tap,请开启后再尝试该脚本 !" && exit 1
#以下为失败,grep 无效
#echo -n "`cat /dev/net/tun`" | grep "device"
#[[ -z "${enable}" ]] && echo -e "${Error} not enable tun/tap !" && exit 1
}
directory(){
[[ ! -d /etc/lklhaproxy ]] && mkdir -p /etc/lklhaproxy
cd /etc/lklhaproxy
}
config(){
# choose one or many port
echo -e "${Info} 你想加速单个端口(例如 443)还是端口段(例如 8080-9090) ?\n1.单个端口\n2.端口段"
read -p "(输入数字以选择):" choose
while [[ ! "${choose}" =~ ^[1-2]$ ]]
do
echo -e "${Error} 无效输入"
echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" choose
done
# download unfully-config-redirect
wget https://github.com/mzz2017/lkl-haproxy/raw/2020e4ab4e167d771fb3545dd094a471619978bc/requirement/redirect.sh
# config: haproxy && redirect
if [[ "${choose}" == "1" ]]; then
echo -e "${Info} 输入你想加速的端口"
read -p "(输入单个端口号,例如:443,默认使用 443):" port1
[[ -z "${port1}" ]] && port1=443
config-haproxy-1
config-redirect-1
else
echo -e "${Info} 输入端口段的第一个端口号"
read -p "(例如端口段为 8080-9090,则此处输入 8080,默认使用 8080):" port1
[[ -z "${port1}" ]] && port1=8080
echo -e "${Info} 输入端口段的第二个端口号"
read -p "(例如端口段为 8080-9090,则此处输入 9090,默认使用 9090):" port2
[[ -z "${port2}" ]] && port2=9090
config-haproxy-2
config-redirect-2
fi
}
config-haproxy-1(){
echo -e "global
defaults
log global
mode tcp
option dontlognull
option tcpka
timeout connect 5000ms
timeout client 8000s
timeout server 8000s
frontend proxy-in
bind :::${port1} v4v6
default_backend proxy-out
backend proxy-out
server server1 10.0.0.1 maxconn 20480\c" > haproxy.cfg
}
config-haproxy-2(){
echo -e "global
defaults
log global
mode tcp
option dontlognull
option tcpka
timeout connect 5000ms
timeout client 8000s
timeout server 8000s
frontend proxy-in
bind :::${port1}-${port2} v4v6
default_backend proxy-out
backend proxy-out
server server1 10.0.0.1 maxconn 20480\c" > haproxy.cfg
}
config-redirect-1(){
echo "iptables -t nat -A PREROUTING -i $(awk '$2 == 00000000 { print $1 }' /proc/net/route) -p tcp --dport ${port1} -j DNAT --to-destination 10.0.0.2" >> redirect.sh
echo "ip6tables -t nat -A PREROUTING -i $(awk '$2 == 00000000 { print $1 }' /proc/net/route) -p tcp --dport ${port1} -j DNAT --to-destination fd00::2" >> redirect.sh
}
config-redirect-2(){
echo "iptables -t nat -A PREROUTING -i $(awk '$2 == 00000000 { print $1 }' /proc/net/route) -p tcp --dport ${port1}:${port2} -j DNAT --to-destination 10.0.0.2" >> redirect.sh
echo "ip6tables -t nat -A PREROUTING -i $(awk '$2 == 00000000 { print $1 }' /proc/net/route) -p tcp --dport ${port1}:${port2} -j DNAT --to-destination fd00::2" >> redirect.sh
}
check-all(){
# check config
[[ ! -f haproxy.cfg ]] && echo -e "${Error} not found haproxy config, please check !" && exit 1
[[ ! -f redirect.sh ]] && echo -e "${Error} not found redirect config, please check !" && exit 1
# check lkl-mod
[[ ! -f liblkl-hijack.so ]] && wget https://github.com/mzz2017/lkl-haproxy/raw/2020e4ab4e167d771fb3545dd094a471619978bc/mod/liblkl-hijack.so
[[ ! -f liblkl-hijack.so ]] && echo -e "${Error} download lkl.mod failed, please check !" && exit 1
# check which
which --help > /dev/null || yum install -y which
# check wget
wget --help > /dev/null || yum install -y wget
# check haproxy
yum install -y iptables bc haproxy
which haproxy || (echo -e "${Error} failed to install haproxy !" && exit 1)
# give privilege
chmod -R +x /etc/lklhaproxy
}
# start immediately
run-it-now(){
systemctl start lkl-haproxy
bash /etc/lklhaproxy/redirect.sh
}
# start with reboot
self-start(){
echo "[Unit]
Description=lkl-haproxy
[Service]
Environment=LD_PRELOAD=/etc/lklhaproxy/liblkl-hijack.so
Environment=LKL_HIJACK_NET_QDISC=root|fq
Environment=\"LKL_HIJACK_SYSCTL=net.ipv4.tcp_congestion_control=bbrplus;net.ipv4.tcp_wmem=4096 131072 6048576;net.ipv4.tcp_sack=1;net.core.wmem_default=8388608;net.core.wmem_max=16777216;net.ipv4.tcp_mem=94500000 915000000 927000000;net.ipv4.tcp_slow_start_after_idle=0\"
Environment=LKL_HIJACK_OFFLOAD=0x9983
Environment=LKL_HIJACK_NET_IFTYPE=tap
Environment=LKL_HIJACK_NET_IFPARAMS=lkl-tap
Environment=LKL_HIJACK_NET_IP=10.0.0.2
Environment=LKL_HIJACK_NET_NETMASK_LEN=24
Environment=LKL_HIJACK_NET_GATEWAY=10.0.0.1
Environment=LKL_HIJACK_NET_IPV6=fd00::2
Environment=LKL_HIJACK_NET_GATEWAY6=fd00::1
Environment=LKL_HIJACK_NET_NETMASK6_LEN=64
Environment=LKL_HIJACK_BOOT_CMDLINE=mem=256M
ExecStart=$(which haproxy) -f /etc/lklhaproxy/haproxy.cfg
Restart=always
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/lkl-haproxy.service
systemctl daemon-reload
systemctl enable lkl-haproxy
rclocaldir=/etc/rc.d
(systemctl status rc-local|grep /etc/rc.local)>/dev/null && rclocaldir=/etc
mkdir -p $rclocaldir
touch $rclocaldir/rc.local
sed -i '/bash \/etc\/lklhaproxy\/redirect.sh/d' $rclocaldir/rc.local
sed -i "s/exit 0/ /ig" $rclocaldir/rc.local
echo -e "\nbash /etc/lklhaproxy/redirect.sh\nexit 0" >> $rclocaldir/rc.local
chmod +x $rclocaldir/rc.local
systemctl status rc-local > /dev/null || (echo "[Unit]
Description=$rclocaldir/rc.local
ConditionFileIsExecutable=$rclocaldir/rc.local
After=network.target
[Service]
Type=forking
ExecStart=$rclocaldir/rc.local start
TimeoutSec=0
RemainAfterExit=yes
" > /etc/systemd/system/rc-local.service && systemctl daemon-reload)
systemctl enable rc-local > /dev/null
}
install(){
check_system
check_root
#check_ovz
check_ldd
check_tuntap
directory
config
check-all
self-start
run-it-now
#status
echo -e "${Info} 已完成,请稍后使用此脚本第二项判断 lkl 是否成功。"
}
status(){
pingstatus=`ping 10.0.0.2 -c 3 | grep ttl`
if [[ ! -z "${pingstatus}" ]]; then
echo -e "${Info} lkl-haproxy is running !"
else echo -e "${Error} lkl-haproxy not running, please check !"
fi
}
uninstall(){
check_system
check_root
yum remove -y haproxy
rm -rf /etc/lklhaproxy
#iptables -F
systemctl disable lkl-haproxy
sed -i '/bash \/etc\/lklhaproxy\/redirect.sh/d' /etc/rc.d/rc.local
echo -e "${Info} please remember 重启 to stop lkl-haproxy"
}
echo -e "${Info} 选择你要使用的功能: "
echo -e "1.安装 lkl bbrplus\n2.检查 lkl bbrplus运行状态\n3.卸载 lkl bbrplus"
read -p "输入数字以选择:" function
while [[ ! "${function}" =~ ^[1-3]$ ]]
do
echo -e "${Error} 无效输入"
echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" function
done
if [[ "${function}" == "1" ]]; then
install
elif [[ "${function}" == "2" ]]; then
status
elif [[ "${function}" == "3" ]]; then
uninstall
else
echo "${Error} 读取选项失败,可能是因为本脚本不能在当前shell上执行"
fi