forked from FunctionClub/SSR-Bash-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
self.sh
230 lines (226 loc) · 7.57 KB
/
self.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
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#Check Root
[ $(id -u) != "0" ] && { echo "Error: You must be root to run this script"; exit 1; }
#Check OS
if [ -n "$(grep 'Aliyun Linux release' /etc/issue)" -o -e /etc/redhat-release ];then
OS=CentOS
[ -n "$(grep ' 7\.' /etc/redhat-release)" ] && CentOS_RHEL_version=7
[ -n "$(grep ' 6\.' /etc/redhat-release)" -o -n "$(grep 'Aliyun Linux release6 15' /etc/issue)" ] && CentOS_RHEL_version=6
[ -n "$(grep ' 5\.' /etc/redhat-release)" -o -n "$(grep 'Aliyun Linux release5' /etc/issue)" ] && CentOS_RHEL_version=5
elif [ -n "$(grep 'Amazon Linux AMI release' /etc/issue)" -o -e /etc/system-release ];then
OS=CentOS
CentOS_RHEL_version=6
elif [ -n "$(grep bian /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Debian' ];then
OS=Debian
[ ! -e "$(which lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
Debian_version=$(lsb_release -sr | awk -F. '{print $1}')
elif [ -n "$(grep Deepin /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Deepin' ];then
OS=Debian
[ ! -e "$(which lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
Debian_version=$(lsb_release -sr | awk -F. '{print $1}')
elif [ -n "$(grep Ubuntu /etc/issue)" -o "$(lsb_release -is 2>/dev/null)" == 'Ubuntu' -o -n "$(grep 'Linux Mint' /etc/issue)" ];then
OS=Ubuntu
[ ! -e "$(which lsb_release)" ] && { apt-get -y update; apt-get -y install lsb-release; clear; }
Ubuntu_version=$(lsb_release -sr | awk -F. '{print $1}')
[ -n "$(grep 'Linux Mint 18' /etc/issue)" ] && Ubuntu_version=16
else
echo "Does not support this OS, Please contact the author! "
kill -9 $$
fi
#Main
updateme(){
cd ~
if [[ -e ~/version.txt ]];then
rm -f ~/version.txt
fi
wget -q https://git.fdos.me/stack/AR-B-P-B/raw/develop/version.txt
version1=`cat ~/version.txt`
version2=`cat /usr/local/SSR-Bash-Python/version.txt`
if [[ "$version1" == "$version2" ]];then
echo "你当前已是最新版"
sleep 2s
ssr
else
echo "当前最新版本为$version1,输入y进行更新,其它按键退出"
read -n 1 yn
if [[ $yn == [Yy] ]];then
export yn=n
wget -q -N --no-check-certificate https://git.fdos.me/stack/AR-B-P-B/raw/master/install.sh && bash install.sh develop
sleep 3s
clear
ssr || exit 0
else
echo "输入错误,退出"
bash /usr/local/SSR-Bash-Python/self.sh
fi
fi
}
sumdc(){
sum1=`cat /proc/sys/kernel/random/uuid| cksum | cut -f1 -d" "|head -c 2`
sum2=`cat /proc/sys/kernel/random/uuid| cksum | cut -f1 -d" "|head -c 1`
solve=`echo "$sum1-$sum2"|bc`
echo -e "请输入\e[32;49m $sum1-$sum2 \e[0m的运算结果,表示你已经确认,输入错误将退出"
read sv
}
backup(){
echo "开始备份!"
mkdir -p ${HOME}/backup/tmp
cd ${HOME}/backup/tmp
cp /usr/local/shadowsocksr/mudb.json ./
if [[ -e /usr/local/SSR-Bash-Python/check.log ]];then
cp /usr/local/SSR-Bash-Python/check.log ./
fi
if [[ -e /usr/local/SSR-Bash-Python/timelimit.db ]];then
cp /usr/local/SSR-Bash-Python/timelimit.db ./
fi
netstat -anlt | awk '{print $4}' | sed -e '1,2d' | awk -F : '{print $NF}' | sort -n | uniq >> ./port.conf
wf=`ls | wc -l`
if [[ $wf -ge 2 ]];then
tar -zcvf ../ssr-conf.tar.gz ./*
fi
cd ..
if [[ -e ./ssr-conf.tar.gz ]];then
rm -rf ./tmp
echo "备份成功,文件位于${HOME}/backup/ssr-conf.tar.gz"
else
echo "备份失败"
fi
}
recover(){
mkdir -p ${HOME}/backup
echo "这将会导致你现有的配置被覆盖"
sumdc
if [[ "$sv" == "$solve" ]];then
bakf=$(ls ${HOME}/backup | wc -l)
if [[ ${bakf} != 1 ]];then
cd /usr/local/SSR-Bash-Python/Explorer
if [[ ! -e /bin/usleep ]];then
gcc -o /bin/usleep ./usleep.c
fi
read -p "未发现备份文件或者存在多个备份文件,请手动选择(按Y键将打开一个文件管理器)" yn
if [[ ${yn} == [Yy] ]];then
chmod +x /usr/local/SSR-Bash-Python/Explorer/*
bash ./Explorer.sh "${HOME}/backup"
chmod -x /usr/local/SSR-Bash-Python/Explorer/*
bakfile=$(cat /tmp/BakFilename.tmp)
if [[ ! -e ${bakfile} ]];then
echo "无效!"
fi
fi
fi
if [[ -z ${bakfile} ]];then
bakfile=${HOME}/backup/ssr-conf.tar.gz
fi
if [[ -e ${bakfile} ]];then
cd ${HOME}/backup
tar -zxvf ${bakfile} -C ./
if [[ -e ./check.log ]];then
mv ./check.log /usr/local/SSR-Bash-Python/check.log
fi
if [[ -e /usr/local/SSR-Bash-Python/timelimit.db ]];then
mv ./timelimit.db /usr/local/SSR-Bash-Python/timelimit.db
fi
if [[ ${OS} =~ ^Ubuntu$|^Debian$ ]];then
iptables-restore < /etc/iptables.up.rules
for port in `cat ./port.conf`; do iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport $port -j ACCEPT ; done
for port in `cat ./port.conf`; do iptables -I INPUT -m state --state NEW -m udp -p udp --dport $port -j ACCEPT ; done
iptables-save > /etc/iptables.up.rules
iptables -vnL
fi
if [[ ${OS} == CentOS ]];then
if [[ $CentOS_RHEL_version == 7 ]];then
iptables-restore < /etc/iptables.up.rules
for port in `cat ./port.conf`; do iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport $port -j ACCEPT ; done
for port in `cat ./port.conf`; do iptables -I INPUT -m state --state NEW -m udp -p udp --dport $port -j ACCEPT ; done
iptables-save > /etc/iptables.up.rules
iptables -vnL
else
for port in `cat ./port.conf`; do iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport $port -j ACCEPT ; done
for port in `cat ./port.conf`; do iptables -I INPUT -m state --state NEW -m udp -p udp --dport $port -j ACCEPT ; done
/etc/init.d/iptables save
/etc/init.d/iptables restart
iptables -vnL && sed -i '5a#tcp port rule' /etc/sysconfig/iptables
fi
fi
rm -f /usr/local/shadowsocksr/mudb.json
mv ./mudb.json /usr/local/shadowsocksr/mudb.json
rm -f ./port.conf
echo "还原操作已完成,开始检测是否已生效!"
bash /usr/local/SSR-Bash-Python/servercheck.sh test
if [[ -z ${SSRcheck} ]];then
echo "配置已生效,还原成功"
else
echo "配置未生效,还原失败,请联系作者解决"
fi
rm /tmp/BakFilename.tmp
else
echo "备份文件不存在,请检查!"
fi
else
echo "计算错误,正确结果为$solve"
fi
}
#Show
echo "输入数字选择功能:"
echo ""
echo "1.检查更新"
echo "2.切换到普通版"
echo "3.程序自检"
echo "4.卸载程序"
echo "5.备份配置"
echo "6.还原配置"
while :; do echo
read -p "请选择: " choice
if [[ ! $choice =~ ^[1-6]$ ]]; then
[ -z "$choice" ] && ssr && break
echo "输入错误! 请输入正确的数字!"
else
break
fi
done
if [[ $choice == 1 ]];then
updateme
fi
if [[ $choice == 2 ]];then
echo "切换到普通版之后你将无法使用一些功能"
sumdc
if [[ "$sv" == "$solve" ]];then
bash /usr/local/SSR-Bash-Python/install.sh
sleep 3s
clear
ssr || exit 0
else
echo "计算错误,正确结果为$solve"
bash /usr/local/SSR-Bash-Python/self.sh
fi
fi
if [[ $choice == 3 ]];then
bash /usr/local/SSR-Bash-Python/self-check.sh
fi
if [[ $choice == 4 ]];then
echo "你在做什么?你真的这么狠心吗?"
sumdc
if [[ "$sv" == "$solve" ]];then
bash /usr/local/SSR-Bash-Python/install.sh uninstall
exit 0
else
echo "计算错误,正确结果为$solve"
bash /usr/local/SSR-Bash-Python/self.sh
fi
fi
if [[ $choice == 5 ]];then
if [[ ! -e ${HOME}/backup/ssr-conf.tar.gz ]];then
backup
else
cd ${HOME}/backup
mv ./ssr-conf.tar.gz ./ssr-conf-`date +%Y-%m-%d_%H:%M:%S`.tar.gz
backup
fi
bash /usr/local/SSR-Bash-Python/self.sh
fi
if [[ $choice == 6 ]];then
recover
bash /usr/local/SSR-Bash-Python/self.sh
fi
exit 0