forked from hijkpw/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntu_install_ssr.sh
428 lines (394 loc) · 10.9 KB
/
ubuntu_install_ssr.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/bin/bash
# shadowsocksR/SSR Ubuntu一键安装教程
# Author: hijk<https://www.hijk.pw>
echo "#############################################################"
echo "# Ubuntu TLS ShadowsocksR/SSR 一键安装脚本 #"
echo "# 网址: https://www.hijk.pw #"
echo "# 作者: hijk #"
echo "#############################################################"
echo ""
red='\033[0;31m'
green="\033[0;32m"
plain='\033[0m'
FILENAME="ShadowsocksR-v3.2.2"
URL="https://github.com/shadowsocksrr/shadowsocksr/archive/3.2.2.tar.gz"
BASE=`pwd`
function checkSystem()
{
result=$(id | awk '{print $1}')
if [ $result != "uid=0(root)" ]; then
echo "请以root身份执行该脚本"
exit 1
fi
res=`lsb_release -d | grep -i ubuntu`
if [ "$?" != "0" ]; then
res=`which apt`
if [ "$?" != "0" ]; then
echo "系统不是Ubuntu"
exit 1
fi
else
result=`lsb_release -d | grep -oE "[0-9.]+"`
main=${result%%.*}
if [ $main -lt 16 ]; then
echo "不受支持的Ubuntu版本"
exit 1
fi
fi
}
function getData()
{
read -p "请设置SSR的密码(不输入则随机生成):" password
[ -z "$password" ] && password=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
echo ""
echo "密码: $password"
echo ""
while true
do
read -p "请设置SSR的端口号[1-65535]:" port
[ -z "$port" ] && port="12345"
expr $port + 0 &>/dev/null
if [ $? -eq 0 ]; then
if [ $port -ge 1 ] && [ $port -le 65535 ]; then
echo ""
echo "端口号: $port"
echo ""
break
else
echo "输入错误,端口号为1-65535的数字"
fi
else
echo "输入错误,端口号为1-65535的数字"
fi
done
echo "请选择SSR的加密方式:"
echo "1)aes-256-cfb"
echo "2)aes-192-cfb"
echo "3)aes-128-cfb"
echo "4)aes-256-ctr"
echo "5)aes-192-ctr"
echo "6)aes-128-ctr"
echo "7)aes-256-cfb8"
echo "8)aes-192-cfb8"
echo "9)aes-128-cfb8"
echo "10)camellia-128-cfb"
echo "11)camellia-192-cfb"
echo "12)camellia-256-cfb"
echo "13)chacha20-ietf"
read -p "请选择加密方式(默认aes-256-cfb)" answer
if [ -z "$answer" ]; then
method="aes-256-cfb"
else
case $answer in
1)
method="aes-256-cfb"
;;
2)
method="aes-192-cfb"
;;
3)
method="aes-128-cfb"
;;
4)
method="aes-256-ctr"
;;
5)
method="aes-192-ctr"
;;
6)
method="aes-128-ctr"
;;
7)
method="aes-256-cfb8"
;;
8)
method="aes-192-cfb8"
;;
9)
method="aes-128-cfb8"
;;
10)
method="camellia-128-cfb"
;;
11)
method="camellia-192-cfb"
;;
12)
method="camellia-256-cfb"
;;
13)
method="chacha20-ietf"
;;
*)
echo "无效的选择,使用默认加密方式"
method="aes-256-cfb"
esac
fi
echo ""
echo "加密方式: $method"
echo ""
echo "请选择SSR的协议:"
echo "1)origin"
echo "2)verify_deflate"
echo "3)auth_sha1_v4"
echo "4)auth_aes128_md5"
echo "5)auth_aes128_sha1"
echo "6)auth_chain_a"
echo "7)auth_chain_b"
echo "8)auth_chain_c"
echo "9)auth_chain_d"
echo "10)auth_chain_e"
echo "11)auth_chain_f"
read -p "请选择加密方式(默认origin)" answer
if [ -z "$answer" ]; then
protocol="origin"
else
case $answer in
1)
protocol="origin"
;;
2)
protocol="verify_deflate"
;;
3)
protocol="auth_sha1_v4"
;;
4)
protocol="auth_aes128_md5"
;;
5)
protocol="auth_aes128_sha1"
;;
6)
protocol="auth_chain_a"
;;
7)
protocol="auth_chain_b"
;;
8)
protocol="auth_chain_c"
;;
9)
protocol="auth_chain_d"
;;
10)
protocol="auth_chain_e"
;;
11)
protocol="auth_chain_f"
;;
*)
echo "无效的选择,使用默认协议"
protocol="origin"
esac
fi
echo ""
echo "协议: $protocol"
echo ""
echo "请选择SSR混淆模式:"
echo "1)plain"
echo "2)http_simple"
echo "3)http_post"
echo "4)tls1.2_ticket_auth"
echo "5)tls1.2_ticket_fastauth"
read -p "请选择混淆模式(默认plain)" answer
if [ -z "$answer" ]; then
obfs="plain"
else
case $answer in
1)
obfs="plain"
;;
2)
obfs="http_simple"
;;
3)
obfs="http_post"
;;
4)
obfs="tls1.2_ticket_auth"
;;
5)
obfs="tls1.2_ticket_fastauth"
;;
*)
echo "无效的选择,使用默认混淆模式"
obfs="plain"
esac
fi
echo ""
echo "混淆: $obfs"
echo ""
}
function preinstall()
{
sed -i 's/#ClientAliveInterval 0/ClientAliveInterval 60/' /etc/ssh/sshd_config
systemctl restart sshd
apt update && apt upgrade -y
echo "安装必要软件"
apt install -y telnet curl wget vim net-tools libsodium18 openssl unzip
apt autoremove -y
res=`which python`
if [ "$?" != "0" ]; then
ln -s /usr/bin/python3 /usr/bin/python
fi
}
function installSSR()
{
if [ ! -d /usr/local/shadowsocks ]; then
echo 下载安装文件
if ! wget --no-check-certificate -O ${FILENAME}.tar.gz ${URL}; then
echo -e "[${red}Error${plain}] 下载文件失败!"
exit 1
fi
tar -zxf ${FILENAME}.tar.gz
mv shadowsocksr-3.2.2/shadowsocks /usr/local
if [ ! -f /usr/local/shadowsocks/server.py ]; then
echo "安装失败,请到 https://www.hijk.pw 网站反馈"
cd ${BASE} && rm -rf shadowsocksr-3.2.2 ${FILENAME}.tar.gz
exit 1
fi
fi
cat > /etc/shadowsocksR.json<<-EOF
{
"server":"0.0.0.0",
"server_ipv6":"[::]",
"server_port":${port},
"local_port":1080,
"password":"${password}",
"timeout":600,
"method":"${method}",
"protocol":"${protocol}",
"protocol_param":"",
"obfs":"${obfs}",
"obfs_param":"",
"redirect":"",
"dns_ipv6":false,
"fast_open":false,
"workers":1
}
EOF
cat > /lib/systemd/system/shadowsocksR.service <<-EOF
[Unit]
Description=shadowsocksR
Documentation=https://www.hijk.pw/
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
LimitNOFILE=32768
ExecStart=/usr/local/shadowsocks/server.py -c /etc/shadowsocksR.json -d start
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable shadowsocksR && systemctl restart shadowsocksR
sleep 3
res=`netstat -nltp | grep ${port} | grep python`
if [ "${res}" = "" ]; then
echo "ssr启动失败,请检查端口是否被占用!"
exit 1
fi
}
function setFirewall()
{
res=`ufw status | grep -i inactive`
if [ "$res" = "" ];then
ufw allow ${port}/tcp
ufw allow ${port}/udp
fi
}
function installBBR()
{
result=$(lsmod | grep bbr)
if [ "$result" != "" ]; then
echo BBR模块已安装
bbr=true
echo "3" > /proc/sys/net/ipv4/tcp_fastopen
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
return;
fi
echo 安装BBR模块...
apt install -y --install-recommends linux-generic-hwe-16.04
grub-set-default 0
echo "tcp_bbr" >> /etc/modules-load.d/modules.conf
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
echo "3" > /proc/sys/net/ipv4/tcp_fastopen
echo "net.ipv4.tcp_fastopen = 3" >> /etc/sysctl.conf
bbr=false
}
function info()
{
ip=`curl -s -4 icanhazip.com`
port=`cat /etc/shadowsocksR.json | grep server_port | cut -d: -f2 | tr -d \",' '`
res=`netstat -nltp | grep ${port} | grep python`
[ -z "$res" ] && status="${red}已停止${plain}" || status="${green}正在运行${plain}"
password=`cat /etc/shadowsocksR.json | grep password | cut -d: -f2 | tr -d \",' '`
method=`cat /etc/shadowsocksR.json | grep method | cut -d: -f2 | tr -d \",' '`
protocol=`cat /etc/shadowsocksR.json | grep protocol | cut -d: -f2 | tr -d \",' '`
obfs=`cat /etc/shadowsocksR.json | grep obfs | cut -d: -f2 | tr -d \",' '`
echo ============================================
echo -e " ssr运行状态:${status}"
echo -e " ssr配置文件:${red}/etc/shadowsocksR.json${plain}"
echo ""
echo -e "${red}ssr配置信息:${plain}"
echo -e " IP(address): ${red}${ip}${plain}"
echo -e " 端口(port):${red}${port}${plain}"
echo -e " 密码(password):${red}${password}${plain}"
echo -e " 加密方式(method): ${red}${method}${plain}"
echo -e " 协议(protocol):" ${red}${protocol}${plain}
echo -e " 混淆(obfuscation):" ${red}${obfs}${plain}
echo
echo ============================================
}
function bbrReboot()
{
if [ "${bbr}" == "false" ]; then
echo
echo 为使BBR模块生效,系统将在30秒后重启
echo
echo -e "您可以按 ctrl + c 取消重启,稍后输入 ${red}reboot${plain} 重启系统"
sleep 30
reboot
fi
}
function install()
{
echo -n "系统版本: "
lsb_release -a
checkSystem
getData
preinstall
installBBR
installSSR
setFirewall
info
cd ${BASE} && rm -rf shadowsocksr-3.2.2 ${FILENAME}.tar.gz
bbrReboot
}
function uninstall()
{
read -p "您确定真的要卸载SSR吗?(y/n)" answer
[ -z ${answer} ] && answer="n"
if [ "${answer}" == "y" ] || [ "${answer}" == "Y" ]; then
rm -f /etc/shadowsocksR.json
rm -f /var/log/shadowsocks.log
rm -rf /usr/local/shadowsocks
systemctl disable shadowsocksR && systemctl stop shadowsocksR && rm -rf /lib/systemd/system/shadowsocksR.service
fi
echo -e " ${red}卸载成功${plain}"
}
action=$1
[ -z $1 ] && action=install
case "$action" in
install|uninstall|info)
${action}
;;
*)
echo "参数错误"
echo "用法: `basename $0` [install|uninstall|info]"
;;
esac