diff --git a/bin/Country.mmdb b/bin/Country.mmdb index b60585b..0a25155 100644 Binary files a/bin/Country.mmdb and b/bin/Country.mmdb differ diff --git a/bin/clashfm.tar.gz b/bin/clashfm.tar.gz index 7dd7001..e0525b0 100644 Binary files a/bin/clashfm.tar.gz and b/bin/clashfm.tar.gz differ diff --git a/bin/version b/bin/version index cfb1fdf..e5cd893 100644 --- a/bin/version +++ b/bin/version @@ -1,2 +1,2 @@ -GeoIP_v=20200913 -versionsh=1.0.0.beta5 +GeoIP_v=20200917 +versionsh=1223 diff --git a/install.sh b/install.sh index e4091be..b7d328b 100644 --- a/install.sh +++ b/install.sh @@ -7,7 +7,8 @@ echo "** ShellClash **" echo "** by Juewuy **" echo "***********************************************" url="https://cdn.jsdelivr.net/gh/juewuy/ShellClash" -release_new=$(curl -kfsSL --resolve api.github.com:443:140.82.113.5 "https://api.github.com/repos/juewuy/ShellClash/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g') +url="http://127.0.0.1:8080/clash-for-Miwifi/" +#release_new=$(curl -kfsSL --resolve api.github.com:443:140.82.113.5 "https://api.github.com/repos/juewuy/ShellClash/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g') [ -z "$release_new" ] && release_new=$(curl -kfsSL $url/bin/version | grep "versionsh" | awk -F "=" '{print $2}') [ -z "$release_new" ] && echo "无法连接服务器!" && exit echo -e "最新版本:\033[32m$release_new\033[0m" @@ -45,7 +46,8 @@ fi echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo 开始从服务器获取安装文件! echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -tarurl=$url@release_new/bin/clashfm.tar.gz +#tarurl=$url@$release_new/bin/clashfm.tar.gz +tarurl=$url/bin/clashfm.tar.gz if command -v curl &> /dev/null; then result=$(curl -w %{http_code} -kLo /tmp/clashfm.tar.gz $tarurl) else $result @@ -59,18 +61,33 @@ echo 开始解压文件! mkdir -p $dir/clash > /dev/null tar -zxvf '/tmp/clashfm.tar.gz' -C $dir/clash/ [ $? -ne 0 ] && echo "文件解压失败!" && exit 1 +#判断系统类型写入不同的启动文件 +if [ -n "$(cat /proc/version | grep -i openwrt)" ];then + mv $dir/clash/clashservice /etc/init.d/clash #将rc服务文件移动到系统目录 + chmod 777 /etc/init.d/clash #授予权限 + rm -rf $dir/clash/clash.service +else + [ -d /etc/systemd/system ] && sysdir=/etc/systemd/system + [ -d /usr/lib/systemd/system/ ] && sysdir=/usr/lib/systemd/system/ + mv $dir/clash/clash.service $sysdir/clash.service #将service服务文件移动到系统目录 + sed -i "s%/etc/clash%${dir}/clash%g" $sysdir/clash.service + rm -rf $dir/clash/clashservice + rm -rf /etc/init.d/clash +fi #初始化文件目录 -mv $dir/clash/clashservice /etc/init.d/clash #将clash服务文件移动到系统目录 -chmod 777 /etc/init.d/clash #授予权限 if [ ! -f "$dir/clash/mark" ]; then cat >$dir/clash/mark<> /etc/profile #设置快捷命令环境变量 sed -i '/export clashdir=*/'d /etc/profile diff --git a/scripts/clash.sh b/scripts/clash.sh index 3deabeb..f112467 100644 --- a/scripts/clash.sh +++ b/scripts/clash.sh @@ -1,9 +1,31 @@ -#!/bin/bash +#!/bin/sh # Copyright (C) Juewuy getconfig(){ +#系统类型 +systype=$(cat /proc/version | grep -io openwrt) +if [ -n "$systype" ];then + ps_type=ps + sh_type=sh + host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) + if [ -f /etc/rc.d/*clash ];then + autostart=enable_rc + else + autostart=disable_rc + fi +else + ps_type='ps aux' + sh_type=bash + host=$(ip a|grep -w 'inet'|grep 'global'|grep -E '192.|10.'|sed 's/.*inet.//g'|sed 's/\/[0-9][0-9].*$//g') + [ -z $host ] && host=127.0.0.1 + if [ -n "$(systemctl list-unit-files clash.service | grep -o enable)" ];then + autostart=enable_sys + else + autostart=disable_sys + fi +fi #服务器地址 -[ -z "$update_url" ] && update_url=https://cdn.jsdelivr.net/gh/juewuy/ShellClash@latest +[ -z "$update_url" ] && update_url=https://cdn.jsdelivr.net/gh/juewuy/ShellClash #文件路径 [ -z "$clashdir" ] && clashdir=$(dirname $(readlink -f "$0")) && echo "export clashdir=\"$clashdir\"" >> /etc/profile ccfg=$clashdir/mark @@ -22,7 +44,7 @@ source $ccfg if [ "$start_old" = "已开启" ];then auto="\033[33m已设置保守模式!\033[0m" auto1="\033[36m设为\033[0m常规模式启动" -elif [ -f /etc/rc.d/*clash ];then +elif [ "$autostart" = "enable_rc" -o "$autostart" = "enable_sys" ]; then auto="\033[32m已设置开机启动!\033[0m" auto1="\033[36m禁用\033[0mclash开机启动" else @@ -35,11 +57,10 @@ if [ -z "$redir_mod" ];then redir_mod=Redir模式 fi #获取运行状态 -status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh|wc -l` -if [[ $status -gt 0 ]];then +PID=$(pidof clash) +if [ -n "$PID" ];then run="\033[32m正在运行($redir_mod)\033[0m" - uid=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh|awk '{print $1}'` - VmRSS=`cat /proc/$uid/status|grep -w VmRSS|awk '{print $2,$3}'` + VmRSS=`cat /proc/$PID/status|grep -w VmRSS|awk '{print $2,$3}'` #获取运行时长 if [ -n "$start_time" ]; then time=$((`date +%s`-$start_time)) @@ -59,7 +80,7 @@ fi echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[30;46m欢迎使用ShellClash!\033[0m 版本:$versionsh_l" echo -e "Clash服务"$run","$auto"" -if [ $status -gt 0 ];then +if [ -n "$PID" ];then echo -e "当前内存占用:\033[44m"$VmRSS"\033[0m,已运行:\033[46;30m"$day"\033[44;37m"$time"\033[0m" fi echo -e "TG群:\033[36;4mhttps://t.me/clashfm\033[0m" @@ -80,55 +101,26 @@ if [ ! -f $clashdir/Country.mmdb ];then clashstart fi } -clashstop(){ - source $clashdir/start.sh && stop_old - /etc/init.d/clash stop > /dev/null 2>&1 -} clashstart(){ if [ ! -f "$yaml" ];then echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[31m没有找到配置文件,请先导入节点/订阅链接!\033[0m" clashlink fi - if [ $status -gt 0 ];then + if [ -n "$PID" ];then echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - clashstop + $sh_type $clashdir/start.sh stop echo -e "\033[31mClash服务已停止!\033[0m" fi echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if [ "$start_old" = "已开启" ];then - source $clashdir/start.sh && start_old - sleep 1 - status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh` - if [ -z "$status" ];then - echo -e "\033[31mclash启动失败!\033[0m" - sed -i /start_old=*/d $ccfg - exit - fi - else - /etc/init.d/clash start - sleep 1 - status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh` - if [ -z "$status" ];then - echo -e "\033[31mclash启动失败!\033[0m" - read -p "是否尝试使用保守方式启动?[1/0] > " res - if [ "$res" = '1' ]; then - source $clashdir/start.sh && start_old - sleep 1 - status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh` - if [ -z "$status" ];then - echo -e "\033[31mclash启动失败!\033[0m" - sed -i /start_old=*/d $ccfg - exit - fi - else - echo -e "\033[33m操作取消!\033[0m" - exit - fi - fi + $sh_type $clashdir/start.sh start + sleep 1 + status=`$ps_type |grep -w 'clash'|grep -v grep|grep -v clash.sh` + if [ -z "$status" ];then + echo -e "\033[31mclash启动失败!\033[0m" + exit fi - host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) echo -e "\033[32mclash服务已启动!\033[0m" if [ -d /www/clash ];then echo -e "请使用\033[30;47m http://$host/clash \033[0m管理内置规则" @@ -337,8 +329,7 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then fi redir_mod=混合模式 elif [[ $num == 4 ]]; then - redir_mod=纯净模式 - host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) + redir_mod=纯净模式 echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[32m已经设置为纯净模式!\033[0m" echo -e "\033[33m当前模式必须手动在设备WiFi或应用中配置HTTP或sock5代理\033[0m" @@ -584,13 +575,13 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then echo -e "\033[33m改为使用保守方式启动clash服务!!\033[0m" echo -e "\033[36m此模式兼容性更好但无法禁用开机启动!!\033[0m" start_old=已开启 - /etc/init.d/clash stop > /dev/null 2>&1 + $sh_type $clashdir/start.sh stop > /dev/null 2>&1 sleep 2 else sed -i "1i\start_old=未开启" $ccfg echo -e "\033[32m改为使用默认方式启动clash服务!!\033[0m" start_old=未开启 - source $clashdir/start.sh && stop_old + $sh_type $clashdir/start.sh stop > /dev/null 2>&1 fi clashadv @@ -689,11 +680,15 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then /etc/init.d/clash stop rm -rf $clashdir rm -rf /etc/init.d/clash + rm -rf /etc/systemd/system/clash.service + rm -rf /usr/lib/systemd/system/clash.service rm -rf /www/clash rm -rf $csh sed -i '/alias clash=*/'d /etc/profile sed -i '/export clashdir=*/'d /etc/profile + source /etc/profile > /dev/null 2>&1 echo 已卸载clash相关文件! + exit fi echo -e "\033[31m操作已取消!\033[0m" exit; @@ -816,19 +811,19 @@ elif [[ $num == 0 ]]; then elif [[ $num == 1 ]]; then cronname=重启clash服务 - cronset='source /etc/profile && source $clashdir/start.sh && restart' + cronset="$clashdir/start.sh restart" setcron elif [[ $num == 2 ]]; then cronname=停止clash服务 - cronset='source /etc/profile && source $clashdir/start.sh && stop' + cronset="$clashdir/start.sh stop" setcron elif [[ $num == 3 ]]; then cronname=开启clash服务 - cronset='source /etc/profile && source $clashdir/start.sh && start' + cronset="$clashdir/start.sh start" setcron elif [[ $num == 4 ]]; then cronname=更新订阅链接 - cronset="source /etc/profile && source $clashdir/getdate.sh && getyaml" + cronset="$clashdir/start.sh getyaml" setcron else @@ -866,7 +861,7 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then clashcfg elif [[ $num == 3 ]]; then - clashstop + $sh_type $clashdir/start.sh stop echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[31mClash服务已停止!\033[0m" echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -879,12 +874,20 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then sed -i "1i\start_old=未开启" $ccfg echo -e "\033[32m已设为使用默认方式启动clash服务!!\033[0m" start_old=未开启 - elif [ -f /etc/rc.d/*clash ]; then + elif [ "$autostart" = "enable_rc" ]; then /etc/init.d/clash disable echo -e "\033[33m已禁止Clash开机启动!\033[0m" - else + elif [ "$autostart" = "disable_rc" ]; then /etc/init.d/clash enable echo -e "\033[32m已设置Clash开机启动!\033[0m" + elif [ "$autostart" = "enable_sys" ]; then + systemctl disable clash.service + echo -e "\033[33m已禁止Clash开机启动!\033[0m" + elif [ "$autostart" = "disable_sys" ]; then + systemctl enable clash.service + echo -e "\033[32m已设置Clash开机启动!\033[0m" + else + echo -e "\033[32m当前系统不支持设置开启启动!\033[0m" fi clashsh @@ -920,7 +923,7 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then elif [[ $num == 0 ]]; then clashsh elif [[ $num == 1 ]]; then - clashstop + $sh_type $clashdir/start.sh stop echo ----------------------------------------------- $clashdir/clash -d $clashdir & { sleep 3 ; kill $! & } echo ----------------------------------------------- @@ -964,7 +967,7 @@ if [[ $num -le 9 ]] > /dev/null 2>&1; then elif [[ $num == 7 ]]; then echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - for PID in $(ps|awk '{print $1}');do + for PID in $($ps_type|awk '{print $1}');do [ -f "/proc/$PID/status" ] && vmrss=$(cat /proc/$PID/status|grep -w VmRSS|awk '{print $2}') [ -n "$vmrss" ] && echo $vmrss $(cat /proc/$PID/status|grep -w Name|awk '{print $2}') done diff --git a/scripts/clashservice b/scripts/clashservice index 6fce57b..4086b23 100644 --- a/scripts/clashservice +++ b/scripts/clashservice @@ -8,12 +8,6 @@ START=92 start_service() { #开机加载环境变量保证找到文件路径 source /etc/profile > /dev/null 2>&1 - #加载修饰脚本 - source $clashdir/start.sh - #读取配置文件 - getconfig - #使用内置规则强行覆盖config配置文件 - [ "$modify_yaml" != "已开启" ] && modify_yaml #创建clash后台进程 procd_open_instance procd_set_param respawn @@ -21,17 +15,6 @@ start_service() { procd_set_param stdout 1 procd_set_param command $clashdir/clash -d $clashdir procd_close_instance - #修改iptables规则使流量进入clash - stop_iptables - [ "$redir_mod" != "纯净模式" ] && start_dns - [ "$redir_mod" != "纯净模式" ] && [ "$redir_mod" != "Tun模式" ] && start_redir - #标记启动时间 - mark_time - #标记启动方式 - sed -i /start_old=*/d $ccfg - sed -i "1i\start_old=未开启" $ccfg + #其他设置 + sh $clashdir/start.sh afstart } -stop_service() { - source $clashdir/start.sh - stop_iptables -} \ No newline at end of file diff --git a/scripts/getdate.sh b/scripts/getdate.sh index 2691abc..62fb5a8 100644 --- a/scripts/getdate.sh +++ b/scripts/getdate.sh @@ -1,160 +1,6 @@ #!/bin/bash # Copyright (C) Juewuy -getyaml(){ -ccfg=$clashdir/mark -source $ccfg -#前后端订阅服务器地址索引,可在此处添加! -Server=`sed -n ""$server_link"p"< /dev/null 2>&1 -result=$(curl -w %{http_code} -kLo $yamlnew $Https) -if [ "$result" != "200" ];then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - echo -e "\033[31m配置文件获取失败!\033[0m" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - echo - if [ -z $markhttp ];then - echo 请尝试使用导入节点/链接功能! - getlink - - else - read -p "是否更换后端地址后重试?[1/0] > " res - if [ "$res" = '1' ]; then - sed -i '/server_link=*/'d $ccfg - if [[ $server_link -ge 6 ]]; then - server_link=0 - fi - server_link=$(($server_link + 1)) - echo $server_link - sed -i "1i\server_link=$server_link" $ccfg - Https="" - getyaml - fi - #exit; - fi -else - Https="" - if cat $yamlnew | grep ', server:' >/dev/null;then - #检测旧格式 - if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - echo -e "\033[31m已经停止对旧格式配置文件的支持!!!\033[0m" - echo -e "请使用新格式或者使用\033[32m导入节点/订阅\033[0m功能!" - sleep 2 - clashlink - fi - #检测不支持的加密协议 - if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then - if [ "$clashcore" = "clash" -o "$clashcore" = "clashpre" ];then - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - echo -e "\033[31m当前核心:$clashcore不支持chacha20加密!!!\033[0m" - echo -e "请更换使用clashR核心!!!" - sleep 2 - getcore - fi - fi - #替换文件 - [ -f $yaml ] && mv $yaml $yaml.bak - mv $yamlnew $yaml - echo 配置文件已生成!正在启动clash使其生效! - #重启clash服务 - killall -9 clash &> /dev/null - start_over(){ - host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) - echo -e "\033[32mclash服务已启动!\033[0m" - echo -e "可以使用\033[30;47m http://clash.razord.top \033[0m管理内置规则" - echo -e "Host地址:\033[36m $host \033[0m 端口:\033[36m 9999 \033[0m" - echo -e "也可前往更新菜单安装本地Dashboard面板,连接更稳定!\033[0m" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - clashsh - } - if [ "$start_old" = "已开启" ];then - source $clashdir/start.sh && start_old - sleep 1 - status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh|wc -l` - if [[ $status -gt 0 ]];then - start_over - fi - else - /etc/init.d/clash start - sleep 1 - status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh|wc -l` - if [[ $status -gt 0 ]];then - start_over - else - echo -e "\033[31mclash服务启动失败!\033[0m" - echo -e "\033[33m5秒后尝试使用保守方式启动!(使用ctrl+c退出!)\033[0m" - echo 5&&sleep 1&&echo 4&&sleep 1&&echo 3&&sleep 1&&echo 2&&sleep 1&&echo 1&&sleep 1 - source $clashdir/start.sh && start_old - sleep 1 - status=`ps |grep -w 'clash'|grep -v grep|grep -v clash.sh|wc -l` - if [[ $status -gt 0 ]];then - start_over - fi - fi - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if [ -f $yaml.bak ];then - echo -e "\033[31mclash服务启动失败!已还原配置文件并重启clash!\033[0m" - mv $yaml.bak $yaml - /etc/init.d/clash start - sleep 1 - clashsh - else - echo -e "\033[31mclash服务启动失败!请查看报错信息!\033[0m" - $clashdir/clash -d $clashdir & { sleep 3 ; kill $! & } - exit; - fi - fi - else - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - echo -e "\033[33m获取到了配置文件,但格式似乎不对!\033[0m" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - sed -n '1,30p' $yamlnew - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - echo -e "\033[33m请检查如上配置文件信息:\033[0m" - echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - fi - #exit; -fi -#exit -} linkconfig(){ echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[44m 实验性功能,遇问题请加TG群反馈:\033[42;30m t.me/clashfm \033[0m" @@ -263,7 +109,7 @@ if [ -n $Url ];then sed -i "6i\Url=\'$Url\'" $ccfg Https="" #获取在线yaml文件 - getyaml + $sh_type $clashdir/start.sh getyaml elif [ "$num" = '2' ]; then linkfilter linkset @@ -366,7 +212,7 @@ if [ -n $Https ];then sed -i '/Https=*/'d $ccfg sed -i "6i\Https=\'$Https\'" $ccfg #获取在线yaml文件 - getyaml + $sh_type $clashdir/start.sh getyaml fi clashlink fi @@ -398,12 +244,22 @@ if [ "$res" = '1' ]; then mkdir -p $clashdir > /dev/null tar -zxvf '/tmp/clashfm.tar.gz' -C $clashdir/ > /dev/null [ $? -ne 0 ] && echo "文件解压失败!" && exit 1 - #初始化文件目录 - mv $clashdir/clashservice /etc/init.d/clash #将clash服务文件移动到系统目录 - chmod 777 /etc/init.d/clash #授予权限 + #判断系统类型写入不同的启动文件 + if [ -n "$(cat /proc/version | grep -i openwrt)" ];then + mv $dir/clash/clashservice /etc/init.d/clash #将rc服务文件移动到系统目录 + chmod 777 /etc/init.d/clash #授予权限 + rm -rf $dir/clash/clash.service + else + [ -d /etc/systemd/system ] && sysdir=/etc/systemd/system + [ -d /usr/lib/systemd/system/ ] && sysdir=/usr/lib/systemd/system/ + mv $dir/clash/clash.service $sysdir/clash.service #将service服务文件移动到系统目录 + sed -i "s%/etc/clash%${dir}/clash%g" $sysdir/clash.service + rm -rf $dir/clash/clashservice + rm -rf /etc/init.d/clash + fi #写入版本号 sed -i '/versionsh_l=*/'d $ccfg - sed -i "1i\versionsh_l=$versionsh" $ccfg + sed -i "1i\versionsh_l=$release_new" $ccfg #删除临时文件 rm -rf /tmp/clashfm.tar.gz rm -rf /tmp/clashversion @@ -433,6 +289,7 @@ cpucore=$(uname -ms | tr ' ' '_' | tr '[A-Z]' '[a-z]') [ -n "$(echo $cpucore | grep -E "linux.*mips.*")" ] && cpucore="mipsle-softfloat" [ -n "$(echo $cpucore | grep -E "linux.*x86.*")" ] && cpucore="386" [ -n "$(echo $cpucore | grep -E "linux.*amd64.*")" ] && cpucore="amd64" +[ -n "$(echo $cpucore | grep -E "linux.*x86_64.*")" ] && cpucore="amd64" ### echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "当前clash核心:\033[47;30m $clashcore \033[46;30m$clashv\033[0m" @@ -549,7 +406,7 @@ clashsh fi } getdb(){ -host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) +#host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[36m安装本地版dashboard管理面板\033[0m" echo ----------------------------------------------- @@ -618,7 +475,7 @@ update catpac(){ #检测目录 [ ! -d /www/clash -a ! -d $clashdir/ui ]&&echo 未检测到本地Dashboard面板,请先安装面板!&&sleep 1&&getdb -host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) +#host=$(ubus call network.interface.lan status | grep \"address\" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';) [ -d /www/clash ]&&dir="/www/clash"&&pac=http://$host/clash/pac [ -d $clashdir/ui ]&&dir="$clashdir/ui"&&pac=http://$host:9999/ui/pac echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -677,7 +534,7 @@ elif [[ $num == 4 ]]; then update fi elif [[ $num == 9 ]]; then - update_url='https://juewuy.xyz/clash' + update_url='http://127.0.0.1:8080/clash-for-Miwifi' else echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ echo -e "\033[31m请输入正确的数字!\033[0m" diff --git a/scripts/start.sh b/scripts/start.sh index 80c1c75..82e0c2c 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,7 +1,9 @@ - #!/bin/sh +#!/bin/sh # Copyright (C) Juewuy getconfig(){ +#加载环境变量 +[ -z "$clashdir" ] && source /etc/profile > /dev/null 2>&1 ccfg=$clashdir/mark if [ ! -f "$ccfg" ]; then echo mark文件不存在,默认以Redir模式运行! @@ -20,6 +22,137 @@ for portx in 1053 7890 7892 9999 ;do [ -n "$(netstat -ntulp |grep :$portx|grep -v clash)" ] && echo -e "检测到端口:\033[30;47m $portx \033[0m被以下进程占用!clash无法启动!" && echo $(netstat -ntulp |grep :$portx) && exit; done } +getyaml(){ +#前后端订阅服务器地址索引,可在此处添加! +Server=`sed -n ""$server_link"p"< /dev/null 2>&1 +result=$(curl -w %{http_code} -kLo $yamlnew $Https) +if [ "$result" != "200" ];then + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo -e "\033[31m配置文件获取失败!\033[0m" + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo + if [ -z $markhttp ];then + echo 请尝试使用导入节点/链接功能! + getlink + + else + read -p "是否更换后端地址后重试?[1/0] > " res + if [ "$res" = '1' ]; then + sed -i '/server_link=*/'d $ccfg + if [[ $server_link -ge 6 ]]; then + server_link=0 + fi + server_link=$(($server_link + 1)) + echo $server_link + sed -i "1i\server_link=$server_link" $ccfg + Https="" + getyaml + fi + #exit; + fi +else + Https="" + if cat $yamlnew | grep ', server:' >/dev/null;then + #检测旧格式 + if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo -e "\033[31m已经停止对旧格式配置文件的支持!!!\033[0m" + echo -e "请使用新格式或者使用\033[32m导入节点/订阅\033[0m功能!" + sleep 2 + clashlink + fi + #检测不支持的加密协议 + if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then + if [ "$clashcore" = "clash" -o "$clashcore" = "clashpre" ];then + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo -e "\033[31m当前核心:$clashcore不支持chacha20加密!!!\033[0m" + echo -e "请更换使用clashR核心!!!" + sleep 2 + getcore + fi + fi + #替换文件 + [ -f $yaml ] && mv $yaml $yaml.bak + mv $yamlnew $yaml + echo 配置文件已生成!正在启动clash使其生效! + #重启clash服务 + $0 stop + start_over(){ + echo -e "\033[32mclash服务已启动!\033[0m" + echo -e "可以使用\033[30;47m http://clash.razord.top \033[0m管理内置规则" + echo -e "Host地址:\033[36m $host \033[0m 端口:\033[36m 9999 \033[0m" + echo -e "也可前往更新菜单安装本地Dashboard面板,连接更稳定!\033[0m" + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + } + $0 start + sleep 1 + PID=$(pidof clash) + if [ -n "$PID" ];then + start_over + else + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + if [ -f $yaml.bak ];then + echo -e "\033[31mclash服务启动失败!已还原配置文件并重启clash!\033[0m" + mv $yaml.bak $yaml + $0 start + sleep 1 + else + echo -e "\033[31mclash服务启动失败!请查看报错信息!\033[0m" + $clashdir/clash -d $clashdir & { sleep 3 ; kill $! & } + exit; + fi + fi + else + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo -e "\033[33m获取到了配置文件,但格式似乎不对!\033[0m" + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + sed -n '1,30p' $yamlnew + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + echo -e "\033[33m请检查如上配置文件信息:\033[0m" + echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + fi + #exit; +fi +#exit +} modify_yaml(){ ##########需要变更的配置########### mix='mixed-port: 7890' @@ -78,7 +211,7 @@ exper='experimental: {ignore-resolve-fail: true, interface-name: en0}' mark_time(){ start_time=`date +%s` sed -i '/start_time*/'d $clashdir/mark - sed -i "3i\start_time=$start_time" $clashdir/mark + sed -i "1i\start_time=$start_time" $clashdir/mark } start_redir(){ #修改iptables规则使流量进入clash @@ -144,6 +277,7 @@ start_dns(){ daemon_old(){ #守护进程状态 status=$(ps |grep -w 'clash'|grep -v grep|grep -v clash.sh) + [ -z $status ] && status=$(ps aux |grep -w 'clash'|grep -v grep|grep -v clash.sh) if [ -z $status ];then $clashdir/clash -d $clashdir> /dev/null & mark_time @@ -159,52 +293,58 @@ checkcron(){ clashsh fi } -start_old(){ +afstart(){ #读取配置文件 getconfig - #使用内置规则强行覆盖config配置文件 - [ "$modify_yaml" != "已开启" ] && modify_yaml - #创建clash后台进程 - $clashdir/clash -d $clashdir> /dev/null & #修改iptables规则使流量进入clash stop_iptables [ "$redir_mod" != "纯净模式" ] && start_dns [ "$redir_mod" != "纯净模式" ] && [ "$redir_mod" != "Tun模式" ] && start_redir #标记启动时间 mark_time - #创建守护进程 - checkcron - sed -i /start.sh/d $cronpath - echo "*/1 * * * * source /etc/profile && source $clashdir/start.sh && daemon_old >/dev/null 2>&1" >> $cronpath - #设定启动方式 - sed -i /start_old=*/d $ccfg - sed -i "1i\start_old=已开启" $ccfg } -stop_old(){ - #删除守护 - checkcron - sed -i /start.sh/d $cronpath - #结束进程 - killall -9 clash &> /dev/null - stop_iptables -} -start(){ - getconfig - if [ "$start_old" ="已开启" ];then - start_old - else - /etc/init.d/clash start - fi -} -stop(){ - getconfig - if [ "$start_old" ="已开启" ];then - stop_old - else - /etc/init.d/clash stop - fi -} -restart(){ - stop - start -} \ No newline at end of file + +case "$1" in + +afstart) + afstart + ;; +start) + #读取配置文件 + getconfig + #使用内置规则强行覆盖config配置文件 + [ "$modify_yaml" != "已开启" ] && modify_yaml + #使用不同方式启动clash服务 + if [ "$start_old" = "已开启" ];then + $clashdir/clash -d $clashdir> /dev/null & + afstart + elif [ -f /etc/rc.common ];then + /etc/init.d/clash start + else + systemctl start clash.service + fi + ;; +stop) + #删除守护 + checkcron + sed -i /start.sh/d $cronpath + #多种方式结束进程 + if [ -f /etc/rc.common ];then + /etc/init.d/clash stop &> /dev/null + else + systemctl stop clash.service &> /dev/null + fi + killall -9 clash &> /dev/null + #清理iptables + stop_iptables + ;; +restart) + $0 stop + $0 start + ;; +getyaml) + getconfig + getyaml + ;; +esac +exit 0 \ No newline at end of file