mirror of
https://github.com/wcwq98/realm.git
synced 2025-01-22 12:58:14 -05:00
添加快捷指令和检查脚本更新
This commit is contained in:
parent
13d63cf393
commit
4d34136779
229
realm.sh
229
realm.sh
@ -13,7 +13,60 @@ CONFIG_PATH="/root/.realm/config.toml"
|
||||
|
||||
# 初始化环境目录
|
||||
init_env() {
|
||||
mkdir -p /root/.realm
|
||||
mkdir -p /root/realm
|
||||
}
|
||||
|
||||
# 处理命令行参数
|
||||
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"
|
||||
|
||||
# 追加配置到 config.toml
|
||||
cat <<EOF >> /root/realm/config.toml
|
||||
|
||||
[[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
|
||||
echo -e "${green}启用${plain}"
|
||||
else
|
||||
echo -e "${red}未启用${plain}"
|
||||
fi
|
||||
}
|
||||
|
||||
# 更新脚本
|
||||
@ -81,13 +134,13 @@ show_menu() {
|
||||
|
||||
# 部署环境的函数
|
||||
deploy_realm() {
|
||||
init_env
|
||||
cd /root/.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 "获取版本号失败,请检查网络连接。"
|
||||
echo "获取版本号失败,请检查本机能否链接 https://api.github.com/repos/zhboner/realm/releases/latest"
|
||||
return 1
|
||||
else
|
||||
echo "当前最新版本为: ${_version}"
|
||||
@ -100,6 +153,21 @@ deploy_realm() {
|
||||
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
|
||||
@ -109,14 +177,19 @@ deploy_realm() {
|
||||
wget -O "realm-${_version}.tar.gz" "$download_url"
|
||||
tar -xvf "realm-${_version}.tar.gz"
|
||||
chmod +x realm
|
||||
|
||||
# 创建默认配置文件
|
||||
cat <<EOF > $CONFIG_PATH
|
||||
|
||||
# 创建 config.toml 模板
|
||||
cat <<EOF > /root/realm/config.toml
|
||||
|
||||
[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"
|
||||
@ -126,15 +199,16 @@ EOF
|
||||
echo "[Unit]
|
||||
Description=realm
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
Wants=network-online.target systemd-networkd-wait-online.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
Restart=on-failure
|
||||
RestartSec=5s
|
||||
WorkingDirectory=/root/.realm
|
||||
ExecStart=/root/.realm/realm -c $CONFIG_PATH
|
||||
DynamicUser=true
|
||||
WorkingDirectory=/root/realm
|
||||
ExecStart=/root/realm/realm -c /root/realm/config.toml
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target" > /etc/systemd/system/realm.service
|
||||
@ -144,6 +218,105 @@ WantedBy=multi-user.target" > /etc/systemd/system/realm.service
|
||||
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
|
||||
echo "配置文件已删除。"
|
||||
else
|
||||
echo "配置文件保留。"
|
||||
fi
|
||||
|
||||
update_realm_status
|
||||
}
|
||||
|
||||
# 删除转发规则的函数
|
||||
delete_forward() {
|
||||
echo "当前转发规则:"
|
||||
local IFS=$'\n'
|
||||
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)
|
||||
|
||||
local start_line=$line_number
|
||||
local end_line=$(($line_number + 2))
|
||||
|
||||
sed -i "${start_line},${end_line}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
|
||||
@ -158,7 +331,6 @@ start_service() {
|
||||
echo "请检查是否存在config.toml或config.toml配置是否正确"
|
||||
fi
|
||||
}
|
||||
|
||||
# 停止服务
|
||||
stop_service() {
|
||||
systemctl stop realm
|
||||
@ -178,6 +350,41 @@ restart_service() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 更新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
|
||||
}
|
||||
|
||||
# 初始化realm状态
|
||||
update_realm_status
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user