Skip to content

Commit

Permalink
add:Automatic override configuration turns on automatic tun routing o…
Browse files Browse the repository at this point in the history
…f clashMeta and is compatible with package.list.(start config auto_config is true)
  • Loading branch information
heinu123 committed Mar 11, 2024
1 parent 18586a8 commit a9849c2
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 23 deletions.
25 changes: 15 additions & 10 deletions module/clash/clash.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ udp="true"
disable_ipv6="false"
# 是否禁用ipv6(不是ipv6不进行代理 而是全局禁用)

auto_config="false"
# 开启配置自动替换

sleeptime="10"
# 监听packages.list的间隔时间

Expand Down Expand Up @@ -57,7 +60,7 @@ ghproxy="https://mirror.ghproxy.com"
safe_ui="true"
#仅允许本机访问api

mode="global"
mode="whitelist"
# cfm运行模式:
# blacklist(指定应用不走代理)
# whitelist(仅指定应用走代理)
Expand All @@ -80,9 +83,6 @@ ml="false"
adguard="false"
# 开启adguard home扩展

mosdns="false"
# 开启mosdns扩展

nat_kernel="true"
# 允许iptables nat转发

Expand All @@ -103,14 +103,18 @@ Cgroup_memory_limit=""
pref_id="5000"
mark_id="2021"
table_id="2021"
version="202403111716"
version="202403112051"
Cgroup_memory_path=""
Clash_data_dir="/data/clash"
Clash_run_path="${Clash_data_dir}/run"
Clash_bin_path="${Clash_data_dir}/bin"
CFM_logs_file="${Clash_run_path}/run.logs"
CFM_OldLogs_file="${Clash_run_path}/run.old.logs"
template_file="${Clash_data_dir}/clash.yaml"
if [ "${Split}"=="true" ]; then
template_file="${Clash_data_dir}/clash.yaml"
else
template_file="${Clash_data_dir}/config.yaml"
fi
rule_file="${Clash_data_dir}/rule.yaml"
appuid_file="${Clash_run_path}/appuid.list"
Clash_pid_file="${Clash_run_path}/clash.pid"
Expand Down Expand Up @@ -169,10 +173,11 @@ else
fi
Clash_ui_port=$(grep "external-controller" ${template_file} | grep -v "#" | awk -F ':' '{print $3}')
Clash_enhanced_mode=$(grep "enhanced-mode" ${template_file} | grep -v "#" | awk -F ': ' '{print $2}')
Clash_tproxy_port=$(grep "tproxy-port" ${template_file} | grep -v "#" | awk -F ': ' '{print $2}')
Clash_tun_status=$(awk -F ': ' '/^tun: *$/{getline; print $2}' ${template_file})
Clash_auto_route=$(grep "auto-route" ${template_file} | grep -v "#" | awk -F ': ' '{print $2}')
tun_device=$(awk -F ': ' '/ +device: /{print $2}' ${template_file})
Clash_tproxy_port=$(grep "tproxy-port" ${temporary_config_file} | grep -v "#" | awk -F ': ' '{print $2}')
Clash_tun_status=$(awk -F ': ' '/^tun: *$/{getline; print $2}' ${temporary_config_file})
Clash_auto_route=$(grep "auto-route" ${temporary_config_file} | grep -v "#" | awk -F ': ' '{print $2}')
Clash_tun_line=$(expr $(grep -n "tun:" ${temporary_config_file} | grep -v "#" | awk -F ':' '{print $1}') + 1)
tun_device=$(awk -F ': ' '/ +device: /{print $2}' ${temporary_config_file})
if [ "${Clash_tun_status}" == "" ]; then
Clash_tun_status="false"
fi
Expand Down
8 changes: 7 additions & 1 deletion module/clash/scripts/clash.iptables
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,13 @@ while getopts ":sk" signal ; do
log "info: auto_route已开启."
exit 0
else
set_tun
if [ "${auto_config}" != "true" ]; then
set_tun
log "warn: tun的auto-route参数未启用将由模块创建路由."
log "warn: 仅支持全局代理."
log "warn: 你也可以修改启动配置auto_config为true."
log "warn: 此时模块将自动修改clashMeta的tun自动路由."
fi
fi
else
log "info: 当前为:tproxy模式."
Expand Down
77 changes: 66 additions & 11 deletions module/clash/scripts/clash.service
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,59 @@ update_file() {
return 1
fi
}
edit_tun() {
addition="auto-route: true"
uidlist=""
ip4list=""
ip6list=""
if [ "${mode}" != "global" ]; then
apps=`cat ${filter_packages_file} | sort -u`

for appuid in ${apps} ; do
if [ "$(grep ":" <<< ${appuid})" ];then
ip6list="${ip6list}\n - ${appuid}"
continue
fi
if [ "$(grep "[0-9].*\." <<< ${appuid})" ];then
ip4list="${ip4list}\n - ${appuid}"
continue
fi
uidlist="${uidlist}\n - ${appuid}"
if [ "${mode}" == "blacklist" ]; then
log "info: ${appuid}已代理."
else
log "info: ${appuid}已排除."
fi
done

if [ "${mode}" == "blacklist" ]; then
if [ "${uidlist}" != "" ]; then
uid="\n exclude-package:${uidlist}"
fi
if [ "${ip4list}" != "" ]; then
ip6="\n inet6-route-exclude-address:${ip4list}"
fi
if [ "${ip6list}" != "" ]; then
ip4="\n inet4-route-exclude-address:${ip6list}"
fi
elif [ "${mode}" == "whitelist" ] ; then
if [ "${uidlist}" != "" ]; then
uid="\n include-package:${uidlist}"
fi
if [ "${ip6list}" != "" ]; then
ip6="\n inet6-route-address:${ip4list}"
fi
if [ "${ip4list}" != "" ]; then
ip4="\n inet4-route-address:${ip6list}"
fi
fi
addition="${addition}${uid}${ip4}${ip6}"
fi
if [ "$Clash_tun_status" == "true" ] || [ "${Clash_auto_route}" != "true" ];then
sed -i "${Clash_tun_line}a\ ${addition}" ${temporary_config_file}
log "info: 已自动覆写clash配置"
fi
}

