v1.0.0beta15.7

~优化了定时任务实现方式,适配更多设备
~优化了配置文件修饰方式
~优化了启动和关闭进程时的检测,修复错误
~优化了非root用户体验
~优化脚本结构
This commit is contained in:
juewuy 2020-10-27 16:36:01 +08:00
parent d4aba59988
commit d9bff51afc
4 changed files with 79 additions and 84 deletions

View File

@ -94,7 +94,7 @@ echoerrornum(){
echo -e "\033[31m请输入正确的数字\033[0m" echo -e "\033[31m请输入正确的数字\033[0m"
} }
catpac(){ catpac(){
cat > /tmp/pac <<EOF cat > /tmp/clash_pac <<EOF
function FindProxyForURL(url, host) { function FindProxyForURL(url, host) {
if ( if (
isInNet(host, "0.0.0.0", "255.0.0.0")|| isInNet(host, "0.0.0.0", "255.0.0.0")||
@ -112,19 +112,18 @@ function FindProxyForURL(url, host) {
} }
EOF EOF
[ ! -d $clashdir/ui ] && mkdir -p $clashdir/ui [ ! -d $clashdir/ui ] && mkdir -p $clashdir/ui
[ "$(cat /tmp/pac)" = "$(cat $clashdir/ui/pac 2>&1)" ] && rm -rf /tmp/pac || mv -f /tmp/pac $clashdir/ui/pac cmp -s /tmp/clash_pac $clashdir/ui/pac
[ "$?" = 0 ] && rm -rf /tmp/clash_pac || mv -f /tmp/clash_pac $clashdir/ui/pac
} }
start_over(){ start_over(){
[ $? -eq 1 ] && exit
echo -e "\033[32mclash服务已启动\033[0m" echo -e "\033[32mclash服务已启动\033[0m"
if [ -n "$hostdir" ];then if [ -n "$hostdir" ];then
echo -e "请使用\033[30;47m http://$host$hostdir \033[0m管理内置规则" echo -e "请使用\033[30;47m http://$host$hostdir \033[0m管理内置规则"
else else
echo -e "可使用\033[30;47m http://clash.razord.top \033[0m管理内置规则" echo -e "可使用\033[30;47m http://clash.razord.top \033[0m管理内置规则"
echo -e "Host地址:\033[36m $host \033[0m 端口:\033[36m $db_port \033[0m" echo -e "Host地址:\033[36m $host \033[0m 端口:\033[36m $db_port \033[0m"
echo -e "也可前往更新菜单安装本地Dashboard面板连接更稳定\033[0m" echo -e "推荐前往更新菜单安装本地Dashboard面板连接更稳定\033[0m"
fi fi
echo -----------------------------------------------
} }
setport(){ setport(){
inputport(){ inputport(){
@ -186,13 +185,18 @@ setport(){
sed -i "/authentication*/"d $ccfg sed -i "/authentication*/"d $ccfg
echo 密码已移除! echo 密码已移除!
else else
[ "$local_proxy" = "已开启" ] && echo -e "\033[32m请先禁用本机代理功能\033[0m" && setport if [ "$local_proxy" = "已开启" ];then
authentication=$(echo $input | grep :) echo -----------------------------------------------
if [ -n "$authentication" ]; then echo -e "\033[33m请先禁用本机代理功能\033[0m"
setconfig authentication \'$authentication\' sleep 1
echo -e "\033[32m设置成功\033[0m"
else else
echo -e "\033[31m输入有误请重新输入\033[0m" authentication=$(echo $input | grep :)
if [ -n "$authentication" ]; then
setconfig authentication \'$authentication\'
echo -e "\033[32m设置成功\033[0m"
else
echo -e "\033[31m输入有误请重新输入\033[0m"
fi
fi fi
fi fi
setport setport
@ -266,7 +270,8 @@ setdns(){
elif [ "$num" = 4 ]; then elif [ "$num" = 4 ]; then
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[31m仅限搭配其他DNS服务(比如dnsmasq、smartDNS)时使用!\033[0m" echo -e "\033[31m仅限搭配其他DNS服务(比如dnsmasq、smartDNS)时使用!\033[0m"
setconfig dns_no 已禁用 dns_no=已禁用
setconfig dns_no $dns_no
echo -e "\033[33m已禁用内置DNS\033[0m" echo -e "\033[33m已禁用内置DNS\033[0m"
clashadv clashadv
else else
@ -307,22 +312,11 @@ clashstart(){
echo -e "\033[31m没有找到配置文件请先导入配置文件\033[0m" echo -e "\033[31m没有找到配置文件请先导入配置文件\033[0m"
clashlink clashlink
fi fi
if [ -n "$PID" ];then
echo -----------------------------------------------
$clashdir/start.sh stop
echo -e "\033[31mClash服务已停止\033[0m"
fi
catpac #生成pac自动代理文件 catpac #生成pac自动代理文件
echo ----------------------------------------------- echo -----------------------------------------------
$clashdir/start.sh start $clashdir/start.sh start
sleep 1 sleep 1
PID=$(pidof clash) [ -n "$(pidof clash)" ] && start_over || exit 1
if [ -z "$PID" ];then
$clashdir/start.sh stop
echo -e "\033[31mclash启动失败\033[0m"
exit;
fi
start_over
} }
macfilter(){ macfilter(){
add_mac(){ add_mac(){

View File

@ -23,10 +23,10 @@ start() {
if [ -z "$(pidof procd)" ];then if [ -z "$(pidof procd)" ];then
#创建后台进程 #创建后台进程
service_start $DIR/clash -d $DIR service_start $DIR/clash -d $DIR
#设置守护进程
$DIR/start.sh deamon
#其他设置 #其他设置
$DIR/start.sh afstart $DIR/start.sh afstart
#设置守护进程
$DIR/start.sh deamon
else else
start_service start_service
fi fi

View File

@ -841,11 +841,8 @@ testcommand(){
elif [ "$num" = 1 ]; then elif [ "$num" = 1 ]; then
$clashdir/start.sh stop $clashdir/start.sh stop
echo ----------------------------------------------- echo -----------------------------------------------
[ -f /tmp/clash/config.yaml ] && confdir='-f /tmp/clash/config.yaml' $clashdir/clash -t -d $clashdir
$clashdir/clash -t -d $clashdir $confdir
[ "$?" = 0 ] && testover=32m测试通过|| testover=31m出现错误请截图后到TG群询问 [ "$?" = 0 ] && testover=32m测试通过|| testover=31m出现错误请截图后到TG群询问
echo -----------------------------------------------
echo -e "\033[$testover\033[0m" echo -e "\033[$testover\033[0m"
exit; exit;
elif [ "$num" = 2 ]; then elif [ "$num" = 2 ]; then

View File

@ -28,9 +28,9 @@ getconfig(){
[ "$common_ports" = "已开启" ] && ports='-m multiport --dports 53,587,465,995,993,143,80,443 ' [ "$common_ports" = "已开启" ] && ports='-m multiport --dports 53,587,465,995,993,143,80,443 '
} }
logger(){ logger(){
[ -z "$1" ] && echo -e "\033[31m$1\033[0m" [ -z "$2" ] && echo -e "\033[31m$1\033[0m"
echo `date "+%G-%m-%d %H:%M:%S"` $1 >> $clashdir/log echo `date "+%G-%m-%d %H:%M:%S"` $1 >> $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(){ cronset(){
# 参数1代表要移除的关键字,参数2代表要添加的任务语句 # 参数1代表要移除的关键字,参数2代表要添加的任务语句
@ -69,18 +69,18 @@ EOF`
markhttp=1 markhttp=1
fi fi
#输出 #输出
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
echo 正在连接服务器获取配置文件…………链接地址为: echo 正在连接服务器获取配置文件…………链接地址为:
echo -e "\033[4;32m$Https\033[0m" echo -e "\033[4;32m$Https\033[0m"
echo 可以手动复制该链接到浏览器打开并查看数据是否正常! echo 可以手动复制该链接到浏览器打开并查看数据是否正常!
echo -e "\033[36m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" echo -e "\033[36m-----------------------------------------------"
echo -e "| |" echo -e "| |"
echo -e "| 需要一点时间,请耐心等待! |" echo -e "| 需要一点时间,请耐心等待! |"
echo -e "| \033[0m如长时间没有数据请用ctrl+c退出\033[36m |" echo -e "| \033[0m如长时间没有数据请用ctrl+c退出\033[36m |"
echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\033[0m" echo -e "-----------------------------------------------\033[0m"
#获取在线yaml文件 #获取在线yaml文件
yaml=$clashdir/config.yaml yaml=$clashdir/config.yaml
yamlnew=/tmp/config.yaml yamlnew=/tmp/clash_config.yaml
rm -rf $yamlnew rm -rf $yamlnew
source $clashdir/getdate.sh && webget $yamlnew $Https source $clashdir/getdate.sh && webget $yamlnew $Https
if [ "$result" != "200" ];then if [ "$result" != "200" ];then
@ -113,55 +113,50 @@ EOF`
Https="" Https=""
#检测节点 #检测节点
if [ -z "$(cat $yamlnew | grep 'server:' | grep -v 'nameserver')" ];then if [ -z "$(cat $yamlnew | grep 'server:' | grep -v 'nameserver')" ];then
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
logger "获取到了配置文件,但似乎并不包含正确的节点信息!" logger "获取到了配置文件,但似乎并不包含正确的节点信息!"
echo ----------------------------------------------- echo -----------------------------------------------
sed -n '1,30p' $yamlnew sed -n '1,30p' $yamlnew
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[33m请检查如上配置文件信息:\033[0m" echo -e "\033[33m请检查如上配置文件信息:\033[0m"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
exit 1 exit 1
fi fi
#检测旧格式 #检测旧格式
if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
logger "已经停止对旧格式配置文件的支持!!!" logger "已经停止对旧格式配置文件的支持!!!"
echo -e "请使用新格式或者使用【导入节点/链接】功能!" echo -e "请使用新格式或者使用【导入节点/链接】功能!"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
exit 1 exit 1
fi fi
#检测不支持的加密协议 #检测不支持的加密协议
if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
logger "不支持chacha20加密请更换节点加密协议" logger "不支持chacha20加密请更换节点加密协议"
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -----------------------------------------------
exit 1 exit 1
fi fi
#替换文件 #如果不同则备份并替换文件
[ -f $yaml ] && mv $yaml $yaml.bak if [ -f $yaml ];then
mv $yamlnew $yaml 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使其生效 echo 配置文件已生成正在启动clash使其生效
#重启clash服务 #重启clash服务
$0 stop $0 stop
$0 start $0 start
sleep 1 if [ "$?" = 0 ];then
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
logger "配置文件获取成功clash服务已启动" echooff 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
fi fi
} }
@ -331,11 +326,8 @@ web_save(){
#使用get_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 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 cmp -s /tmp/clash_web_save $clashdir/web_save
rm -rf /tmp/clash_web_save [ "$?" = 0 ] && rm -rf /tmp/clash_web_save || mv -f /tmp/clash_web_save $clashdir/web_save
else
mv -f /tmp/clash_web_save $clashdir/web_save
fi
} }
web_restore(){ web_restore(){
put_save(){ put_save(){
@ -368,25 +360,33 @@ web_restore(){
exit 0 exit 0
} }
afstart(){ afstart(){
#读取配置文件 $clashdir/clash -t -d $clashdir >/dev/null
getconfig if [ "$?" = 0 ];then
#修改iptables规则使流量进入clash #读取配置文件
[ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && start_dns getconfig
[ "$redir_mod" != "纯净模式" ] && [ "$redir_mod" != "Tun模式" ] && start_redir #修改iptables规则使流量进入clash
[ "$redir_mod" = "Redir模式" ] && [ "$tproxy_mod" = "已开启" ] && start_udp [ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && start_dns
#标记启动时间 [ "$redir_mod" != "纯净模式" ] && [ "$redir_mod" != "Tun模式" ] && start_redir
mark_time [ "$redir_mod" = "Redir模式" ] && [ "$tproxy_mod" = "已开启" ] && start_udp
#设置本机代理 #标记启动时间
[ "$local_proxy" = "已开启" ] && $0 set_proxy $mix_port $db_port mark_time
#启用面板配置自动保存 #设置本机代理
cronset '#每10分钟保存节点配置' "*/10 * * * * test -n \"$(pidof clash)\" && $clashdir/start.sh web_save #每10分钟保存节点配置" [ "$local_proxy" = "已开启" ] && $0 set_proxy $mix_port $db_port
[ -f $clashdir/web_save ] && web_restore & #后台还原面板配置 #启用面板配置自动保存
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(){ start_old(){
$clashdir/clash -d $clashdir >/dev/null & $clashdir/clash -d $clashdir >/dev/null &
sleep 1
cronset '#clash保守模式守护进程' "*/1 * * * * test -z \"$(pidof clash)\" && $clashdir/start.sh restart #clash保守模式守护进程"
afstart afstart
daemon
} }
case "$1" in case "$1" in
@ -395,6 +395,7 @@ afstart)
afstart afstart
;; ;;
start) start)
[ -n "$(pidof clash)" ] && $0 stop #禁止多实例
getconfig getconfig
#使用内置规则强行覆盖config配置文件 #使用内置规则强行覆盖config配置文件
[ "$modify_yaml" != "已开启" ] && modify_yaml [ "$modify_yaml" != "已开启" ] && modify_yaml
@ -421,8 +422,7 @@ stop)
elif [ "$USER" = "root" ];then elif [ "$USER" = "root" ];then
systemctl stop clash.service >/dev/null 2>&1 systemctl stop clash.service >/dev/null 2>&1
fi fi
pidof clash | xargs kill -9 >/dev/null 2>&1 PID=$(pidof clash) && [ -n "$PID" ] && kill -9 $PID >/dev/null 2>&1
killall -9 clash >/dev/null 2>&1
stop_iptables #清理iptables stop_iptables #清理iptables
[ "$local_proxy" = "已开启" ] && $0 unset_proxy #禁用本机代理 [ "$local_proxy" = "已开启" ] && $0 unset_proxy #禁用本机代理
;; ;;
@ -438,6 +438,10 @@ web_save)
getconfig getconfig
web_save web_save
;; ;;
daemon)
getconfig
cronset '#clash保守模式守护进程' "*/1 * * * * test -z \"$(pidof clash)\" && $clashdir/start.sh restart #clash保守模式守护进程"
;;
set_proxy) set_proxy)
#GNOME配置 #GNOME配置
if gsettings --version >/dev/null 2>&1 ;then if gsettings --version >/dev/null 2>&1 ;then