Skip to content

Commit 3272258

Browse files
gongzili456haoel
authored andcommitted
colorful prompt,check docker container
1 parent 25158b2 commit 3272258

File tree

1 file changed

+85
-34
lines changed

1 file changed

+85
-34
lines changed

scripts/install.ubuntu.18.04.sh

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#!/bin/bash
22

33
# Author
4-
# original author: https://github.com/gongzili456
5-
# modified by: https://github.com/haoel
4+
# original author:https://github.com/gongzili456
5+
# modified by:https://github.com/haoel
66

77
# Ubuntu 18.04 系统环境
88

9+
COLOR_ERROR="\e[38;5;198m"
10+
COLOR_NONE="\e[0m"
11+
COLOR_SUCC="\e[92m"
912

1013
update_core(){
11-
echo "更新系统内核"
14+
echo -e "${COLOR_ERROR}当前系统内核版本太低 <$VERSION_CURR>,需要更新系统内核.${COLOR_NONE}"
1215
sudo apt install -y -qq --install-recommends linux-generic-hwe-18.04
1316
sudo apt autoremove
1417

15-
echo "内核更新完成重新启动机器。。。"
18+
echo -e "内核更新完成,重新启动机器...${COLOR_NONE}"
1619
sudo reboot
1720
}
1821

