-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrustdesktool.sh
executable file
·336 lines (286 loc) · 8.6 KB
/
rustdesktool.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
#!/bin/bash
export LANG=en_US.UTF-8
#初始化
initself() {
version='1.2.1'
#官方版本号
rustdeskserverversion='1.1.10-3'
installType='yum -y install'
removeType='yum -y remove'
upgrade="yum -y update"
release='linux'
#菜单名称(默认首页)
menuname='首页'
#安装目录
installdirectory='/usr/local/rustdesk-sever'
#字体颜色定义
_red() {
printf '\033[0;31;31m%b\033[0m' "$1"
echo
}
_green() {
printf '\033[0;31;32m%b\033[0m' "$1"
echo
}
_yellow() {
printf '\033[0;31;33m%b\033[0m' "$1"
echo
}
_blue() {
printf '\033[0;31;36m%b\033[0m' "$1"
echo
}
#按任意键继续
waitinput() {
echo
read -n1 -r -p "按任意键继续...(退出 Ctrl+C)"
}
#菜单头部
menutop() {
clear
_green '# RustDesk-Server 安装脚本'
_green '# Github <https://github.com/sshpc/rustdesktool>'
_blue '# You Server:'${release}
echo
_blue ">~~~~~~~~~~~~~~ rustdesk-server tool ~~~~~~~~~~~~< v: $version"
echo
_yellow "当前菜单: $menuname "
echo
}
#菜单渲染
menu() {
menutop
options=("$@")
num_options=${#options[@]}
# 计算数组中的字符最大长度
max_len=0
for ((i = 0; i < num_options; i++)); do
# 获取当前字符串的长度
str_len=${#options[i]}
# 更新最大长度
if ((str_len > max_len)); then
max_len=$str_len
fi
done
# 渲染菜单
for ((i = 0; i < num_options; i += 4)); do
printf "%s%*s " "$((i / 2 + 1)): ${options[i]}" "$((max_len - ${#options[i]}))"
if [[ "${options[i + 2]}" != "" ]]; then printf "$((i / 2 + 2)): ${options[i + 2]}"; fi
echo
echo
done
echo
printf '\033[0;31;36m%b\033[0m' "q: 退出 "
if [[ "$number" != "" ]]; then printf '\033[0;31;36m%b\033[0m' "b: 返回 0: 首页"; fi
echo
echo
# 获取用户输入
read -ep "请输入命令号: " number
if [[ $number -ge 1 && $number -le $((num_options / 2)) ]]; then
#找到函数名索引
action_index=$((2 * (number - 1) + 1))
#函数名赋值
parentfun=${options[action_index]}
#函数执行
${options[action_index]}
elif [[ $number == 0 ]]; then
main
elif [[ $number == 'b' ]]; then
${FUNCNAME[3]}
elif [[ $number == 'q' ]]; then
echo
exit
else
echo
_red '输入有误 回车返回首页'
waitinput
main
fi
}
clear
}
#检查系统
checkSystem() {
if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
release="centos"
installType='yum -y install'
removeType='yum -y remove'
upgrade="yum update -y --skip-broken"
elif [[ -f "/etc/issue" ]] && grep </etc/issue -q -i "debian" || [[ -f "/proc/version" ]] && grep </etc/issue -q -i "debian" || [[ -f "/etc/os-release" ]] && grep </etc/os-release -q -i "ID=debian"; then
release="debian"
installType='apt -y install'
upgrade="apt update"
removeType='apt -y autoremove'
elif [[ -f "/etc/issue" ]] && grep </etc/issue -q -i "ubuntu" || [[ -f "/proc/version" ]] && grep </etc/issue -q -i "ubuntu"; then
release="ubuntu"
installType='apt -y install'
upgrade="apt update"
removeType='apt -y autoremove'
elif [[ -f "/etc/issue" ]] && grep </etc/issue -q -i "Alpine" || [[ -f "/proc/version" ]] && grep </proc/version -q -i "Alpine"; then
release="alpine"
installType='apk add'
upgrade="apk update"
removeType='apt del'
fi
if [[ -z ${release} ]]; then
echoContent red "\n不支持此系统\n"
_red "$(cat /etc/issue)"
_red "$(cat /proc/version)"
exit 0
fi
}
#脚本升级
updateself() {
_blue '下载github最新版'
wget -N http://raw.githubusercontent.com/sshpc/rustdesktool/main/rustdesktool.sh
# 检查上一条命令的退出状态码
if [ $? -eq 0 ]; then
chmod +x ./rustdesktool.sh && ./rustdesktool.sh
else
_red "下载失败,请重试"
fi
}
#查看状态
viewstatus() {
echo
_blue 'RustDeskHbbs status:'
systemctl status RustDeskHbbs | awk '/Active/'
echo
_blue 'RustDeskHbbr status:'
systemctl status RustDeskHbbr | awk '/Active/'
echo
_blue 'net status:'
echo
netstat -tuln | grep -E ":(21115|21116|21117|21118|21119)\b"
}
startservice() {
_blue "启动服务"
systemctl start RustDeskHbbs
systemctl start RustDeskHbbr
viewstatus
}
stopservice() {
_blue "停止服务"
systemctl stop RustDeskHbbs
systemctl stop RustDeskHbbr
viewstatus
}
viewkey() {
echo
_blue '公钥:'
cat $installdirectory/id_ed25519.pub
echo
echo
}
#卸载
uninstall() {
stopservice
systemctl disable RustDeskHbbs
systemctl disable RustDeskHbbr
rm -rf $installdirectory
rm -rf /usr/lib/systemd/system/RustDeskHbbs.service
rm -rf /usr/lib/systemd/system/RustDeskHbbr.service
_blue '已卸载'
echo
}
#安装
install() {
#检查是否已安装
if [ -f "/usr/lib/systemd/system/RustDeskHbbr.service" ]; then
_yellow '检测到文件存在 覆盖安装...'
uninstall
fi
_blue "开始安装"
echo
mkdir -p $installdirectory
${installType} wget
${installType} unzip
# 下载链接列表#兼容国内环境
links=(
"https://gh.ddlc.top/https://github.com/rustdesk/rustdesk-server/releases/download/$rustdeskserverversion/rustdesk-server-linux-amd64.zip"
"https://ghproxy.com/https://github.com/rustdesk/rustdesk-server/releases/download/$rustdeskserverversion/rustdesk-server-linux-amd64.zip"
"https://github.com/rustdesk/rustdesk-server/releases/download/$rustdeskserverversion/rustdesk-server-linux-amd64.zip"
)
# 设置超时时间(秒)
timeout=7
# 遍历链接列表
for link in "${links[@]}"; do
echo "正在尝试下载:$link"
# 使用 wget 下载文件,并设置超时时间
wget --timeout="$timeout" "$link" -P $installdirectory
# 检查 wget 的退出状态
if [ $? -eq 0 ]; then
echo "下载成功:$link"
break # 下载成功,跳出循环
else
_yellow "下载失败,继续尝试下一个镜像链接"
fi
done
if [ ! -f "$installdirectory/rustdesk-server-linux-amd64.zip" ]; then
_red "尝试全部链接下载失败,请检查"
fi
unzip $installdirectory/rustdesk-server-linux-amd64.zip -d $installdirectory
mv $installdirectory/amd64/* $installdirectory/
rm $installdirectory/rustdesk-server-linux-amd64.zip
rm -r $installdirectory/amd64
chmod +x $installdirectory/hbbr
chmod +x $installdirectory/hbbs
if [ ! -f "/usr/lib/systemd/system/RustDeskHbbr.service" ]; then
#文件不存在
touch /usr/lib/systemd/system/RustDeskHbbr.service
fi
cat <<EOF >/usr/lib/systemd/system/RustDeskHbbr.service
[Unit]
Description=RustDesk Hbbr
After=network.target
[Service]
User=root
Type=simple
WorkingDirectory=$installdirectory
ExecStart=$installdirectory/hbbr -k _
ExecStop=/bin/kill -TERM \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
if [ ! -f "/usr/lib/systemd/system/RustDeskHbbs.service" ]; then
#文件不存在
touch /usr/lib/systemd/system/RustDeskHbbs.service
fi
cat <<EOF >/usr/lib/systemd/system/RustDeskHbbs.service
[Unit]
Description=RustDesk Hbbs
After=network.target
[Service]
User=root
Type=simple
WorkingDirectory=$installdirectory
ExecStart=$installdirectory/hbbs -k _
ExecStop=/bin/kill -TERM \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
_blue "配置开机自启"
systemctl enable RustDeskHbbs
systemctl enable RustDeskHbbr
echo
#启动服务
startservice
clear
_green '安装成功'
viewkey
local ip="$(wget -q -T10 -O- ipinfo.io/ip)"
_green '公网 IP:'
echo $ip
echo
_yellow "请手动放行防火墙 TCP & UDP端口 21115-21119"
echo
}
#主函数
main() {
menuname='首页'
options=("安装" install "卸载" uninstall "查看状态" viewstatus "查看key" viewkey "启动服务" startservice "停止服务" stopservice "升级脚本" updateself)
menu "${options[@]}"
}
initself
checkSystem
main