-
Notifications
You must be signed in to change notification settings - Fork 24
/
RealmOneKey.sh
189 lines (169 loc) · 4.75 KB
/
RealmOneKey.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
#!/bin/bash
# 检查realm是否已安装
if [ -f "/root/realm/realm" ]; then
echo "检测到realm已安装。"
realm_status="已安装"
realm_status_color="\033[0;32m" # 绿色
else
echo "realm未安装。"
realm_status="未安装"
realm_status_color="\033[0;31m" # 红色
fi
# 检查realm服务状态
check_realm_service_status() {
if systemctl is-active --quiet realm; then
echo -e "\033[0;32m启用\033[0m" # 绿色
else
echo -e "\033[0;31m未启用\033[0m" # 红色
fi
}
# 显示菜单的函数
show_menu() {
clear
echo "欢迎使用realm一键转发脚本"
echo "================="
echo "1. 部署环境"
echo "2. 添加转发"
echo "3. 删除转发"
echo "4. 启动服务"
echo "5. 停止服务"
echo "6. 一键卸载"
echo "================="
echo -e "realm 状态:${realm_status_color}${realm_status}\033[0m"
echo -n "realm 转发状态:"
check_realm_service_status
}
# 部署环境的函数
deploy_realm() {
mkdir -p /root/realm
cd /root/realm
wget -O realm.tar.gz https://github.com/zhboner/realm/releases/download/v2.6.0/realm-x86_64-unknown-linux-gnu.tar.gz
tar -xvf realm.tar.gz
chmod +x realm
# 创建服务文件
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
# 更新realm状态变量
realm_status="已安装"
realm_status_color="\033[0;32m" # 绿色
echo "部署完成。"
}
# 卸载realm
uninstall_realm() {
systemctl stop realm
systemctl disable realm
rm -f /etc/systemd/system/realm.service
systemctl daemon-reload
rm -rf /root/realm
echo "realm已被卸载。"
# 更新realm状态变量
realm_status="未安装"
realm_status_color="\033[0;31m" # 红色
}
# 删除转发规则的函数
delete_forward() {
echo "当前转发规则:"
local IFS=$'\n' # 设置IFS仅以换行符作为分隔符
local lines=($(grep -n 'remote =' /root/realm/config.toml)) # 搜索所有包含转发规则的行
if [ ${#lines[@]} -eq 0 ]; then
echo "没有发现任何转发规则。"
return
fi
local index=1
for line in "${lines[@]}"; do
echo "${index}. $(echo $line | cut -d '"' -f 2)" # 提取并显示端口信息
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 chosen_line=${lines[$((choice-1))]} # 根据用户选择获取相应行
local line_number=$(echo $chosen_line | cut -d ':' -f 1) # 获取行号
# 计算要删除的范围,从listen开始到remote结束
local start_line=$line_number
local end_line=$(($line_number + 2))
# 使用sed删除选中的转发规则
sed -i "${start_line},${end_line}d" /root/realm/config.toml
echo "转发规则已删除。"
}
# 添加转发规则
add_forward() {
while true; do
read -p "请输入IP: " ip
read -p "请输入端口: " port
# 追加到config.toml文件
echo "[[endpoints]]
listen = \"0.0.0.0:$port\"
remote = \"$ip:$port\"" >> /root/realm/config.toml
read -p "是否继续添加(Y/N)? " answer
if [[ $answer != "Y" && $answer != "y" ]]; then
break
fi
done
}
# 启动服务
start_service() {
sudo systemctl unmask realm.service
sudo systemctl daemon-reload
sudo systemctl restart realm.service
sudo systemctl enable realm.service
echo "realm服务已启动并设置为开机自启。"
}
# 停止服务
stop_service() {
systemctl stop realm
echo "realm服务已停止。"
}
# 主循环
while true; do
show_menu
read -p "请选择一个选项: " choice
case $choice in
1)
deploy_realm
;;
2)
add_forward
;;
3)
delete_forward
;;
4)
start_service
;;
5)
stop_service
;;
6)
uninstall_realm
;;
*)
echo "无效选项: $choice"
;;
esac
read -p "按任意键继续..." key
done