-
Notifications
You must be signed in to change notification settings - Fork 210
/
pserver.sh
266 lines (252 loc) · 7.59 KB
/
pserver.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
#!/usr/bin/env bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================
# System Required: CentOS/Debian/Ubuntu
# Description: Peerflix Server
# Version: 1.0.3
# Author: Toyo
# Blog: https://doub.io/wlzy-13/
#=================================================
node_ver="v6.9.1"
node_file="/etc/node"
ps_file="/etc/node/lib/node_modules/peerflix-server"
#检查系统
check_sys(){
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -q -E -i "debian"; then
release="debian"
elif cat /etc/issue | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
elif cat /proc/version | grep -q -E -i "debian"; then
release="debian"
elif cat /proc/version | grep -q -E -i "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
release="centos"
fi
bit=`uname -m`
}
deliptables(){
port_total=`netstat -lntp | grep node | awk '{print $4}' | awk -F ":" '{print $4}' | wc -l`
for((integer = 1; integer <= ${port_total}; integer++))
do
port=`netstat -lntp | grep node | awk '{print $4}' | awk -F ":" '{print $4}' | sed -n "${integer}p"`
if [ ${port} != "" ]; then
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport ${port} -j ACCEPT
iptables -D INPUT -m state --state NEW -m udp -p udp --dport ${port} -j ACCEPT
fi
done
iptables -D OUTPUT -m state --state NEW -m tcp -p tcp --dport 6881 -j ACCEPT
iptables -D OUTPUT -m state --state NEW -m udp -p udp --dport 6881 -j ACCEPT
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 6881 -j ACCEPT
iptables -D INPUT -m state --state NEW -m udp -p udp --dport 6881 -j ACCEPT
}
# 安装PS
installps(){
# 判断是否安装PS
if [ -e ${ps_file} ];
then
echo -e "\033[41;37m [错误] \033[0m 检测到 Peerflix Server 已安装,如需继续,请先卸载 !"
exit 1
fi
check_sys
# 系统判断
if [ ${release} == "centos" ]; then
yum update
yum install -y build-essential curl vim xz tar
elif [ ${release} == "debian" ]; then
apt-get update
apt-get install -y build-essential curl vim xz tar
elif [ ${release} == "ubuntu" ]; then
sudo apt-get update
sudo apt-get install -y build-essential curl vim xz tar
else
echo -e "\033[41;37m [错误] \033[0m 本脚本不支持当前系统 !"
exit 1
fi
#修改DNS为8.8.8.8
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf
if [ ${bit} == "x86_64" ]; then
wget -N -O node.tar.xz "https://nodejs.org/dist/v6.9.1/node-v6.9.1-linux-x64.tar.xz"
xz -d node.tar.xz
tar -xvf node.tar -C "/etc"
mv /etc/node-v6.9.1-linux-x64 ${node_file}
rm -rf node.tar
ln -s ${node_file}/bin/node /usr/local/bin/node
ln -s ${node_file}/bin/npm /usr/local/bin/npm
elif [ ${bit} == "i386" ]; then
wget -N -O node.tar.xz "https://nodejs.org/dist/v6.9.1/node-v6.9.1-linux-x86.tar.xz"
xz -d node.tar.xz
tar -xvf node.tar -C "/etc"
mv /etc/node-v6.9.1-linux-x86 ${node_file}
rm -rf node.tar
ln -s ${node_file}/bin/node /usr/local/bin/node
ln -s ${node_file}/bin/npm /usr/local/bin/npm
else
echo -e "\033[41;37m [错误] \033[0m 不支持 ${bit} !"
exit 1
fi
npm install -g peerflix-server
# 判断是否下载成功
if [ ! -e ${ps_file} ]; then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 安装失败 !"
exit 1
fi
startps
}
startps(){
# 检查是否安装
if [ ! -e ${ps_file} ]; then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 没有安装,请检查 !"
exit 1
fi
# 判断进程是否存在
PID=`ps -ef | grep peerflix-server | grep -v grep | awk '{print $2}'`
if [ ! -z $PID ]; then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 进程正在运行,请检查 !"
exit 1
fi
#设置端口
while true
do
echo -e "请输入 Peerflix Server 监听端口 [1-65535]"
stty erase '^H' && read -p "(默认端口: 9000):" PORT
[ -z "$PORT" ] && PORT="9000"
expr ${PORT} + 0 &>/dev/null
if [ $? -eq 0 ]; then
if [ ${PORT} -ge 1 ] && [ ${PORT} -le 65535 ]; then
echo
echo "——————————————————————————————"
echo -e " 端口 : \033[41;37m ${PORT} \033[0m"
echo "——————————————————————————————"
echo
break
else
echo "输入错误,请输入正确的数字 !"
fi
else
echo "输入错误,请输入正确的数字 !"
fi
done
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport ${PORT} -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport ${PORT} -j ACCEPT
iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 6881 -j ACCEPT
iptables -I INPUT -m state --state NEW -m udp -p udp --dport 6881 -j ACCEPT
iptables -I OUTPUT -m state --state NEW -m tcp -p tcp --dport 6881 -j ACCEPT
iptables -I OUTPUT -m state --state NEW -m udp -p udp --dport 6881 -j ACCEPT
PORT=${PORT} nohup node ${ps_file}>> ${ps_file}/peerflixs.log 2>&1 &
sleep 2s
# 判断进程是否存在
PID=`ps -ef | grep peerflix-server | grep -v grep | awk '{print $2}'`
if [ -z $PID ]; then
echo
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 启动失败 !"
exit 1
fi
# 获取IP
ip=`curl -m 10 -s http://members.3322.org/dyndns/getip`
if [ -z $ip ]; then
ip="ip"
fi
echo
echo "Peerflix Server 已启动 !"
echo -e "浏览器访问,地址: \033[41;37m http://${ip}:${PORT} \033[0m "
echo
}
stopps(){
# 判断进程是否存在
PID=`ps -ef | grep peerflix-server | grep -v grep | awk '{print $2}'`
if [ -z $PID ]; then
echo -e "\033[41;37m [错误] \033[0m 没有发现 Peerflix Server 进程运行,请检查 !"
exit 1
fi
deliptables
kill -9 ${PID}
sleep 2s
PID=`ps -ef | grep peerflix-server | grep -v grep | awk '{print $2}'`
if [ ! -z $PID ];
then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 停止失败 !"
exit 1
else
echo
echo "Peerflix Server 已停止 !"
echo
fi
}
# 查看日志
tailps(){
# 判断日志是否存在
if [ ! -e ${ps_file}/peerflixs.log ];
then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 日志文件不存在 !"
exit 1
else
tail -f ${ps_file}/peerflixs.log
fi
}
autops(){
if [ ! -e ${ps_file} ]; then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 没有安装,开始安装 !"
installps
else
PID=`ps -ef | grep peerflix-server | grep -v grep | awk '{print $2}'`
if [ -z $PID ];
then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 没有启动,开始启动 !"
startps
else
printf "Peerflix Server 正在运行,是否停止 ? (y/N)"
printf "\n"
stty erase '^H' && read -p "(默认: n):" autoyn
[ -z ${autoyn} ] && autoyn="n"
if [[ ${autoyn} == [Yy] ]]; then
stopps
fi
fi
fi
}
uninstallps(){
# 检查是否安装
if [ ! -e ${ps_file} ]; then
echo -e "\033[41;37m [错误] \033[0m Peerflix Server 没有安装,请检查 !"
exit 1
fi
printf "确定要卸载 Peerflix Server ? (y/N)"
printf "\n"
stty erase '^H' && read -p "(默认: n):" unyn
[ -z ${unyn} ] && unyn="n"
if [[ ${unyn} == [Yy] ]]; then
PID=`ps -ef | grep peerflix-server | grep -v grep | awk '{print $2}'`
if [ ! -z $PID ]; then
deliptables
kill -9 ${PID}
fi
rm -rf /usr/local/bin/node
rm -rf /usr/local/bin/npm
rm -rf ${node_file}
echo
echo "Peerflix Server 卸载完成 !"
echo
else
echo
echo "卸载已取消..."
echo
fi
}
action=$1
[ -z $1 ] && action=auto
case "$action" in
auto|install|start|stop|tail|uninstall)
${action}ps
;;
*)
echo "输入错误 !"
echo "用法: {install | start | stop | tail | uninstall}"
;;
esac