-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrealm.sh
585 lines (497 loc) · 15.6 KB
/
realm.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
#!/bin/bash
# 定义颜色变量
red="\033[0;31m"
green="\033[0;32m"
plain="\033[0m"
# 脚本版本
sh_ver="2.1"
# 初始化环境目录
init_env() {
mkdir -p /root/realm
mkdir -p /root/.realm
}
# 配置文件路径
CONFIG_PATH="/root/.realm/config.toml"
# 处理命令行参数
while getopts "l:r:" opt; do
case $opt in
l)
listen_ip_port="$OPTARG"
;;
r)
remote_ip_port="$OPTARG"
;;
*)
echo "Usage: $0 [-l listen_ip:port] [-r remote_ip:port]"
exit 1
;;
esac
done
# 如果提供了 -l 和 -r 参数,追加配置到 config.toml
if [ -n "$listen_ip_port" ] && [ -n "$remote_ip_port" ]; then
echo "配置中转机 IP 和端口为: $listen_ip_port"
echo "配置落地机 IP 和端口为: $remote_ip_port"
cat <<EOF >> "$CONFIG_PATH"
[[endpoints]]
listen = "$listen_ip_port"
remote = "$remote_ip_port"
EOF
echo "配置已追加,listen = $listen_ip_port,remote = $remote_ip_port"
exit 0
fi
# 更新realm状态
update_realm_status() {
if [ -f "/root/realm/realm" ]; then
realm_status="已安装"
realm_status_color=$green
else
realm_status="未安装"
realm_status_color=$red
fi
}
# 检查realm服务状态
check_realm_service_status() {
if systemctl is-active --quiet realm; then
realm_service_status="启用"
realm_service_status_color=$green
else
realm_service_status="未启用"
realm_service_status_color=$red
fi
}
# 更新面板状态
update_panel_status() {
if [ -f "/root/realm/web/realm_web" ]; then
panel_status="已安装"
panel_status_color=$green
else
panel_status="未安装"
panel_status_color=$red
fi
}
# 检查面板服务状态
check_panel_service_status() {
if systemctl is-active --quiet realm-panel; then
panel_service_status="启用"
panel_service_status_color=$green
else
panel_service_status="未启用"
panel_service_status_color=$red
fi
}
# 更新脚本
Update_Shell() {
echo -e "当前脚本版本为 [ ${sh_ver} ],开始检测最新版本..."
sh_new_ver=$(wget --no-check-certificate -qO- "https://raw.githubusercontent.com/wcwq98/realm/main/realm.sh" | grep 'sh_ver="' | awk -F "=" '{print $NF}' | sed 's/\"//g' | head -1)
if [[ -z ${sh_new_ver} ]]; then
echo -e "${red}检测最新版本失败!请检查网络或稍后再试。${plain}"
return 1
fi
if [[ ${sh_new_ver} == ${sh_ver} ]]; then
echo -e "当前已是最新版本 [ ${sh_new_ver} ]!"
return 0
fi
echo -e "发现新版本 [ ${sh_new_ver} ],是否更新?[Y/n]"
read -p "(默认: y): " yn
yn=${yn:-y}
if [[ ${yn} =~ ^[Yy]$ ]]; then
wget -N --no-check-certificate https://raw.githubusercontent.com/wcwq98/realm/main/realm.sh -O realm.sh
if [[ $? -ne 0 ]]; then
echo -e "${red}下载脚本失败,请检查网络连接!${plain}"
return 1
fi
chmod +x realm.sh
echo -e "脚本已更新为最新版本 [ ${sh_new_ver} ]!"
exec bash realm.sh
else
echo -e "已取消更新。"
fi
}
# 检查依赖
check_dependencies() {
echo "正在检查当前环境依赖"
local dependencies=("wget" "tar" "systemctl" "sed" "grep" "curl" "unzip")
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &> /dev/null; then
echo "正在安装 $dep..."
if [ -x "$(command -v apt-get)" ]; then
apt-get update && apt-get install -y "$dep"
elif [ -x "$(command -v yum)" ]; then
yum install -y "$dep"
else
echo "无法安装 $dep。请手动安装后重试。"
exit 1
fi
fi
done
echo "所有依赖已满足。"
}
# 显示菜单的函数
show_menu() {
clear
update_realm_status
check_realm_service_status
update_panel_status
check_panel_service_status
echo "欢迎使用realm一键转发脚本"
echo "================="
echo "1. 部署环境"
echo "2. 添加转发"
echo "3. 添加端口段转发"
echo "4. 删除转发"
echo "5. 启动服务"
echo "6. 停止服务"
echo "7. 重启服务"
echo "8. 检测更新"
echo "9. 一键卸载"
echo "10. 更新脚本"
echo "11. 面板管理"
echo "0. 退出脚本"
echo "================="
echo -e "realm 状态:${realm_status_color}${realm_status}${plain}"
echo -e "realm 转发状态:${realm_service_status_color}${realm_service_status}${plain}"
echo -e "面板状态:${panel_status_color}${panel_status}${plain}"
echo -e "面板服务状态:${panel_service_status_color}${panel_service_status}${plain}"
}
# 部署环境的函数
deploy_realm() {
mkdir -p /root/realm
cd /root/realm
_version=$(curl -s https://api.github.com/repos/zhboner/realm/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$_version" ]; then
echo "获取版本号失败,请检查本机能否链接 https://api.github.com/repos/zhboner/realm/releases/latest"
return 1
else
echo "当前最新版本为: ${_version}"
fi
arch=$(uname -m)
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$arch-$os" in
x86_64-linux)
download_url="https://github.com/zhboner/realm/releases/download/${_version}/realm-x86_64-unknown-linux-gnu.tar.gz"
;;
x86_64-darwin)
download_url="https://github.com/zhboner/realm/releases/download/${_version}/realm-x86_64-apple-darwin.tar.gz"
;;
aarch64-linux)
download_url="https://github.com/zhboner/realm/releases/download/${_version}/realm-aarch64-unknown-linux-gnu.tar.gz"
;;
aarch64-darwin)
download_url="https://github.com/zhboner/realm/releases/download/${_version}/realm-aarch64-apple-darwin.tar.gz"
;;
arm-linux)
download_url="https://github.com/zhboner/realm/releases/download/${_version}/realm-arm-unknown-linux-gnueabi.tar.gz"
;;
armv7-linux)
download_url="https://github.com/zhboner/realm/releases/download/${_version}/realm-armv7-unknown-linux-gnueabi.tar.gz"
;;
*)
echo "不支持的架构: $arch-$os"
return
;;
esac
wget -O "/root/realm/realm-${_version}.tar.gz" "$download_url"
tar -xvf "/root/realm/realm-${_version}.tar.gz" -C /root/realm/
chmod +x /root/realm/realm
# 创建 config.toml 模板
mkdir -p /root/.realm
cat <<EOF > "$CONFIG_PATH"
[network]
no_tcp = false #是否关闭tcp转发
use_udp = true #是否开启udp转发
#参考模板
# [[endpoints]]
# listen = "0.0.0.0:本地端口"
# remote = "落地鸡ip:目标端口"
[[endpoints]]
listen = "0.0.0.0:1234"
remote = "0.0.0.0:5678"
EOF
echo "[Unit]
Description=realm
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service
[Service]
Type=simple
User=root
Restart=on-failure
RestartSec=5s
DynamicUser=true
WorkingDirectory=/root/realm
ExecStart=/root/realm/realm -c /root/.realm/config.toml
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/realm.service
systemctl daemon-reload
update_realm_status
echo "部署完成。"
}
# 卸载realm
uninstall_realm() {
systemctl stop realm
systemctl disable realm
rm -f /etc/systemd/system/realm.service
systemctl daemon-reload
rm -f /root/realm/realm
echo "realm已被卸载。"
read -e -p "是否删除配置文件 (Y/N, 默认N): " delete_config
delete_config=${delete_config:-N}
if [[ $delete_config == "Y" || $delete_config == "y" ]]; then
rm -rf /root/realm
rm -rf /root/.realm
echo "配置文件已删除。"
else
echo "配置文件保留。"
fi
update_realm_status
}
# 删除转发规则的函数
delete_forward() {
echo "当前转发规则:"
local lines=($(grep -n 'remote =' /root/.realm/config.toml | grep -v '#' | awk -F: '{print $1}'))
if [ ${#lines[@]} -eq 0 ]; then
echo "没有发现任何转发规则。"
return
fi
local index=1
for line_num in "${lines[@]}"; do
listen_line=$((line_num - 1))
listen_port=$(sed -n "${listen_line}p" /root/.realm/config.toml | cut -d '"' -f 2)
remote_port=$(sed -n "${line_num}p" /root/.realm/config.toml | cut -d '"' -f 2)
echo "${index}. 本地监听: ${listen_port} --> 远程目标: ${remote_port}"
let index+=1
done
echo "请输入要删除的转发规则序号,直接按回车返回主菜单。"
read -p "选择: " choice
if [ -z "$choice" ]; then
echo "返回主菜单。"
return
fi
if ! [[ $choice =~ ^[0-9]+$ ]]; then
echo "无效输入,请输入数字。"
return
fi
if [ $choice -lt 1 ] || [ $choice -gt ${#lines[@]} ]; then
echo "选择超出范围,请输入有效序号。"
return
fi
local line_number=${lines[$((choice-1))]}
# 找到 [[endpoints]] 的起始行
local start_line=$line_number
while [ $start_line -ge 1 ]; do
local line_content=$(sed -n "${start_line}p" /root/.realm/config.toml)
if [[ $line_content =~ $$\[endpoints$$\] ]]; then
break
fi
((start_line--))
done
# 删除从 start_line 开始的 3 行
sed -i "${start_line},$(($start_line+3))d" /root/.realm/config.toml
echo "转发规则已删除。"
}
# 添加转发规则
add_forward() {
while true; do
read -e -p "请输入落地鸡的IP: " ip
read -e -p "请输入本地中转鸡的端口(port1): " port1
read -e -p "请输入落地鸡端口(port2): " port2
echo "
[[endpoints]]
listen = \"0.0.0.0:$port1\"
remote = \"$ip:$port2\"" >> /root/.realm/config.toml
read -e -p "是否继续添加转发规则(Y/N)? " answer
if [[ $answer != "Y" && $answer != "y" ]]; then
break
fi
done
}
# 添加端口段转发
add_port_range_forward() {
read -e -p "请输入落地鸡的IP: " ip
read -e -p "请输入本地中转鸡的起始端口: " start_port
read -e -p "请输入本地中转鸡的截止端口: " end_port
read -e -p "请输入落地鸡端口: " remote_port
for ((port=$start_port; port<=$end_port; port++)); do
echo "
[[endpoints]]
listen = \"0.0.0.0:$port\"
remote = \"$ip:$remote_port\"" >> /root/.realm/config.toml
done
echo "端口段转发规则已添加。"
}
# 启动服务
start_service() {
systemctl unmask realm.service
systemctl daemon-reload
systemctl restart realm.service
systemctl enable realm.service
echo "realm服务已启动并设置为开机自启。"
check_realm_service_status
}
# 停止服务
stop_service() {
systemctl stop realm.service
systemctl disable realm.service
echo "realm服务已停止并已禁用开机自启。"
check_realm_service_status
}
# 重启服务
restart_service() {
systemctl daemon-reload
systemctl restart realm.service
echo "realm服务已重启。"
check_realm_service_status
}
# 更新realm
update_realm() {
echo "> 检测并更新 realm"
current_version=$(/root/realm/realm --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+')
tag_version=$(curl -Ls "https://api.github.com/repos/zhboner/realm/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [[ -z "$tag_version" ]]; then
echo -e "${red}获取 realm 版本失败,可能是由于 GitHub API 限制,请稍后再试${plain}"
exit 1
fi
if [[ "$current_version" == "$tag_version" ]]; then
echo "当前已经是最新版本: ${current_version}"
return
fi
echo -e "获取到 realm 最新版本: ${tag_version},开始安装..."
arch=$(uname -m)
wget -N --no-check-certificate -O /root/realm/realm.tar.gz "https://github.com/zhboner/realm/releases/download/${tag_version}/realm-${arch}-unknown-linux-gnu.tar.gz"
if [[ $? -ne 0 ]]; then
echo -e "${red}下载 realm 失败,请确保您的服务器可以访问 GitHub${plain}"
exit 1
fi
cd /root/realm
tar -xvf realm.tar.gz
chmod +x realm
echo -e "realm 更新成功。"
update_realm_status
}
# 面板管理函数
panel_management() {
clear
echo "==========================="
echo "Realm 面板管理"
echo "==========================="
echo "1. 启动面板"
echo "2. 暂停面板"
echo "3. 安装面板"
echo "4. 卸载面板"
echo "5. 修改面板配置"
echo "0. 返回主菜单"
echo "==========================="
read -p "请选择操作 [0-5]: " panel_choice
case $panel_choice in
1) start_panel ;;
2) stop_panel ;;
3) install_panel ;;
4) uninstall_panel ;;
5) modify_panel_config ;;
0) return ;;
*) echo "无效的选择" ;;
esac
}
install_panel() {
echo "开始安装 Realm 面板..."
# 检测系统架构
arch=$(uname -m)
case "$arch" in
x86_64)
panel_file="realm-panel-linux-amd64.zip"
;;
aarch64|arm64)
panel_file="realm-panel-linux-arm64.zip"
;;
*)
echo "不支持的系统架构: $arch"
return 1
;;
esac
cd /root/realm
# 从 GitHub 下载面板文件
echo "正在从 GitHub 下载面板文件..."
echo "检测到系统架构: $arch,将下载: $panel_file"
# 下载面板文件
download_url="https://github.com/wcwq98/realm/releases/download/v2.1/${panel_file}"
if ! wget -O "${panel_file}" "$download_url"; then
echo "下载失败,请检查网络连接或稍后再试。"
return 1
fi
mkdir web
# 解压并设置权限
unzip "${panel_file}" -d /root/realm/web
cd web
# 设置权限
chmod +x realm_web
# 创建服务文件
echo "[Unit]
Description=Realm Web Panel
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/realm/web
ExecStart=/root/realm/web/realm_web
Restart=on-failure
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/realm-panel.service
# 重新加载 systemd 并启动服务
systemctl daemon-reload
systemctl enable realm-panel
systemctl start realm-panel
update_panel_status
echo "Realm 面板安装完成。"
}
# 启动面板
start_panel() {
systemctl start realm-panel
echo "面板服务已启动。"
check_panel_service_status
}
# 停止面板
stop_panel() {
systemctl stop realm-panel
echo "面板服务已停止。"
check_panel_service_status
}
# 卸载面板
uninstall_panel() {
systemctl stop realm-panel
systemctl disable realm-panel
rm -f /etc/systemd/system/realm-panel.service
systemctl daemon-reload
rm -f /root/realm/realm_web
echo "面板已被卸载。"
update_panel_status
}
# 修改面板配置
modify_panel_config() {
echo "修改面板配置..."
# 在此添加修改配置的具体逻辑
echo "配置已修改。"
}
# 主程序
main() {
check_dependencies
init_env
while true; do
show_menu
read -p "请输入选项 [0-11]: " choice
case $choice in
1) deploy_realm ;;
2) add_forward ;;
3) add_port_range_forward ;;
4) delete_forward ;;
5) start_service ;;
6) stop_service ;;
7) restart_service ;;
8) update_realm ;;
9) uninstall_realm ;;
10) Update_Shell ;;
11) panel_management ;;
0) exit 0 ;;
*) echo "无效的选项,请重新输入。" ;;
esac
done
}
main