forked from hijkpw/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathubuntu_install_ss.sh
403 lines (368 loc) · 11.2 KB
/
ubuntu_install_ss.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
# shadowsocks/ss Ubuntu一键安装脚本
# Author: hijk<https://hijk.art>
RED="\033[31m" # Error message
GREEN="\033[32m" # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m" # Info message
PLAIN='\033[0m'
BASE=`pwd`
OS=`hostnamectl | grep -i system | cut -d: -f2`
V6_PROXY=""
IP=`curl -sL -4 ip.sb`
if [[ "$?" != "0" ]]; then
IP=`curl -sL -6 ip.sb`
V6_PROXY="https://gh.hijk.art/"
fi
CONFIG_FILE="/etc/shadowsocks-libev/config.json"
colorEcho() {
echo -e "${1}${@:2}${PLAIN}"
}
checkSystem() {
result=$(id | awk '{print $1}')
if [ $result != "uid=0(root)" ]; then
colorEcho $RED " 请以root身份执行该脚本"
exit 1
fi
res=`lsb_release -d | grep -i ubuntu`
if [ "$?" != "0" ]; then
res=`which apt`
if [ "$?" != "0" ]; then
colorEcho $RED " 系统不是Ubuntu"
exit 1
fi
else
result=`lsb_release -d | grep -oE "[0-9.]+"`
main=${result%%.*}
if [ $main -lt 16 ]; then
colorEcho $RED " 不受支持的Ubuntu版本"
exit 1
fi
fi
}
slogon() {
clear
echo "#############################################################"
echo -e "# ${RED}Ubuntu LTS Shadowsocks/SS 一键安装脚本${PLAIN} #"
echo -e "# ${GREEN}作者${PLAIN}: 网络跳越(hijk) #"
echo -e "# ${GREEN}网址${PLAIN}: https://hijk.art #"
echo -e "# ${GREEN}论坛${PLAIN}: https://hijk.club #"
echo -e "# ${GREEN}TG群${PLAIN}: https://t.me/hijkclub #"
echo -e "# ${GREEN}Youtube频道${PLAIN}: https://youtube.com/channel/UCYTB--VsObzepVJtc9yvUxQ #"
echo "#############################################################"
echo ""
}
getData() {
read -p " 请设置SS的密码(不输入则随机生成):" PASSWORD
[ -z "$PASSWORD" ] && PASSWORD=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
echo ""
colorEcho $BLUE " 密码: $PASSWORD"
echo ""
while true
do
read -p " 请设置SS的端口号[1025-65535]:" PORT
[ -z "$PORT" ] && PORT="12345"
if [ "${PORT:0:1}" = "0" ]; then
echo -e "${RED}端口不能以0开头${PLAIN}"
exit 1
fi
expr $PORT + 0 &>/dev/null
if [ $? -eq 0 ]; then
if [ $PORT -ge 1025 ] && [ $PORT -le 65535 ]; then
echo ""
colorEcho $BLUE " 端口号: $PORT"
echo ""
break
else
colorEcho $RED " 输入错误,端口号为1025-65535的数字"
fi
else
colorEcho $RED " 输入错误,端口号为1025-65535的数字"
fi
done
colorEcho $BLUE " 请选择SS的加密方式:"
echo " 1)aes-256-gcm"
echo " 2)aes-192-gcm"
echo " 3)aes-128-gcm"
echo " 4)aes-256-ctr"
echo " 5)aes-192-ctr"
echo " 6)aes-128-ctr"
echo " 7)aes-256-cfb"
echo " 8)aes-192-cfb"
echo " 9)aes-128-cfb"
echo " 10)camellia-128-cfb"
echo " 11)camellia-192-cfb"
echo " 12)camellia-256-cfb"
echo " 13)chacha20-ietf"
echo " 14)chacha20-ietf-poly1305"
echo " 15)xchacha20-ietf-poly1305"
read -p " 请选择加密方式(默认aes-256-gcm)" answer
if [ -z "$answer" ]; then
METHOD="aes-256-gcm"
else
case $answer in
1)
METHOD="aes-256-gcm"
;;
2)
METHOD="aes-192-gcm"
;;
3)
METHOD="aes-128-gcm"
;;
4)
METHOD="aes-256-ctr"
;;
5)
METHOD="aes-192-ctr"
;;
6)
METHOD="aes-128-ctr"
;;
7)
METHOD="aes-256-cfb"
;;
8)
METHOD="aes-192-cfb"
;;
9)
METHOD="aes-128-cfb"
;;
10)
METHOD="camellia-128-cfb"
;;
11)
METHOD="camellia-192-cfb"
;;
12)
METHOD="camellia-256-cfb"
;;
13)
METHOD="chacha20-ietf"
;;
14)
METHOD="chacha20-ietf-poly1305"
;;
15)
METHOD="xchacha20-ietf-poly1305"
;;
*)
colorEcho $RED " 无效的选择,使用默认的aes-256-gcm"
METHOD="aes-256-gcm"
esac
fi
echo ""
colorEcho $BLUE " 加密方式: $METHOD"
echo ""
}
preinstall() {
colorEcho $BLUE " 更新系统..."
apt clean all
apt update
#apt upgrade -y
colorEcho $BLUE " 安装必要软件"
apt install -y telnet wget vim net-tools unzip tar qrencode
apt install -y make openssl libssl-dev gettext gcc autoconf libtool automake make asciidoc xmlto libudns-dev libev-dev libpcre3 libpcre3-dev libmbedtls-dev libsodium-dev libc-ares2 libc-ares-dev g++
apt install -y libsodium*
apt autoremove -y
res=`which wget`
[ "$?" != "0" ] && apt install -y wget
res=`which netstat`
[ "$?" != "0" ] && apt install -y net-tools
}
normalizeVersion() {
if [ -n "$1" ]; then
case "$1" in
v*)
echo "${1:1}"
;;
*)
echo "$1"
;;
esac
else
echo ""
fi
}
installNewVer() {
new_ver=$1
if ! wget "${V6_PROXY}https://github.com/shadowsocks/shadowsocks-libev/releases/download/v${new_ver}/shadowsocks-libev-${new_ver}.tar.gz" -O shadowsocks-libev.tar.gz; then
colorEcho $RED " 下载安装文件失败!"
exit 1
fi
tar zxf shadowsocks-libev.tar.gz
cd shadowsocks-libev-${new_ver}
./configure
make && make install
if [[ $? -ne 0 ]]; then
echo
echo -e " [${RED}错误${PLAIN}]: $OS Shadowsocks-libev 安装失败! 请打开 https://hijk.art 反馈"
cd ${BASE} && rm -rf shadowsocks-libev*
exit 1
fi
cd ${BASE} && rm -rf shadowsocks-libev*
}
installSS() {
colorEcho $BLUE " 安装SS..."
tag_url="${V6_PROXY}https://api.github.com/repos/shadowsocks/shadowsocks-libev/releases/latest"
new_ver="$(normalizeVersion "$(curl -s "${tag_url}" --connect-timeout 10| grep 'tag_name' | cut -d\" -f4)")"
export PATH=/usr/local/bin:$PATH
res=`which ss-server`
if [ "$?" != "0" ]; then
installNewVer $new_ver
else
ver=`ss-server -h | grep shadowsocks-libev | grep -oE '[0-9+\.]+'`
if [[ $ver != $new_ver ]]; then
installNewVer $new_ver
else
colorEcho $YELLOW " 已安装最新版SS"
fi
fi
interface="0.0.0.0"
if [[ "$V6_PROXY" != "" ]]; then
interface="::"
fi
mkdir -p /etc/shadowsocks-libev
ssPath=`which ss-server`
cat > $CONFIG_FILE<<-EOF
{
"server":"$interface",
"server_port":${PORT},
"local_port":1080,
"password":"${PASSWORD}",
"timeout":600,
"method":"${METHOD}",
"nameserver":"8.8.8.8",
"mode":"tcp_and_udp",
"fast_open":false
}
EOF
cat > /lib/systemd/system/shadowsocks-libev.service <<-EOF
[Unit]
Description=shadowsocks
Documentation=https://hijk.art/
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
PIDFile=/var/run/shadowsocks-libev.pid
LimitNOFILE=32768
ExecStart=/usr/local/bin/ss-server -c $CONFIG_FILE -f /var/run/shadowsocks-libev.pid
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable shadowsocks-libev
systemctl restart shadowsocks-libev
sleep 3
res=`netstat -nltp | grep ${PORT} | grep 'ss-server'`
if [ "${res}" = "" ]; then
colorEcho $RED " ss启动失败,请检查端口是否被占用!"
exit 1
fi
}
setFirewall() {
res=`ufw status | grep -i inactive`
if [ "$res" = "" ];then
ufw allow ${PORT}/tcp
ufw allow ${PORT}/udp
fi
}
installBBR() {
result=$(lsmod | grep bbr)
if [ "$result" != "" ]; then
colorEcho $BLUE " BBR模块已安装"
INSTALL_BBR=false
return;
fi
res=`hostnamectl | grep -i openvz`
if [ "$res" != "" ]; then
colorEcho $YELLOW " openvz机器,跳过安装"
INSTALL_BBR=false
return
fi
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
result=$(lsmod | grep bbr)
if [[ "$result" != "" ]]; then
colorEcho $GREEN " BBR模块已启用"
INSTALL_BBR=false
return
fi
colorEcho $BLUE " 安装BBR模块..."
apt install -y --install-recommends linux-generic-hwe-16.04
grub-set-default 0
echo "tcp_bbr" >> /etc/modules-load.d/modules.conf
INSTALL_BBR=true
}
info() {
port=`grep server_port $CONFIG_FILE| cut -d: -f2 | tr -d \",' '`
res=`netstat -nltp | grep ${port} | grep 'ss-server'`
[ -z "$res" ] && status="${RED}已停止${PLAIN}" || status="${GREEN}正在运行${PLAIN}"
password=`grep password $CONFIG_FILE| cut -d: -f2 | tr -d \",' '`
method=`grep method $CONFIG_FILE| cut -d: -f2 | tr -d \",' '`
res=`echo -n ${method}:${password}@${IP}:${port} | base64 -w 0`
link="ss://${res}"
echo ============================================
echo -e " ${BLUE}ss运行状态:${PLAIN}${status}"
echo -e " ${BLUE}ss配置文件:${PLAIN}${RED}$CONFIG_FILE${PLAIN}"
echo ""
echo -e " ${RED}ss配置信息:${PLAIN}"
echo -e " ${BLUE}IP(address): ${PLAIN} ${RED}${IP}${PLAIN}"
echo -e " ${BLUE}端口(port):${PLAIN}${RED}${port}${PLAIN}"
echo -e " ${BLUE}密码(password):${PLAIN}${RED}${password}${PLAIN}"
echo -e " ${BLUE}加密方式(method):${PLAIN} ${RED}${method}${PLAIN}"
echo
echo -e " ${BLUE}ss链接:${PLAIN} ${link}"
qrencode -o - -t utf8 ${link}
}
bbrReboot() {
if [ "${INSTALL_BBR}" == "true" ]; then
echo
colorEcho $BLUE " 为使BBR模块生效,系统将在30秒后重启"
echo
echo -e " 您可以按 ctrl + c 取消重启,稍后输入 ${RED}reboot${PLAIN} 重启系统"
sleep 30
reboot
fi
}
install() {
echo -n " 系统版本: "
lsb_release -a
checkSystem
getData
preinstall
installSS
setFirewall
info
bbrReboot
}
uninstall() {
read -p " 确定卸载SS吗?(y/n)" answer
[ -z ${answer} ] && answer="n"
if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
systemctl stop shadowsocks-libev && systemctl disable shadowsocks-libev
rm -rf /lib/systemd/system/shadowsocks-libev.service
cd /usr/local/bin && rm -rf ss-local ss-manager ss-nat ss-redir ss-server ss-tunnel
rm -rf /usr/lib64/libshadowsocks-libev*
rm -rf /usr/share/doc/shadowsocks-libev
rm -rf /usr/share/man/man1/ss-*.1.gz
rm -rf /usr/share/man/man8/shadowsocks-libev.8.gz
colorEcho $GREEN " SS卸载完成"
fi
}
slogon
action=$1
[ -z $1 ] && action=install
case "$action" in
install|uninstall|info)
${action}
;;
*)
echo " 参数错误"
echo " 用法: `basename $0` [install|uninstall|info]"
;;
esac