diff --git a/install.sh b/install.sh index bdf1787..3e44432 100755 --- a/install.sh +++ b/install.sh @@ -17,7 +17,7 @@ _valid_config "$TEMP_CONFIG" || { _download_config "$url" "$TEMP_CONFIG" || _error_quit "下载失败: 请自行粘贴配置内容到 ${TEMP_CONFIG} 后再执行安装脚本" _valid_config "$TEMP_CONFIG" || { _failcat "配置无效:尝试进行本地订阅转换..." - _convert_config "$TEMP_CONFIG" + _download_convert_config "$TEMP_CONFIG" _valid_config "$TEMP_CONFIG" || _error_quit '配置无效:请检查配置内容' } } @@ -29,8 +29,7 @@ echo "$url" >"$CLASH_CONFIG_URL" tar -xf "$ZIP_UI" -C "$CLASH_BASE_DIR" tar -xf $ZIP_YQ -C "${TEMP_TOOL_DIR}" && install -m +x ${TEMP_TOOL_DIR}/yq_* "$TOOL_YQ" -_mark_raw -_concat_config_restart >&/dev/null +_merge_config_restart cat </etc/systemd/system/clash.service [Unit] @@ -40,7 +39,7 @@ After=network-online.target [Service] Type=simple Restart=always -ExecStart=${TOOL_CLASH} -d ${CLASH_BASE_DIR} -f ${CLASH_CONFIG_RUNTIME} -ext-ui public -secret '' +ExecStart=${TOOL_CLASH} -d ${CLASH_BASE_DIR} -f ${CLASH_CONFIG_RUNTIME} [Install] WantedBy=multi-user.target diff --git a/resource/mixin.yaml b/resource/mixin.yaml index e265ecd..53fd275 100644 --- a/resource/mixin.yaml +++ b/resource/mixin.yaml @@ -1,7 +1,13 @@ # mixin-config-start -# 开箱即用 clashui + +# clashui 配置 external-controller: '0.0.0.0:9090' -# clashtun 相关配置 +external-ui: public + +# clashsecret 配置 +secret: '' + +# clashtun 配置 tun: enable: false stack: system # or gvisor diff --git a/resource/zip/mihomo-linux-amd64-compatible-v1.19.2.gz b/resource/zip/mihomo-linux-amd64-compatible-v1.19.2.gz new file mode 100644 index 0000000..4094733 Binary files /dev/null and b/resource/zip/mihomo-linux-amd64-compatible-v1.19.2.gz differ diff --git a/script/clashctl.sh b/script/clashctl.sh index 7097adf..a7c76ff 100644 --- a/script/clashctl.sh +++ b/script/clashctl.sh @@ -6,7 +6,7 @@ function clashon() { sudo systemctl start clash && _okcat '已开启代理环境' || _failcat '启动失败: 执行 "systemctl status clash" 查看日志' || return 1 _get_port - local proxy_addr=http://127.0.0.1:${MIXED_PORT} + local proxy_addr=http://127.0.0.1:${PROXY_PORT} export http_proxy=$proxy_addr export https_proxy=$proxy_addr export HTTP_PROXY=$proxy_addr @@ -34,37 +34,41 @@ function clashui() { # cip.cc _get_port local public_ip=$(curl -s --noproxy "*" ifconfig.me) - local public_address="http://${public_ip}:${EXT_PORT}/ui" + local public_address="http://${public_ip}:${UI_PORT}/ui" # 内网ip # ip route get 1.1.1.1 | grep -oP 'src \K\S+' local local_ip=$(hostname -I | awk '{print $1}') - local local_address="http://${local_ip}:${EXT_PORT}/ui" + local local_address="http://${local_ip}:${UI_PORT}/ui" printf "\n" printf "╔═══════════════════════════════════════════════╗\n" printf "║ 😼 Web 面板地址 ║\n" printf "║═══════════════════════════════════════════════║\n" printf "║ ║\n" - printf "║ 🔓 请注意放行 %s 端口 ║\n" "$EXT_PORT" + printf "║ 🔓 请注意放行 %s 端口 ║\n" "$UI_PORT" printf "║ 🏠 内网:%-30s ║\n" "$local_address" printf "║ 🌍 公网:%-30s ║\n" "$public_address" - printf "║ ☁️ 公共:https://clash.razord.top ║\n" + printf "║ ☁️ 公共:%-30s ║\n" "$URL_CLASH_UI" printf "║ ║\n" printf "╚═══════════════════════════════════════════════╝\n" printf "\n" clashon >&/dev/null } +_merge_config_restart() { + _valid_config "$CLASH_CONFIG_MIXIN" || _error_quit "Mixin 配置验证失败,请检查" + sudo "$TOOL_YQ" -n "load(\"$CLASH_CONFIG_RAW\") * load(\"$CLASH_CONFIG_MIXIN\")" | sudo tee "$CLASH_CONFIG_RUNTIME" >&/dev/null && clashrestart +} + function clashsecret() { case "$#" in 0) - _okcat "当前密钥:$(sed -nE 's/.*secret\s(.*)/\1/p' /etc/systemd/system/clash.service)" + _okcat "当前密钥:$(sudo "$TOOL_YQ" '.secret' "$CLASH_CONFIG_RUNTIME")" ;; 1) local secret=$1 - [ -z "$secret" ] && secret=\'\' - sudo sed -iE s/"secret\s.*"/"secret $secret"/ /etc/systemd/system/clash.service - sudo systemctl daemon-reload - clashrestart + [ -z "$secret" ] && secret=\"\" + sudo "$TOOL_YQ" -i ".secret = $secret" "$CLASH_CONFIG_MIXIN" + _merge_config_restart _okcat "密钥更新成功,已重启生效" ;; *) @@ -73,26 +77,21 @@ function clashsecret() { esac } -_concat_config_restart() { - _valid_config "$CLASH_CONFIG_MIXIN" || _error_quit "Mixin 配置验证失败,请检查" - sudo "$TOOL_YQ" -n "load(\"$CLASH_CONFIG_RAW\") * load(\"$CLASH_CONFIG_MIXIN\")" | sudo tee "$CLASH_CONFIG_RUNTIME" >&/dev/null && clashrestart -} - _tunstatus() { - local status=$($TOOL_YQ '.tun.enable' "${CLASH_CONFIG_RUNTIME}") + local status=$(sudo "$TOOL_YQ" '.tun.enable' "${CLASH_CONFIG_RUNTIME}") [ "$status" = 'true' ] && _okcat 'Tun 状态:启用' || _failcat 'Tun 状态:关闭' } _tunoff() { _tunstatus >/dev/null || return 0 sudo "$TOOL_YQ" -i '.tun.enable = false' "$CLASH_CONFIG_MIXIN" - _concat_config_restart >/dev/null && _okcat "Tun 模式已关闭" + _merge_config_restart && _okcat "Tun 模式已关闭" } _tunon() { _tunstatus 2>/dev/null && return 0 sudo "$TOOL_YQ" -i '.tun.enable = true' "$CLASH_CONFIG_MIXIN" - _concat_config_restart >/dev/null + _merge_config_restart systemctl status clash | grep -qs 'unsupported kernel version' && { _tunoff >&/dev/null _error_quit '当前系统内核版本不支持' @@ -147,14 +146,13 @@ function clashupdate() { _download_config "$url" "$CLASH_CONFIG_RAW" # 校验并更新配置 - _valid_config "$CLASH_CONFIG_RAW" || _convert_config "$CLASH_CONFIG_RAW" + _valid_config "$CLASH_CONFIG_RAW" || _download_convert_config "$CLASH_CONFIG_RAW" _valid_config "$CLASH_CONFIG_RAW" || { echo "$(date +"%Y-%m-%d %H:%M:%S") 配置更新失败 ❌ $url" | sudo tee -a "${CLASH_UPDATE_LOG}" >&/dev/null sudo cat "$CLASH_CONFIG_RAW_BAK" | sudo tee "$CLASH_CONFIG_RAW" >&/dev/null _error_quit '下载失败或配置无效:已回滚配置' } - _mark_raw - _concat_config_restart && _okcat '配置更新成功,已重启生效' + _merge_config_restart && _okcat '配置更新成功,已重启生效' echo "$url" | sudo tee "$CLASH_CONFIG_URL" >&/dev/null echo "$(date +"%Y-%m-%d %H:%M:%S") 配置更新成功 ✅ $url" | sudo tee -a "${CLASH_UPDATE_LOG}" >&/dev/null } @@ -163,7 +161,7 @@ function clashmixin() { case "$1" in -e) sudo vim "$CLASH_CONFIG_MIXIN" && { - _concat_config_restart && _okcat "配置更新成功,已重启生效" + _merge_config_restart && _okcat "配置更新成功,已重启生效" } ;; -r) diff --git a/script/common.sh b/script/common.sh index 1753977..dbd2059 100644 --- a/script/common.sh +++ b/script/common.sh @@ -3,6 +3,7 @@ # shellcheck disable=SC2155 GH_PROXY='https://gh-proxy.com/' URL_YQ="https://github.com/mikefarah/yq/releases/tag/v4.45.1" +URL_CLASH_UI="http://board.zash.run.place" TEMP_RESOURCE='./resource' TEMP_TOOL_DIR="${TEMP_RESOURCE}/tool" @@ -10,6 +11,7 @@ TEMP_CONFIG="${TEMP_RESOURCE}/config.yaml" ZIP_BASE_DIR="${TEMP_RESOURCE}/zip" ZIP_CLASH="${ZIP_BASE_DIR}/clash*.gz" +# ZIP_CLASH="${ZIP_BASE_DIR}/mihomo*.gz" ZIP_YQ="${ZIP_BASE_DIR}/yq*.tar.gz" ZIP_CONVERT="${ZIP_BASE_DIR}/subconverter*.tar.gz" ZIP_UI="${ZIP_BASE_DIR}/yacd.tar.xz" @@ -40,24 +42,16 @@ function _get_os() { local cpu_arch=$(uname -m) # shellcheck disable=SC2086 - { /bin/ls $ZIP_CLASH | grep clash; } >&/dev/null || _download_clash "$cpu_arch" + { /bin/ls $ZIP_CLASH | grep -E 'clash|mihomo'; } >&/dev/null || _download_clash "$cpu_arch" } -_get_value() { - sed -En "s/$1:\s(.*)/\1/p" $CLASH_CONFIG_RUNTIME -} function _get_port() { - local ext_ctl=$(_get_value 'external-controller') - EXT_PORT=${ext_ctl##*:} - EXT_PORT=${EXT_PORT//\'/} - MIXED_PORT=$(_get_value 'mixed-port') + local port=$(sudo $TOOL_YQ '.port' $CLASH_CONFIG_RUNTIME) + local mixed_port=$(sudo $TOOL_YQ '.mixed-port' $CLASH_CONFIG_RUNTIME) + local external_port=$(sudo $TOOL_YQ '.external-controller' $CLASH_CONFIG_RUNTIME | cut -d':' -f2) - [ -z "$MIXED_PORT" ] && MIXED_PORT=7890 - [ -z "$EXT_PORT" ] && EXT_PORT=9090 -} - -function _mark_raw() { - sudo sed -i -e '1i\# raw-config-start' -e '$a\# raw-config-end\n' "${CLASH_CONFIG_RAW}" + PROXY_PORT="${mixed_port:-${port:-7890}}" + UI_PORT=${external_port:-9090} } function _okcat() { @@ -126,8 +120,10 @@ function _valid_config() { local bin_path="${TOOL_CLASH}" [ ! -e "$bin_path" ] && bin_path="${TEMP_TOOL_DIR}/clash" - [ -e "$1" ] && [ "$(wc -l <"$1")" -gt 1 ] && - "${bin_path}" -d "$(dirname "$1")" -f "$1" -t + [ -e "$1" ] && [ "$(wc -l <"$1")" -gt 1 ] && { + local test="$bin_path -d $(dirname "$1") -f $1 -t" + eval "$test >&/dev/null" || eval "$test" + } } function _download_config() { @@ -148,7 +144,7 @@ function _download_config() { "$url" } -function _convert_url() { +_convert_url() { local raw_url="$1" local base_url="http://127.0.0.1:25500/sub?target=clash&url=" @@ -187,7 +183,7 @@ _stop_convert() { pkill -9 -f subconverter >&/dev/null } -function _convert_config() { +function _download_convert_config() { _start_convert _download_config "$(_convert_url "$url")" "$1" _stop_convert