forked from 233boy/caddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddy.sh
379 lines (342 loc) · 8.96 KB
/
caddy.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
#!/bin/bash
red='\e[91m'
green='\e[92m'
yellow='\e[93m'
magenta='\e[95m'
cyan='\e[96m'
none='\e[0m'
[[ $(id -u) != 0 ]] && echo -e " \n哎呀……请使用 ${red}root ${none}用户运行 ${yellow}~(^_^) ${none}\n" && exit 1
cmd="apt-get"
sys_bit=$(uname -m)
# 笨笨的检测方法
if [[ -f /usr/bin/apt-get || -f /usr/bin/yum ]] && [[ -f /bin/systemctl ]]; then
if [[ -f /usr/bin/yum ]]; then
cmd="yum"
fi
else
echo -e "
哈哈……这个 ${red}辣鸡脚本${none} 不支持你的系统。 ${yellow}(-_-) ${none}
" && exit 1
fi
if [[ $sys_bit == "i386" || $sys_bit == "i686" ]]; then
caddy_download_link="https://caddyserver.com/download/linux/386?license=personal"
elif [[ $sys_bit == "x86_64" ]]; then
caddy_download_link="https://caddyserver.com/download/linux/amd64?license=personal"
else
echo -e "
哈哈……这个 ${red}辣鸡脚本${none} 不支持你的系统。 ${yellow}(-_-) ${none}
" && exit 1
fi
ask() {
while :; do
echo
echo -e "请输入一个 $magenta正确的域名$none,一定一定一定要正确,不!能!出!错!"
read -p "(例如:233blog.com): " domain
[ -z "$domain" ] && error && continue
echo
echo
echo -e "$yellow 你的域名 = $cyan$domain$none"
echo "----------------------------------------------------------------"
break
done
get_ip
echo
echo -e "$yellow 请将 $magenta$domain$none $yellow解析到: $cyan$ip$none"
echo
echo -e "$yellow 请将 $magenta$domain$none $yellow解析到: $cyan$ip$none"
echo
echo -e "$yellow 请将 $magenta$domain$none $yellow解析到: $cyan$ip$none"
echo
echo " 重要的事情要说三次....(^_^)"
echo "----------------------------------------------------------------"
echo
while :; do
read -p "$(echo -e "(是否已经正确解析: [${magenta}Y$none]):") " record
if [[ -z "$record" ]]; then
error
else
if [[ "$record" == [Yy] ]]; then
echo
echo
echo -e "$yellow 域名解析 = ${cyan}OK $none"
echo "----------------------------------------------------------------"
echo
break
else
error
fi
fi
done
while :; do
read -p "$(echo -e "请输入登录用户名...(默认用户名: ${magenta}233blog$none)"): " username
[ -z "$username" ] && username="233blog"
echo
echo
echo -e "$yellow 用户名 = $cyan$username$none"
echo "----------------------------------------------------------------"
echo
break
done
while :; do
read -p "$(echo -e "请输入用户密码...(默认密码: ${magenta}233blog.com$none)"): " userpass
[ -z "$userpass" ] && userpass="233blog.com"
echo
echo
echo -e "$yellow 用户密码 = $cyan$userpass$none"
echo "----------------------------------------------------------------"
echo
break
done
}
# plugins_ask() {
# echo
# while :; do
# echo -e "是否 添加 Caddy 插件 [${magenta}Y/N$none]"
# read -p "$(echo -e "(默认: [${cyan}N$none]):")" plugins_add
# [[ -z $plugins_add ]] && plugins_add="n"
# case $plugins_add in
# Y | y)
# plugins_config
# break
# ;;
# N | n)
# echo
# echo
# echo -e "$yellow 添加 Caddy 插件 = $cyan不想添加$none"
# echo "----------------------------------------------------------------"
# echo
# break
# ;;
# *)
# error
# ;;
# esac
# done
# }
# plugins_config(){
# }
install_info() {
clear
echo
echo " ....准备安装了咯..看看有毛有配置正确了..."
echo
echo "---------- 配置信息 -------------"
echo
echo -e "$yellow 你的域名 = $cyan$domain$none"
echo
echo -e "$yellow 域名解析 = ${cyan}OK${none}"
echo
echo -e "$yellow 用户名 = ${cyan}$username$none"
echo
echo -e "$yellow 密码 = ${cyan}$userpass$none"
echo
pause
}
domain_check() {
test_domain=$(ping $domain -c 1 | grep -oE -m1 "([0-9]{1,3}\.){3}[0-9]{1,3}")
if [[ $test_domain != $ip ]]; then
echo -e "
$red 检测域名解析错误....$none
你的域名: $yellow$domain$none 未解析到: $cyan$ip$none
备注...如果你的域名是使用 Cloudflare 解析的话..在 Status 那里点一下那图标..让它变灰
" && exit 1
fi
}
install_caddy() {
local caddy_tmp="/tmp/install_caddy/"
local caddy_tmp_file="/tmp/install_caddy/caddy.tar.gz"
[[ -d $caddy_tmp ]] && rm -rf $caddy_tmp
[[ -f $caddy_tmp_file ]] && rm -rf $caddy_tmp_file
mkdir -p $caddy_tmp
if [[ ! $(command -v wget) ]]; then
$cmd update -y
$cmd install wget -y
fi
if ! wget --no-check-certificate -O "$caddy_tmp_file" $caddy_download_link; then
echo -e "
$red 下载 Caddy 失败啦..可能是你的小鸡鸡的网络太辣鸡了...重新安装也许能解决$none
" && exit 1
fi
tar zxf $caddy_tmp_file -C $caddy_tmp
cp -f ${caddy_tmp}caddy /usr/local/bin/
if [[ ! -f /usr/local/bin/caddy ]]; then
echo -e "
$red 哎呀...安装 Caddy 失败咯....$none
" && exit 1
fi
cp ${caddy_tmp}init/linux-systemd/caddy.service /lib/systemd/system/
sed -i "s/www-data/root/g" /lib/systemd/system/caddy.service
systemctl enable caddy
mkdir -p /etc/ssl/caddy
mkdir -p /etc/caddy
rm -rf $caddy_tmp $caddy_tmp_file
}
open_port() {
if [[ $cmd == "apt-get" ]]; then
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
else
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/udp
firewall-cmd --reload
fi
}
del_port() {
if [[ $cmd == "apt-get" ]]; then
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
else
firewall-cmd --permanent --zone=public --remove-port=80/tcp
firewall-cmd --permanent --zone=public --remove-port=443/udp
fi
}
config_caddy() {
email=$(shuf -i1-10000000000 -n1)
cat >/etc/caddy/Caddyfile <<-EOF
$domain {
gzip
basicauth / $username $userpass
header / Strict-Transport-Security "max-age=31536000;"
tls ${email}@gmail.com
proxy / https://www.google.com.hk
}
EOF
open_port
systemctl restart caddy
}
show_config_info() {
clear
echo
echo "---------- 安装完成 -------------"
echo
echo -e "$yellow 你的域名 = ${cyan}https://$domain$none"
echo
echo -e "$yellow 用户名 = ${cyan}$username$none"
echo
echo -e "$yellow 密码 = ${cyan}$userpass$none"
echo
echo " 帮助或反馈: https://233blog.com/post/21/"
echo
}
uninstall() {
if [[ -f /usr/local/bin/caddy && -f /etc/caddy/Caddyfile ]] && [[ -f /lib/systemd/system/caddy.service ]]; then
uninstall_caddy
else
echo -e "
$red 大胸弟...你貌似毛有安装 Caddy ....卸载个鸡鸡哦...$none
备注...仅支持卸载使用我(233blog.com)提供的 Caddy 一键反代谷歌安装脚本
" && exit 1
fi
}
uninstall_caddy() {
caddy_pid=$(pgrep "caddy")
while :; do
echo
read -p "是否卸载[Y/N]:" uninstall_caddy_ask
if [ -z $uninstall_caddy_ask ]; then
error
else
if [[ $uninstall_caddy_ask == [Yy] ]]; then
if [[ $caddy_pid ]]; then
systemctl stop caddy
fi
systemctl disable caddy
rm -rf /lib/systemd/system/caddy.service
rm -rf /usr/local/bin/caddy /etc/caddy
rm -rf /etc/ssl/caddy
del_port
echo
echo -e "$green 卸载完成啦.... $none"
echo
break
elif [[ $uninstall_caddy_ask == [Nn] ]]; then
echo
echo -e "$red....已取消卸载....$none"
echo
break
else
error
fi
fi
done
}
error() {
echo -e "\n$red 输入错误!$none\n"
}
pause() {
read -rsp "$(echo -e "按$green Enter 回车键 $none继续....或按$red Ctrl + C $none取消.")" -d $'\n'
}
get_ip() {
ip=$(curl -s ipinfo.io/ip)
}
try_enable_bbr() {
if [[ $(uname -r | cut -b 1) -eq 4 ]]; then
case $(uname -r | cut -b 3-4) in
9. | [1-9][0-9])
sed -i '/net.ipv4.tcp_congestion_control/d' /etc/sysctl.conf
sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control = bbr" >>/etc/sysctl.conf
echo "net.core.default_qdisc = fq" >>/etc/sysctl.conf
sysctl -p >/dev/null 2>&1
;;
esac
fi
}
only_install_caddy() {
install_caddy
open_port
echo "#https://caddyserver.com/docs" >/etc/caddy/Caddyfile
clear
echo
echo "---------- 安装完成 -------------"
echo
echo -e "$yellow Caddy 已安装完成...但还没有启动$none"
echo
echo -e "$yellow 请修改${cyan} /etc/caddy/Caddyfile $yellow文件$none"
echo
echo -e "$yellow 然后使用${cyan} systemctl start caddy $yellow启动 Caddy$none"
echo
echo -e "$yellow Caddy 帮助文档: ${cyan}https://caddyserver.com/docs$none"
echo
}
install() {
ask
install_info
domain_check
install_caddy
config_caddy
show_config_info
}
try_enable_bbr
clear
while :; do
echo
echo "........... Caddy 一键反代谷歌安装脚本 by 233blog.com .........."
echo
echo "帮助说明: https://233blog.com/post/13/"
echo
echo " 1. 安装"
echo
echo " 2. 仅安装 Caddy..不配置反代"
echo
echo " 3. 卸载"
echo
read -p "请选择[1-3]:" choose
case $choose in
1)
install
break
;;
2)
only_install_caddy
break
;;
3)
uninstall
break
;;
*)
error
;;
esac
done