~适配本地安装,增加初始化脚本
~增加内置hosts优化开关(读取本机hosts及劫持NTP)
~更新/下载失败时增加换源跳转
~新手引导路由模式自动设置开机启动
~修复因获取配置文件失败导致脚本运行出错的bug
~部分文本描述优化
This commit is contained in:
juewuy
2023-01-13 20:36:52 +08:00
parent 3376b24394
commit 21a5186940
3 changed files with 110 additions and 108 deletions

View File

@@ -1,14 +1,13 @@
#!/bin/sh
# Copyright (C) Juewuy
#读取配置相关
getconfig(){
#服务器缺省地址
[ -z "$update_url" ] && update_url=https://fastly.jsdelivr.net/gh/juewuy/ShellClash
#文件路径
[ -z "$clashdir" ] && echo 环境变量配置有误!请重新安装脚本!
ccfg=$clashdir/mark
yaml=$clashdir/config.yaml
#检查/读取标识文件
[ ! -f $ccfg ] && echo '#标识clash运行状态的文件不明勿动' > $ccfg
#检查重复行并去除
@@ -31,8 +30,9 @@ getconfig(){
[ ! -f $clashdir/mac ] && touch $clashdir/mac
#获取本机host地址
[ -z "$host" ] && 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}';)
[ -z "$host" ] && host=$(ip a 2>&1 | grep -w 'inet' | grep 'global' | grep 'lan' | grep -E ' 1(92|0|72)\.' | sed 's/.*inet.//g' | sed 's/\/[0-9][0-9].*$//g' | head -n 1)
[ -z "$host" ] && host=$(ip a 2>&1 | grep -w 'inet' | grep 'global' | grep -E ' 1(92|0|72)\.' | sed 's/.*inet.//g' | sed 's/\/[0-9][0-9].*$//g' | head -n 1)
[ -z "$host" ] && host=127.0.0.1
[ -z "$host" ] && host='设备IP地址'
#dashboard目录位置
[ -d $clashdir/ui ] && dbdir=$clashdir/ui && hostdir=":$db_port/ui"
[ -d /www/clash ] && dbdir=/www/clash && hostdir=/clash
@@ -115,7 +115,7 @@ startover(){
}
clashstart(){
#检查yaml配置文件
if [ ! -f "$yaml" ];then
if [ ! -f "$clashdir/config.yaml" ];then
echo -----------------------------------------------
echo -e "\033[31m没有找到配置文件请先导入配置文件\033[0m"
source $clashdir/getdate.sh && clashlink
@@ -413,19 +413,21 @@ setport(){
setdns(){
[ -z "$dns_nameserver" ] && dns_nameserver='114.114.114.114, 223.5.5.5'
[ -z "$dns_fallback" ] && dns_fallback='1.0.0.1, 8.8.4.4'
[ -z "$hosts_opt" ] && hosts_opt=已开启
[ -z "$dns_redir" ] && dns_redir=未开启
[ -z "$dns_no" ] && dns_no=未禁用
echo -----------------------------------------------
echo -e "当前基础DNS\033[32m$dns_nameserver\033[0m"
echo -e "fallbackDNS\033[36m$dns_fallback\033[0m"
echo -e "FallbackDNS\033[36m$dns_fallback\033[0m"
echo -e "多个DNS地址请用\033[30;47m“|”\033[0m或者\033[30;47m“, ”\033[0m分隔输入"
echo -e "\033[33m必须拥有本地根证书文件才能使用dot/doh类型的加密dns\033[0m"
echo -----------------------------------------------
echo -e " 1 修改\033[32m基础DNS\033[0m"
echo -e " 2 修改\033[36mfallback_DNS\033[0m"
echo -e " 2 修改\033[36mFallback_DNS\033[0m"
echo -e " 3 \033[33m重置\033[0mDNS配置"
echo -e " 4 一键配置\033[32m加密DNS\033[0m"
echo -e " 6 Dnsmasq转发 \033[36m$dns_redir\033[0m ————用于解决dns劫持失败的问题"
echo -e " 5 hosts优化 \033[36m$hosts_opt\033[0m ————调用本机hosts并劫持NTP服务"
echo -e " 6 Dnsmasq转发 \033[36m$dns_redir\033[0m ————不明勿动"
echo -e " 7 禁用内置DNS \033[36m$dns_no\033[0m ————不明勿动"
echo -e " 0 返回上级菜单"
echo -----------------------------------------------
@@ -479,7 +481,20 @@ setdns(){
rm -rf /tmp/ssl_test
sleep 1
setdns
elif [ "$num" = 5 ]; then
echo -----------------------------------------------
if [ "$hosts_opt" = "已启用" ]; then
hosts_opt=未启用
echo -e "\033[32m已禁用hosts优化功能\033[0m"
else
hosts_opt=已启用
echo -e "\033[33m已启用hosts优化功能\033[0m"
fi
sleep 1
setconfig hosts_opt $hosts_opt
setdns
elif [ "$num" = 6 ]; then
echo -----------------------------------------------
if [ "$dns_redir" = "未开启" ]; then
@@ -1810,6 +1825,15 @@ clashsh(){
fi
}
[ -z "$clashdir" ] && {
echo 环境变量配置有误!正在初始化~~~
clashdir=$(cd `dirname $0`; pwd)
source $clashdir/init.sh
sleep 1
echo 请重启SSH窗口以完成初始化
exit
}
[ -z "$1" ] && clashsh
case "$1" in

View File

@@ -1,6 +1,11 @@
#!/bin/bash
# Copyright (C) Juewuy
error_down(){
echo -e "\033[33m请尝试切换至其他安装源后重新下载\033[0m"
sleep 1
setserver
}
#导入订阅、配置文件相关
linkconfig(){
echo -----------------------------------------------
@@ -325,85 +330,26 @@ clashlink(){
#下载更新相关
gettar(){
$clashdir/start.sh webget /tmp/clashfm.tar.gz $tarurl
[ "$?" != "0" ] && echo "文件下载失败,请尝试使用其他安装源!" && exit 1
$clashdir/start.sh stop 2>/dev/null
#解压
echo -----------------------------------------------
echo 开始解压文件!
mkdir -p $clashdir > /dev/null
tar -zxvf '/tmp/clashfm.tar.gz' -C $clashdir/
[ $? -ne 0 ] && echo "文件解压失败!" && rm -rf /tmp/clashfm.tar.gz && exit 1
#初始化文件目录
[ -f "$clashdir/mark" ] || echo '#标识clash运行状态的文件不明勿动' > $clashdir/mark
#判断系统类型写入不同的启动文件
if [ -f /etc/rc.common ];then
#设为init.d方式启动
cp -f $clashdir/clashservice /etc/init.d/clash
chmod +x /etc/init.d/clash
if [ "$?" != "0" ];then
echo -e "\033[33m文件下载失败\033[0m"
error_down
else
[ -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
$clashdir/start.sh stop 2>/dev/null
#解压
echo -----------------------------------------------
echo 开始解压文件!
mkdir -p $clashdir > /dev/null
tar -zxvf '/tmp/clashfm.tar.gz' -C $clashdir/
if [ $? -ne 0 ];then
rm -rf /tmp/clashfm.tar.gz
echo -e "\033[33m文件解压失败\033[0m"
error_down
else
#设为保守模式启动
setconfig start_old 已开启
fi
echo -e "\033[32m脚本更新成功\033[0m"
fi
fi
#修饰文件及版本号
shtype=sh && command -v bash &>/dev/null && shtype=bash
sed -i "s|/bin/sh|/bin/$shtype|" $clashdir/start.sh
chmod +x $clashdir/start.sh
setconfig versionsh_l $release_new
#设置更新地址
[ -n "$url" ] && setconfig update_url $url
#设置环境变量
[ -w /opt/etc/profile ] && profile=/opt/etc/profile
[ -w /jffs/configs/profile.add ] && profile=/jffs/configs/profile.add
[ -w ~/.bashrc ] && profile=~/.bashrc
[ -w /etc/profile ] && profile=/etc/profile
if [ -n "$profile" ];then
sed -i '/alias clash=*/'d $profile
echo "alias clash=\"$shtype $clashdir/clash.sh\"" >> $profile #设置快捷命令环境变量
sed -i '/export clashdir=*/'d $profile
echo "export clashdir=\"$clashdir\"" >> $profile #设置clash路径环境变量
#适配zsh环境变量
[ -n "$(ls -l /bin/sh|grep -oE 'zsh')" ] && {
echo "alias clash=\"$shtype $clashdir/clash.sh\"" >> ~/.zshrc
echo "export clashdir=\"$clashdir\"" >> ~/.zshrc
}
else
echo 无法写入环境变量!请检查安装权限!
exit 1
fi
#梅林/Padavan额外设置
[ -n "$initdir" ] && {
sed -i '/ShellClash初始化/'d $initdir
touch $initdir
echo "$clashdir/start.sh init #ShellClash初始化脚本" >> $initdir
setconfig initdir $initdir
}
#小米镜像化OpenWrt额外设置
if [ "$systype" = "mi_snapshot" ];then
chmod +x $clashdir/misnap_init.sh
uci set firewall.ShellClash=include
uci set firewall.ShellClash.type='script'
uci set firewall.ShellClash.path='/data/clash/misnap_init.sh'
uci set firewall.ShellClash.enabled='1'
uci commit firewall
setconfig systype $systype
else
rm -rf $clashdir/misnap_init.sh
rm -rf $clashdir/clashservice
fi
#删除临时文件
rm -rf /tmp/clashfm.tar.gz
rm -rf $clashdir/clash.service
exit
}
getsh(){
echo -----------------------------------------------
echo -e "当前脚本版本为:\033[33m $versionsh_l \033[0m"
@@ -422,6 +368,7 @@ getsh(){
exit;
fi
}
getcpucore(){
cputype=$(uname -ms | tr ' ' '_' | tr '[A-Z]' '[a-z]')
[ -n "$(echo $cputype | grep -E "linux.*armv.*")" ] && cpucore="armv5"
@@ -467,12 +414,14 @@ getcore(){
if [ "$?" = "1" ];then
echo -e "\033[31m核心文件下载失败\033[0m"
rm -rf /tmp/clash.new
error_down
else
chmod +x /tmp/clash.new
clashv=$($bindir/clash -v 2>/dev/null | sed 's/ linux.*//;s/.* //')
if [ -z "$clashv" ];then
echo -e "\033[31m核心文件下载失败\033[0m"
rm -rf /tmp/clash.new
error_down
else
echo -e "\033[32m$clashcore核心下载成功\033[0m"
mv -f /tmp/clash.new $bindir/clash
@@ -532,6 +481,7 @@ setcore(){
update
fi
}
getgeo(){
echo -----------------------------------------------
echo 正在从服务器获取数据库文件…………
@@ -539,7 +489,7 @@ getgeo(){
if [ "$?" = "1" ];then
echo -----------------------------------------------
echo -e "\033[31m文件下载失败\033[0m"
exit 1
error_down
else
mv -f /tmp/$geoname $bindir/$geoname
echo -----------------------------------------------
@@ -602,6 +552,7 @@ setgeo(){
update
fi
}
getdb(){
#下载及安装
if [ -d /www/clash -o -d $clashdir/ui ];then
@@ -623,6 +574,7 @@ getdb(){
echo -----------------------------------------------
echo -e "\033[31m文件下载失败\033[0m"
echo -----------------------------------------------
error_down
setdb
else
echo -e "\033[33m下载成功正在解压文件\033[0m"
@@ -719,6 +671,7 @@ setdb(){
errornum
fi
}
getcrt(){
crtlink="${update_url}/bin/ca-certificates.crt"
echo -----------------------------------------------
@@ -727,6 +680,7 @@ getcrt(){
if [ "$?" = "1" ];then
echo -----------------------------------------------
echo -e "\033[31m文件下载失败\033[0m"
error_down
else
echo -----------------------------------------------
mv -f /tmp/ca-certificates.crt $crtdir
@@ -768,6 +722,7 @@ setcrt(){
sleep 1
fi
}
#安装源
setserver(){
saveserver(){
#写入mark文件
@@ -853,6 +808,7 @@ setserver(){
errornum
fi
}
#检查更新
checkupdate(){
if [ -z "$release_new" ];then
if [ -n "$release_url" ];then
@@ -867,7 +823,14 @@ if [ -z "$release_new" ];then
fi
$clashdir/start.sh webget /tmp/clashversion $update_url/bin/version echooff
[ "$?" = "0" ] && release_new=$(cat /tmp/clashversion | grep -oE 'versionsh=.*' | awk -F'=' '{ print $2 }')
[ -n "$release_new" ] && source /tmp/clashversion 2>/dev/null || echo -e "\033[31m检查更新失败请检查网络连接或切换安装源\033[0m"
if [ -n "$release_new" ];then
source /tmp/clashversion 2>/dev/null
else
echo -e "\033[31m检查更新失败请切换其他安装源\033[0m"
echo -e "\033[36m如全部安装源都无法使用请先运行服务后再使用更新\033[0m"
sleep 1
setserver
fi
rm -rf /tmp/clashversion
fi
}
@@ -891,8 +854,10 @@ update(){
echo -e " 6 查看\033[32mPAC\033[0m自动代理配置"
echo -----------------------------------------------
echo -e " 7 切换\033[36m安装源\033[0m及\033[36m安装版本\033[0m"
echo -e " 8 鸣谢"
echo -e " 8 \033[32m重新初始化运行环境\033[0m"
echo -e " 9 \033[31m卸载\033[34mShellClash\033[0m"
echo -----------------------------------------------
echo -e "99 \033[36m鸣谢\033[0m"
echo -e " 0 返回上级菜单"
echo -----------------------------------------------
read -p "请输入对应数字 > " num
@@ -929,21 +894,28 @@ update(){
elif [ "$num" = 7 ]; then
setserver
update
elif [ "$num" = 8 ]; then
echo -----------------------------------------------
echo -e "感谢:\033[32mClash \033[0m作者\033[36m Dreamacro\033[0m 项目地址:\033[32mhttps://github.com/Dreamacro/clash\033[0m"
echo -e "感谢:\033[32msubconverter \033[0m作者\033[36m tindy2013\033[0m 项目地址:\033[32mhttps://github.com/tindy2013/subconverter\033[0m"
echo -e "感谢:\033[32malecthw提供的GeoIP数据库\033[0m 项目地址:\033[32mhttps://github.com/alecthw/mmdb_china_ip_list\033[0m"
echo -e "感谢:\033[32mHackl0us提供的GeoIP精简数据库\033[0m 项目地址:\033[32mhttps://github.com/Hackl0us/GeoIP2-CN\033[0m"
echo -e "感谢:\033[32m17mon提供的CN-IP列表\033[0m 项目地址:\033[32mhttps://github.com/17mon/china_ip_list\033[0m"
echo -e "感谢:\033[32myacd \033[0m作者\033[36m haishanh\033[0m 项目地址:\033[32mhttps://github.com/haishanh/yacd\033[0m"
echo -e "感谢:\033[32m更多的帮助过我的人\033[0m"
sleep 2
update
elif [ "$num" = 8 ]; then
source $clashdir/init.sh
update
elif [ "$num" = 9 ]; then
$0 -u
exit
elif [ "$num" = 99 ]; then
echo -----------------------------------------------
echo -e "感谢:\033[32mClash项目 \033[0m作者\033[36m Dreamacro\033[0m 项目地址:\033[32mhttps://github.com/Dreamacro/clash\033[0m"
echo -e "感谢:\033[32mClash.meta项目 \033[0m作者\033[36m MetaCubeX\033[0m 项目地址:\033[32mhttps://github.com/MetaCubeX/Clash.Meta\033[0m"
echo -e "感谢:\033[32mYACD面板项目 \033[0m作者\033[36m haishanh\033[0m 项目地址:\033[32mhttps://github.com/haishanh/yacd\033[0m"
echo -e "感谢:\033[32mSubconverter \033[0m作者\033[36m tindy2013\033[0m 项目地址:\033[32mhttps://github.com/tindy2013/subconverter\033[0m"
echo -e "感谢:\033[32m由alecthw提供的GeoIP数据库\033[0m 项目地址:\033[32mhttps://github.com/alecthw/mmdb_china_ip_list\033[0m"
echo -e "感谢:\033[32m由Hackl0us提供的GeoIP精简数据库\033[0m 项目地址:\033[32mhttps://github.com/Hackl0us/GeoIP2-CN\033[0m"
echo -e "感谢:\033[32m由17mon提供的CN-IP列表\033[0m 项目地址:\033[32mhttps://github.com/17mon/china_ip_list\033[0m"
echo -e "感谢:\033[32m由ChanthMiao提供的CN-IPV6列表\033[0m 项目地址:\033[32mhttps://github.com/ChanthMiao/China-IPv6-List\033[0m"
echo -----------------------------------------------
echo -e "特别感谢:\033[36m所有帮助及赞助过此项目的同仁们\033[0m"
echo -----------------------------------------------
sleep 2
update
else
errornum
@@ -973,6 +945,11 @@ userguide(){
else
setconfig redir_mod "Redir模式"
fi
#设置开机启动
[ -f /etc/rc.common ] && /etc/init.d/clash enable
command -v systemctl >/dev/null 2>&1 && systemctl enable clash.service > /dev/null 2>&1
rm -rf $clashdir/.dis_startup
autostart=enable
#检测IP转发
if [ "$(cat /proc/sys/net/ipv4/ip_forward)" = "0" ];then
echo -----------------------------------------------

View File

@@ -208,7 +208,7 @@ EOF`
echo -e "\033[32m如担心安全性请在5s内使用【ctrl+c】退出\033[0m"
sleep 5
server_link=6
echo server_link=$server_link >> $ccfg
setconfig server_link 6
Https=""
getyaml
else
@@ -221,7 +221,7 @@ EOF`
server_link=0
fi
server_link=$((server_link+1))
echo server_link=$server_link >> $ccfg
setconfig server_link $server_link
Https=""
getyaml
fi
@@ -379,8 +379,13 @@ store-selected: $restore
EOF
###################################
#读取本机hosts并生成配置文件
if [ "$redir_mod" != "纯净模式" ] && [ "$dns_no" != "已禁用" ] && [ -f /etc/hosts ] && [ -z "$(grep -E '^hosts:' $clashdir/user.yaml 2>/dev/null)" ];then
echo 'hosts:' >> $tmpdir/hosts.yaml
if [ "$hosts_opt" != "未启用" ] && [ -z "$(grep -E '^hosts:' $clashdir/user.yaml 2>/dev/null)" ];then
#NTP劫持
cat >> $tmpdir/hosts.yaml <<EOF
hosts:
'time.android.com': 203.107.6.88
'time.facebook.com': 203.107.6.88
EOF
while read line;do
[ -n "$(echo "$line" | grep -oE "([0-9]{1,3}[\.]){3}" )" ] && \
[ -z "$(echo "$line" | grep -oE '^#')" ] && \
@@ -390,11 +395,6 @@ EOF
echo " '$hosts_domain': $hosts_ip" >> $tmpdir/hosts.yaml
done < /etc/hosts
fi
#NTP劫持
cat >> $tmpdir/hosts.yaml <<EOF
'time.android.com': 203.107.6.88
'time.facebook.com': 203.107.6.88
EOF
#合并文件
[ -f $clashdir/user.yaml ] && yaml_user=$clashdir/user.yaml
[ -f $tmpdir/hosts.yaml ] && yaml_hosts=$tmpdir/hosts.yaml
@@ -1043,6 +1043,7 @@ catpac(){
[ -z "$host_pac" ] && host_pac=$(ip a 2>&1 | grep -w 'inet' | grep 'global' | grep -E ' 1(92|0|72)\.' | sed 's/.*inet.//g' | sed 's/\/[0-9][0-9].*$//g' | head -n 1)
cat > /tmp/clash_pac <<EOF
//如看见此处内容,请重新安装本地面板!
//之后返回上一级页面,清理浏览器缓存并刷新页面!
function FindProxyForURL(url, host) {
if (
isInNet(host, "0.0.0.0", "255.0.0.0")||