From ff8781711f1646ba43e63c3406a6c4f116c52fd0 Mon Sep 17 00:00:00 2001 From: wcwq98 <85041970+wcwq98@users.noreply.github.com> Date: Sun, 8 Dec 2024 15:35:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BF=AB=E6=8D=B7=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E5=92=8C=E6=A3=80=E6=9F=A5=E8=84=9A=E6=9C=AC=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加快捷指令和检查脚本更新 --- realm.sh | 232 ++++++++++--------------------------------------------- 1 file changed, 41 insertions(+), 191 deletions(-) diff --git a/realm.sh b/realm.sh index 82b40be..7f5da6c 100644 --- a/realm.sh +++ b/realm.sh @@ -5,7 +5,39 @@ red="\033[0;31m" green="\033[0;32m" plain="\033[0m" -# 更新realm状态 +# 脚本版本 +sh_ver="1.1" + +# 配置文件路径 +CONFIG_PATH="/root/realm/config.toml" + +# 初始化环境目录 +init_env() { + mkdir -p /root/realm +} + +# 更新脚本 +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) + [[ -z ${sh_new_ver} ]] && echo -e "${red}检测最新版本失败!${plain}" && return + if [[ ${sh_new_ver} != ${sh_ver} ]]; then + echo -e "发现新版本 [ ${sh_new_ver} ],是否更新?[Y/n]" + read -p "(默认: y):" yn + [[ -z "${yn}" ]] && yn="y" + if [[ ${yn} == [Yy] ]]; then + wget -N --no-check-certificate https://raw.githubusercontent.com/wcwq98/realm/main/realm.sh && chmod +x realm.sh + echo -e "脚本已更新为最新版本 [ ${sh_new_ver} ]!" + exit 0 + else + echo -e "已取消更新。" + fi + else + echo -e "当前已是最新版本 [ ${sh_new_ver} ]!" + fi +} + +# 初始化realm状态 update_realm_status() { if [ -f "/root/realm/realm" ]; then realm_status="已安装" @@ -39,6 +71,7 @@ show_menu() { echo "7. 重启服务" echo "8. 检测更新" echo "9. 一键卸载" + echo "10. 更新脚本" echo "0. 退出脚本" echo "=================" echo -e "realm 状态:${realm_status_color}${realm_status}${plain}" @@ -48,13 +81,13 @@ show_menu() { # 部署环境的函数 deploy_realm() { - mkdir -p /root/realm + init_env 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" + echo "获取版本号失败,请检查网络连接。" return 1 else echo "当前最新版本为: ${_version}" @@ -67,21 +100,6 @@ 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 @@ -92,8 +110,8 @@ deploy_realm() { tar -xvf "realm-${_version}.tar.gz" chmod +x realm - # 创建 config.toml 模板 - cat < /root/realm/config.toml + # 创建默认配置文件 + cat < $CONFIG_PATH [network] no_tcp = false #是否关闭tcp转发 @@ -113,16 +131,15 @@ EOF echo "[Unit] Description=realm After=network-online.target -Wants=network-online.target systemd-networkd-wait-online.service +Wants=network-online.target [Service] Type=simple User=root Restart=on-failure RestartSec=5s -DynamicUser=true WorkingDirectory=/root/realm -ExecStart=/root/realm/realm -c /root/realm/config.toml +ExecStart=/root/realm/realm -c $CONFIG_PATH [Install] WantedBy=multi-user.target" > /etc/systemd/system/realm.service @@ -132,174 +149,6 @@ 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 - systemctl daemon-reload - systemctl restart realm.service - systemctl enable realm.service - echo "realm服务已启动并设置为开机自启。" - update_realm_status - - # 检查服务状态 - if ! systemctl is-active --quiet realm; then - echo "请检查是否存在config.toml或config.toml配置是否正确" - fi -} - -# 停止服务 -stop_service() { - systemctl stop realm - echo "realm服务已停止。" - update_realm_status -} - -# 重启服务 -restart_service() { - systemctl restart realm - echo "realm服务已重启。" - update_realm_status - - # 检查服务状态 - if ! systemctl is-active --quiet realm; then - echo "请检查是否存在config.toml或config.toml配置是否正确" - 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 @@ -317,6 +166,7 @@ while true; do 7) restart_service ;; 8) update_realm ;; 9) uninstall_realm ;; + 10) Update_Shell ;; 0) echo "退出脚本。"; exit 0 ;; *) echo "无效选项: $choice" ;; esac