diff --git a/install.sh b/install.sh index c6e610a..5a0170c 100755 --- a/install.sh +++ b/install.sh @@ -8,23 +8,25 @@ _valid_env [ -d "$CLASH_BASE_DIR" ] && _error_quit "已安装,如需重新安装请先执行卸载脚本" -gzip -dc < $TEMP_CLASH_RAR > ./resource/clash && chmod +x ./resource/clash +# shellcheck disable=SC2086 +gzip -dc $TEMP_CLASH_RAR >"${TEMP_RESOURCE}clash" && chmod +x "${TEMP_RESOURCE}clash" _valid_config "$TEMP_CONFIG" && echo '✅ 配置可用' || { read -r -p '😼 输入订阅链接:' url _download_config "$url" "$TEMP_CONFIG" || _error_quit "下载失败: 请自行粘贴配置内容到 ${TEMP_CONFIG} 后再执行安装脚本" _valid_config "$TEMP_CONFIG" || _error_quit "配置无效:请检查配置内容" } mkdir -p "$CLASH_BASE_DIR" -echo "$url" > "$CLASH_CONFIG_URL" +echo "$url" >"$CLASH_CONFIG_URL" /bin/cp -rf script "$CLASH_BASE_DIR" /bin/ls resource | grep -Ev 'gz|png|xz' | xargs -I {} /bin/cp -rf "resource/{}" "$CLASH_BASE_DIR" tar -xf "$TEMP_UI_RAR" -C "$CLASH_BASE_DIR" -gzip -dc < $TEMP_YQ_RAR > "$YQ_BIN" && chmod +x "$YQ_BIN" +# shellcheck disable=SC2086 +gzip -dc $TEMP_YQ_RAR >"$YQ_BIN" && chmod +x "$YQ_BIN" _mark_raw -cat "${CLASH_CONFIG_RAW}" > "${CLASH_CONFIG_RUNTIME}" +cat "${CLASH_CONFIG_RAW}" >"${CLASH_CONFIG_RUNTIME}" -cat << EOF > /etc/systemd/system/clash.service +cat </etc/systemd/system/clash.service [Unit] Description=Clash 守护进程, Go 语言实现的基于规则的代理. After=network-online.target @@ -38,8 +40,8 @@ ExecStart=${CLASH_BASE_DIR}/clash -d ${CLASH_BASE_DIR} -f ${CLASH_CONFIG_RUNTIME WantedBy=multi-user.target EOF -echo "source $CLASH_BASE_DIR/script/common.sh && source $CLASH_BASE_DIR/script/clashctl.sh" >> "$BASHRC" +echo "source $CLASH_BASE_DIR/script/common.sh && source $CLASH_BASE_DIR/script/clashctl.sh" >>"$BASHRC" systemctl daemon-reload -systemctl enable clash > /dev/null 2>&1 && _okcat "已设置开机自启" || _failcat "设置自启失败" +systemctl enable clash >/dev/null 2>&1 && _okcat "已设置开机自启" || _failcat "设置自启失败" clashon && clashui clash diff --git a/script/clashctl.sh b/script/clashctl.sh index bd28344..55efd63 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -3,8 +3,8 @@ # shellcheck disable=SC2155 # clash快捷指令 function clashon() { - sudo systemctl start clash && _okcat '已开启代理环境' \ - || _failcat '启动失败: 执行 "systemctl status clash" 查看日志' || return 1 + sudo systemctl start clash && _okcat '已开启代理环境' || + _failcat '启动失败: 执行 "systemctl status clash" 查看日志' || return 1 _get_port local proxy_addr=http://127.0.0.1:${MIXED_PORT} export http_proxy=$proxy_addr @@ -14,8 +14,8 @@ function clashon() { } function clashoff() { - sudo systemctl stop clash && _okcat '已关闭代理环境' \ - || _failcat '关闭失败: 执行 "systemctl status clash" 查看日志' || return 1 + sudo systemctl stop clash && _okcat '已关闭代理环境' || + _failcat '关闭失败: 执行 "systemctl status clash" 查看日志' || return 1 unset http_proxy unset https_proxy unset HTTP_PROXY @@ -74,17 +74,12 @@ function clashsecret() { } _valid_yq() { - yq -V >&/dev/null && return 0 - read -r -p '依赖 yq 命令,是否安装?[y/N]: ' flag - [ "$flag" = "y" ] && { - sudo wget "${YQ_URL}" -O /usr/bin/yq && sudo chmod +x /usr/bin/yq - _okcat 'yq 安装成功' - } || _failcat '取消安装' + yq -V >&/dev/null || _error_quit "依赖 yq 命令,请下载对应版本至 PATH 路径内。$YQ_URL" } _concat_config_restart() { _valid_config "$CLASH_CONFIG_MIXIN" || _error_quit "Mixin 配置验证失败,请检查" - _valid_yq || return 1 + _valid_yq sudo yq -n "load(\"$CLASH_CONFIG_RAW\") * load(\"$CLASH_CONFIG_MIXIN\")" | sudo tee "$CLASH_CONFIG_RUNTIME" >&/dev/null && clashrestart } @@ -94,15 +89,15 @@ _tunstatus() { } _tunoff() { - _tunstatus > /dev/null || return 0 + _tunstatus >/dev/null || return 0 sudo yq -i '.tun.enable = false' "$CLASH_CONFIG_MIXIN" - _concat_config_restart > /dev/null && _okcat "Tun 模式已关闭" + _concat_config_restart >/dev/null && _okcat "Tun 模式已关闭" } _tunon() { - _tunstatus 2> /dev/null && return 0 + _tunstatus 2>/dev/null && return 0 sudo yq -i '.tun.enable = true' "$CLASH_CONFIG_MIXIN" - _concat_config_restart > /dev/null + _concat_config_restart >/dev/null systemctl status clash | grep -qs 'unsupported kernel version' && { _tunoff >&/dev/null _error_quit '当前系统内核版本不支持' @@ -111,7 +106,7 @@ _tunon() { } function clashtun() { - _valid_yq || return 1 + _valid_yq case "$1" in on) _tunon @@ -187,14 +182,14 @@ function clashmixin() { } function clash() { - cat << EOF | column -t -s ':' + cat <&/dev/null || _download_clash "$cpu_arch" } # 配置文件和clash在同一目录 function _valid_config() { - [ -e "$1" ] && [ "$(wc -l < "$1")" -gt 1 ] \ - && "$(dirname "$1")/clash" -d "$(dirname "$1")" -f "$1" -t + [ -e "$1" ] && [ "$(wc -l <"$1")" -gt 1 ] && + "$(dirname "$1")/clash" -d "$(dirname "$1")" -f "$1" -t } function _download_config() { @@ -123,8 +131,8 @@ function _download_config() { --user-agent "$agent" \ -k \ -o "$output" \ - "$url" \ - || sudo wget --timeout=5 \ + "$url" || + sudo wget --timeout=5 \ --tries=1 \ --user-agent="$agent" \ --no-check-certificate \ diff --git a/uninstall.sh b/uninstall.sh index 1e24cc0..ea008e0 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -3,13 +3,13 @@ . script/common.sh . script/clashctl.sh -_valid_env +# _valid_env [ ! -d "$CLASH_BASE_DIR" ] && echo "😾 未安装或已卸载,开始自动清理相关配置..." -clashoff > /dev/null 2>&1 +clashoff >/dev/null 2>&1 -systemctl disable clash > /dev/null 2>&1 +systemctl disable clash >/dev/null 2>&1 rm -f /etc/systemd/system/clash.service systemctl daemon-reload rm -rf "$YQ_BIN"