forked from chln/shadowsocks_bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadowsocks-libev.sh
646 lines (596 loc) · 20.4 KB
/
shadowsocks-libev.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#! /usr/bin/env bash
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin"
export PATH
#Github: https://github.com/uxh/shadowsocks_bash
#Author: https://www.banwagongzw.com & https://www.vultrcn.com
#Thanks: https://teddysun.com
#Color
red="\033[0;31m"
green="\033[0;32m"
yellow="\033[0;33m"
plain="\033[0m"
#Directory
currentdir=$(pwd)
#Streamcipher
ciphers=(
aes-256-gcm
aes-256-ctr
aes-256-cfb
chacha20-ietf-poly1305
chacha20-ietf
chacha20
rc4-md5
)
#Version and url
libsodiumver="libsodium-1.0.16"
libsodiumurl="https://github.com/jedisct1/libsodium/releases/download/1.0.16/libsodium-1.0.16.tar.gz"
mbedtlsver="mbedtls-2.11.0"
mbedtlsurl="https://tls.mbed.org/download/mbedtls-2.11.0-gpl.tgz"
shadowsocksver="shadowsocks-libev-3.2.0"
shadowsocksurl="https://github.com/shadowsocks/shadowsocks-libev/releases/download/v3.2.0/shadowsocks-libev-3.2.0.tar.gz"
initscripturl="https://raw.githubusercontent.com/uxh/shadowsocks_bash/master/shadowsocks-libev"
#Disable selinux
function disable_selinux() {
if [ -s /etc/selinux/config ] && grep "SELINUX=enforcing" /etc/selinux/config; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
fi
}
#Check release
function check_release() {
local value=$1
local release="none"
if [ -f /etc/redhat-release ]; then
release="centos"
elif grep -qi "centos|red hat|redhat" /etc/issue; then
release="centos"
elif grep -qi "debian" /etc/issue; then
release="debian"
elif grep -qi "ubuntu" /etc/issue; then
release="ubuntu"
elif grep -qi "centos|red hat|redhat" /proc/version; then
release="centos"
elif grep -qi "debian" /proc/version; then
release="debian"
elif grep -qi "ubuntu" /proc/version; then
release="ubuntu"
elif grep -qi "centos|red hat|redhat" /etc/*-release; then
release="centos"
elif grep -qi "debian" /etc/*-release; then
release="debian"
elif grep -qi "ubuntu" /etc/*-release; then
release="ubuntu"
fi
if [[ ${value} == ${release} ]]; then
return 0
else
return 1
fi
}
#Check shadowsocks status
function check_shadowsocks_status() {
installedornot="not"
runningornot="not"
updateornot="not"
command -v ss-server > /dev/null 2>&1
if [ $? -eq 0 ]; then
installedornot="installed"
ps -ef | grep -v "grep" | grep "ss-server" > /dev/null 2>&1
if [ $? -eq 0 ]; then
runningornot="running"
fi
local installedversion=$(ss-server -h | grep "shadowsocks-libev" | cut -d " " -f 2)
local latestversion=$(echo "$(wget --no-check-certificate -qO- https://api.github.com/repos/shadowsocks/shadowsocks-libev/releases/latest | grep "tag_name" | cut -d "\"" -f 4)" | sed -e 's/^[a-zA-Z]//g')
if [ ! -z ${latestversion} ]; then
if [[ ${installedversion} != ${latestversion} ]]; then
updateornot="update"
shadowsocksnewver="shadowsocks-libev-${latestversion}"
shadowsocksnewurl="https://github.com/shadowsocks/shadowsocks-libev/releases/download/${ver}/${shadowsocks_libev_ver}.tar.gz"
fi
fi
fi
}
#Check centos main version
function check_centos_main_version() {
local value=$1
local version="0.0.0"
if [ -s /etc/redhat-release ]; then
version=$(grep -Eo "[0-9.]+" /etc/redhat-release)
else
version=$(grep -Eo "[0-9.]+" /etc/issue)
fi
local mainversion=${version%%.*}
if [ ${value} -eq ${mainversion} ]; then
return 0
else
return 1
fi
}
#Check kernel version
function check_kernel_version() {
local kernelversion=$(uname -r | cut -d "-" -f 1)
local olderversion=$(echo "${kernelversion} 3.7.0" | tr " " "\n" | sort -V | head -n 1)
if [[ ${olderversion} == "3.7.0" ]]; then
return 0
else
return 1
fi
}
#Check kernel headers
function check_kernel_headers() {
local nowkernel=$(uname -r)
if check_release centos; then
rpm -qa | grep "headers-${nowkernel}" > /dev/null 2>&1
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
else
dpkg -s linux-headers-${nowkernel} > /dev/null 2>&1
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
fi
}
#Get ipv4
function get_ipv4() {
local ipv4=$(ip addr | grep -Eo "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" | grep -Ev "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1)
if [ -z ${ipv4} ]; then
ipv4=$(wget -qO- -t 1 -T 10 ipv4.icanhazip.com)
fi
if [ -z ${ipv4} ]; then
ipv4=$(wget -qO- -t 1 -T 10 ipinfo.io/ip)
fi
echo -e "${ipv4}"
}
#Get ipv6
function check_ipv6() {
local ipv6=$(wget -qO- -t 1 -T 10 ipv6.icanhazip.com)
if [ -z ${ipv6} ]; then
return 1
else
return 0
fi
}
#Set shadowsocks config
function set_shadowsocks_config() {
clear
echo -e "${green}[Info]${plain} Start set shadowsocks's config information..."
echo -e "${green}[Info]${plain} Wherever you are not sure, just press Enter to continue."
echo ""
echo "Please enter shadowsocks's password"
read -p "[Default is Number1433223]:" sspassword
if [ -z ${sspassword} ]; then
sspassword="Number1433223"
fi
echo "-------------------------------"
echo "Shadowsocks's Password: ${sspassword}"
echo "-------------------------------"
local defaultport=$(shuf -i 9000-9999 -n 1)
echo "Please enter shadowsocks's port (1~65535)"
while true
do
read -p "[Default is ${defaultport}]:" ssport
if [ -z ${ssport} ]; then
ssport=${defaultport}
fi
expr ${ssport} + 1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
if [ ${ssport} -ge 1 ] && [ ${ssport} -le 65535 ]; then
echo "-------------------------------"
echo "Shadowsocks's Port: ${ssport}"
echo "-------------------------------"
break
else
echo -e "${red}[Error]${plain} Please enter a number between 1 and 65535!"
fi
else
echo -e "${red}[Error]${plain} Please enter a number between 1 and 65535!"
fi
done
echo "Please select shadowsocks's stream cipher"
for ((i=1;i<=${#ciphers[@]};i++));
do
local cipher=${ciphers[$i-1]}
echo -e "${i}) ${cipher}"
done
while true
do
read -p "[Default is ${ciphers[0]}]:" ciphernumber
if [ -z ${ciphernumber} ]; then
ciphernumber="1"
fi
expr ${ciphernumber} + 1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
if [ ${ciphernumber} -ge 1 ] && [ ${ciphernumber} -le ${#ciphers[@]} ]; then
sscipher=${ciphers[${ciphernumber}-1]}
echo "-------------------------------"
echo "Shadowsocks's Streamcipher: ${sscipher}"
echo "-------------------------------"
break
else
echo -e "${red}[Error]${plain} Please enter a number between 1 and ${#ciphers[@]}!"
fi
else
echo -e "${red}[Error]${plain} Please enter a number between 1 and ${#ciphers[@]}!"
fi
done
echo ""
echo "Press Enter to continue...or Press Ctrl+C to cancel"
read -n 1
}
#install dependencies
function install_dependencies() {
if check_release centos; then
if [ ! -f /etc/yum.repos.d/epel.repo ]; then
yum install -y epel-release
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} EPEL install failed, please try again!"
exit 1
fi
fi
command -v yum-config-manager > /dev/null 2>&1
if [ $? -ne 0 ]; then
yum install -y yum-utils
fi
local epelstatus=$(yum-config-manager epel | grep -w "enabled" | cut -d " " -f 3)
if [[ ${epelstatus} != "True" ]]; then
yum-config-manager --enable epel
fi
yum install -y unzip openssl openssl-devel gettext gcc autoconf libtool automake make asciidoc xmlto libev-devel pcre pcre-devel git c-ares-devel
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} Dependencies install failed, please try again!"
exit 1
fi
else
apt-get update
apt-get install --no-install-recommends -y gettext build-essential autoconf automake libtool openssl libssl-dev zlib1g-dev libpcre3-dev libev-dev libc-ares-dev
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} Dependencies install failed, please try again!"
exit 1
fi
fi
echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" > /etc/resolv.conf
}
#Set firewall
function set_firewall() {
if check_release centos; then
if check_centos_main_version 6; then
/etc/init.d/iptables status > /dev/null 2>&1
if [ $? -eq 0 ]; then
iptables -L -n | grep "${ssport}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${ssport} -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${ssport} -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart
fi
fi
elif check_centos_main_version 7; then
systemctl status firewalld > /dev/null 2>&1
if [ $? -eq 0 ]; then
firewall-cmd --query-port=${ssport}/tcp > /dev/null 2>&1
if [ $? -ne 0 ]; then
firewall-cmd --permanent --zone=public --add-port=${ssport}/tcp
firewall-cmd --permanent --zone=public --add-port=${ssport}/udp
firewall-cmd --reload
fi
fi
fi
fi
}
#Download
function download() {
local filename=$1
if [ -s ${filename} ]; then
echo -e "${green}[Info]${plain} ${filename} found."
else
echo -e "${green}[Info]${plain} ${filename} not found, start to download..."
wget --no-check-certificate -c -t 3 -T 60 -O $1 $2
if [ $? -eq 0 ]; then
echo -e "${green}[Info]${plain} ${filename} download complete."
else
echo -e "${green}[Info]${plain} ${filename} download failed, please try again!"
exit 1
fi
fi
}
#Install libsodium
function install_libsodium() {
cd ${currentdir}
if [ ! -f /usr/lib/libsodium.a ]; then
download "${libsodiumver}.tar.gz" "${libsodiumurl}"
tar zxf ${libsodiumver}.tar.gz
cd ${libsodiumver}
./configure --prefix=/usr && make && make install
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} ${libsodiumver} install failed, please try again!"
exit 1
fi
else
echo -e "${green}[Info]${plain} ${libsodiumver} has been installed."
fi
cd ${currentdir}
rm -rf ${libsodiumver} ${libsodiumver}.tar.gz
}
#nstall mbedtls
function install_mbedtls() {
cd ${currentdir}
if [ ! -f /usr/lib/libmbedtls.a ]; then
download "${mbedtlsver}-gpl.tgz" "${mbedtlsurl}"
tar xf ${mbedtlsver}-gpl.tgz
cd ${mbedtlsver}
make SHARED=1 CFLAGS=-fPIC
make DESTDIR=/usr install
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} ${mbedtlsver} install failed, please try again!"
exit 1
fi
else
echo -e "${green}[Info]${plain} ${mbedtlsver} has been installed."
fi
cd ${currentdir}
rm -rf ${mbedtlsver} ${mbedtlsver}-gpl.tgz
}
#Config shadowsocks
function config_shadowsocks() {
if check_ipv6; then
server_value="[\"[::0]\",\"0.0.0.0\"]"
else
server_value="\"0.0.0.0\""
fi
if check_kernel_version && check_kernel_headers; then
fast_open="true"
else
fast_open="false"
fi
if [ ! -d /etc/shadowsocks-libev ]; then
mkdir -p /etc/shadowsocks-libev
fi
cat > /etc/shadowsocks-libev/config.json << EOF
{
"server":${server_value},
"server_port":${ssport},
"password":"${sspassword}",
"timeout":300,
"user":"nobody",
"method":"${sscipher}",
"fast_open":${fast_open},
"nameserver":"8.8.8.8",
"mode":"tcp_and_udp"
}
EOF
}
#Install shadowsocks
function install_shadowsocks() {
ldconfig
cd ${currentdir}
if [[ ${updateornot} == "not" ]]; then
download "${shadowsocksver}.tar.gz" "${shadowsocksurl}"
else
download "${shadowsocksnewver}.tar.gz" "${shadowsocksnewurl}"
fi
tar zxf ${shadowsocksver}.tar.gz
cd ${shadowsocksver}
./configure --disable-documentation
make && make install
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} Shadowsocks install failed, please try again!"
exit 1
fi
if [ ! -f /etc/init.d/shadowsocks ]; then
download "/etc/init.d/shadowsocks" "${initscripturl}"
fi
chmod +x /etc/init.d/shadowsocks
/etc/init.d/shadowsocks start
if [ $? -ne 0 ]; then
echo -e "${red}[Error]${plain} Shadowsocks start failed, please try again!"
exit 1
fi
if check_release centos; then
chkconfig --add shadowsocks
chkconfig shadowsocks on
else
update-rc.d -f shadowsocks defaults
fi
cd ${currentdir}
rm -rf ${shadowsocksver} ${shadowsocksver}.tar.gz
}
#Uninstall shadowsocks
function uninstall_shadowsocks() {
ps -ef | grep -v "grep" | grep "ss-server" > /dev/null 2>&1
if [ $? -eq 0 ]; then
/etc/init.d/shadowsocks stop
fi
if check_release centos; then
chkconfig --del shadowsocks
else
update-rc.d -f shadowsocks remove
fi
rm -rf /etc/shadowsocks-libev
rm -f /usr/local/bin/ss-local
rm -f /usr/local/bin/ss-tunnel
rm -f /usr/local/bin/ss-server
rm -f /usr/local/bin/ss-manager
rm -f /usr/local/bin/ss-redir
rm -f /usr/local/bin/ss-nat
rm -f /usr/local/lib/libshadowsocks-libev.a
rm -f /usr/local/lib/libshadowsocks-libev.la
rm -f /usr/local/include/shadowsocks.h
rm -f /usr/local/lib/pkgconfig/shadowsocks-libev.pc
rm -f /usr/local/share/man/man1/ss-local.1
rm -f /usr/local/share/man/man1/ss-tunnel.1
rm -f /usr/local/share/man/man1/ss-server.1
rm -f /usr/local/share/man/man1/ss-manager.1
rm -f /usr/local/share/man/man1/ss-redir.1
rm -f /usr/local/share/man/man1/ss-nat.1
rm -f /usr/local/share/man/man8/shadowsocks-libev.8
rm -rf /usr/local/share/doc/shadowsocks-libev
rm -f /etc/init.d/shadowsocks
rm -f /root/shadowsocks.txt
}
#Install success
function install_success() {
local ssurl=$(echo -n "${sscipher}:${sspassword}@$(get_ipv4):${ssport}" | base64 -w0)
clear
echo -e "${green}[Info]${plain} Congratulations, Shadowsocks has been installed successfully."
echo -e "================================================="
echo -e "Server IP : \033[41;37m $(get_ipv4) \033[0m"
echo -e "Server Port : \033[41;37m ${ssport} \033[0m"
echo -e "Password : \033[41;37m ${sspassword} \033[0m"
echo -e "Encryption Method: \033[41;37m ${sscipher} \033[0m"
echo -e "-------------------------------------------------"
echo -e "ss://${ssurl}"
echo -e "================================================="
cat > /root/shadowsocks.txt << EOF
===============================================
服务器地址 : $(get_ipv4)
服务端口 : ${ssport}
连接密码 : ${sspassword}
加密方式 : ${sscipher}
-----------------------------------------------
ss://${ssurl}
===============================================
EOF
echo -e "You can find the config's backup in /root/shadowsocks.txt."
}
install_main() {
disable_selinux
set_shadowsocks_config
install_dependencies
set_firewall
install_libsodium
install_mbedtls
config_shadowsocks
install_shadowsocks
install_success
}
uninstall_main() {
uninstall_shadowsocks
echo -e "${green}[Info]${plain} Shadowsocks uninstall successfully."
}
update_main() {
if [[ ${updateornot} == "update" ]]; then
ps -ef | grep -v grep | grep -i "ss-server" > /dev/null 2>&1
[ $? -eq 0 ] && /etc/init.d/shadowsocks stop
install_shadowsocks
echo -e "${green}[Info]${plain} Shadowsocks Update successfully."
else
echo -e "${green}[Info]${plain} Latest version has been installed."
fi
}
start_main() {
/etc/init.d/shadowsocks start
if [ $? -eq 0 ]; then
echo -e "${green}[Info]${plain} Shadowsocks start successfully."
else
echo -e "${red}[Error]${plain} Shadowsocks start failed, please try again!"
fi
}
stop_main() {
/etc/init.d/shadowsocks stop
if [ $? -eq 0 ]; then
echo -e "${green}[Info]${plain} Shadowsocks start successfully."
else
echo -e "${red}[Error]${plain} Shadowsocks stop failed, please try again!"
fi
}
restart_main() {
/etc/init.d/shadowsocks stop
/etc/init.d/shadowsocks start
if [ $? -eq 0 ]; then
echo -e "${green}[Info]${plain} Shadowsocks restart successfully."
else
echo -e "${red}[Error]${plain} Shadowsocks restart failed, please try again!"
fi
}
status_main() {
echo -e "${green}[Info]${plain} Congratulations, Shadowsocks has been installed successfully."
cat /root/shadowsocks.txt
echo "This information is just for reference. Please view the Shadowsocks configuration file."
}
modify_main() {
set_shadowsocks_config
/etc/init.d/shadowsocks stop
set_firewall
config_shadowsocks
/etc/init.d/shadowsocks start
install_success
}
#Main control
if [ $EUID -eq 0 ]; then
if check_release centos || check_release debian || check_release ubuntu; then
clear
echo "===================================="
echo "Shadowsocks Server Management Script"
echo " www.banwagongzw.com & vultrcn.com "
echo "===================================="
echo " 1.Shadowsocks Server Install"
echo " 2.Shadowsocks Server Uninstall"
echo " 3.Shadowsocks Server Update"
echo "------------------------------------"
echo " 4.Shadowsocks Server Start"
echo " 5.Shadowsocks Server Stop"
echo " 6.Shadowsocks Server Restart"
echo "------------------------------------"
echo " 7.Shadowsocks Config Status"
echo " 8.Shadowsocks Config Modify"
echo "===================================="
check_shadowsocks_status
if [[ ${installedornot} == "installed" ]]; then
if [[ ${runningornot} == "running" ]]; then
if [[ ${updateornot} == "not" ]]; then
echo -e "${green}Installed and Running${plain}"
else
echo -e "${green}Installed and Running, Update available${plain}"
fi
else
echo -e "${yellow}Installed but Not running${plain}"
fi
else
echo -e "${red}Not installed${plain}"
fi
while true
do
echo ""
read -p "Please Enter the Number:" choice
[[ -z ${choice} ]] && choice="0"
expr ${choice} + 1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
if [ ${choice} -ge 1 ] && [ ${choice} -le 8 ]; then
if [ "${choice}" == "1" ]; then
install_main
elif [ "${choice}" == "2" ]; then
uninstall_main
elif [ "${choice}" == "3" ]; then
update_main
elif [ "${choice}" == "4" ]; then
start_main
elif [ "${choice}" == "5" ]; then
stop_main
elif [ "${choice}" == "6" ]; then
restart_main
elif [ "${choice}" == "7" ]; then
status_main
elif [ "${choice}" == "8" ]; then
modify_main
fi
break
else
echo -e "${red}[Error]${plain} Please enter a number between 1 and 8!"
fi
else
echo -e "${red}[Error]${plain} Please enter a number between 1 and 8!"
fi
done
else
echo -e "${red}[Error]${plain} This script only support CentOS, Debian and Ubuntu!"
exit 1
fi
else
echo -e "${red}[Error]${plain} This script need to run as root!"
exit 1
fi