fix: extract variable and reformat
This commit is contained in:
1 parent
3e92d5e4b6
commit
be70751f53
4 files changed
+70
-65
No files matched your search
+9
-7
@@ -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 <<EOF >/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
|
||||
+19
-24
@@ -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 <<EOF | column -t -s ':'
|
||||
Usage:
|
||||
clashon 开启代理:
|
||||
clashoff 关闭代理:
|
||||
clashui 面板地址:
|
||||
clashtun [on|off] Tun模式:
|
||||
clashsecret [secret] 查看/设置密钥:
|
||||
clashmixin [-e|-r] Mixin配置:
|
||||
clashupdate [auto|log] 更新订阅:
|
||||
clashon 开启代理:
|
||||
clashoff 关闭代理:
|
||||
clashui 面板地址:
|
||||
clashtun [on|off] Tun模式:
|
||||
clashsecret [secret] 查看/设置密钥:
|
||||
clashmixin [-e|-r] Mixin配置:
|
||||
clashupdate [auto|log] 更新订阅:
|
||||
EOF
|
||||
}
|
||||
+39
-31
@@ -2,12 +2,13 @@
|
||||
# shellcheck disable=SC2034
|
||||
# shellcheck disable=SC2155
|
||||
GH_PROXY='https://gh-proxy.com/'
|
||||
YQ_URL="${GH_PROXY}github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64"
|
||||
YQ_URL="https://github.com/mikefarah/yq/releases/tag/v4.45.14"
|
||||
|
||||
TEMP_CONFIG='./resource/config.yaml'
|
||||
TEMP_CLASH_RAR='./resource/clash-*.gz'
|
||||
TEMP_UI_RAR='./resource/yacd.tar.xz'
|
||||
TEMP_YQ_RAR='./resource/yq*.gz'
|
||||
TEMP_RESOURCE='./resource/'
|
||||
TEMP_CONFIG="${TEMP_RESOURCE}config.yaml"
|
||||
TEMP_CLASH_RAR="${TEMP_RESOURCE}clash*.gz"
|
||||
TEMP_UI_RAR="${TEMP_RESOURCE}yacd.tar.xz"
|
||||
TEMP_YQ_RAR="${TEMP_RESOURCE}yq*.gz"
|
||||
|
||||
CLASH_BASE_DIR='/opt/clash'
|
||||
CLASH_CONFIG_URL="${CLASH_BASE_DIR}/url"
|
||||
@@ -16,7 +17,7 @@ CLASH_CONFIG_RAW_BAK="${CLASH_CONFIG_RAW}.bak"
|
||||
CLASH_CONFIG_MIXIN="${CLASH_BASE_DIR}/mixin.yaml"
|
||||
CLASH_CONFIG_RUNTIME="${CLASH_BASE_DIR}/runtime.yaml"
|
||||
CLASH_UPDATE_LOG="${CLASH_BASE_DIR}/clashupdate.log"
|
||||
YQ_BIN='/usr/bin'
|
||||
YQ_BIN='/usr/bin/yq'
|
||||
|
||||
function _get_os() {
|
||||
local os_info=$(cat /etc/os-release)
|
||||
@@ -32,7 +33,7 @@ function _get_os() {
|
||||
_get_os
|
||||
|
||||
function _get_value() {
|
||||
sed -En "s/$1:\s(.*)/\1/p" $CLASH_CONFIG_RUNTIME
|
||||
sed -En "s/$1:\s(.*)/\1/p" $CLASH_CONFIG_RUNTIME
|
||||
}
|
||||
function _get_port() {
|
||||
local ext_ctl=$(_get_value 'external-controller')
|
||||
@@ -68,31 +69,37 @@ function _error_quit() {
|
||||
function _download_clash() {
|
||||
local url sha256sum
|
||||
case "$1" in
|
||||
*86*)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-386-2023.08.17.gz
|
||||
sha256sum='254125efa731ade3c1bf7cfd83ae09a824e1361592ccd7c0cccd2a266dcb92b5'
|
||||
x86_64)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-amd64-2023.08.17.gz
|
||||
sha256sum='92380f053f083e3794c1681583be013a57b160292d1d9e1056e7fa1c2d948747'
|
||||
;;
|
||||
armv*)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-armv5-2023.08.17.gz
|
||||
sha256sum='622f5e774847782b6d54066f0716114a088f143f9bdd37edf3394ae8253062e8'
|
||||
*86*)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-386-2023.08.17.gz
|
||||
sha256sum='254125efa731ade3c1bf7cfd83ae09a824e1361592ccd7c0cccd2a266dcb92b5'
|
||||
;;
|
||||
aarch64)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-arm64-2023.08.17.gz
|
||||
sha256sum='c45b39bb241e270ae5f4498e2af75cecc0f03c9db3c0db5e55c8c4919f01afdd'
|
||||
armv*)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-armv5-2023.08.17.gz
|
||||
sha256sum='622f5e774847782b6d54066f0716114a088f143f9bdd37edf3394ae8253062e8'
|
||||
;;
|
||||
aarch64)
|
||||
url=https://downloads.clash.wiki/ClashPremium/clash-linux-arm64-2023.08.17.gz
|
||||
sha256sum='c45b39bb241e270ae5f4498e2af75cecc0f03c9db3c0db5e55c8c4919f01afdd'
|
||||
;;
|
||||
*)
|
||||
# shellcheck disable=SC2086
|
||||
/bin/rm -rf $TEMP_CLASH_RAR
|
||||
_error_quit "未知的架构版本:$1,请自行下载对应版本至 resource 目录下:https://downloads.clash.wiki/ClashPremium/"
|
||||
;;
|
||||
*)
|
||||
/bin/rm -rf "$TEMP_CLASH_RAR"
|
||||
_error_quit "未知的架构版本:$1,请自行下载对应版本至 resource 目录下:https://downloads.clash.wiki/ClashPremium/"
|
||||
;;
|
||||
esac
|
||||
_failcat "当前CPU架构为:$1,正在下载对应版本..."
|
||||
wget --timeout=30 \
|
||||
--tries=1 \
|
||||
--no-check-certificate \
|
||||
-O "$TEMP_CLASH_RAR" \
|
||||
"$url"
|
||||
echo "$sha256sum $TEMP_CLASH_RAR" | sha256sum -c || {
|
||||
/bin/rm -rf "$TEMP_CLASH_RAR"
|
||||
--tries=1 \
|
||||
--no-check-certificate \
|
||||
-P "$TEMP_RESOURCE" \
|
||||
"$url"
|
||||
# shellcheck disable=SC2086
|
||||
echo $sha256sum $TEMP_CLASH_RAR | sha256sum -c || {
|
||||
/bin/rm -rf $TEMP_CLASH_RAR
|
||||
_error_quit '下载失败:请自行下载对应版本至 resource 目录下:https://downloads.clash.wiki/ClashPremium/'
|
||||
}
|
||||
|
||||
@@ -104,14 +111,15 @@ function _valid_env() {
|
||||
[ "$(ps -p 1 -o comm=)" != "systemd" ] && _error_quit "系统不具备 systemd"
|
||||
|
||||
local cpu_arch=$(uname -m)
|
||||
[ "$cpu_arch" = 'x86_64' ] || _download_clash "$cpu_arch"
|
||||
# shellcheck disable=SC2086
|
||||
{ /bin/ls $TEMP_CLASH_RAR | grep clash; } >&/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 \
|
||||
|
||||
+3
-3
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user