-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrustdesktool.sh
More file actions
executable file
·401 lines (346 loc) · 9.36 KB
/
rustdesktool.sh
File metadata and controls
executable file
·401 lines (346 loc) · 9.36 KB
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
#!/bin/bash
export LANG=en_US.UTF-8
#安装目录
installdirectory='/usr/local/rustdesk-server'
#官方版本号
rustdeskserverversion='1.1.15'
# 配置文件下载代理主机列表(github加速)
proxyhost=(
"https://gh.ddlc.top"
"https://gh-proxy.com"
"https://edgeone.gh-proxy.com"
"https://cdn.gh-proxy.com"
"https://hk.gh-proxy.com"
)
#下载超时时间
timeout=3
installType=''
removeType=''
upgrade=''
release='linux'
#字体颜色定义
_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)"
}
# 加载动画
loading() {
local pids=("$@")
local delay=0.1
local spinstr='|/-\'
tput civis # 隐藏光标
while :; do
local all_done=true
for pid in "${pids[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
all_done=false
local temp=${spinstr#?}
printf "\r\033[0;31;36m[ %c ] loading ...\033[0m" "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
fi
done
[[ $all_done == true ]] && break
done
tput cnorm # 恢复光标
printf "\r\033[K" # 清除行
}
# 通用下载函数:从镜像列表中依次尝试下载文件
download_file() {
local url="$1"
local path="$2"
local output=""
local dest=""
if [[ -n "$path" ]]; then
dest="$path"
else
dest="."
fi
for base in "${proxyhost[@]}"; do
output="${dest}/$(basename "$url")"
(
wget -q --timeout="$timeout" "${base}/$url" -O "$output" > /dev/null 2>&1
) &
local pid=$!
loading $pid
wait $pid
if [[ -s "$output" ]]; then
return 0
fi
done
return 1
}
#菜单渲染
menu() {
printf "\033[H\033[2J"
_green '# RustDesk-Server x86 一键安装脚本'
_green '# Rep <https://github.com/sshpc/rustdesktool>'
_blue '# You Server:'${release}
_blue "服务状态: [$(check_rustdesk_status)]"
echo
_blue ">~~~~~~~~~~~~~~ rustdesk-server tool ~~~~~~~~~~~~< v: $rustdeskserverversion"
echo
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
printf '\033[0;31;31m%b\033[0m' "q: 退出 "
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]}
waitinput
main
elif [[ $number == 'q' ]]; then
echo
exit
else
echo
_red '输入有误 回车返回首页'
waitinput
main
fi
}
#检查系统
checkSystem() {
if grep -qi "centos\|red hat" /etc/os-release; then
release="centos"
installType='yum -y install'
removeType='yum -y remove'
upgrade="yum update -y --skip-broken"
elif grep -qi "ubuntu" /etc/os-release; then
release="ubuntu"
installType='apt -y install'
removeType='apt -y autoremove'
upgrade="apt update"
elif grep -qi "debian" /etc/os-release; then
release="debian"
installType='apt -y install'
removeType='apt -y autoremove'
upgrade="apt update"
elif grep -qi "alpine" /etc/os-release; then
release="alpine"
installType='apk add'
upgrade="apk update"
removeType='apk del'
else
_red "不支持此系统"
_red "$(cat /etc/issue)"
_red "$(cat /proc/version)"
exit 1
fi
}
#脚本升级
updateself() {
_blue '下载最新版脚本'
if ! download_file "https://raw.githubusercontent.com/sshpc/rustdesktool/main/rustdesktool.sh"; then
_red "rustdesktool.sh 下载失败!"
return 1
fi
chmod +x ./rustdesktool.sh
_green "脚本更新成功"
exec bash ./rustdesktool.sh
}
check_rustdesk_status() {
local hbbs_service="/usr/lib/systemd/system/RustDeskHbbs.service"
local hbbr_service="/usr/lib/systemd/system/RustDeskHbbr.service"
# 判断是否安装
if [[ ! -f "$hbbs_service" || ! -f "$hbbr_service" ]]; then
echo "未安装"
return
fi
# 判断是否运行
if systemctl is-active --quiet RustDeskHbbs && systemctl is-active --quiet RustDeskHbbr; then
echo "运行中"
else
echo "未运行"
fi
}
#查看状态
viewstatus() {
echo
_blue 'RustDeskHbbs status:'
systemctl status RustDeskHbbs | awk '/Active/'
_blue 'RustDeskHbbr status:'
systemctl status RustDeskHbbr | awk '/Active/'
_blue 'net status:'
netstat -tuln | grep -E ":(21115|21116|21117|21118|21119)\b"
}
startservice() {
_blue "启动服务"
(
systemctl start RustDeskHbbs > /dev/null 2>&1
systemctl start RustDeskHbbr > /dev/null 2>&1
) &
local pid=$!
loading $pid
wait $pid
_green "服务已启动"
}
stopservice() {
_blue "停止服务"
(
systemctl stop RustDeskHbbs > /dev/null 2>&1
systemctl stop RustDeskHbbr > /dev/null 2>&1
) &
local pid=$!
loading $pid
wait $pid
_yellow "服务已停止"
}
viewkey() {
echo
_blue '公钥:'
cat $installdirectory/id_ed25519.pub
echo
echo
}
#卸载
uninstall() {
read -rp "确认卸载?(y/N): " c
[[ $c == y ]] || return
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 "开始安装"
mkdir -p $installdirectory
# 检查并安装 wget
if ! command -v wget >/dev/null 2>&1; then
_yellow "未检测到 wget,正在安装..."
${installType} wget || { _red "wget 安装失败"; exit 1; }
fi
# 检查并安装 unzip
if ! command -v unzip >/dev/null 2>&1; then
_yellow "未检测到 unzip,正在安装..."
${installType} unzip || { _red "unzip 安装失败"; exit 1; }
fi
_blue "下载安装文件"
if ! download_file "https://github.com/rustdesk/rustdesk-server/releases/download/$rustdeskserverversion/rustdesk-server-linux-amd64.zip" "$installdirectory"; then
_red "rustdesk-server-linux-amd64.zip下载失败!"
exit 1
fi
_blue "解压文件"
(
unzip $installdirectory/rustdesk-server-linux-amd64.zip -d $installdirectory > /dev/null 2>&1
) &
local pid=$!
loading $pid
wait $pid
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
ExecStop=/bin/kill -TERM \$MAINPID
Restart=on-failure
RestartSec=3
[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
ExecStop=/bin/kill -TERM \$MAINPID
Restart=on-failure
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
_blue "配置开机自启"
systemctl enable RustDeskHbbs
systemctl enable RustDeskHbbr
echo
#启动服务
startservice
#printf "\033[H\033[2J"
echo
_green '安装完成'
viewkey
local ip="$(wget -q -T10 -O- ipinfo.io/ip)"
_green '公网 IP:'
echo $ip
echo
_yellow "请手动放行防火墙 TCP & UDP端口 21115-21119"
echo
}
#主函数
main() {
options=("安装" install "卸载" uninstall "查看状态" viewstatus "查看key" viewkey "启动服务" startservice "停止服务" stopservice "升级脚本" updateself)
menu "${options[@]}"
}
checkSystem
main