diff --git a/bin/clashfm.tar.gz b/bin/clashfm.tar.gz index c4970b1..054320b 100644 Binary files a/bin/clashfm.tar.gz and b/bin/clashfm.tar.gz differ diff --git a/bin/clashpre/clash-linux-386 b/bin/clashpre/clash-linux-386 index 952e3ed..d9e6137 100644 Binary files a/bin/clashpre/clash-linux-386 and b/bin/clashpre/clash-linux-386 differ diff --git a/bin/clashpre/clash-linux-amd64 b/bin/clashpre/clash-linux-amd64 index 8094f5a..418f9e1 100644 Binary files a/bin/clashpre/clash-linux-amd64 and b/bin/clashpre/clash-linux-amd64 differ diff --git a/bin/clashpre/clash-linux-armv5 b/bin/clashpre/clash-linux-armv5 index f531a71..1969133 100644 Binary files a/bin/clashpre/clash-linux-armv5 and b/bin/clashpre/clash-linux-armv5 differ diff --git a/bin/clashpre/clash-linux-armv7 b/bin/clashpre/clash-linux-armv7 index d7322ed..4753e55 100644 Binary files a/bin/clashpre/clash-linux-armv7 and b/bin/clashpre/clash-linux-armv7 differ diff --git a/bin/clashpre/clash-linux-armv8 b/bin/clashpre/clash-linux-armv8 index 0785b4d..1533508 100644 Binary files a/bin/clashpre/clash-linux-armv8 and b/bin/clashpre/clash-linux-armv8 differ diff --git a/bin/clashpre/clash-linux-mips-softfloat b/bin/clashpre/clash-linux-mips-softfloat index 4b9a083..9e3340b 100644 Binary files a/bin/clashpre/clash-linux-mips-softfloat and b/bin/clashpre/clash-linux-mips-softfloat differ diff --git a/bin/clashpre/clash-linux-mipsle-hardfloat b/bin/clashpre/clash-linux-mipsle-hardfloat index 55a0b85..586d203 100644 Binary files a/bin/clashpre/clash-linux-mipsle-hardfloat and b/bin/clashpre/clash-linux-mipsle-hardfloat differ diff --git a/bin/clashpre/clash-linux-mipsle-softfloat b/bin/clashpre/clash-linux-mipsle-softfloat index 021a955..aea4c6a 100644 Binary files a/bin/clashpre/clash-linux-mipsle-softfloat and b/bin/clashpre/clash-linux-mipsle-softfloat differ diff --git a/bin/version b/bin/version index 62a258a..7173b25 100644 --- a/bin/version +++ b/bin/version @@ -1,4 +1,4 @@ clash_v=1.2.0 -clashpre_v=2020.10.09 GeoIP_v=20201021 -versionsh=1.0.0beta15.6 +versionsh=123 +clashpre_v=2020.10.26.gc025a01 diff --git a/install.sh b/install.sh index 21a9191..06586be 100644 --- a/install.sh +++ b/install.sh @@ -15,7 +15,7 @@ if [ "$USER" != "root" ];then echo 当前用户:$USER $echo "\033[31m请尽量使用root用户执行安装!\033[0m" echo ----------------------------------------------- - read -p "仍要安装?可能会产生未知错误!(1/0) > " res + read -p "仍要安装?可能会产生大量未知错误!(1/0) > " res [ "$res" != "1" ] && exit fi webget(){ diff --git a/scripts/clash.sh b/scripts/clash.sh index 36353d6..4f2083f 100644 --- a/scripts/clash.sh +++ b/scripts/clash.sh @@ -11,11 +11,12 @@ getconfig(){ #检查/读取标识文件 [ ! -f $ccfg ] && echo '#标识clash运行状态的文件,不明勿动!' > $ccfg source $ccfg - #设置默认端口 + #设置默认端口及变量 [ -z "$mix_port" ] && mix_port=7890 [ -z "$redir_port" ] && redir_port=7892 [ -z "$db_port" ] && db_port=9999 [ -z "$dns_port" ] && dns_port=1053 + [ -z "$local_proxy" ] && local_proxy=未开启 #检查mac地址记录 [ ! -f $clashdir/mac ] && touch $clashdir/mac #获取本机host地址 @@ -37,7 +38,7 @@ getconfig(){ #开机自启描述 if [ "$start_old" = "已开启" ];then auto="\033[33m已设置保守模式!\033[0m" - auto1="\033[36m设为\033[0m常规模式启动" + auto1="代理本机:\033[36m$local_proxy\033[0m" elif [ "$autostart" = "enable_rc" -o "$autostart" = "enable_sys" ]; then auto="\033[32m已设置开机启动!\033[0m" auto1="\033[36m禁用\033[0mclash开机启动" @@ -46,10 +47,8 @@ getconfig(){ auto1="\033[36m允许\033[0mclash开机启动" fi #获取运行模式 - if [ -z "$redir_mod" ];then - sed -i "2i\redir_mod=Redir模式" $ccfg - redir_mod=Redir模式 - fi + [ -z "$redir_mod" ] && [ "$USER" = "root" -o "$USER" = "admin" ] && redir_mod=Redir模式 + [ -z "$redir_mod" ] && redir_mod=纯净模式 #获取运行状态 PID=$(pidof clash) if [ -n "$PID" ];then @@ -67,14 +66,6 @@ getconfig(){ #检测系统端口占用 checkport fi - #检查定时任务配置文件 - if [ -z "$cronpath" ];then - [ -d /etc/crontabs/ ] && cronpath="/etc/crontabs/root" - [ -d /var/spool/cron/ ] && cronpath="/var/spool/cron/root" - [ -d /var/spool/cron/crontabs/ ] && cronpath="/var/spool/cron/crontabs/root" - [ -d /etc/storage/cron/crontabs ] && cronpath="/etc/storage/cron/crontabs/admin" - [ -n "$cronpath" ] && sed -i "1i\cronpath=\'$cronpath\'" $ccfg - fi #输出状态 echo ----------------------------------------------- echo -e "\033[30;46m欢迎使用ShellClash!\033[0m 版本:$versionsh_l" @@ -92,6 +83,12 @@ getconfig(){ [ "$res" = 1 ] && source $clashdir/getdate.sh && userguide fi } +setconfig(){ + #参数1代表变量名,参数2代表变量值,参数3即文件路径 + [ -z "$3" ] && configpath=$clashdir/mark || configpath=$3 + sed -i "/${1}*/"d $configpath + echo "${1}=${2}" >> $configpath +} echoerrornum(){ echo ----------------------------------------------- echo -e "\033[31m请输入正确的数字!\033[0m" @@ -144,8 +141,7 @@ setport(){ echo -e "\033[31m当前端口已被其他进程占用,请重新输入!\033[0m" inputport else - sed -i "/$xport*/"d $ccfg - sed -i "1i$xport=$portx" $ccfg + setconfig $xport $portx echo -e "\033[32m设置成功!!!\033[0m" setport fi @@ -193,8 +189,7 @@ setport(){ [ "$local_proxy" = "已开启" ] && echo -e "\033[32m请先禁用本机代理功能!\033[0m" && setport authentication=$(echo $input | grep :) if [ -n "$authentication" ]; then - sed -i "/authentication*/"d $ccfg - sed -i "1i\authentication=\'$authentication\'" $ccfg + setconfig authentication \'$authentication\' echo -e "\033[32m设置成功!!!\033[0m" else echo -e "\033[31m输入有误,请重新输入!\033[0m" @@ -213,8 +208,8 @@ setport(){ elif [ "$num" = 6 ]; then read -p "请输入面板访问密码(输入0删除密码) > " secret if [ -n "$secret" ]; then - sed -i "/secret*/"d $ccfg - [ "$secret" = "0" ] && secret="" || sed -i "1i\secret=$secret" $ccfg + [ "$secret" = "0" ] && secret="" + setconfig secret $secret echo -e "\033[32m设置成功!!!\033[0m" fi setport @@ -222,7 +217,7 @@ setport(){ } setdns(){ source $ccfg - if [ "$dns_no" = "true" ];then + if [ "$dns_no" = "已禁用" ];then read -p "检测到内置DNS已被禁用,是否启用内置DNS?(1/0) > " res if [ "$res" = "1" ];then sed -i "/dns_no*/"d $ccfg @@ -252,16 +247,14 @@ setdns(){ read -p "请输入新的DNS > " dns_nameserver dns_nameserver=$(echo $dns_nameserver | sed 's/|/\,\ /') if [ -n "$dns_nameserver" ]; then - sed -i "/dns_nameserver*/"d $ccfg - sed -i "1i\dns_nameserver=\'$dns_nameserver\'" $ccfg + setconfig dns_nameserver \'$dns_nameserver\' echo -e "\033[32m设置成功!!!\033[0m" fi elif [ "$num" = 2 ]; then read -p "请输入新的DNS > " dns_fallback dns_fallback=$(echo $dns_fallback | sed 's/|/\,\ /') if [ -n "$dns_fallback" ]; then - sed -i "/dns_fallback*/"d $ccfg - sed -i "1i\dns_fallback=\'$dns_fallback\'" $ccfg + setconfig dns_fallback \'$dns_fallback\' echo -e "\033[32m设置成功!!!\033[0m" fi elif [ "$num" = 3 ]; then @@ -273,8 +266,7 @@ setdns(){ elif [ "$num" = 4 ]; then echo ----------------------------------------------- echo -e "\033[31m仅限搭配其他DNS服务(比如dnsmasq、smartDNS)时使用!\033[0m" - sed -i "/dns_no*/"d $ccfg - sed -i "1i\dns_no=true" $ccfg + setconfig dns_no 已禁用 echo -e "\033[33m已禁用内置DNS!!!\033[0m" clashadv else @@ -433,6 +425,27 @@ macfilter(){ macfilter fi } +set_local_proxy(){ + echo ----------------------------------------------- + if [ "$local_proxy" = "未开启" ] > /dev/null 2>&1; then + if [ -n "$authentication" ] && [ "$authentication" != "未设置" ] ;then + echo -e "\033[32m检测到您已经设置了Http/Sock5代理密码,请先取消密码!\033[0m" + sleep 1 + setport + else + local_proxy=已开启 + $clashdir/start.sh set_proxy $mix_port $db_port + echo -e "\033[32m已经成功配置本机代理~\033[0m" + echo -e "\033[36m如未生效,请重新启动终端或重新连接SSH!\033[0m" + fi + else + local_proxy=未开启 + $clashdir/start.sh unset_proxy + echo -e "\033[33m已经停用本机代理规则!!\033[0m" + echo -e "\033[36m如未生效,请重新启动终端或重新连接SSH!\033[0m" + fi + setconfig local_proxy $local_proxy +} clashcfg(){ set_redir_mod(){ echo ----------------------------------------------- @@ -466,10 +479,12 @@ clashcfg(){ if [ "$?" != 0 ];then echo ----------------------------------------------- echo -e "\033[31m当前设备内核不支持开启Tun/混合模式,请使用其他模式!\033[0m" + sleep 1 set_redir_mod elif [ "$clashcore" = "clash" ] || [ "$clashcore" = "clashr" ];then echo ----------------------------------------------- echo -e "\033[31m当前核心不支持开启Tun模式!请先切换clash核心!!!\033[0m" + sleep 1 clashcfg else redir_mod=Tun模式 @@ -480,10 +495,12 @@ clashcfg(){ if [ "$?" != 0 ];then echo ----------------------------------------------- echo -e "\033[31m当前设备内核不支持开启Tun/混合模式,请使用其他模式!\033[0m" + sleep 1 set_redir_mod elif [ "$clashcore" = "clash" ] || [ "$clashcore" = "clashr" ];then echo ----------------------------------------------- echo -e "\033[31m当前核心不支持开启Tun模式!请先切换clash核心!!!\033[0m" + sleep 1 clashcfg else redir_mod=混合模式 @@ -501,10 +518,8 @@ clashcfg(){ echoerrornum clashcfg fi - sed -i '/redir_mod*/'d $ccfg - sed -i "1i\redir_mod=$redir_mod" $ccfg - sed -i '/dns_mod*/'d $ccfg - sed -i "1i\dns_mod=$dns_mod" $ccfg + setconfig redir_mod $redir_mod + setconfig dns_mod $dns_mod echo ----------------------------------------------- echo -e "\033[36m已设为 $redir_mod !!\033[0m" } @@ -528,15 +543,11 @@ clashcfg(){ dns_mod=fake-ip elif [ "$num" = 2 ]; then dns_mod=redir_host - redir_mod=Redir模式 else echoerrornum clashcfg fi - sed -i '/dns_mod*/'d $ccfg - sed -i "1i\dns_mod=$dns_mod" $ccfg - sed -i '/redir_mod*/'d $ccfg - sed -i "1i\redir_mod=$redir_mod" $ccfg + setconfig dns_mod $dns_mod echo ----------------------------------------------- echo -e "\033[36m已设为 $dns_mod 模式!!\033[0m" } @@ -546,7 +557,6 @@ clashcfg(){ [ -z "$common_ports" ] && common_ports=已开启 [ -z "$dns_mod" ] && dns_mod=redir_host [ -z "$dns_over" ] && dns_over=已开启 - [ -z "$local_proxy" ] && local_proxy=未开启 [ -z "$(cat $clashdir/mac)" ] && mac_return=未开启 || mac_return=已启用 # echo ----------------------------------------------- @@ -571,7 +581,13 @@ clashcfg(){ elif [ "$num" = 0 ]; then clashsh elif [ "$num" = 1 ]; then - set_redir_mod + if [ "$USER" != "root" -a "$USER" != "admin" ];then + echo ----------------------------------------------- + echo -e "\033[33m非root用户无法启用静态路由,仅可以使用纯净模式!\033[0m" + sleep 1 + else + set_redir_mod + fi clashcfg elif [ "$num" = 2 ]; then @@ -579,75 +595,49 @@ clashcfg(){ clashcfg elif [ "$num" = 3 ]; then - sed -i '/skip_cert*/'d $ccfg echo ----------------------------------------------- if [ "$skip_cert" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\skip_cert=已开启" $ccfg echo -e "\033[33m已设为开启跳过本地证书验证!!\033[0m" skip_cert=已开启 else - sed -i "1i\skip_cert=未开启" $ccfg echo -e "\033[33m已设为禁止跳过本地证书验证!!\033[0m" skip_cert=未开启 fi + setconfig skip_cert $skip_cert clashcfg elif [ "$num" = 4 ]; then - sed -i '/common_ports*/'d $ccfg echo ----------------------------------------------- if [ "$common_ports" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\common_ports=已开启" $ccfg - echo -e "\033[33m已设为仅代理(22,53,587,465,995,993,143,80,443)等常用端口!!\033[0m" + echo -e "\033[33m已设为仅代理(53,587,465,995,993,143,80,443)等常用端口!!\033[0m" common_ports=已开启 else - sed -i "1i\common_ports=未开启" $ccfg echo -e "\033[33m已设为代理全部端口!!\033[0m" common_ports=未开启 fi + setconfig common_ports $common_ports clashcfg elif [ "$num" = 5 ]; then macfilter elif [ "$num" = 6 ]; then - sed -i '/dns_over*/'d $ccfg echo ----------------------------------------------- if [ "$dns_over" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\dns_over=已开启" $ccfg echo -e "\033[33m已设置DNS为不走本地dnsmasq服务器!\033[0m" echo -e "可能会对浏览速度产生一定影响,介意勿用!" dns_over=已开启 else /etc/init.d/clash enable - sed -i "1i\dns_over=未开启" $ccfg echo -e "\033[32m已设置DNS通过本地dnsmasq服务器!\033[0m" echo -e "redir-host模式下部分网站可能会被运营商dns污染导致无法打开" dns_over=未开启 fi + setconfig dns_over $dns_over clashcfg elif [ "$num" = 7 ]; then - sed -i '/local_proxy*/'d $ccfg - echo ----------------------------------------------- - if [ "$local_proxy" = "未开启" ] > /dev/null 2>&1; then - if [ -n "$authentication" ] && [ "$authentication" != "未设置" ] ;then - echo -e "\033[32m检测到您已经设置了Http/Sock5代理密码,请先取消密码!\033[0m" - sleep 1 - setport - else - sed -i "1i\local_proxy=已开启" $ccfg - local_proxy=已开启 - $clashdir/start.sh set_proxy $mix_port - echo -e "\033[32m已经成功配置本机代理~\033[0m" - echo -e "\033[36m如未生效,请重新启动终端或重新连接SSH!\033[0m" - fi - else - sed -i "1i\local_proxy=未开启" $ccfg - local_proxy=未开启 - $clashdir/start.sh unset_proxy - echo -e "\033[33m已经停用本机代理规则!!\033[0m" - echo -e "\033[36m如未生效,请重新启动终端或重新连接SSH!\033[0m" - fi + set_local_proxy sleep 1 clashcfg @@ -675,8 +665,8 @@ clashadv(){ echo -e " 2 启用ipv6支持: \033[36m$ipv6_support\033[0m ————实验性功能,可能不稳定" echo -e " 3 使用保守方式启动: \033[36m$start_old\033[0m ————切换时会停止clash服务" echo -e " 4 Redir模式udp转发: \033[36m$tproxy_mod\033[0m ————依赖iptables-mod-tproxy" - echo -e " 5 手动指定clash运行端口及秘钥" - echo -e " 6 手动配置内置DNS服务" + echo -e " 5 配置内置DNS服务: \033[36m$dns_no\033[0m" + echo -e " 6 手动指定clash运行端口及秘钥" echo ----------------------------------------------- echo -e " 8 \033[31m重置\033[0m配置文件" echo -e " 9 \033[32m重启\033[0mclash服务" @@ -690,82 +680,82 @@ clashadv(){ clashsh elif [ "$num" = 1 ]; then - sed -i '/modify_yaml*/'d $ccfg echo ----------------------------------------------- if [ "$modify_yaml" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\modify_yaml=已开启" $ccfg echo -e "\033[33m已设为使用用户完全自定义的配置文件!!" echo -e "\033[36m不明白原理的用户切勿随意开启此选项" echo -e "\033[31m!!!必然会导致上不了网!!!\033[0m" modify_yaml=已开启 sleep 3 else - sed -i "1i\modify_yaml=未开启" $ccfg echo -e "\033[32m已设为使用脚本内置规则管理config.yaml配置文件!!\033[0m" modify_yaml=未开启 fi + setconfig modify_yaml $modify_yaml clashadv - elif [ "$num" = 2 ]; then - sed -i '/ipv6_support*/'d $ccfg + elif [ "$num" = 2 ]; then echo ----------------------------------------------- if [ "$ipv6_support" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\ipv6_support=已开启" $ccfg echo -e "\033[33m已开启对ipv6协议的支持!!\033[0m" echo -e "Clash对ipv6的支持并不友好,如不能使用请静等修复!" ipv6_support=已开启 sleep 2 else - sed -i "1i\ipv6_support=未开启" $ccfg echo -e "\033[32m已禁用对ipv6协议的支持!!\033[0m" ipv6_support=未开启 fi + setconfig ipv6_support $ipv6_support clashadv elif [ "$num" = 3 ]; then - sed -i '/start_old*/'d $ccfg echo ----------------------------------------------- if [ "$start_old" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\start_old=已开启" $ccfg echo -e "\033[33m改为使用保守方式启动clash服务!!\033[0m" echo -e "\033[36m此模式兼容性更好但无法禁用开机启动!!\033[0m" start_old=已开启 + setconfig start_old $start_old $clashdir/start.sh stop sleep 2 else - sed -i "1i\start_old=未开启" $ccfg - echo -e "\033[32m改为使用默认方式启动clash服务!!\033[0m" - start_old=未开启 - $clashdir/start.sh stop + if [ -f /etc/init.d/clash -o -f /etc/systemd/system -o -f /usr/lib/systemd/system ];then + echo -e "\033[32m改为使用默认方式启动clash服务!!\033[0m" + start_old=未开启 + setconfig start_old $start_old + $clashdir/start.sh stop + else + echo -e "\033[31m当前设备不支持以其他模式启动!!\033[0m" + sleep 1 + fi fi clashadv elif [ "$num" = 4 ]; then - sed -i '/tproxy_mod*/'d $ccfg echo ----------------------------------------------- if [ "$tproxy_mod" = "未开启" ]; then if [ -n "$(iptables -j TPROXY 2>&1 | grep 'on-port')" ];then - sed -i "1i\tproxy_mod=已开启" $ccfg tproxy_mod=已开启 echo -e "\033[32m已经为Redir模式启用udp转发功能!\033[0m" else + tproxy_mod=未开启 echo -e "\033[31m您的设备不支持tproxy模式,无法开启!\033[0m" fi else - sed -i "1i\tproxy_mod=未开启" $ccfg tproxy_mod=未开启 echo -e "\033[33m已经停止使用tproxy转发udp流量!!\033[0m" fi + setconfig tproxy_mod $tproxy_mod sleep 1 - clashadv + clashadv + elif [ "$num" = 5 ]; then - setport - clashadv + setdns + clashadv elif [ "$num" = 6 ]; then - setdns + setport clashadv - + elif [ "$num" = 8 ]; then read -p "确认重置配置文件?(1/0) > " res if [ "$res" = "1" ];then @@ -805,7 +795,7 @@ clashcron(){ elif [ "$num" = 0 ]; then clashcron elif [ "$num" = 9 ]; then - sed -i /$cronname/d $cronpath + crontab -l > /tmp/conf && sed -i "/$cronname/d" /tmp/conf && crontab /tmp/conf && rm -f /tmp/conf echo ----------------------------------------------- echo -e "\033[31m定时任务:$cronname已删除!\033[0m" clashcron @@ -845,41 +835,20 @@ clashcron(){ echo 将在$week1的$hour点$min分$cronname(旧的任务会被覆盖) read -p "是否确认添加定时任务?(1/0) > " res if [ "$res" = '1' ]; then - sed -i /$cronname/d $cronpath - echo "$min $hour * * $week $cronset >/dev/null 2>&1 #$week1的$hour点$min分$cronname" >> $cronpath + cronwords="$min $hour * * $week $cronset >/dev/null 2>&1 #$week1的$hour点$min分$cronname" + crontab -l > /tmp/conf && sed -i "/$cronname/d" /tmp/conf && echo "$cronwords" >> /tmp/conf && crontab /tmp/conf && rm -f /tmp/conf echo ----------------------------------------------- echo -e "\033[31m定时任务已添加!!!\033[0m" - chmod 600 $cronpath #修改权限 fi clashcron } - checkcron(){ - if [ -z "$cronpath" ];then - echo ----------------------------------------------- - echo -e "\033[33m找不到定时任务配置文件,无法添加添加定时任务!" - echo -e "\033[0m请手动指定定时任务配置文件,文件位置可以通过【crontab -e】命令查看\033[0m" - echo ----------------------------------------------- - read -p "请输入crontab文件路径(输入回车返回主菜单) > " path - if [ -z "$path" ];then - clashsh - elif [ -f $path ];then - cronpath=$path - sed -i '/cronpath*/'d $ccfg - sed -i "1i\cronpath=\'$cronpath\'" $ccfg - else - echo -e "\033[33m输入的路径不正确,请重新输入!\033[0m" - checkcron - fi - fi - } #定时任务菜单 - checkcron #检测定时任务文件 echo ----------------------------------------------- echo -e "\033[30;47m欢迎使用定时任务功能:\033[0m" echo -e "\033[44m 实验性功能,遇问题请加TG群反馈:\033[42;30m t.me/clashfm \033[0m" echo ----------------------------------------------- echo -e "\033[33m已添加的定时任务:\033[36m" - cat $cronpath | grep -oE ' #.*' + crontab -l | grep -oE ' #.*' echo -e "\033[0m"----------------------------------------------- echo -e " 1 设置\033[33m定时重启\033[0mclash服务" echo -e " 2 设置\033[31m定时停止\033[0mclash服务" @@ -958,10 +927,7 @@ clashsh(){ elif [ "$num" = 4 ]; then echo ----------------------------------------------- if [ "$start_old" = "已开启" ];then - sed -i "/start_old*/d" $ccfg - sed -i "1i\start_old=未开启" $ccfg - echo -e "\033[32m已设为使用默认方式启动clash服务!!\033[0m" - start_old=未开启 + set_local_proxy elif [ "$autostart" = "enable_rc" ]; then /etc/init.d/clash disable echo -e "\033[33m已禁止Clash开机启动!\033[0m" diff --git a/scripts/getdate.sh b/scripts/getdate.sh index afb5f03..d9449db 100644 --- a/scripts/getdate.sh +++ b/scripts/getdate.sh @@ -44,9 +44,8 @@ linkconfig(){ echo elif [ "$num" -le 13 ];then #将对应标记值写入mark - sed -i '/rule_link*/'d $ccfg - sed -i "4i\rule_link="$num"" $ccfg rule_link=$num + setconfig rule_link $rule_link echo ----------------------------------------------- echo -e "\033[32m设置成功!返回上级菜单\033[0m" fi @@ -70,9 +69,8 @@ linkserver(){ echo elif [ "$num" -le 5 ];then #将对应标记值写入mark - sed -i '/server_link*/'d $ccfg - sed -i "4i\server_link="$num"" $ccfg server_link=$num + setconfig server_link $server_link echo ----------------------------------------------- echo -e "\033[32m设置成功!返回上级菜单\033[0m" fi @@ -98,8 +96,7 @@ linkfilter(){ exclude='' echo -e "\033[31m 已删除节点过滤关键字!!!\033[0m" fi - sed -i '/exclude=*/'d $ccfg - sed -i "1i\exclude=\'$exclude\'" $ccfg + setconfig exclude \'$exclude\' linkset } linkfilter2(){ @@ -123,8 +120,7 @@ linkfilter2(){ include='' echo -e "\033[31m 已删除节点匹配关键字!!!\033[0m" fi - sed -i '/include=*/'d $ccfg - sed -i "1i\include=\'$include\'" $ccfg + setconfig include \'$include\' linkset } linkset(){ @@ -150,9 +146,8 @@ linkset(){ clashlink elif [ "$num" = '1' ]; then #将用户链接写入mark - sed -i '/Url=*/'d $ccfg sed -i '/Https=*/'d $ccfg - sed -i "6i\Url=\'$Url\'" $ccfg + setconfig Url \'$Url\' Https="" #获取在线yaml文件 $clashdir/start.sh getyaml @@ -171,17 +166,9 @@ linkset(){ linkserver linkset elif [ "$num" = '6' ]; then - sed -i '/skip_cert*/'d $ccfg echo ----------------------------------------------- - if [ "$skip_cert" = "未开启" ] > /dev/null 2>&1; then - sed -i "1i\skip_cert=已开启" $ccfg - #echo -e "\033[33m已设为开启跳过本地证书验证!!\033[0m" - skip_cert=已开启 - else - sed -i "1i\skip_cert=未开启" $ccfg - #echo -e "\033[33m已设为禁止跳过本地证书验证!!\033[0m" - skip_cert=未开启 - fi + [ "$skip_cert" = "未开启" ] && skip_cert=已开启 || skip_cert=未开启 + setconfig skip_cert $skip_cert linkset else echoerrornum @@ -255,8 +242,7 @@ getlink2(){ if [ "$res" = '1' ]; then #将用户链接写入mark sed -i '/Url=*/'d $ccfg - sed -i '/Https=*/'d $ccfg - sed -i "6i\Https=\'$Https\'" $ccfg + setconfig Https \'$Https\' #获取在线yaml文件 $clashdir/start.sh getyaml start_over @@ -272,16 +258,8 @@ getlink2(){ fi } clashlink(){ - #获取订阅规则 - if [ -z "$rule_link" ]; then - sed -i "4i\rule_link=1" $ccfg - rule_link=1 - fi - #获取后端服务器地址 - if [ -z "$server_link" ]; then - sed -i "5i\server_link=1" $ccfg - server_link=1 - fi + [ -z "$rule_link" ] && rule_link=1 + [ -z "$server_link" ] && server_link=1 echo ----------------------------------------------- echo -e "\033[30;47m 欢迎使用导入配置文件功能!\033[0m" echo ----------------------------------------------- @@ -413,18 +391,17 @@ gettar(){ mv $clashdir/clashservice /etc/init.d/clash chmod 777 /etc/init.d/clash else - [ -d /etc/systemd/system ] && sysdir=/etc/systemd/system - [ -d /usr/lib/systemd/system/ ] && sysdir=/usr/lib/systemd/system/ + [ -w /etc/systemd/system ] && sysdir=/etc/systemd/system + [ -w /usr/lib/systemd/system ] && sysdir=/usr/lib/systemd/system if [ -n "$sysdir" ];then #设为systemd方式启动 mv $clashdir/clash.service $sysdir/clash.service sed -i "s%/etc/clash%$clashdir%g" $sysdir/clash.service systemctl daemon-reload - rm -rf /etc/init.d/clash else #设为保守模式启动 sed -i '/start_old=*/'d $clashdir/mark - sed -i "1i\start_old=已开启" $clashdir/mark + echo start_old=已开启 >> $clashdir/mark fi fi #修饰文件及版本号 @@ -432,7 +409,7 @@ gettar(){ sed -i "s%#!/bin/sh%#!/bin/$shtype%g" $clashdir/start.sh chmod 777 $clashdir/start.sh sed -i '/versionsh_l=*/'d $clashdir/mark - sed -i "1i\versionsh_l=$release_new" $clashdir/mark + echo versionsh_l=$release_new >> $clashdir/mark #设置环境变量 [ -w ~/.bashrc ] && profile=~/.bashrc [ -w /etc/profile ] && profile=/etc/profile @@ -555,10 +532,8 @@ getcore(){ echo -e "\033[32m$clashcore核心下载成功,正在替换!\033[0m" mv /tmp/clash.new $clashdir/clash chmod 777 $clashdir/clash #授予权限 - sed -i '/clashcore=*/'d $ccfg - sed -i "1i\clashcore=$clashcore" $ccfg - sed -i '/clashv=*/'d $ccfg - sed -i "1i\clashv=$version" $ccfg + setconfig clashcore $clashcore + setconfig clashv $version rm -rf /tmp/clashversion echo ----------------------------------------------- echo -e "\033[32m$clashcore核心安装成功!\033[0m" @@ -583,8 +558,7 @@ getgeo(){ echo ----------------------------------------------- echo -e "\033[32mGeoIP数据库文件下载成功!\033[0m" mv /tmp/Country.mmdb $clashdir/Country.mmdb - sed -i '/Geo_v=*/'d $ccfg - sed -i "1i\Geo_v=$GeoIP_v" $ccfg + setconfig Geo_v $GeoIP_v rm -rf /tmp/clashversion fi else @@ -684,8 +658,7 @@ getdb(){ #如果clash在运行则重启clash服务 [ "$dbdir" != "/www/clash" ] && [ -n "$PID" ] && $clashdir/start.sh restart #写入配置文件 - sed -i '/hostdir*/'d $ccfg - sed -i "1i\hostdir=\'$hostdir\'" $ccfg + setconfig hostdir \'$hostdir\' echo ----------------------------------------------- echo -e "\033[32m面板安装成功!\033[0m" echo -e "\033[36m请使用\033[32;4mhttp://$host$hostdir\033[0;36m访问面板\033[0m" @@ -732,8 +705,7 @@ setserver(){ update fi #写入mark文件 - sed -i '/update_url*/'d $ccfg - sed -i "1i\update_url=\'$update_url\'" $ccfg + setconfig update_url \'$update_url\' echo ----------------------------------------------- echo -e "\033[32m源地址更新成功!\033[0m" release_new="" @@ -869,9 +841,12 @@ testcommand(){ elif [ "$num" = 1 ]; then $clashdir/start.sh stop echo ----------------------------------------------- - $clashdir/clash -t -d $clashdir + [ -f /tmp/clash/config.yaml ] && confdir='-f /tmp/clash/config.yaml' + $clashdir/clash -t -d $clashdir $confdir + + [ "$?" = 0 ] && testover=32m测试通过!|| testover=31m出现错误!请截图后到TG群询问!!! echo ----------------------------------------------- - echo -e "\033[31m如有报错请截图后到TG群询问!!!\033[0m" + echo -e "\033[$testover\033[0m" exit; elif [ "$num" = 2 ]; then echo ----------------------------------------------- diff --git a/scripts/start.sh b/scripts/start.sh index 663afc6..e115c46 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -8,6 +8,8 @@ getconfig(){ ccfg=$clashdir/mark [ -f $ccfg ] && source $ccfg #默认设置 + [ -z "$redir_mod" ] && [ "$USER" = "root" -o "$USER" = "admin" ] && redir_mod=Redir模式 + [ -z "$redir_mod" ] && redir_mod=纯净模式 [ -z "$skip_cert" ] && skip_cert=已开启 [ -z "$common_ports" ] && common_ports=已开启 [ -z "$dns_mod" ] && dns_mod=redir_host @@ -30,7 +32,14 @@ logger(){ 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 } +cronset(){ + # 参数1代表要移除的关键字,参数2代表要添加的任务语句 + crontab -l > /tmp/conf && sed -i "/$1/d" /tmp/conf && echo "$2" >> /tmp/conf && crontab /tmp/conf + rm -f /tmp/conf +} getyaml(){ + [ -z "$rule_link" ] && rule_link=1 + [ -z "$server_link" ] && server_link=1 #前后端订阅服务器地址索引,可在此处添加! Server=`sed -n ""$server_link"p"<> $ccfg Https="" getyaml fi @@ -173,42 +182,42 @@ modify_yaml(){ else dns='dns: {enable: true, ipv6: true, listen: 0.0.0.0:'$dns_port', use-hosts: true, enhanced-mode: redir-host, nameserver: ['$dns_nameserver$dns_local'], fallback: ['$dns_fallback'], fallback-filter: {geoip: true}}' fi - ################################### yaml=$clashdir/config.yaml #预删除需要添加的项目 a=$(grep -n "port:" $yaml | head -1 | cut -d ":" -f 1) b=$(grep -n "^prox" $yaml | head -1 | cut -d ":" -f 1) b=$((b-1)) - sed -i "${a},${b}d" $yaml - #添加配置 - sed -i "1imixed-port:\ $mix_port" $yaml - sed -i "1aredir-port:\ $redir_port" $yaml - sed -i "2aauthentication:\ \[\"$authentication\"\]" $yaml - sed -i "3a$lan" $yaml - sed -i "4a$mode" $yaml - sed -i "5a$log" $yaml - sed -i "6a$ipv6" $yaml - sed -i "7aexternal-controller:\ :$db_port" $yaml - sed -i "8aexternal-ui:\ $db_ui" $yaml - sed -i "9asecret:\ $secret" $yaml - sed -i "10a$tun" $yaml - sed -i "11a$exper" $yaml - sed -i "12a$dns" $yaml + mkdir -p /tmp/clash > /dev/null + sed "${a},${b}d" $yaml > /tmp/clash/rule.yaml #跳过本地tls证书验证 - if [ "$skip_cert" = "已开启" ];then - sed -i '10,99s/skip-cert-verify: false/skip-cert-verify: true/' $yaml - else - sed -i '10,99s/skip-cert-verify: true/skip-cert-verify: false/' $yaml - fi - #禁止fake-ip回环流量 - #sed -i '/198.18.0.0/'d $yaml - #sed -i '/rules:/a \ - IP-CIDR,198.18.0.0/16,REJECT' $yaml + [ "$skip_cert" = "已开启" ] && sed -i '10,99s/skip-cert-verify: false/skip-cert-verify: true/' /tmp/clash/rule.yaml + #添加配置 + cat > /tmp/clash/set.yaml < /tmp/clash/config.yaml + cmp -s /tmp/clash/config.yaml $yaml + [ "$?" != 0 ] && mv -f /tmp/clash/config.yaml $yaml || rm -f /tmp/clash/config.yaml + rm -f /tmp/clash/set.yaml + rm -f /tmp/clash/rule.yaml } mark_time(){ start_time=`date +%s` sed -i '/start_time*/'d $clashdir/mark - sed -i "1i\start_time=$start_time" $clashdir/mark + echo start_time=$start_time >> $clashdir/mark } start_redir(){ #流量过滤规则 @@ -307,12 +316,6 @@ start_dns(){ ip6tables -I INPUT -p udp --dport 53 -j REJECT fi } -daemon(){ - if [ -n "$cronpath" ];then - echo '*/1 * * * * test -z "$(pidof clash)" && /etc/init.d/clash restart #clash保守模式守护进程' >> $cronpath - chmod 600 $cronpath - fi -} web_save(){ get_save(){ if curl --version > /dev/null 2>&1;then @@ -322,7 +325,7 @@ web_save(){ else logger 当前系统未安装curl且wget的版本太低,无法保存节点配置! getconfig - sed -i /保存节点配置/d $cronpath >/dev/null 2>&1 + cronset '保存节点配置' fi } #使用get_save获取面板节点设置 @@ -339,7 +342,7 @@ web_restore(){ 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 + wget -q --method=PUT --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" --body-data="$2" "$1" >/dev/null fi } #设置循环检测clash面板端口 @@ -364,29 +367,27 @@ web_restore(){ done exit 0 } -web_save_auto(){ - if [ -n "$cronpath" ];then - if [ -z "$(cat $cronpath | grep '保存节点配置')" ];then - echo '*/10 * * * * test -n "$(pidof clash)" && /etc/init.d/clash web_save #每10分钟保存节点配置' >> $cronpath - chmod 600 $cronpath - fi - fi -} afstart(){ #读取配置文件 getconfig #修改iptables规则使流量进入clash - [ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "true" ] && start_dns + [ "$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 - #还原面板配置 - web_save_auto #启用面板配置自动保存 + [ "$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 & #后台还原面板配置 } +start_old(){ + $clashdir/clash -d $clashdir >/dev/null & + sleep 1 + cronset '#clash保守模式守护进程' "*/1 * * * * test -z \"$(pidof clash)\" && $clashdir/start.sh restart #clash保守模式守护进程" + afstart +} case "$1" in @@ -395,29 +396,29 @@ afstart) ;; start) getconfig - [ "$modify_yaml" != "已开启" ] && modify_yaml #使用内置规则强行覆盖config配置文件 + #使用内置规则强行覆盖config配置文件 + [ "$modify_yaml" != "已开启" ] && modify_yaml #使用不同方式启动clash服务 if [ "$start_old" = "已开启" ];then - $clashdir/clash -d $clashdir >/dev/null 2>&1 & - sleep 1 - daemon - afstart + start_old elif [ -f /etc/rc.common ];then /etc/init.d/clash start - else + elif [ "$USER" = "root" ];then systemctl start clash.service + else + start_old fi ;; stop) getconfig web_save #保存面板配置 #删除守护进程&面板配置自动保存 - sed -i /clash保守模式守护进程/d $cronpath >/dev/null 2>&1 - sed -i /保存节点配置/d $cronpath >/dev/null 2>&1 + cronset "clash保守模式守护进程" + cronset "保存节点配置" #多种方式结束进程 if [ -f /etc/rc.common ];then /etc/init.d/clash stop >/dev/null 2>&1 - else + elif [ "$USER" = "root" ];then systemctl stop clash.service >/dev/null 2>&1 fi pidof clash | xargs kill -9 >/dev/null 2>&1 @@ -433,9 +434,6 @@ getyaml) getconfig getyaml ;; -daemon) - daemon - ;; web_save) getconfig web_save @@ -443,12 +441,12 @@ web_save) set_proxy) #GNOME配置 if gsettings --version >/dev/null 2>&1 ;then - gsettings set org.gnome.system.proxy autoconfig-url "http://127.0.0.1:$1/ui/pac" + gsettings set org.gnome.system.proxy autoconfig-url "http://127.0.0.1:$3/ui/pac" gsettings set org.gnome.system.proxy mode "auto" [ "$?" = 0 ] && check=$? #KDE配置 elif kwriteconfig5 -h >/dev/null 2>&1 ;then - kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key "Proxy Config Script" "http://127.0.0.1:$1/ui/pac" + kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key "Proxy Config Script" "http://127.0.0.1:$3/ui/pac" kwriteconfig5 --file kioslaverc --group "Proxy Settings" --key "ProxyType" 2 [ "$?" = 0 ] && check=$? #环境变量方式 @@ -456,7 +454,7 @@ set_proxy) if [ -z "$check" ];then [ -w ~/.bashrc ] && profile=~/.bashrc [ -w /etc/profile ] && profile=/etc/profile - echo 'export all_proxy=http://127.0.0.1:'"$1" >> $profile + echo 'export all_proxy=http://127.0.0.1:'"$2" >> $profile echo 'export ALL_PROXY=$all_proxy' >> $profile fi ;;