start_clash() {
mkdir -p ${Clash_run_path}
Expand All @@ -37,6 +90,15 @@ start_clash() {
fi
fi

if [ "${Split}" == "true" ];then
cp -f ${template_file} ${temporary_config_file}.swp && echo "\n" >> ${temporary_config_file}.swp
sed -n -E '/^proxies:.*$/,$p' ${Clash_config_file} >> ${temporary_config_file}.swp
echo "\n" >> ${temporary_config_file}.swp
sed -i '/^[ ]*$/d' ${temporary_config_file}.swp
mv -f ${temporary_config_file}.swp ${temporary_config_file}
else
cp -f ${Clash_config_file} ${temporary_config_file}
fi

if [ "${ipv6}" = "false" ] ; then
for net in /proc/sys/net/ipv6/conf/{wlan*,*data*} ; do
Expand Down Expand Up @@ -71,14 +133,16 @@ start_clash() {
fi
fi

if [ "$Clash_tun_status" == "true" ] || [ "${auto_config}" != "true" ];then
edit_tun
fi

if [ "${Clash_tproxy_port}" == 0 ] || [ "${Clash_tproxy_port}" == "" ]; then
if [ "${Clash_tun_status}" != "true" ]; then
log "err: tproxy和tun得二选一."
exit 1
fi
fi

if [ -f "${Clash_bin_path}" ] ; then
mkdir -p ${Clash_run_path}
chown ${Clash_user_group} ${Clash_bin_path}
Expand All @@ -90,7 +154,7 @@ start_clash() {
rm -rf ${Clash_run_path}/root
touch ${Clash_run_path}/root
chmod 777 ${Clash_run_path}/root
if [ "${Geo_auto_update}" != "true"];then
if [ "${Geo_auto_update}" != "true" ];then
if [ "${auto_updateGeoSite}" == "true" ]; then
echo "${update_geoXInterval} ${scripts_dir}/clash.tool -u" >> ${Clash_run_path}/root \
&& log "info: 自动更新GeoX定时已开启."
Expand Down Expand Up @@ -162,15 +226,6 @@ start_clash() {
log "info: 当前百度系免流ip:${baidumlip}"
log "info: 当前腾讯系免流ip:${txmlip}"
fi
if [ "${Split}" == "true" ];then
cp -f ${template_file} ${temporary_config_file}.swp && echo "\n" >> ${temporary_config_file}.swp
sed -n -E '/^proxies:.*$/,$p' ${Clash_config_file} >> ${temporary_config_file}.swp
echo "\n" >> ${temporary_config_file}.swp
sed -i '/^[ ]*$/d' ${temporary_config_file}.swp
mv -f ${temporary_config_file}.swp ${temporary_config_file}
else
cp -f ${Clash_config_file} ${temporary_config_file}
fi
if [ ${ml} == "true" ];then
sed -i "s/百度系免流ip/${baidumlip}/g" ${temporary_config_file}
sed -i "s/腾讯系免流ip/${txmlip}/g" ${temporary_config_file}
Expand Down
20 changes: 19 additions & 1 deletion module/clash/scripts/clash.tool
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ update_file() {
}

find_packages_uid() {
rm -f ${appuid_file}
rm -f ${appuid_file}.tmp
hd=""
if [ "${mode}" == "global" ]; then
Expand All @@ -218,6 +219,24 @@ find_packages_uid() {
log "warn: Tproxy模式下fake-ip不可使用黑白名单."
exit 1
fi
if [ "$(grep ":" <<< ${package})" ];then
echo "${package}" >> ${appuid_file}
if [ "${mode}" = "blacklist" ]; then
log "info: ${package}已过滤."
elif [ "${mode}" = "whitelist" ]; then
log "info: ${package}已代理."
fi
continue
fi
if [ "$(grep "[0-9].*\." <<< ${package})" ];then
echo "${package}" >> ${appuid_file}
if [ "${mode}" = "blacklist" ]; then
log "info: ${package}已过滤."
elif [ "${mode}" = "whitelist" ]; then
log "info: ${package}已代理."
fi
continue
fi
nhd=$(awk -F ">" '/^[0-9]+>$/{print $1}' <<< "${package}")
if [ "${nhd}" != "" ]; then
hd=${nhd}
Expand All @@ -235,7 +254,6 @@ find_packages_uid() {
log "info: ${hd}${package}已代理."
fi
done
rm -f ${appuid_file}
for uid in $(cat ${appuid_file}.tmp | sort -u); do
echo ${uid} >> ${appuid_file}
done
Expand Down

0 comments on commit a9849c2

Please sign in to comment.