~配置文件覆写代码优化
~在线配置生成后端部分地址替换
~新手引导增加自动配置ipv6路由
~屏蔽QUIC功能增加ipv6支持
~修复others.yaml不会被正确读取的bug
~修复局域网过滤功能不会提示重启的bug
This commit is contained in:
juewuy
2023-07-21 20:39:10 +08:00
parent 17872a97b2
commit 29b94a19d7
6 changed files with 62 additions and 43 deletions

View File

@@ -5,7 +5,7 @@ After=network.target
[Service]
Type=simple
User=root
ExecStart=/etc/clash/clash -d /etc/clash >/dev/null
ExecStart=/etc/clash/clash -d /etc/clash -f /tmp/clash_$USER/config.yaml >/dev/null
ExecStartPost=/etc/clash/start.sh afstart
Restart=on-failure
RestartSec=3s

View File

@@ -961,13 +961,12 @@ setboot(){
2)
if [ "$start_old" = "未开启" ] > /dev/null 2>&1; then
echo -e "\033[33m改为使用保守模式启动clash服务\033[0m"
echo -e "\033[31m注意部分设备保守模式可能无法禁用开机启动\033[0m"
start_old=已开启
setconfig start_old $start_old
$clashdir/start.sh stop
else
if [ -f /etc/init.d/clash -o -w /etc/systemd/system -o -w /usr/lib/systemd/system ];then
echo -e "\033[32m改为使用默认方式启动clash服务\033[0m"
echo -e "\033[32m改为使用系统守护进程启动clash服务\033[0m"
$clashdir/start.sh cronset "ShellClash初始化"
start_old=未开启
setconfig start_old $start_old
@@ -1316,7 +1315,12 @@ clashcfg(){
clashcfg
elif [ "$num" = 5 ]; then
checkcfg_mac=$(cat $clashdir/mac)
macfilter
if [ -n "$PID" ];then
checkcfg_mac_new=$(cat $clashdir/mac)
[ "$checkcfg_mac" != "$checkcfg_mac_new" ] && checkrestart
fi
clashcfg
elif [ "$num" = 6 ]; then

View File

@@ -10,6 +10,7 @@ DIR=$(cat /etc/profile | grep clashdir | awk -F "\"" '{print $2}')
[ -z "$DIR" ] && DIR=$(cat ~/.bashrc | grep clashdir | awk -F "\"" '{print $2}')
BINDIR=$(cat $DIR/mark | grep bindir | awk -F "=" '{print $2}')
[ -z "$BINDIR" ] && BINDIR=$DIR
TMPDIR=/tmp/clash_$USER
start_service() {
#检测必须文件
@@ -20,7 +21,7 @@ start_service() {
procd_set_param respawn
procd_set_param stderr 0
procd_set_param stdout 0
procd_set_param command $BINDIR/clash -d $BINDIR
procd_set_param command $BINDIR/clash -d $BINDIR -f $TMPDIR/config.yaml
procd_close_instance
#其他设置
$DIR/start.sh afstart
@@ -33,7 +34,7 @@ start() {
$DIR/start.sh bfstart
if [ "$?" = "0" ];then
#创建后台进程
service_start $BINDIR/clash -d $BINDIR
service_start $BINDIR/clash -d $BINDIR -f $TMPDIR/config.yaml
#其他设置
$DIR/start.sh afstart
#设置守护进程

View File

@@ -539,7 +539,6 @@ setproxies(){
;;
esac
}
override(){
[ -z "$rule_link" ] && rule_link=1
[ -z "$server_link" ] && server_link=1
@@ -547,11 +546,10 @@ override(){
echo -e "\033[30;47m 欢迎使用配置文件覆写功能!\033[0m"
echo -----------------------------------------------
echo -e " 1 自定义\033[32m端口及秘钥\033[0m"
echo -e " 2 配置\033[33m内置DNS服务\033[0m"
echo -e " 3 管理\033[36m自定义规则\033[0m"
echo -e " 4 管理\033[33m自定义节点\033[0m"
echo -e " 5 管理\033[36m自定义策略组\033[0m"
echo -e " 6 \033[32m自定义\033[0m其他功能"
echo -e " 2 管理\033[36m自定义规则\033[0m"
echo -e " 3 管理\033[33m自定义节点\033[0m"
echo -e " 4 管理\033[36m自定义策略组\033[0m"
echo -e " 5 \033[32m自定义\033[0m高级功能"
[ "$disoverride" != 1 ] && echo -e " 9 \033[33m禁用\033[0m配置文件覆写"
echo -----------------------------------------------
[ "$inuserguide" = 1 ] || echo -e " 0 返回上级菜单"
@@ -573,24 +571,21 @@ override(){
override
;;
2)
setdns
override
;;
3)
setrules
override
;;
4)
3)
setproxies
override
;;
5)
4)
setgroups
override
;;
6)
5)
[ ! -f $clashdir/user.yaml ] && cat > $clashdir/user.yaml <<EOF
#用于编写自定义设定(可参考https://lancellc.gitbook.io/clash)
#用于编写自定义设定(可参考https://lancellc.gitbook.io/clash/clash-config-file/general 或 https://docs.metacubex.one/function/general)
#端口之类请在脚本中修改,否则不会加载
#port: 7890
EOF
[ ! -f $clashdir/others.yaml ] && cat > $clashdir/others.yaml <<EOF
@@ -605,9 +600,13 @@ EOF
#script:
#listeners:
EOF
echo -----------------------------------------------
echo -e "\033[32m已经创建自定义设定文件$clashdir/user.yaml \033[0m"
echo -e "\033[33m可用于编写自定义的DNS等功能\033[0m"
echo -----------------------------------------------
echo -e "\033[32m已经创建自定义功能文件$clashdir/others.yaml \033[0m"
echo -e "\033[33m可用于编写自定义的锚点、入站、proxy-providers、sub-rules、rule-set、script等功能\033[0m"
echo -----------------------------------------------
echo -e "Windows下请\n使用\033[33mWinSCP软件\033[0m进行编辑\033[0m"
echo -e "MacOS下请\n使用\033[33mSecureFX软件\033[0m进行编辑\033[0m"
echo -e "Linux本机可\n使用\033[33mvim\033[0m进行编辑(路由设备可能不显示中文请勿使用)\033[0m"
@@ -635,6 +634,7 @@ EOF
;;
esac
}
clashlink(){
[ -z "$rule_link" ] && rule_link=1
[ -z "$server_link" ] && server_link=1
@@ -1404,10 +1404,17 @@ userguide(){
errornum
forwhat
elif [ "$num" = 1 ];then
#设置运行模式
redir_mod="Redir模式"
ckcmd nft && redir_mod="Nft基础"
ckcmd nft && modprobe nft_tproxy &> /dev/null && redir_mod="Nft混合"
modprobe nft_tproxy &> /dev/null && redir_mod="Nft混合"
setconfig redir_mod "$redir_mod"
#自动识别IPV6
[ -n "$(ip a 2>&1 | grep -w 'inet6' | grep -E 'global' | sed 's/.*inet6.//g' | sed 's/scope.*$//g')" ] && {
setconfig ipv6_redir 已开启
setconfig ipv6_support 已开启
setconfig ipv6_dns 已开启
}
#设置开机启动
[ -f /etc/rc.common ] && /etc/init.d/clash enable
ckcmd systemctl && systemctl enable clash.service > /dev/null 2>&1
@@ -1549,7 +1556,8 @@ testcommand(){
$clashdir/start.sh stop
echo -----------------------------------------------
if $clashdir/clash -v &>/dev/null;then
$clashdir/clash -t -d $clashdir
clash -s modify_yaml &>/dev/null
$clashdir/clash -t -d $clashdir -f $tmpdir/config.yaml
[ "$?" = 0 ] && testover=32m测试通过|| testover=31m出现错误请截图后到TG群询问
echo -e "\033[$testover\033[0m"
else

View File

@@ -229,6 +229,8 @@ fi
#删除临时文件
rm -rf /tmp/*lash*gz
rm -rf /tmp/SC_tmp
#转换&清理旧版本文件
mkdir -p $clashdir/configs
mkdir -p $clashdir/tools
sleep 1
echo -e "\033[32m脚本初始化完成,请输入\033[30;47m clash \033[0;33m命令开始使用\033[0m"

View File

@@ -163,7 +163,7 @@ getyaml(){
#前后端订阅服务器地址索引,可在此处添加!
Server=`sed -n ""$server_link"p"<<EOF
https://api.dler.io
https://api.v1.mk
https://sub.d1.mk
https://sub.xeton.dev
https://v.id9.cc
https://sub.maoxiongnet.com
@@ -183,7 +183,7 @@ https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/config/ACL4SSR_Ba
EOF`
#如果传来的是Url链接则合成Https链接否则直接使用Https链接
if [ -z "$Https" ];then
[ -n "$(echo $Url | grep -oE 'vless:')" -a -z "$retry" ] && Server='https://api.v1.mk'
[ -n "$(echo $Url | grep -oE 'vless:')" -a -z "$retry" ] && Server='https://sub.d1.mk'
[ -n "$(echo $Url | grep -oE 'hysteria:')" -a -z "$retry" ] && Server='https://sub.jwsc.eu.org'
Https="$Server/sub?target=clash&insert=true&new_name=true&scv=true&udp=true&exclude=$exclude&include=$include&url=$Url&config=$Config"
url_type=true
@@ -334,9 +334,6 @@ modify_yaml(){
#设置目录
yaml=$clashdir/config.yaml
#预读取变量
mode=$(grep "^mode" $yaml | head -1 | awk '{print $2}')
[ -z "$mode" ] && mode='Rule'
#分割配置文件
yaml_char='proxies proxy-groups proxy-providers rules rule-providers'
for char in $yaml_char;do
@@ -457,28 +454,32 @@ EOF
fi
#合并文件
[ -s $clashdir/user.yaml ] && yaml_user=$clashdir/user.yaml
for char in $yaml_char;do
[ -s $tmpdir/hosts.yaml ] && yaml_hosts=$tmpdir/hosts.yaml
[ -s $tmpdir/others.yaml ] && yaml_others=$clashdir/others.yaml
yaml_add=
for char in $yaml_char;do #将额外配置文件合并
[ -s $tmpdir/${char}.yaml ] && {
sed -i "1i\\${char}:" $tmpdir/${char}.yaml
yaml_add="$yaml_add $tmpdir/${char}.yaml"
}
done
cut -c 1- $tmpdir/set.yaml $yaml_hosts $yaml_user $yaml_add > $tmpdir/config.yaml
cut -c 1- $tmpdir/set.yaml $yaml_hosts $yaml_user $yaml_others $yaml_add > $tmpdir/config.yaml
#测试自定义配置文件
$bindir/clash -t -d $bindir -f $tmpdir/config.yaml >/dev/null
if [ "$?" != 0 ];then
logger "$($bindir/clash -t -d $bindir -f $tmpdir/config.yaml | grep -Eo 'error.*=.*')" 31
logger "自定义配置文件校验失败!将使用基础配置文件启动!" 33
sed -i "/#自定义策略组开始/,/#自定义策略组结束/d" $tmpdir/config.yaml
sed -i "/#自定义/d" $tmpdir/config.yaml
sed -i "/#自定义策略组开始/,/#自定义策略组结束/d" $tmpdir/proxy-groups.yaml
cut -c 1- $tmpdir/set.yaml $yaml_add > $tmpdir/config.yaml
sed -i "/#自定义/d" $tmpdir/config.yaml
fi
#存档
if [ "$clashdir" = "$bindir" ];then
cmp -s $tmpdir/config.yaml $yaml >/dev/null 2>&1
[ "$?" != 0 ] && mv -f $tmpdir/config.yaml $yaml || rm -f $tmpdir/config.yaml
elif [ "$tmpdir" != "$bindir" ];then
mv -f $tmpdir/config.yaml $bindir/config.yaml
fi
# if [ "$clashdir" = "$bindir" ];then
# cmp -s $tmpdir/config.yaml $yaml >/dev/null 2>&1
# [ "$?" != 0 ] && mv -f $tmpdir/config.yaml $yaml || rm -f $tmpdir/config.yaml
# elif [ "$tmpdir" != "$bindir" ];then
# mv -f $tmpdir/config.yaml $bindir/config.yaml
# fi
#清理缓存
for char in $yaml_char set hosts;do
rm -f $tmpdir/${char}.yaml
@@ -740,9 +741,12 @@ start_tun(){
ip6tables -I FORWARD -o utun -j ACCEPT > /dev/null 2>&1
#屏蔽QUIC
if [ "$quic_rj" = 已启用 ];then
[ "$dns_mod" = "redir_host" -a "$cn_ip_route" = "已开启" ] && set_cn_ip='-m set ! --match-set cn_ip dst'
[ "$dns_mod" = "redir_host" -a "$cn_ip_route" = "已开启" ] && {
set_cn_ip='-m set ! --match-set cn_ip dst'
set_cn_ip6='-m set ! --match-set cn_ip6 dst'
}
iptables -I FORWARD -p udp --dport 443 -o utun -m comment --comment "ShellClash-QUIC-REJECT" $set_cn_ip -j REJECT >/dev/null 2>&1
#ip6tables -I FORWARD -p udp --dport 443 -o utun -m comment --comment "ShellClash-QUIC-REJECT" -j REJECT >/dev/null 2>&1
ip6tables -I FORWARD -p udp --dport 443 -o utun -m comment --comment "ShellClash-QUIC-REJECT" $set_cn_ip6 -j REJECT >/dev/null 2>&1
fi
modprobe xt_mark &>/dev/null && {
i=1
@@ -1220,7 +1224,7 @@ bfstart(){
[ -z "$(grep 'procd_set_param user shellclash' /etc/init.d/clash)" ] && \
sed -i '/procd_close_instance/i\\t\tprocd_set_param user shellclash' /etc/init.d/clash
elif [ -w "$servdir" ]; then
setconfig ExecStart "/bin/su shellclash -c \"$bindir/clash -d $bindir >/dev/null\"" $servdir
setconfig ExecStart "/bin/su shellclash -c \"$bindir/clash -d $bindir -f $tmpdir/config.yaml >/dev/null\"" $servdir
systemctl daemon-reload >/dev/null
fi
fi
@@ -1297,10 +1301,10 @@ start_old(){
#使用传统后台执行二进制文件的方式执行
if [ "$local_proxy" = "已开启" -a -n "$(echo $local_type | grep '增强模式')" ];then
ckcmd su && su=su
$su shellclash -c "$bindir/clash -d $bindir >/dev/null" &
$su shellclash -c "$bindir/clash -d $bindir -f $tmpdir/config.yaml >/dev/null" &
else
ckcmd nohup && nohup=nohup
$nohup $bindir/clash -d $bindir >/dev/null 2>&1 &
$nohup $bindir/clash -d $bindir -f $tmpdir/config.yaml >/dev/null 2>&1 &
fi
afstart
$0 daemon
@@ -1321,7 +1325,7 @@ start)
bfstart
stop_firewall #清理路由策略
#使用内置规则强行覆盖config配置文件
[ "$disoverride" != "1" ] && modify_yaml
[ "$disoverride" != "1" ] && modify_yaml || ln -s $clashdir/config.yaml $tmpdir/config.yaml
#使用不同方式启动clash服务
if [ "$start_old" = "已开启" ];then
start_old