增加自定义代理和自定义策略

Signed-off-by: BetaXO <n.b@live.com>
This commit is contained in:
Ning Bo 2022-09-16 12:03:43 +08:00
parent c8205cabde
commit f3ccaeb429
2 changed files with 43 additions and 0 deletions

View File

@ -1032,10 +1032,22 @@ EOF
# - IP-CIDR6,2620:0:2d0:200::7/32,🚀 节点选择
# - DST-PORT,80,DIRECT
# - SRC-PORT,7777,DIRECT
EOF
[ ! -f $clashdir/proxies.yaml ] && cat > $clashdir/proxies.yaml <<EOF
#proxies:
# - {name: "test", server: 192.168.1.1, port: 9050, type: socks5, udp: true}
EOF
[ ! -f $clashdir/proxy-groups.yaml ] && cat > $clashdir/proxy-groups.yaml <<EOF
#proxy-groups:
# - name: OFFICE
# type: select
# proxies:
# - office-router
EOF
echo -e "\033[32m已经启用自定义配置功能\033[0m"
echo -e "Windows下请\n使用\033[33mwinscp软件\033[0m进入$clashdir目录后手动编辑\033[0m"
echo -e "Shell下(\033[31m部分旧设备可能不显示中文\033[0m)可\n使用【\033[36mvi $clashdir/user.yaml\033[0m】编辑自定义设定文件;\n使用【\033[36mvi $clashdir/rules.yaml\033[0m】编辑自定义规则文件。"
echo -e "使用【\033[36mvi $clashdir/proxies.yaml\033[0m】编辑自定义代理文件;\n使用【\033[36mvi $clashdir/proxy-groups.yaml\033[0m】编辑自定义策略组文件。"
echo -e "如需自定义节点可以在config.yaml文件中修改或者直接替换config.yaml文件\033[0m"
sleep 3
clashadv

View File

@ -384,6 +384,37 @@ EOF
sed -i "/^rules:/a\\$space$line #自定义规则" $tmpdir/config.yaml
done < $clashdir/rules.yaml
fi
#插入自定义代理
sed -i "/#自定义代理/d" $tmpdir/config.yaml
space=$(sed -n '/^proxies:/{n;p}' $tmpdir/config.yaml | grep -oE '^\ *') #获取空格数
if [ -f $clashdir/proxies.yaml ];then
sed -i '/^$/d' $clashdir/proxies.yaml && echo >> $clashdir/proxies.yaml #处理换行
while read line;do
[ -z "$(echo "$line" | grep '^proxies:')" ] && \
[ -z "$(echo "$line" | grep '#')" ] && \
[ -n "$(echo "$line" | grep '\-\ ')" ] && \
line=$(echo "$line" | sed 's#/#\\/#') && \
sed -i "/^proxies:/a\\$space$line #自定义代理" $tmpdir/config.yaml
done < $clashdir/proxies.yaml
fi
#插入自定义策略组
sed -i "/#自定义策略组/d" $tmpdir/config.yaml
space=$(sed -n '/^proxy-groups:/{n;p}' $tmpdir/config.yaml | grep -oE '^\ *') #获取原始配置空格数
if [ -f $clashdir/proxy-groups.yaml ];then
c_space=$(sed -n '/^proxy-groups:/{n;p}' $clashdir/proxy-groups.yaml | grep -oE '^\ *') #获取自定义配置空格数
sed -i "s/$c_space/$space/g" $clashdir/proxy-groups.yaml && echo >> $clashdir/proxy-groups.yaml #处理缩进空格数
sed -i '/^$/d' $clashdir/proxy-groups.yaml && echo >> $clashdir/proxy-groups.yaml #处理换行
cat $clashdir/proxy-groups.yaml | awk '{array[NR]=$0} END { for(i=NR;i>0;i--){print array[i];} }' | while IFS= read line;do
[ -z "$(echo "$line" | grep '^proxy-groups:')" ] && \
[ -n "${line// /}" ] && \
[ -z "$(echo "$line" | grep '#')" ] && \
line=$(echo "$line" | sed 's#/#\\/#') && \
sed -i "/^proxy-groups:/a\\$line #自定义策略组" $tmpdir/config.yaml
done
fi
#tun/fake-ip防止流量回环
if [ "$redir_mod" = "混合模式" -o "$redir_mod" = "Tun模式" -o "$dns_mod" = "fake-ip" ];then
sed -i "/^rules:/a\\$space- SRC-IP-CIDR,198.18.0.0/16,REJECT #自定义规则(防止回环)" $tmpdir/config.yaml