v1.0.0beta15.3

~优化脚本语句和结构
~完美适配支持wget!!!
~增加Gitee下载源
~增加脚本日志以及查看日志功能
~修复部分情况下开机自启显示错误的bug
This commit is contained in:
juewuy
2020-10-24 20:09:49 +08:00
parent c642ca4b8c
commit 4743965c58
3 changed files with 49 additions and 32 deletions

View File

@@ -278,15 +278,13 @@ clashstart(){
if [ ! -f $clashdir/clash ];then if [ ! -f $clashdir/clash ];then
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo -e "\033[31m没有找到核心文件请先下载clash核心\033[0m" echo -e "\033[31m没有找到核心文件请先下载clash核心\033[0m"
checkupdate source $clashdir/getdate.sh && checkupdate && getcore
source $clashdir/getdate.sh && getcore
fi fi
#检查GeoIP数据库 #检查GeoIP数据库
if [ ! -f $clashdir/Country.mmdb ];then if [ ! -f $clashdir/Country.mmdb ];then
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo -e "\033[31m没有找到GeoIP数据库文件请下载数据库文件\033[0m" echo -e "\033[31m没有找到GeoIP数据库文件请下载数据库文件\033[0m"
checkupdate source $clashdir/getdate.sh && checkupdate && getgeo
source $clashdir/getdate.sh && getgeo
fi fi
#检查yaml配置文件 #检查yaml配置文件
if [ ! -f "$yaml" ];then if [ ! -f "$yaml" ];then

View File

@@ -4,15 +4,17 @@
webget(){ webget(){
[ -n "$(pidof clash)" ] && export all_proxy="http://127.0.0.1:$mix_port" #设置临时http代理 [ -n "$(pidof clash)" ] && export all_proxy="http://127.0.0.1:$mix_port" #设置临时http代理
#参数【$1】代表下载目录【$2】代表在线地址 #参数【$1】代表下载目录【$2】代表在线地址
#参数【$3】代表输出显示【$4】不启用重定向
if curl --version > /dev/null 2>&1;then if curl --version > /dev/null 2>&1;then
[ -z "$3" ] && progress='-#' || progress='-s' [ "$3" = "echooff" ] && progress='-s' || progress='-#'
result=$(curl -w %{http_code} --connect-timeout 5 $progress -kLo $1 $2) [ -z "$4" ] && redirect='-L' || redirect=''
elif wget --version > /dev/null 2>&1;then result=$(curl -w %{http_code} --connect-timeout 5 $progress $redirect -ko $1 $2)
[ -z "$3" ] && progress='--show-progress' || progress=''
wget -Y on -q $progress --no-check-certificate --timeout=5 -O $1 $2
[ $? -eq 0 ] && result="200"
else else
echo 找不到curl或者wget无法下载请先安装相应依赖 [ "$3" = "echooff" ] && progress='-q' || progress='-q --show-progress'
[ "$3" = "echoon" ] && progress=''
[ -z "$4" ] && redirect='' || redirect='--max-redirect=0'
wget -Y on $progress $redirect --no-check-certificate --timeout=5 -O $1 $2
[ $? -eq 0 ] && result="200"
fi fi
export all_proxy='' export all_proxy=''
} }
@@ -783,15 +785,14 @@ setserver(){
checkupdate(){ checkupdate(){
if [ -z "$release_new" ];then if [ -z "$release_new" ];then
if [ "$update_url" = "https://cdn.jsdelivr.net/gh/juewuy/ShellClash" ];then if [ "$update_url" = "https://cdn.jsdelivr.net/gh/juewuy/ShellClash" ];then
webget /tmp/clashrelease https://api.github.com/repos/juewuy/ShellClash/releases/latest noprogress release_new=$(webget /dev/null https://github.com.cnpmjs.org/juewuy/ShellClash/releases/latest echoon rediroff 2>&1 | grep -oE "releases/tag/.*" | awk -F '[/" ]' '{print $3}')
[ "$result" = "200" ] && release_new=$(cat /tmp/clashrelease | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g') || release_new=master [ -z "$release_new" ] && release_new=master
update_url=$update_url@$release_new update_url=$update_url@$release_new
fi fi
webget /tmp/clashversion $update_url/bin/version noprogress webget /tmp/clashversion $update_url/bin/version echooff
[ "$result" = "200" ] && source /tmp/clashversion || echo -e "\033[31m检查更新失败\033[0m" [ "$result" = "200" ] && source /tmp/clashversion || echo -e "\033[31m检查更新失败\033[0m"
[ -z "$release_new" ] && release_new=$versionsh [ -z "$release_new" ] && release_new=$versionsh
rm -rf /tmp/clashversion rm -rf /tmp/clashversion
rm -rf /tmp/clashrelease
fi fi
} }
update(){ update(){
@@ -823,6 +824,7 @@ update(){
elif [ "$num" = 2 ]; then elif [ "$num" = 2 ]; then
getcore getcore
update
elif [ "$num" = 3 ]; then elif [ "$num" = 3 ]; then
getgeo getgeo
@@ -830,6 +832,7 @@ update(){
elif [ "$num" = 4 ]; then elif [ "$num" = 4 ]; then
getdb getdb
update
elif [ "$num" = 5 ]; then elif [ "$num" = 5 ]; then
setpac setpac

View File

@@ -291,8 +291,19 @@ daemon(){
fi fi
} }
web_save(){ web_save(){
#使用curl获取面板节点设置 get_save(){
curl -s -H "Authorization: Bearer ${secret}" -H "Content-Type:application/json" http://localhost:${db_port}/proxies | awk -F "{" '{for(i=1;i<=NF;i++) print $i}' | grep -E '^"all".*"Selector"' | grep -oE '"name".*"now".*",' | sed 's/"name"://g' | sed 's/"now"://g'| sed 's/"//g' > /tmp/clash_web_save if curl --version > /dev/null 2>&1;then
curl -s -H "Authorization: Bearer ${secret}" -H "Content-Type:application/json" "$1"
elif [ -n "$(wget --help 2>&1|grep '\-\-method')" ];then
wget -q --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" -O - "$1"
else
logger 当前系统未安装curl且wget的版本太低无法保存节点配置
getconfig
sed -i /保存节点配置/d $cronpath >/dev/null 2>&1
fi
}
#使用get_save获取面板节点设置
get_save http://localhost:${db_port}/proxies | awk -F "{" '{for(i=1;i<=NF;i++) print $i}' | grep -E '^"all".*"Selector"' | grep -oE '"name".*"now".*",' | sed 's/"name"://g' | sed 's/"now"://g'| sed 's/"//g' > /tmp/clash_web_save
#对比文件,如果有变动则写入磁盘,否则清除缓存 #对比文件,如果有变动则写入磁盘,否则清除缓存
if [ "$(cat /tmp/clash_web_save)" = "$(cat $clashdir/web_save 2>/dev/null)" ];then if [ "$(cat /tmp/clash_web_save)" = "$(cat $clashdir/web_save 2>/dev/null)" ];then
rm -rf /tmp/clash_web_save rm -rf /tmp/clash_web_save
@@ -301,12 +312,24 @@ web_save(){
fi fi
} }
web_restore(){ web_restore(){
put_save(){
if curl --version > /dev/null 2>&1;then
curl -sS -X PUT -H "Authorization: Bearer ${secret}" -H "Content-Type:application/json" "$1" -d "$2" >/dev/null
else
wget --method=PUT --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" --body-data="$2" "$1" >/dev/null
fi
}
#设置循环检测clash面板端口 #设置循环检测clash面板端口
i=1 i=1
while [ $i -lt 10 ] while [ $i -lt 10 ]
do do
sleep 1 sleep 1
[ -n "$(curl -s http://localhost:${db_port})" ] && i=10 if curl --version > /dev/null 2>&1;then
test=$(curl -s http://localhost:${db_port})
else
test=$(wget -q -O - http://localhost:${db_port})
fi
[ -n "$test" ] && i=10
done done
#发送数据 #发送数据
num=$(cat $clashdir/web_save | wc -l) num=$(cat $clashdir/web_save | wc -l)
@@ -314,7 +337,7 @@ web_restore(){
do do
group_name=$(awk -F ',' 'NR=="'${i}'" {print $1}' $clashdir/web_save | sed 's/ /%20/g') group_name=$(awk -F ',' 'NR=="'${i}'" {print $1}' $clashdir/web_save | sed 's/ /%20/g')
now_name=$(awk -F ',' 'NR=="'${i}'" {print $2}' $clashdir/web_save) now_name=$(awk -F ',' 'NR=="'${i}'" {print $2}' $clashdir/web_save)
curl -sS -X PUT -H "Authorization: Bearer ${secret}" -H "Content-Type:application/json" http://localhost:${db_port}/proxies/"${group_name}" -d "{\"name\":\"${now_name}\"}" >/dev/null put_save http://localhost:${db_port}/proxies/${group_name} "{\"name\":\"${now_name}\"}"
done done
exit 0 exit 0
} }
@@ -336,13 +359,9 @@ afstart(){
mark_time mark_time
#设置本机代理 #设置本机代理
[ "$local_proxy" = "已开启" ] && $0 set_proxy $mix_port $hostdir [ "$local_proxy" = "已开启" ] && $0 set_proxy $mix_port $hostdir
#还原面板配置相关 #还原面板配置
if curl --version > /dev/null 2>&1;then
web_save_auto #启用面板配置自动保存 web_save_auto #启用面板配置自动保存
[ -f $clashdir/web_save ] && web_restore & #后台还原面板配置 [ -f $clashdir/web_save ] && web_restore & #后台还原面板配置
fi
#clash启动校验
[ -z "$(pidof clash)" ] && logger clash启动失败 && $0 stop && exit 0
} }
case "$1" in case "$1" in
@@ -351,7 +370,6 @@ afstart)
afstart afstart
;; ;;
start) start)
[ -n "$(pidof clash)" ] && logger clash服务已经运行请勿重复运行 && exit 0
#读取配置文件 #读取配置文件
getconfig getconfig
#使用内置规则强行覆盖config配置文件 #使用内置规则强行覆盖config配置文件
@@ -372,9 +390,7 @@ stop)
#读取配置文件 #读取配置文件
getconfig getconfig
#保存面板配置 #保存面板配置
if curl --version > /dev/null 2>&1;then
web_save web_save
fi
#删除守护进程&面板配置自动保存 #删除守护进程&面板配置自动保存
sed -i /clash保守模式守护进程/d $cronpath >/dev/null 2>&1 sed -i /clash保守模式守护进程/d $cronpath >/dev/null 2>&1
sed -i /保存节点配置/d $cronpath >/dev/null 2>&1 sed -i /保存节点配置/d $cronpath >/dev/null 2>&1