From fb65e28f2a67abb9450ab2b532a4e7e976dbf09b Mon Sep 17 00:00:00 2001 From: libm Date: Tue, 26 Nov 2024 18:31:21 +0800 Subject: [PATCH] fix: declare function & magic value --- clashctl.sh | 82 ---------------------------------------------- install.sh | 50 ++++++++++------------------ script/clashctl.sh | 67 +++++++++++++++++++++++++++++++++++++ script/common.sh | 41 +++++++++++++++++++++++ uninstall.sh | 23 ++++--------- 5 files changed, 132 insertions(+), 131 deletions(-) delete mode 100644 clashctl.sh create mode 100644 script/clashctl.sh create mode 100644 script/common.sh diff --git a/clashctl.sh b/clashctl.sh deleted file mode 100644 index 623d4d7..0000000 --- a/clashctl.sh +++ /dev/null @@ -1,82 +0,0 @@ -#!/bin/bash -CLASH_PATH='/etc/clash' -CONFIG_PATH="$CLASH_PATH/config.yaml" -CONFIG_PATH_BAK="${CONFIG_PATH}.bak" -CRONTAB_PATH_1='/var/spool/cron/root' -CRONTAB_PATH_2='/var/spool/cron/crontabs/root' -[ -e $CRONTAB_PATH_1 ] && TARGET_PATH=$CRONTAB_PATH_1 -[ -e $CRONTAB_PATH_2 ] && TARGET_PATH=$CRONTAB_PATH_2 - -function is_valid() { - grep -qs 'port' "$1" -} -# 1url 2output -function download_config() { - agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0' - wget --timeout=3 --tries=1 --no-check-certificate --user-agent="$agent" -O "$2" "$1" - is_valid "$2" || - curl --connect-timeout 3 \ - --retry 1 \ - --user-agent "$agent" \ - -k -o "$2" "$1" -} - -function quit() { - echo "$0" | grep -qs install.sh && exit 1 -} -# clash快捷指令 -function clashon() { - addr=http://127.0.0.1:7890 - export http_proxy=$addr - export https_proxy=$addr - export HTTP_PROXY=$addr - export HTTPS_PROXY=$addr - systemctl start clash && echo 'clash: 已开启代理环境!' || echo 'clash: 启动失败: 执行 "systemctl status clash" 查看日志' -} - -function clashoff() { - unset http_proxy - unset https_proxy - unset HTTP_PROXY - unset HTTPS_PROXY - systemctl stop clash && echo 'clash: 已关闭代理环境!' || echo 'clash: 关闭失败: 执行 "systemctl status clash" 查看日志' -} - -function clashui() { - # 查询公网ip - # ifconfig.me - # cip.cc - ip=$(curl -s --noproxy "*" ifconfig.me) - cat <>"$TARGET_PATH" <<<"$URL" - echo "clash: 定时任务设置成功!" && return 0 - } - - cat "$CONFIG_PATH" >"$CONFIG_PATH_BAK" - download_config "$URL" "$CONFIG_PATH" - # shellcheck disable=SC2015 - is_valid "$CONFIG_PATH" && { - clashoff && clashon - echo 'clash: 配置更新成功,已重启生效!' - } || { - cat "$CONFIG_PATH_BAK" >"$CONFIG_PATH" - echo '错误:下载失败或配置无效!' - } -} diff --git a/install.sh b/install.sh index 36560c3..bee771c 100755 --- a/install.sh +++ b/install.sh @@ -1,42 +1,30 @@ #!/bin/bash -# shellcheck disable=SC1091 -source clashctl.sh -# shellcheck disable=SC2034 -FIRST_CONFIG_PATH='./resource/config.yaml' -CLASH_GZ_PATH='./resource/clash-linux-amd64-v3-2023.08.17.gz' -UI_PATH='./resource/yacd.tar.xz' +source ./script/clashctl.sh +TEMP_CONFIG_PATH='./resource/config.yaml' +TEMP_CLASH_PATH='./resource/clash-linux-amd64-v3-2023.08.17.gz' +TEMP_UI_PATH='./resource/yacd.tar.xz' -# begin -[ "$(whoami)" != root ] && { - echo "警告: 需要root权限!" && quit || return 1 -} - -[ -d /etc/clash ] && { - echo "clash: 已安装,如需重新安装请先执行卸载脚本" && quit || return 1 +_valid_root +[ -d "$CLASH_BASE_PATH" ] && { + echo "clash: 已安装,如需重新安装请先执行卸载脚本" && _quit } # shellcheck disable=SC2015 -is_valid "$FIRST_CONFIG_PATH" && echo '配置可用√' || { - read -r -p '输入订阅链接:' url - download_config "$url" "$FIRST_CONFIG_PATH" - is_valid "$FIRST_CONFIG_PATH" || (echo "错误:下载失败或配置无效: 自行粘贴配置内容到 ${CONFIG_PATH} 并重新运行" && quit || return 1) +_valid_config "$TEMP_CONFIG_PATH" && echo '✅ 配置可用' || { + read -r -p '输入订阅链接:' URL + _download_config "$URL" "$TEMP_CONFIG_PATH" + _valid_config "$TEMP_CONFIG_PATH" || (echo "❌ 错误:下载失败或配置无效: 请自行粘贴配置内容到 ${TEMP_CONFIG_PATH} 后再执行安装脚本" && _quit) } echo ------------------------- -gzip -dc "$CLASH_GZ_PATH" >./clash && chmod +x ./clash -/usr/bin/mv -f ./clash /usr/local/bin/clash - -# clash配置目录 -mkdir -p /etc/clash -tar -xf "$UI_PATH" -C $CLASH_PATH -/bin/cp -f "$FIRST_CONFIG_PATH" $CLASH_PATH -/bin/cp -f ./resource/Country.mmdb $CLASH_PATH -/bin/cp -f clashctl.sh $CLASH_PATH - -echo "source $CLASH_PATH/clashctl.sh" >>/etc/bashrc +mkdir -p "$CLASH_BASE_PATH" +gzip -dc "$TEMP_CLASH_PATH" >"$CLASH_BASE_PATH/clash" && chmod +x "$CLASH_BASE_PATH/clash" +tar -xf "$TEMP_UI_PATH" -C "$CLASH_BASE_PATH" +/bin/cp -f "$TEMP_CONFIG_PATH" ./resource/Country.mmdb ./script/* "$CLASH_BASE_PATH" +echo "source $CLASH_BASE_PATH/clashctl.sh" >>/etc/bashrc # 定时任务:更新配置 # Deprecated 改为手动配置 -# echo '0 0 */2 * * . /etc/bashrc;clashupdate url' >>/var/spool/cron/root +# echo '0 0 */2 * * . /etc/bashrc;clashupdate URL' >>/var/spool/cron/root # 服务配置文件 cat </etc/systemd/system/clash.service @@ -47,13 +35,11 @@ After=network-online.target [Service] Type=simple Restart=always -ExecStart=/usr/local/bin/clash -d /etc/clash -ext-ui public -ext-ctl 0.0.0.0:9090 +ExecStart=${CLASH_BASE_PATH}/clash -d ${CLASH_BASE_PATH} -ext-ui public -ext-ctl 0.0.0.0:9090 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload - systemctl enable clash >/dev/null 2>&1 && echo "clash: 设置自启成功!" || echo "clash: 设置自启失败!" - clashon && clashui diff --git a/script/clashctl.sh b/script/clashctl.sh new file mode 100644 index 0000000..589d1ec --- /dev/null +++ b/script/clashctl.sh @@ -0,0 +1,67 @@ +#!/bin/bash +source common.sh +# clash快捷指令 +function clashon() { + systemctl start clash && echo 'clash: 已开启代理环境!' \ + || echo 'clash: 启动失败: 执行 "systemctl status clash" 查看日志' || return 1 + addr=http://127.0.0.1:7890 + export http_proxy=$addr + export https_proxy=$addr + export HTTP_PROXY=$addr + export HTTPS_PROXY=$addr +} + +function clashoff() { + systemctl stop clash && echo 'clash: 已关闭代理环境!' \ + || echo 'clash: 关闭失败: 执行 "systemctl status clash" 查看日志' || return 1 + unset http_proxy + unset https_proxy + unset HTTP_PROXY + unset HTTPS_PROXY +} + +function clashui() { + # 查询公网ip + # ifconfig.me + # cip.cc + PUBLIC_IP="http://$(curl -s --noproxy "*" ifconfig.me):9090/ui" + LOCAL_IP="http://$(ifconfig eth0 | awk 'NR==2{print $2}'):9090/ui" + printf "\n" + printf "╔═══════════════════════════════════════════════╗\n" + printf "║ 😼 Clash Web 面板地址 ║\n" + printf "║═══════════════════════════════════════════════║\n" + printf "║ ║\n" + printf "║ 🔓 请注意放行 9090 端口 ║\n" + printf "║ 🏠 内网:%-30s ║\n" "$LOCAL_IP" + printf "║ 🌍 公网:%-30s ║\n" "$PUBLIC_IP" + printf "║ ☁️ 公共:https://clash.razord.top ║\n" + printf "║ ║\n" + printf "╚═══════════════════════════════════════════════╝\n" + printf "\n" +} + +function clashupdate() { + IS_AUTO=false + URL="" + for ARG in "$@"; do + [ "$ARG" = "--auto" ] && IS_AUTO=true + [ "${ARG:0:4}" = 'http' ] && URL=$ARG + done + + [ "$URL" = "" ] && echo '错误:请正确填写订阅链接!' && return 1 + [ "$IS_AUTO" = true ] && { + grep -qs 'clashupdate' "$CRONTAB_TARGET_PATH" || xARGs -I {} echo '0 0 */2 * * . /etc/bashrc;clashupdate {}' >>"$CRONTAB_TARGET_PATH" <<<"$URL" + echo "clash: 定时任务设置成功!" && return 0 + } + + cat "$CLASH_CONFIG_PATH" >"$CLASH_CONFIG_BAK_PATH" + _download_config "$URL" "$CLASH_CONFIG_PATH" + # shellcheck disable=SC2015 + _valid_config "$CLASH_CONFIG_PATH" && { + clashoff && clashon + echo 'clash: 配置更新成功,已重启生效!' + } || { + cat "$CLASH_CONFIG_BAK_PATH" >"$CLASH_CONFIG_PATH" + echo '错误:下载失败或配置无效!' + } +} diff --git a/script/common.sh b/script/common.sh new file mode 100644 index 0000000..e0ecb2c --- /dev/null +++ b/script/common.sh @@ -0,0 +1,41 @@ +# shellcheck disable=SC2034 +CLASH_BASE_PATH='/etc/clash' +CLASH_CONFIG_PATH="${CLASH_BASE_PATH}/config.yaml" +CLASH_CONFIG_BAK_PATH="${CLASH_CONFIG_PATH}.bak" +CLASH_CRONTAB_CENTOS_PATH='/var/spool/cron/root' +CLASH_CRONTAB_UBUNTU_PATH='/var/spool/cron/crontabs/root' +[ -e $CLASH_CRONTAB_CENTOS_PATH ] && CLASH_CRONTAB_TARGET_PATH=$CLASH_CRONTAB_CENTOS_PATH +[ -e $CLASH_CRONTAB_UBUNTU_PATH ] && CLASH_CRONTAB_TARGET_PATH=$CLASH_CRONTAB_UBUNTU_PATH + +# bash执行 $0为脚本执行路径 +# source执行 $0为bash +function _quit() { + echo "$0" | grep -qs 'bash' && exec bash || exit 1 +} + +function _valid_root() { + [ "$(whoami)" != root ] && { + echo "警告: 需要root或sudo权限执行!" && _quit + } +} + +function _valid_config() { + grep -qs 'port' "$1" +} + +# 1url 2output +function _download_config() { + AGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0' + wget --timeout=3 \ + --tries=1 \ + --no-check-certificate \ + --user-agent="$AGENT" \ + -O "$2" \ + "$1" + _valid_config "$2" || \ + curl --connect-timeout 3 \ + --retry 1 \ + --user-agent "$AGENT" \ + -k -o "$2" \ + "$1" +} diff --git a/uninstall.sh b/uninstall.sh index 5f07ded..dd8e741 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,32 +1,21 @@ #!/bin/bash -source clashctl.sh +source ./script/clashctl.sh -[ "$(whoami)" != root ] && { - echo "警告: 需要root权限运行!" && quit || return 1 -} +_valid_root -[ ! -d /etc/clash ] && { +[ ! -d "$CLASH_BASE_PATH" ] && { echo "clash: has already been uninstalled" - read -r -p "按 Enter 键退出,按其它键重新清除代理环境:" answer - [ "$answer" == "" ] && { - echo "已退出" - [ "$0" == ./uninstall.sh ] && exit 1 || return 1 - } || echo "清除中..." + read -r -p "按 Enter 键退出,按其它键重新清除代理环境:" ANSWER + [ "$ANSWER" = "" ] && _quit || echo "清除中..." } clashoff -unset clashon -unset clashoff -unset clashui - # 重载daemon -systemctl stop clash >/dev/null 2>&1 systemctl disable clash >/dev/null 2>&1 rm -f /etc/systemd/system/clash.service systemctl daemon-reload -rm -rf "$CLASH_PATH" -rm -f /usr/local/bin/clash +rm -rf "$CLASH_BASE_PATH" sed -i '/clashctl.sh/d' /etc/bashrc sed -i '/clashupdate/d' "$TARGET_PATH" echo 'clash: 已卸载,相关配置已清除!'