@@ -21,7 +24,7 @@ check_bbr(){
2124

2225
# 如果已经发现 bbr 进程
2326
if [ -n "$has_bbr" ] ;then
24-
echo "TCP BBR 拥塞控制算法已经启动"
27+
echo -e "${COLOR_SUCC}TCP BBR 拥塞控制算法已经启动${COLOR_NONE}"
2528
else
2629
start_bbr
2730
fi
@@ -48,18 +51,32 @@ install_bbr() {
4851
}
4952

5053
install_docker() {
51-
echo "开始安装 Docker CE"
52-
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
53-
sudo add-apt-repository \
54-
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
55-
$(lsb_release -cs) \
56-
stable"
57-
sudo apt-get update -qq
58-
sudo apt-get install -y docker-ce
54+
if ! [ -x "$(command -v docker)" ]; then
55+
echo "开始安装 Docker CE"
56+
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
57+
sudo add-apt-repository \
58+
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
59+
$(lsb_release -cs) \
60+
stable"
61+
sudo apt-get update -qq
62+
sudo apt-get install -y docker-ce
63+
else
64+
echo -e "${COLOR_SUCC}Docker CE 已经安装成功了${COLOR_NONE}"
65+
fi
66+
}
67+
68+
check_container(){
69+
has_container=$(sudo docker ps --format "{{.Names}}" | grep "$1")
70+
71+
if [ -n "$has_container" ] ;then
72+
return 1
73+
else
74+
return 0
75+
fi
5976
}
6077

6178
install_certbot() {
62-
echo "开始安装 certbot"
79+
echo "开始安装 certbot 命令行工具"
6380
sudo apt-get update -qq
6481
sudo apt-get install -y software-properties-common
6582
sudo add-apt-repository universe
@@ -69,24 +86,39 @@ install_certbot() {
6986
}
7087

7188
create_cert() {
89+
if ! [ -x "$(command -v certbot)" ]; then
90+
install_certbot
91+
fi
92+
7293
echo "开始生成 SSL 证书"
73-
read -p "请输入你要使用的域名: " domain
94+
echo -e "${COLOR_ERROR}注意:生成证书前,需要将域名指向一个有效的 IP,否则无法创建证书.${COLOR_NONE}"
95+
read -p "是否已经将域名指向了 IP?[Y/n]" has_record
96+
97+
if ! [[ "$has_record" = "Y" ]] ;then
98+
echo "请操作完成后再继续."
99+
exit
100+
fi
101+
102+
read -p "请输入你要使用的域名:" domain
74103

75104
create_cert $domain
76105
sudo certbot certonly --standalone -d $domain
77-
78106
}
79107

80108
install_gost() {
109+
if [ 1 = "$(check_container gost)" ]; then
110+
echo "Gost 容器已经在运行了,你可以手动停止容器,并删除容器,然后再执行本命令来重新安装 Gost。"
111+
return
112+
fi
81113

82-
echo "准备启动 Gost 代理程序为了安全需要使用用户名与密码进行认证"
114+
echo "准备启动 Gost 代理程序,为了安全,需要使用用户名与密码进行认证."
83115
read -p "请输入你要使用的域名:" DOMAIN
84-
read -p "请输入你要使用的用户名: " USER
85-
read -p "请输入你要使用的密码: " PASS
116+
read -p "请输入你要使用的用户名:" USER
117+
read -p "请输入你要使用的密码:" PASS
86118
read -p "请输入HTTP/2需要侦听的端口号(443):" PORT
87119

88120
if [[ -z "${PORT// }" ]] || ! [[ "${PORT}" =~ ^[0-9]+$ ]] || ! [ "$PORT" -ge 1 -a "$PORT" -le 655535 ]; then
89-
echo "非法端口,使用默认端口443!"
121+
echo -e "${COLOR_ERROR}非法端口,使用默认端口 443 !${COLOR_NONE}"
90122
PORT=443
91123
fi
92124

@@ -102,21 +134,25 @@ install_gost() {
102134
}
103135

104136
create_cront_job(){
137+
# TODO: 写入前先检查,避免重复任务。
105138
echo "0 0 1 * * /usr/bin/certbot renew --force-renewal" >> /var/spool/cron/crontabs/root
106139
echo "5 0 1 * * /usr/bin/docker restart gost" >> /var/spool/cron/crontabs/root
107140
}
108141

109-
110142
install_shadowsocks(){
143+
if [ 1 = "$(check_container ss)" ]; then
144+
echo "ShadowSocks 容器已经在运行了,你可以手动停止容器,并删除容器,然后再执行本命令来重新安装 ShadowSocks。"
145+
return
146+
fi
111147

112-
echo "准备启动 ShadowSocks 代理程序为了安全需要使用用户名与密码进行认证"
113-
read -p "请输入你要使用的密码: " PASS
148+
echo "准备启动 ShadowSocks 代理程序,为了安全,需要使用用户名与密码进行认证."
149+
read -p "请输入你要使用的密码:" PASS
114150
read -p "请输入ShadowSocks需要侦听的端口号(1984):" PORT
115151

116152
BIND_IP=0.0.0.0
117153

118154
if [[ -z "${PORT// }" ]] || ! [[ "${PORT}" =~ ^[0-9]+$ ]] || ! [ "$PORT" -ge 1 -a "$PORT" -le 655535 ]; then
119-
echo "非法端口,使用默认端口1984!"
155+
echo -e "${COLOR_ERROR}非法端口,使用默认端口 1984 !${COLOR_NONE}"
120156
PORT=1984
121157
fi
122158

@@ -126,13 +162,17 @@ install_shadowsocks(){
126162
}
127163

128164
install_vpn(){
165+
if [ 1 = "$(check_container vpn)" ]; then
166+
echo "VPN 容器已经在运行了,你可以手动停止容器,并删除容器,然后再执行本命令来重新安装 VPN。"
167+
return
168+
fi
129169

130-
echo "准备启动 VPN/L2TP 代理程序为了安全需要使用用户名与密码进行认证"
131-
read -p "请输入你要使用的用户名: " USER
132-
read -p "请输入你要使用的密码: " PASS
133-
read -p "请输入你要使用的PSK Key: " PSK
170+
echo "准备启动 VPN/L2TP 代理程序,为了安全,需要使用用户名与密码进行认证."
171+
read -p "请输入你要使用的用户名:" USER
172+
read -p "请输入你要使用的密码:" PASS
173+
read -p "请输入你要使用的PSK Key:" PSK
134174

135-
sudo docker run -d --privileged \
175+
sudo docker run -d --name vpn --privileged \
136176
-e PSK=${PSK} \
137177
-e USERNAME=${USER} -e PASSWORD=${PASS} \
138178
-p 500:500/udp \
@@ -143,9 +183,12 @@ install_vpn(){
143183
}
144184

145185
install_brook(){
186+
# TODO: 检查 brook 进程是否存在。
146187
wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/brook.sh && chmod +x brook.sh && bash brook.sh
147188
}
148189

190+
# TODO: install v2ray
191+
149192
init(){
150193
VERSION_CURR=$(uname -r | awk -F '-' '{print $1}')
151194
VERSION_MIN="4.9.0"
@@ -158,10 +201,18 @@ init(){
158201

159202
while [ 1 == 1 ]
160203
do
161-
PS3="Please select a option: "
204+
PS3="Please select a option:"
162205
re='^[0-9]+$'
163-
select opt in "安装 TCP BBR 拥塞控制算法" "安装 Docker 服务程序" "安装 Let's crypt 证书" "安装 Gost HTTP/2 代理服务" \
164-
"安装 ShadowSocks 代理服务" "安装 VPN/L2TP 服务" "安装 Brook 代理服务" "创建证书更新 CronJob" "退出" ; do
206+
select opt in "安装 TCP BBR 拥塞控制算法" \
207+
"安装 Docker 服务程序" \
208+
"创建 SSL 证书" \
209+
"安装 Gost HTTP/2 代理服务" \
210+
"安装 ShadowSocks 代理服务" \
211+
"安装 VPN/L2TP 服务" \
212+
"安装 Brook 代理服务" \
213+
"创建证书更新 CronJob" \
214+
"退出" ; do
215+
165216
if ! [[ $REPLY =~ $re ]] ; then
166217
echo -e "${COLOR_ERROR}Invalid option. Please input a number.${COLOR_NONE}"
167218
break;
@@ -170,9 +221,9 @@ init(){
170221
break;
171222
elif (( REPLY == 2 )) ; then
172223
install_docker
173-
break
224+
break
174225
elif (( REPLY == 3 )) ; then
175-
install_certbot
226+
create_cert
176227
loop=1
177228
break
178229
elif (( REPLY == 4 )) ; then

0 commit comments

Comments
 (0)