v1.0.0beta15.7

~同步premium最新测试版核心,修复tun模式下回环bug
~优化了本机代理实现方式,增加了选项菜单
~优化了定时任务实现方式,适配更多设备
~优化了配置文件修饰方式
~优化了启动和关闭进程时的检测,修复错误
~优化了非root用户体验
~优化脚本结构
This commit is contained in:
juewuy
2020-10-30 16:21:09 +08:00
parent c43a927280
commit 1afe716ce3
14 changed files with 300 additions and 142 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,4 +1,4 @@
clash_v=1.2.0 clash_v=1.2.0
GeoIP_v=20201021 GeoIP_v=20201021
clashpre_v=2020.10.26.gc025a01
versionsh=1.0.0beta15.7 versionsh=1.0.0beta15.7
clashpre_v=2020.10.29.g5e54f48

View File

@@ -12,6 +12,8 @@ getconfig(){
#检查/读取标识文件 #检查/读取标识文件
[ ! -f $ccfg ] && echo '#标识clash运行状态的文件不明勿动' > $ccfg [ ! -f $ccfg ] && echo '#标识clash运行状态的文件不明勿动' > $ccfg
source $ccfg source $ccfg
#设置默认核心资源目录
[ -z "$bindir" ] && bindir=$clashdir
#设置默认端口及变量 #设置默认端口及变量
[ -z "$mix_port" ] && mix_port=7890 [ -z "$mix_port" ] && mix_port=7890
[ -z "$redir_port" ] && redir_port=7892 [ -z "$redir_port" ] && redir_port=7892
@@ -21,12 +23,9 @@ getconfig(){
#检查mac地址记录 #检查mac地址记录
[ ! -f $clashdir/mac ] && touch $clashdir/mac [ ! -f $clashdir/mac ] && touch $clashdir/mac
#获取本机host地址 #获取本机host地址
if [ -f /bin/opkg ];then host=$(ubus call network.interface.lan status 2>&1 | 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}';) [ -z "$host" ] && host=$(ip a 2>&1 | grep -w 'inet' | grep 'global' | grep -E '192.|10.' | sed 's/.*inet.//g' | sed 's/\/[0-9][0-9].*$//g' | head -n 1)
else
host=$(ip a|grep -w 'inet'|grep 'global'|grep -E '192.|10.'|sed 's/.*inet.//g'|sed 's/\/[0-9][0-9].*$//g'|head -n 1)
[ -z "$host" ] && host=127.0.0.1 [ -z "$host" ] && host=127.0.0.1
fi
#dashboard目录位置 #dashboard目录位置
[ -d $clashdir/ui ] && dbdir=$clashdir/ui && hostdir=":$db_port/ui" [ -d $clashdir/ui ] && dbdir=$clashdir/ui && hostdir=":$db_port/ui"
[ -d /www/clash ] && dbdir=/www/clash && hostdir=/clash [ -d /www/clash ] && dbdir=/www/clash && hostdir=/clash
@@ -95,28 +94,6 @@ errornum(){
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[31m请输入正确的数字\033[0m" echo -e "\033[31m请输入正确的数字\033[0m"
} }
catpac(){
cat > /tmp/clash_pac <<EOF
function FindProxyForURL(url, host) {
if (
isInNet(host, "0.0.0.0", "255.0.0.0")||
isInNet(host, "10.0.0.0", "255.0.0.0")||
isInNet(host, "127.0.0.0", "255.0.0.0")||
isInNet(host, "224.0.0.0", "224.0.0.0")||
isInNet(host, "240.0.0.0", "240.0.0.0")||
isInNet(host, "172.16.0.0", "255.240.0.0")||
isInNet(host, "192.168.0.0", "255.255.0.0")||
isInNet(host, "169.254.0.0", "255.255.0.0")
)
return "DIRECT";
else
return "SOCKS5 $host:$mix_port; PROXY $host:$mix_port; DIRECT;"
}
EOF
[ ! -d $clashdir/ui ] && mkdir -p $clashdir/ui
cmp -s /tmp/clash_pac $clashdir/ui/pac
[ "$?" = 0 ] && rm -rf /tmp/clash_pac || mv -f /tmp/clash_pac $clashdir/ui/pac
}
startover(){ startover(){
echo -e "\033[32mclash服务已启动\033[0m" echo -e "\033[32mclash服务已启动\033[0m"
if [ -n "$hostdir" ];then if [ -n "$hostdir" ];then
@@ -133,25 +110,12 @@ startover(){
fi fi
} }
clashstart(){ clashstart(){
#检查clash核心
if [ ! -f $clashdir/clash ];then
echo -----------------------------------------------
echo -e "\033[31m没有找到核心文件请先下载clash核心\033[0m"
source $clashdir/getdate.sh && checkupdate && getcore
fi
#检查GeoIP数据库
if [ ! -f $clashdir/Country.mmdb ];then
echo -----------------------------------------------
echo -e "\033[31m没有找到GeoIP数据库文件请下载数据库文件\033[0m"
source $clashdir/getdate.sh && checkupdate && getgeo
fi
#检查yaml配置文件 #检查yaml配置文件
if [ ! -f "$yaml" ];then if [ ! -f "$yaml" ];then
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[31m没有找到配置文件请先导入配置文件\033[0m" echo -e "\033[31m没有找到配置文件请先导入配置文件\033[0m"
clashlink clashlink
fi fi
catpac #生成pac自动代理文件
echo ----------------------------------------------- echo -----------------------------------------------
$clashdir/start.sh start $clashdir/start.sh start
sleep 1 sleep 1
@@ -699,6 +663,7 @@ clashadv(){
[ -z "$ipv6_support" ] && ipv6_support=未开启 [ -z "$ipv6_support" ] && ipv6_support=未开启
[ -z "$start_old" ] && start_old=未开启 [ -z "$start_old" ] && start_old=未开启
[ -z "$tproxy_mod" ] && tproxy_mod=未开启 [ -z "$tproxy_mod" ] && tproxy_mod=未开启
[ "$bindir" = "/tmp/clash_$USER" ] && mini_clash=已开启 || mini_clash=未开启
# #
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[30;47m欢迎使用进阶模式菜单\033[0m" echo -e "\033[30;47m欢迎使用进阶模式菜单\033[0m"
@@ -709,8 +674,9 @@ clashadv(){
echo -e " 2 启用ipv6支持: \033[36m$ipv6_support\033[0m ————实验性功能,可能不稳定" echo -e " 2 启用ipv6支持: \033[36m$ipv6_support\033[0m ————实验性功能,可能不稳定"
echo -e " 3 使用保守方式启动: \033[36m$start_old\033[0m ————切换时会停止clash服务" 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 " 4 Redir模式udp转发: \033[36m$tproxy_mod\033[0m ————依赖iptables-mod-tproxy"
echo -e " 5 配置内置DNS服务: \033[36m$dns_no\033[0m" echo -e " 5 启用小闪存模式: \033[36m$mini_clash\033[0m ————启动时方下载核心及数据库文件"
echo -e " 6 手动指定clash运行端口及秘钥" echo -e " 6 配置内置DNS服务: \033[36m$dns_no\033[0m"
echo -e " 7 手动指定clash运行端口及秘钥"
echo ----------------------------------------------- echo -----------------------------------------------
echo -e " 8 \033[31m重置\033[0m配置文件" echo -e " 8 \033[31m重置\033[0m配置文件"
echo -e " 9 \033[32m重启\033[0mclash服务" echo -e " 9 \033[32m重启\033[0mclash服务"
@@ -793,10 +759,37 @@ clashadv(){
clashadv clashadv
elif [ "$num" = 5 ]; then elif [ "$num" = 5 ]; then
setdns echo -----------------------------------------------
dir_size=$(df $clashdir | awk '{print $4}' | sed 1d)
if [ "$mini_clash" = "未开启" ]; then
if [ "$dir_size" -gt 20480 ];then
echo -e "\033[33m您的设备空间充足(>20M),无需开启!\033[0m"
elif pidof systemd >/dev/null 2>&1;then
echo -e "\033[33m该设备不支持开启此模式\033[0m"
else
bindir="/tmp/clash_$USER"
echo -e "\033[32m已经启用小闪存功能\033[0m"
echo -e "核心及数据库文件将存储在内存中执行,并在每次开机运行后自动下载\033[0m"
fi
else
if [ "$dir_size" -lt 8192 ];then
echo -e "\033[31m您的设备剩余空间不足8M停用后可能无法正常运行\033[0m"
read -p "确认停用此功能?(1/0) > " res
[ "$res" = 1 ] && bindir="$clashdir" && echo -e "\033[33m已经停用小闪存功能\033[0m"
else
bindir="$clashdir"
echo -e "\033[33m已经停用小闪存功能\033[0m"
fi
fi
setconfig bindir $bindir
sleep 1
clashadv clashadv
elif [ "$num" = 6 ]; then elif [ "$num" = 6 ]; then
setdns
clashadv
elif [ "$num" = 7 ]; then
setport setport
clashadv clashadv

View File

@@ -5,15 +5,20 @@ START=92
SERVICE_DAEMONIZE=1 SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1 SERVICE_WRITE_PID=1
USE_PROCD=1 USE_PROCD=1
DIR=$(cat /etc/profile|grep clashdir|awk -F "\"" '{print $2}') #获取目录
DIR=$(cat /etc/profile | grep clashdir | awk -F "\"" '{print $2}')
BINDIR=$(cat $DIR/mark | grep bindir | awk -F "=" '{print $2}')
[ -z "$BINDIR" ] && BINDIR=$DIR
start_service() { start_service() {
#检测必须文件
$DIR/start.sh bfstart
#使用procd创建clash后台进程 #使用procd创建clash后台进程
procd_open_instance procd_open_instance
procd_set_param respawn procd_set_param respawn
procd_set_param stderr 1 procd_set_param stderr 1
procd_set_param stdout 1 procd_set_param stdout 1
procd_set_param command $DIR/clash -d $DIR procd_set_param command $BINDIR/clash -d $BINDIR
procd_close_instance procd_close_instance
#其他设置 #其他设置
$DIR/start.sh afstart $DIR/start.sh afstart
@@ -21,8 +26,10 @@ start_service() {
start() { start() {
if [ -z "$(pidof procd)" ];then if [ -z "$(pidof procd)" ];then
#检测必须文件
$DIR/start.sh bfstart
#创建后台进程 #创建后台进程
service_start $DIR/clash -d $DIR service_start $BINDIR/clash -d $BINDIR
#其他设置 #其他设置
$DIR/start.sh afstart $DIR/start.sh afstart
#设置守护进程 #设置守护进程

View File

@@ -443,11 +443,7 @@ getsh(){
update update
fi fi
} }
getcore(){ getcpucore(){
#获取核心及版本信息
[ ! -f $clashdir/clash ] && clashcore="未安装核心"
#获取设备处理器架构
getcputype(){
cputype=$(uname -ms | tr ' ' '_' | tr '[A-Z]' '[a-z]') cputype=$(uname -ms | tr ' ' '_' | tr '[A-Z]' '[a-z]')
[ -n "$(echo $cputype | grep -E "linux.*armv.*")" ] && cpucore="armv5" [ -n "$(echo $cputype | grep -E "linux.*armv.*")" ] && cpucore="armv5"
[ -n "$(echo $cputype | grep -E "linux.*armv7.*")" ] && [ -n "$(cat /proc/cpuinfo | grep vfp)" ] && cpucore="armv7" [ -n "$(echo $cputype | grep -E "linux.*armv7.*")" ] && [ -n "$(cat /proc/cpuinfo | grep vfp)" ] && cpucore="armv7"
@@ -456,20 +452,33 @@ getcore(){
[ -n "$(echo $cputype | grep -E "linux.*86_64.*")" ] && cpucore="amd64" [ -n "$(echo $cputype | grep -E "linux.*86_64.*")" ] && cpucore="amd64"
if [ -n "$(echo $cputype | grep -E "linux.*mips.*")" ];then if [ -n "$(echo $cputype | grep -E "linux.*mips.*")" ];then
mipstype=$(echo -n I | hexdump -o | awk '{ print substr($2,6,1); exit}') #通过判断大小端判断mips或mipsle mipstype=$(echo -n I | hexdump -o | awk '{ print substr($2,6,1); exit}') #通过判断大小端判断mips或mipsle
if [ "$mipstype" = "1" ];then [ "$mipstype" = "1" ] && cpucore="mipsle-softfloat" || cpucore="mips-softfloat"
cpucore="mipsle-softfloat" fi
#[ -n "$(uname -a | grep -E "M2100")" ] && cpucore="mipsle-hardfloat" [ -n "$cpucore" ] && setconfig cpucore $cpucore
}
getcore(){
[ -z "$clashcore" ] && clashcore=clashpre
[ -z "$cpucore" ] && getcpucore
#生成链接
corelink="$update_url/bin/$clashcore/clash-linux-$cpucore"
#获取在线clash核心文件
webget /tmp/clash.new $corelink
if [ "$result" != "200" ];then
exit 1
else else
cpucore="mips-softfloat" mv -f /tmp/clash.new $bindir/clash
chmod 777 $bindir/clash #授予权限
fi fi
fi }
} setcore(){
#获取核心及版本信息
[ ! -f $clashdir/clash ] && clashcore="未安装核心"
### ###
echo ----------------------------------------------- echo -----------------------------------------------
[ -z "$cpucore" ] && getcputype [ -z "$cpucore" ] && getcpucore
echo -e "当前clash核心\033[47;30m $clashcore \033[46;30m$clashv\033[0m" echo -e "当前clash核心\033[47;30m $clashcore \033[46;30m$clashv\033[0m"
echo -e "当前系统处理器架构:\033[32m $cpucore \033[0m" echo -e "当前系统处理器架构:\033[32m $cpucore \033[0m"
echo -e "\033[33m请选择需要下载的核心版本!\033[0m" echo -e "\033[33m请选择需要使用的核心版本!\033[0m"
echo ----------------------------------------------- echo -----------------------------------------------
echo "1 clash 稳定,内存占用小,推荐!" echo "1 clash 稳定,内存占用小,推荐!"
echo "(官方正式版) 不支持Tun模式、混合模式" echo "(官方正式版) 不支持Tun模式、混合模式"
@@ -493,7 +502,7 @@ getcore(){
clashcore=clashpre clashcore=clashpre
version=$clashpre_v version=$clashpre_v
elif [ "$num" = 3 ]; then elif [ "$num" = 3 ]; then
cpucore_list="armv5 armv7 armv8 386 amd64 mipsle-softfloat mipsle-hardfloat mips-softfloat" cpucore_list="armv5 armv7 armv8 386 amd64 \nmipsle-softfloat mipsle-hardfloat mips-softfloat"
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[31m仅适合脚本无法正确识别核心或核心无法正常运行时使用\033[0m" echo -e "\033[31m仅适合脚本无法正确识别核心或核心无法正常运行时使用\033[0m"
echo -e "当前可供在线下载的处理器架构为:" echo -e "当前可供在线下载的处理器架构为:"
@@ -506,42 +515,34 @@ getcore(){
echo -e "\033[31m请输入正确的处理器架构\033[0m" echo -e "\033[31m请输入正确的处理器架构\033[0m"
sleep 1 sleep 1
cpucore="" cpucore=""
else
setconfig cpucore $cpucore
fi fi
getcore setcore
else else
errornum errornum
update update
fi fi
#生成链接
corelink="$update_url/bin/$clashcore/clash-linux-$cpucore"
echo -----------------------------------------------
echo 正在连接服务器获取clash核心文件…………链接地址为
echo -e "\033[4;32m$corelink\033[0m"
echo 如无法正常下载可以手动复制到浏览器下载核心文件!
echo -e "\033[36m-----------------------------------------------"
echo -e "| |"
echo -e "| 需要一点时间,请耐心等待! |"
echo -e "| \033[0m如长时间没有数据请用ctrl+c退出 |"
echo -e "-----------------------------------------------\033[0m"
#获取在线clash核心文件
webget /tmp/clash.new $corelink
if [ "$result" != "200" ];then
echo -----------------------------------------------
echo -e "\033[31m核心文件下载失败\033[0m"
echo ----------------------------------------------- echo -----------------------------------------------
echo 在线获取clash核心文件……
getcore getcore
else if [ "$?" = 0 ];then
echo -e "\033[32m$clashcore核心下载成功,正在替换\033[0m" echo -e "\033[32m$clashcore核心下载成功\033[0m"
mv -f /tmp/clash.new $clashdir/clash
chmod 777 $clashdir/clash #授予权限
setconfig clashcore $clashcore setconfig clashcore $clashcore
setconfig clashv $version setconfig clashv $version
rm -rf /tmp/clashversion else
echo ----------------------------------------------- echo -e "\033[31m核心文件下载失败\033[0m"
echo -e "\033[32m$clashcore核心安装成功\033[0m"
fi fi
} }
getgeo(){ getgeo(){
webget /tmp/Country.mmdb $update_url/bin/Country.mmdb
if [ "$result" != "200" ];then
exit 1
else
mv -f /tmp/Country.mmdb $bindir/Country.mmdb
fi
}
setgeo(){
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "当前GeoIP版本为\033[33m $Geo_v \033[0m" echo -e "当前GeoIP版本为\033[33m $Geo_v \033[0m"
echo -e "最新GeoIP版本为\033[32m $GeoIP_v \033[0m" echo -e "最新GeoIP版本为\033[32m $GeoIP_v \033[0m"
@@ -550,18 +551,14 @@ getgeo(){
if [ "$res" = '1' ]; then if [ "$res" = '1' ]; then
echo ----------------------------------------------- echo -----------------------------------------------
echo 正在从服务器获取数据库文件………… echo 正在从服务器获取数据库文件…………
webget /tmp/Country.mmdb $update_url/bin/Country.mmdb getgeo
if [ "$result" != "200" ];then if [ "$?" != 0 ];then
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[31m文件下载失败\033[0m" echo -e "\033[31m文件下载失败\033[0m"
echo -----------------------------------------------
getgeo
else else
echo ----------------------------------------------- echo -----------------------------------------------
echo -e "\033[32mGeoIP数据库文件下载成功\033[0m" echo -e "\033[32mGeoIP数据库文件下载成功\033[0m"
mv -f /tmp/Country.mmdb $clashdir/Country.mmdb
setconfig Geo_v $GeoIP_v setconfig Geo_v $GeoIP_v
rm -rf /tmp/clashversion
fi fi
else else
update update
@@ -651,8 +648,8 @@ getdb(){
[ $? -ne 0 ] && echo "文件解压失败!" && rm -rf /tmp/clashfm.tar.gz && exit 1 [ $? -ne 0 ] && echo "文件解压失败!" && rm -rf /tmp/clashfm.tar.gz && exit 1
#修改默认host和端口 #修改默认host和端口
if [ "$db_type" = "clashdb" ];then if [ "$db_type" = "clashdb" ];then
sed -i "s/127.0.0.1/${host}/g" $dbdir/js/*.js sed -i "s/127.0.0.1/${host}/g" $dbdir/static/js/*.js
sed -i "s/9090/${db_port}/g" $dbdir/js/*.js sed -i "s/9090/${db_port}/g" $dbdir/static/js/*.js
else else
sed -i "s/127.0.0.1/${host}/g" $dbdir/app*.js sed -i "s/127.0.0.1/${host}/g" $dbdir/app*.js
sed -i "s/7892/${db_port}/g" $dbdir/app*.js sed -i "s/7892/${db_port}/g" $dbdir/app*.js
@@ -811,12 +808,74 @@ exit;
} }
#新手引导 #新手引导
userguide(){ userguide(){
echo 欢迎使用ShellClash新手引导
whichmod(){
echo -----------------------------------------------
echo -e "\033[36m 是否需要代理UDP(主要用于游戏) \033[0m"
echo -e "\033[0m你之后依然可以在设置中更改各种配置\033[0m"
echo -e " 1 \033[36m不需要代理UDP流量\033[0m"
modinfo tun >/dev/null 2>&1 && [ "$?" = 0 ] && \
echo -e " 2 \033[36m使用Tun虚拟网卡代理UDP流量(更低的延迟但更多的CPU消耗)\033[0m" || \
echo -e " x \033[36m(你的设备不支持此模式,如为虚拟机运行请调整虚拟网络)\033[0m"
[ -n "$(iptables -j TPROXY 2>&1 | grep 'on-port')" ] && \
echo -e " 3 \033[36m使用Tproxy模式代理UDP流量(较低CPU消耗但更高的延迟)033[0m"
echo -----------------------------------------------
read -p "请输入对应数字 > " num
if [ -z "$num" ] || [ "$num" -gt 4 ];then
errornum
whichmod
elif [ "$num" = 1 ];then
setconfig redir_mod "Redir模式"
setconfig clashcore "clash"
elif [ "$num" = 2 ];then
setconfig redir_mod "混合模式"
setconfig clashcore "clashpre"
elif [ "$num" = 3 ];then
setconfig redir_mod "Redir模式"
setconfig clashcore "clash"
setconfig tproxy_mod "已开启"
fi
}
forwhat(){
echo -----------------------------------------------
echo -e "\033[30;46m 欢迎使用ShellClash新手引导 \033[0m"
echo -----------------------------------------------
echo -e "\033[33m 请选择你的使用环境! \033[0m"
echo -e " 1 \033[32m各类路由设备\033[0m配置局域网透明路由"
echo -e " 2 \033[36mLinux系统带GUI桌面\033[0m配置本机路由"
echo -e " 3 \033[32m服务器Linux系统\033[0m配置本机路由"
echo -e " 4 \033[36m多功能设备\033[0m配置本机及局域网路由"
echo -----------------------------------------------
read -p "请输入对应数字 > " num
if [ -z "$num" ] || [ "$num" -gt 4 ];then
errornum
forwhat
elif [ "$num" = 1 ];then
whichmod
elif [ "$num" = 2 -o "$num" = 3 ];then
setconfig redir_mod "纯净模式"
setconfig clashcore "clash"
echo -e "\033[36m请选择设置本机代理的方式\033[0m"
localproxy
elif [ "$num" = 4 ];then
whichmod
fi
}
checkupdate checkupdate
getcore forwhat
getgeo dir_size=$(df $clashdir | awk '{print $4}' | sed 1d)
getdb if [ "$dir_size" -lt 10240 ];then
clashlink echo -e "\033[33m检测到你的安装目录空间不足10M是否开启小闪存模式\033[0m"
echo -e "\033[33m开启后核心及数据库文件将被下载到内存中\033[0m"
read -p "是否开启?(1/0) > " res
[ "$res" = 1 ] && setconfig bindir="/tmp/clash_$USER"
fi
read -p "需要安装本地Dashboard面板吗(1/0) > " res
[ "$res" = 1 ] && getdb
read -p "现在导入订阅或者配置文件链接?(1/0) > " res
[ "$res" = 1 ] && clashlink
} }
#测试菜单 #测试菜单
testcommand(){ testcommand(){
@@ -824,7 +883,7 @@ testcommand(){
echo -e "\033[30;47m这里是测试命令菜单\033[0m" echo -e "\033[30;47m这里是测试命令菜单\033[0m"
echo -e "\033[33m如遇问题尽量运行相应命令后截图发群\033[0m" echo -e "\033[33m如遇问题尽量运行相应命令后截图发群\033[0m"
echo -e "磁盘占用/所在目录:" echo -e "磁盘占用/所在目录:"
du -h $clashdir du -sh $clashdir
echo ----------------------------------------------- echo -----------------------------------------------
echo " 1 查看clash运行时的报错信息" echo " 1 查看clash运行时的报错信息"
echo " 2 查看系统DNS端口(:53)占用 " echo " 2 查看系统DNS端口(:53)占用 "

View File

@@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# Copyright (C) Juewuy # Copyright (C) Juewuy
#相关工具 #脚本内部工具
getconfig(){ getconfig(){
#加载配置文件 #加载配置文件
[ -z "$clashdir" ] && source /etc/profile > /dev/null [ -z "$clashdir" ] && source /etc/profile > /dev/null
@@ -9,6 +9,7 @@ getconfig(){
ccfg=$clashdir/mark ccfg=$clashdir/mark
[ -f $ccfg ] && source $ccfg [ -f $ccfg ] && source $ccfg
#默认设置 #默认设置
[ -z "$bindir" ] && bindir=$clashdir
[ -z "$redir_mod" ] && [ "$USER" = "root" -o "$USER" = "admin" ] && redir_mod=Redir模式 [ -z "$redir_mod" ] && [ "$USER" = "root" -o "$USER" = "admin" ] && redir_mod=Redir模式
[ -z "$redir_mod" ] && redir_mod=纯净模式 [ -z "$redir_mod" ] && redir_mod=纯净模式
[ -z "$skip_cert" ] && skip_cert=已开启 [ -z "$skip_cert" ] && skip_cert=已开启
@@ -28,8 +29,31 @@ 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 '
} }
setconfig(){
#参数1代表变量名参数2代表变量值,参数3即文件路径
[ -z "$3" ] && configpath=$clashdir/mark || configpath=$3
sed -i "/${1}*/"d $configpath
echo "${1}=${2}" >> $configpath
}
webget(){
[ -n "$(pidof clash)" ] && export all_proxy="http://127.0.0.1:$mix_port" #设置临时http代理
#参数【$1】代表下载目录【$2】代表在线地址
#参数【$3】代表输出显示【$4】不启用重定向
if curl --version > /dev/null 2>&1;then
[ "$3" = "echooff" ] && progress='-s' || progress='-#'
[ -z "$4" ] && redirect='-L' || redirect=''
result=$(curl -w %{http_code} --connect-timeout 5 $progress $redirect -ko $1 $2)
else
[ "$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
export all_proxy=''
}
logger(){ logger(){
[ -z "$2" ] && echo -e "\033[31m$1\033[0m" [ -n "$2" ] && echo -e "\033[$2m$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 '1,5d' $clashdir/log [ "$(wc -l $clashdir/log | awk '{print $1}')" -gt 30 ] && sed -i '1,5d' $clashdir/log
} }
@@ -84,32 +108,27 @@ EOF`
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 "| |"
echo -e "| 需要一点时间,请耐心等待! |"
echo -e "| \033[0m如长时间没有数据请用ctrl+c退出\033[36m |"
echo -e "-----------------------------------------------\033[0m"
#获取在线yaml文件 #获取在线yaml文件
yaml=$clashdir/config.yaml yaml=$clashdir/config.yaml
yamlnew=/tmp/clash_config_$USER.yaml yamlnew=/tmp/clash_config_$USER.yaml
rm -rf $yamlnew rm -rf $yamlnew
source $clashdir/getdate.sh && webget $yamlnew $Https webget $yamlnew $Https
if [ "$result" != "200" ];then if [ "$result" != "200" ];then
if [ -z "$markhttp" ];then if [ -z "$markhttp" ];then
echo ----------------------------------------------- echo -----------------------------------------------
logger "配置文件获取失败!" logger "配置文件获取失败!" 31
echo -e "\033[31m请尝试使用【导入订阅】功能\033[0m" echo -e "\033[31m请尝试使用【导入订阅】功能\033[0m"
echo ----------------------------------------------- echo -----------------------------------------------
exit 1 exit 1
else else
if [ "$retry" -ge 5 ];then if [ "$retry" -ge 5 ];then
logger "无法获取配置文件,请检查链接格式以及网络连接状态!" logger "无法获取配置文件,请检查链接格式以及网络连接状态!" 31
exit 1 exit 1
else else
retry=$((retry+1)) retry=$((retry+1))
logger "配置文件获取失败!" logger "配置文件获取失败!" 31
echo -e "\033[32m尝试使用其他服务器获取配置\033[0m" echo -e "\033[32m尝试使用其他服务器获取配置\033[0m"
logger "正在重试第$retry次/共5次" logger "正在重试第$retry次/共5次" 32
sed -i '/server_link=*/'d $ccfg sed -i '/server_link=*/'d $ccfg
if [ "$server_link" -ge 5 ]; then if [ "$server_link" -ge 5 ]; then
server_link=0 server_link=0
@@ -125,7 +144,7 @@ EOF`
#检测节点 #检测节点
if [ -z "$(cat $yamlnew | grep 'server:' | grep -v 'nameserver')" ];then if [ -z "$(cat $yamlnew | grep 'server:' | grep -v 'nameserver')" ];then
echo ----------------------------------------------- echo -----------------------------------------------
logger "获取到了配置文件,但似乎并不包含正确的节点信息!" logger "获取到了配置文件,但似乎并不包含正确的节点信息!" 31
echo ----------------------------------------------- echo -----------------------------------------------
sed -n '1,30p' $yamlnew sed -n '1,30p' $yamlnew
echo ----------------------------------------------- echo -----------------------------------------------
@@ -136,7 +155,7 @@ EOF`
#检测旧格式 #检测旧格式
if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then if cat $yamlnew | grep 'Proxy Group:' >/dev/null;then
echo ----------------------------------------------- echo -----------------------------------------------
logger "已经停止对旧格式配置文件的支持!!!" logger "已经停止对旧格式配置文件的支持!!!" 31
echo -e "请使用新格式或者使用【导入节点/链接】功能!" echo -e "请使用新格式或者使用【导入节点/链接】功能!"
echo ----------------------------------------------- echo -----------------------------------------------
exit 1 exit 1
@@ -144,7 +163,7 @@ EOF`
#检测不支持的加密协议 #检测不支持的加密协议
if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then if cat $yamlnew | grep 'cipher: chacha20,' >/dev/null;then
echo ----------------------------------------------- echo -----------------------------------------------
logger "不支持chacha20加密请更换节点加密协议" logger "不支持chacha20加密请更换节点加密协议" 31
echo ----------------------------------------------- echo -----------------------------------------------
exit 1 exit 1
fi fi
@@ -160,13 +179,15 @@ EOF`
$0 stop $0 stop
$0 start $0 start
if [ "$?" = 0 ];then if [ "$?" = 0 ];then
logger "配置文件获取成功clash服务已启动" echooff logger "配置文件获取成功clash服务已启动"
exit 0
else else
if [ -f $yaml.bak ];then if [ -f $yaml.bak ];then
$0 stop $0 stop
mv -f $yaml.bak $yaml mv -f $yaml.bak $yaml
$0 start $0 start
[ "$?" = 0 ] && logger "已还原配置文件并重启clash" && exit 0 [ "$?" = 0 ] && logger "已还原配置文件并重启clash" 32 && exit 0
logger "已还原配置文件但依然无法启动clash" 31 && exit 1
fi fi
fi fi
fi fi
@@ -189,18 +210,19 @@ modify_yaml(){
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}}' 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 fi
################################### ###################################
#设置目录
yaml=$clashdir/config.yaml yaml=$clashdir/config.yaml
tmp_clash=/tmp/clash_$USER tmpdir=/tmp/clash_$USER
#预删除需要添加的项目 #预删除需要添加的项目
a=$(grep -n "port:" $yaml | head -1 | cut -d ":" -f 1) a=$(grep -n "port:" $yaml | head -1 | cut -d ":" -f 1)
b=$(grep -n "^prox" $yaml | head -1 | cut -d ":" -f 1) b=$(grep -n "^prox" $yaml | head -1 | cut -d ":" -f 1)
b=$((b-1)) b=$((b-1))
mkdir -p $tmp_clash > /dev/null mkdir -p $tmpdir > /dev/null
sed "${a},${b}d" $yaml > $tmp_clash/rule.yaml sed "${a},${b}d" $yaml > $tmpdir/rule.yaml
#跳过本地tls证书验证 #跳过本地tls证书验证
[ "$skip_cert" = "已开启" ] && sed -i '10,99s/skip-cert-verify: false/skip-cert-verify: true/' $tmp_clash/rule.yaml [ "$skip_cert" = "已开启" ] && sed -i '10,99s/skip-cert-verify: false/skip-cert-verify: true/' $tmpdir/rule.yaml
#添加配置 #添加配置
cat > $tmp_clash/set.yaml <<EOF cat > $tmpdir/set.yaml <<EOF
mixed-port: $mix_port mixed-port: $mix_port
redir-port: $redir_port redir-port: $redir_port
authentication: ["$authentication"] authentication: ["$authentication"]
@@ -215,11 +237,13 @@ $tun
$exper $exper
$dns $dns
EOF EOF
cat $tmp_clash/set.yaml $tmp_clash/rule.yaml > $tmp_clash/config.yaml cat $tmpdir/set.yaml $tmpdir/rule.yaml > $tmpdir/config.yaml
cmp -s $tmp_clash/config.yaml $yaml if [ "$tmpdir" != "$bindir" ];then #如果没有使用小闪存模式
[ "$?" != 0 ] && mv -f $tmp_clash/config.yaml $yaml || rm -f $tmp_clash/config.yaml cmp -s $tmpdir/config.yaml $yaml
rm -f $tmp_clash/set.yaml [ "$?" != 0 ] && mv -f $tmpdir/config.yaml $yaml || rm -f $tmpdir/config.yaml
rm -f $tmp_clash/rule.yaml fi
rm -f $tmpdir/set.yaml
rm -f $tmpdir/rule.yaml
} }
#设置路由规则 #设置路由规则
start_redir(){ start_redir(){
@@ -327,7 +351,7 @@ web_save(){
elif [ -n "$(wget --help 2>&1|grep '\-\-method')" ];then elif [ -n "$(wget --help 2>&1|grep '\-\-method')" ];then
wget -q --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" -O - "$1" wget -q --header="Authorization: Bearer ${secret}" --header="Content-Type:application/json" -O - "$1"
else else
logger 当前系统未安装curl且wget的版本太低无法保存节点配置 logger 当前系统未安装curl且wget的版本太低无法保存节点配置 31
getconfig getconfig
cronset '保存节点配置' cronset '保存节点配置'
fi fi
@@ -369,10 +393,76 @@ web_restore(){
exit 0 exit 0
} }
#启动相关 #启动相关
catpac(){
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}';)
[ -z "$host" ] && host=$(ip a|grep -w 'inet'|grep 'global'|grep -E '192.|10.'|sed 's/.*inet.//g'|sed 's/\/[0-9][0-9].*$//g'|head -n 1)
[ -z "$host" ] && host=127.0.0.1
cat > /tmp/clash_pac <<EOF
function FindProxyForURL(url, host) {
if (
isInNet(host, "0.0.0.0", "255.0.0.0")||
isInNet(host, "10.0.0.0", "255.0.0.0")||
isInNet(host, "127.0.0.0", "255.0.0.0")||
isInNet(host, "224.0.0.0", "224.0.0.0")||
isInNet(host, "240.0.0.0", "240.0.0.0")||
isInNet(host, "172.16.0.0", "255.240.0.0")||
isInNet(host, "192.168.0.0", "255.255.0.0")||
isInNet(host, "169.254.0.0", "255.255.0.0")
)
return "DIRECT";
else
return "SOCKS5 $host:$mix_port; PROXY $host:$mix_port; DIRECT;"
}
EOF
cmp -s /tmp/clash_pac $bindir/ui/pac
[ "$?" = 0 ] && rm -rf /tmp/clash_pac || mv -f /tmp/clash_pac $bindir/ui/pac
}
bfstart(){
[ ! -d $bindir/ui ] && mkdir -p $bindir/ui
[ -z "$update_url" ] && update_url=https://cdn.jsdelivr.net/gh/juewuy/ShellClash
#检查clash核心
if [ ! -f $bindir/clash ];then
if [ -f $clashdir/clash ];then
mv $clashdir/clash $bindir/clash && chmod 777 $bindir/clash
else
logger "未找到clash核心正在下载" 33
[ -z "$clashcore" ] && clashcore=clash
[ -z "$cpucore" ] && source $clashdir/getdate.sh && getcpucore
webget $bindir/clash "$update_url/bin/$clashcore/clash-linux-$cpucore"
[ "$?" = 1 ] && logger "核心下载失败,已退出!" 31 && rm -f $bindir/clash && exit 1
fi
fi
#检查数据库文件
if [ ! -f $bindir/Country.mmdb ];then
if [ -f $clashdir/Country.mmdb ];then
mv $clashdir/Country.mmdb $bindir/Country.mmdb
else
logger "未找到GeoIP数据库正在下载" 33
webget /tmp/Country.mmdb $update_url/bin/Country.mmdb
[ "$?" = 1 ] && logger "数据库下载失败,已退出!" 31 && rm -f $bindir/Country.mmdb && exit 1
fi
fi
#检查dashboard文件
if [ -f $clashdir/ui/index.html -a ! -f $bindir/ui/index.html ];then
cp -rf $clashdir/ui $bindir
fi
catpac #生成pac文件
#检查yaml配置文件
if [ ! -f $clashdir/config.yaml ];then
if [ -n "$Url" -o -n "$Https" ];then
logger "未找到配置文件,正在下载!" 33
getyaml
exit 0
else
logger "未找到配置文件链接,请先导入配置文件!" 31
exit 1
fi
fi
}
afstart(){ afstart(){
#读取配置文件 #读取配置文件
getconfig getconfig
$clashdir/clash -t -d $clashdir >/dev/null $bindir/clash -t -d $bindir >/dev/null
if [ "$?" = 0 ];then if [ "$?" = 0 ];then
#修改iptables规则使流量进入clash #修改iptables规则使流量进入clash
[ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && start_dns [ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && start_dns
@@ -386,27 +476,33 @@ afstart(){
cronset '#每10分钟保存节点配置' "*/10 * * * * test -n \"$(pidof clash)\" && $clashdir/start.sh web_save #每10分钟保存节点配置" cronset '#每10分钟保存节点配置' "*/10 * * * * test -n \"$(pidof clash)\" && $clashdir/start.sh web_save #每10分钟保存节点配置"
[ -f $clashdir/web_save ] && web_restore & #后台还原面板配置 [ -f $clashdir/web_save ] && web_restore & #后台还原面板配置
else else
logger "clash服务启动失败请查看报错信息" logger "clash服务启动失败请查看报错信息" 31
logger `$clashdir/clash -t -d $clashdir 1>&0` logger `$bindir/clash -t -d $bindir 1>&0` 0
$0 stop & $0 stop &
exit 1 exit 1
fi fi
exit 0 exit 0
} }
start_old(){ start_old(){
$clashdir/clash -d $clashdir >/dev/null & #使用传统后台执行二进制文件的方式执行
$bindir/clash -d $bindir >/dev/null &
afstart afstart
daemon daemon
} }
case "$1" in case "$1" in
bfstart)
bfstart
;;
afstart) afstart)
afstart afstart
;; ;;
start) start)
[ -n "$(pidof clash)" ] && $0 stop #禁止多实例 [ -n "$(pidof clash)" ] && $0 stop #禁止多实例
getconfig getconfig
#检测必须文件并下载
bfstart
#使用内置规则强行覆盖config配置文件 #使用内置规则强行覆盖config配置文件
[ "$modify_yaml" != "已开启" ] && modify_yaml [ "$modify_yaml" != "已开启" ] && modify_yaml
#使用不同方式启动clash服务 #使用不同方式启动clash服务
@@ -444,6 +540,9 @@ getyaml)
getconfig getconfig
getyaml getyaml
;; ;;
webget)
webget $1 $2 $3 $4
;;
web_save) web_save)
getconfig getconfig
web_save web_save