From d9bff51afcb307aa00e7b0ea100030bf1e6ff3ef Mon Sep 17 00:00:00 2001 From: juewuy Date: Tue, 27 Oct 2020 16:36:01 +0800 Subject: [PATCH] =?UTF-8?q?v1.0.0beta15.7=20~=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=AE=9E=E7=8E=B0=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E9=80=82=E9=85=8D=E6=9B=B4=E5=A4=9A=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=20~=E4=BC=98=E5=8C=96=E4=BA=86=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=BF=AE=E9=A5=B0=E6=96=B9=E5=BC=8F=20~?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E5=90=AF=E5=8A=A8=E5=92=8C=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E8=BF=9B=E7=A8=8B=E6=97=B6=E7=9A=84=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF=20~=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BA=86=E9=9D=9Eroot=E7=94=A8=E6=88=B7=E4=BD=93?= =?UTF-8?q?=E9=AA=8C=20~=E4=BC=98=E5=8C=96=E8=84=9A=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/clash.sh | 42 +++++++--------- scripts/clashservice | 4 +- scripts/getdate.sh | 5 +- scripts/start.sh | 112 ++++++++++++++++++++++--------------------- 4 files changed, 79 insertions(+), 84 deletions(-) diff --git a/scripts/clash.sh b/scripts/clash.sh index 4f2083f..79de5f5 100644 --- a/scripts/clash.sh +++ b/scripts/clash.sh @@ -94,7 +94,7 @@ echoerrornum(){ echo -e "\033[31m请输入正确的数字!\033[0m" } catpac(){ - cat > /tmp/pac < /tmp/clash_pac <> $clashdir/log - [ "$(wc -l $clashdir/log | awk '{print $1}')" -gt 30 ] && sed -i '1d' $clashdir/log + [ "$(wc -l $clashdir/log | awk '{print $1}')" -gt 30 ] && sed -i '1,5d' $clashdir/log } cronset(){ # 参数1代表要移除的关键字,参数2代表要添加的任务语句 @@ -69,18 +69,18 @@ EOF` markhttp=1 fi #输出 - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- echo 正在连接服务器获取配置文件…………链接地址为: echo -e "\033[4;32m$Https\033[0m" echo 可以手动复制该链接到浏览器打开并查看数据是否正常! - echo -e "\033[36m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + echo -e "\033[36m-----------------------------------------------" echo -e "| |" echo -e "| 需要一点时间,请耐心等待! |" echo -e "| \033[0m如长时间没有数据请用ctrl+c退出\033[36m |" - echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\033[0m" + echo -e "-----------------------------------------------\033[0m" #获取在线yaml文件 yaml=$clashdir/config.yaml - yamlnew=/tmp/config.yaml + yamlnew=/tmp/clash_config.yaml rm -rf $yamlnew source $clashdir/getdate.sh && webget $yamlnew $Https if [ "$result" != "200" ];then @@ -113,55 +113,50 @@ EOF` Https="" #检测节点 if [ -z "$(cat $yamlnew | grep 'server:' | grep -v 'nameserver')" ];then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- logger "获取到了配置文件,但似乎并不包含正确的节点信息!" echo ----------------------------------------------- sed -n '1,30p' $yamlnew echo ----------------------------------------------- echo -e "\033[33m请检查如上配置文件信息:\033[0m" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- exit 1 fi #检测旧格式 if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- logger "已经停止对旧格式配置文件的支持!!!" echo -e "请使用新格式或者使用【导入节点/链接】功能!" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- exit 1 fi #检测不支持的加密协议 if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- logger "不支持chacha20加密,请更换节点加密协议!!!" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo ----------------------------------------------- exit 1 fi - #替换文件 - [ -f $yaml ] && mv $yaml $yaml.bak - mv $yamlnew $yaml + #如果不同则备份并替换文件 + if [ -f $yaml ];then + cmp -s $yamlnew $yaml + [ "$?" = 0 ] && rm -f $yamlnew || mv -f $yaml $yaml.bak && mv -f $yamlnew $yaml + else + mv -f $yamlnew $yaml + fi echo 配置文件已生成!正在启动clash使其生效! #重启clash服务 $0 stop $0 start - sleep 1 - if [ -z "$(pidof clash)" ];then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if [ -f $yaml.bak ];then - $clashdir/start.sh stop - mv $yaml.bak $yaml - $0 start - logger "clash服务启动失败!已还原配置文件并重启clash!" - sleep 1 - [ -n "$(pidof clash)" ] && exit 0 - fi - logger "clash服务启动失败!请查看报错信息!" - $0 stop - $clashdir/clash -t -d $clashdir - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - exit 1 - else + if [ "$?" = 0 ];then logger "配置文件获取成功!clash服务已启动!" echooff + else + if [ -f $yaml.bak ];then + $0 stop + mv -f $yaml.bak $yaml + $0 start + [ "$?" = 0 ] && logger "已还原配置文件并重启clash!" && exit 0 + fi fi fi } @@ -331,11 +326,8 @@ web_save(){ #使用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 - rm -rf /tmp/clash_web_save - else - mv -f /tmp/clash_web_save $clashdir/web_save - fi + cmp -s /tmp/clash_web_save $clashdir/web_save + [ "$?" = 0 ] && rm -rf /tmp/clash_web_save || mv -f /tmp/clash_web_save $clashdir/web_save } web_restore(){ put_save(){ @@ -368,25 +360,33 @@ web_restore(){ exit 0 } afstart(){ - #读取配置文件 - getconfig - #修改iptables规则使流量进入clash - [ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && start_dns - [ "$redir_mod" != "纯净模式" ] && [ "$redir_mod" != "Tun模式" ] && start_redir - [ "$redir_mod" = "Redir模式" ] && [ "$tproxy_mod" = "已开启" ] && start_udp - #标记启动时间 - mark_time - #设置本机代理 - [ "$local_proxy" = "已开启" ] && $0 set_proxy $mix_port $db_port - #启用面板配置自动保存 - cronset '#每10分钟保存节点配置' "*/10 * * * * test -n \"$(pidof clash)\" && $clashdir/start.sh web_save #每10分钟保存节点配置" - [ -f $clashdir/web_save ] && web_restore & #后台还原面板配置 + $clashdir/clash -t -d $clashdir >/dev/null + if [ "$?" = 0 ];then + #读取配置文件 + getconfig + #修改iptables规则使流量进入clash + [ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && start_dns + [ "$redir_mod" != "纯净模式" ] && [ "$redir_mod" != "Tun模式" ] && start_redir + [ "$redir_mod" = "Redir模式" ] && [ "$tproxy_mod" = "已开启" ] && start_udp + #标记启动时间 + mark_time + #设置本机代理 + [ "$local_proxy" = "已开启" ] && $0 set_proxy $mix_port $db_port + #启用面板配置自动保存 + cronset '#每10分钟保存节点配置' "*/10 * * * * test -n \"$(pidof clash)\" && $clashdir/start.sh web_save #每10分钟保存节点配置" + [ -f $clashdir/web_save ] && web_restore & #后台还原面板配置 + else + logger "clash服务启动失败!请查看报错信息!" + logger `$clashdir/clash -t -d $clashdir 1>&0` + $0 stop & + exit 1 + fi + exit 0 } start_old(){ $clashdir/clash -d $clashdir >/dev/null & - sleep 1 - cronset '#clash保守模式守护进程' "*/1 * * * * test -z \"$(pidof clash)\" && $clashdir/start.sh restart #clash保守模式守护进程" afstart + daemon } case "$1" in @@ -395,6 +395,7 @@ afstart) afstart ;; start) + [ -n "$(pidof clash)" ] && $0 stop #禁止多实例 getconfig #使用内置规则强行覆盖config配置文件 [ "$modify_yaml" != "已开启" ] && modify_yaml @@ -421,8 +422,7 @@ stop) elif [ "$USER" = "root" ];then systemctl stop clash.service >/dev/null 2>&1 fi - pidof clash | xargs kill -9 >/dev/null 2>&1 - killall -9 clash >/dev/null 2>&1 + PID=$(pidof clash) && [ -n "$PID" ] && kill -9 $PID >/dev/null 2>&1 stop_iptables #清理iptables [ "$local_proxy" = "已开启" ] && $0 unset_proxy #禁用本机代理 ;; @@ -438,6 +438,10 @@ web_save) getconfig web_save ;; +daemon) + getconfig + cronset '#clash保守模式守护进程' "*/1 * * * * test -z \"$(pidof clash)\" && $clashdir/start.sh restart #clash保守模式守护进程" + ;; set_proxy) #GNOME配置 if gsettings --version >/dev/null 2>&1 ;then