fix: declare function & magic value

This commit is contained in:
libm committed 2024-11-26 18:31:21 +08:00
1 parent 65d34fda27
commit fb65e28f2a
5 files changed
+132 -131

No files matched your search

+67
View File
@@ -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 '错误:下载失败或配置无效!'
}
}
+41
View File
@@ -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"
}