@@ -281,8 +281,8 @@ rules(){
281281 fake_ip_range=$( egrep ' ^ {0,}fake-ip-range' /etc/clash/config.yaml | grep fake-ip-range: | awk -F ' : ' ' {print $2}' )
282282
283283 if [ ! -z " ${fake_ip_range} " ]; then
284- iptables -t nat -A OUTPUT -p tcp -d $fake_ip_range -j REDIRECT --to-ports " ${redir_port} "
285284 if [ " ${enable_udp} " -eq 1 ]; then
285+ iptables -t nat -A OUTPUT -p tcp -d $fake_ip_range -j REDIRECT --to-ports " ${redir_port} "
286286 iptables -t mangle -A OUTPUT -p udp -d $fake_ip_range -j MARK --set-mark " $PROXY_FWMARK "
287287 fi
288288 else
@@ -340,7 +340,13 @@ rules(){
340340 ip link set $TUN_DEVICE_NAME up
341341 ip address replace 198.18.0.0/16 dev $TUN_DEVICE_NAME
342342 ip route replace default dev $TUN_DEVICE_NAME table " $PROXY_ROUTE_TABLE "
343+
344+ nat=$( iptables -nvL PREROUTING -t nat | sed 1,2d | sed -n ' /tcp dpt:53/=' | sort -r)
345+ for natx in $nat ; do
346+ iptables -t nat -D PREROUTING $natx > /dev/null 2>&1
347+ done
343348 iptables -t nat -I PREROUTING -p tcp --dport 53 -j ACCEPT
349+
344350 elif [ " ${core} " -eq 4 ]; then
345351 ip route replace default dev utun table " $PROXY_ROUTE_TABLE "
346352 fi
@@ -353,6 +359,10 @@ rules(){
353359 iptables -t mangle -A clash -m set --match-set localnetwork dst -j RETURN
354360 iptables -t mangle -I OUTPUT -j clash
355361 iptables -t mangle -I PREROUTING -m set ! --match-set localnetwork dst -j MARK --set-mark " $PROXY_FWMARK "
362+ nat=$( iptables -nvL PREROUTING -t nat | sed 1,2d | sed -n ' /tcp dpt:53/=' | sort -r)
363+ for natx in $nat ; do
364+ iptables -t nat -D PREROUTING $natx > /dev/null 2>&1
365+ done
356366 iptables -t nat -I PREROUTING -p tcp --dport 53 -j ACCEPT
357367
358368 if [ -f /usr/sbin/ip6tables ] && [ " ${ipv6} " -eq 1 ]; then
@@ -373,16 +383,81 @@ restore() {
373383}
374384
375385
386+ use_dns (){
387+
388+ if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
389+ echo " Checking Dns Mode " > $REAL_LOG
390+ fi
391+
392+ if [ " $core " -eq 1 ] || [ " $core " -eq 2 ]; then
393+
394+
395+ if [ " $mode " -eq 1 ] || [ " $tun_mode " -eq 1 ] ; then
396+
397+ if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
398+ echo " Enabling Custom Dns Mode " > $REAL_LOG
399+ elif [ " ${lang} " == " zh_cn" ]; then
400+ echo " 启用自定义Dns模式" > $REAL_LOG
401+ fi
402+ uci set clash.config.mode=" 1" && uci del clash.config.tun_mode && uci commit clash
403+
404+ elif [ -z " $mode " ] || [ " $mode " -eq 0 ]; then
405+
406+ if [ ! -z " $( grep " ^ \{0,\}tun:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}listen:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}enhanced-mode:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}enable:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}dns:" $CONFIG_YAML ) " ] ; then
407+
408+
409+ if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
410+ echo " Enabling Custom Dns Mode " > $REAL_LOG
411+ elif [ " ${lang} " == " zh_cn" ]; then
412+ echo " 启用自定义Dns模式" > $REAL_LOG
413+ fi
414+ uci set clash.config.mode=" 1" && uci del clash.config.tun_mode && uci commit clash
415+ fi
416+ fi
417+ fi
418+
419+
420+ if [ " $core " -eq 3 ] || [ " $core " -eq 4 ]; then
421+
422+ if [ " $tun_mode " -eq 1 ] || [ " $mode " -eq 1 ]; then
423+
424+
425+
426+ if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
427+ echo " Enabling Tun Dns Mode " > $REAL_LOG
428+ elif [ " ${lang} " == " zh_cn" ]; then
429+ echo " 启用自定义Tun Dns模式" > $REAL_LOG
430+ fi
431+ uci del clash.config.mode && uci set clash.config.tun_mode=" 1" && uci commit clash
432+
433+
434+ elif [ -z " $tun_mode " ] || [ " $tun_mode " -eq 0 ]; then
435+
436+ if [ -z " $( grep " ^ \{0,\}tun:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}listen:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}enhanced-mode:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}enable:" $CONFIG_YAML ) " ] || [ -z " $( grep " ^ \{0,\}dns:" $CONFIG_YAML ) " ] ; then
437+ if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
438+ echo " Enabling Tun Dns Mode " > $REAL_LOG
439+ elif [ " ${lang} " == " zh_cn" ]; then
440+ echo " 启用自定义Tun Dns模式" > $REAL_LOG
441+ fi
442+ uci del clash.config.mode && uci set clash.config.tun_mode=" 1" && uci commit clash
443+ fi
444+ fi
445+
446+ fi
447+
448+ }
376449
377450start (){
378451# ===========================================================================================================================
379452lang=$( uci get luci.main.lang 2> /dev/null)
380453landns=$( uci get clash.config.landns 2> /dev/null)
381454enable=$( uci get clash.config.enable 2> /dev/null)
382455core=$( uci get clash.config.core 2> /dev/null)
456+ mode=$( uci get clash.config.mode 2> /dev/null)
383457tun_mode=$( uci get clash.config.tun_mode 2> /dev/null)
384458
385459
460+
386461if [ -f /etc/clash/config.yaml ]; then
387462 rm -rf /etc/clash/config.yaml > /dev/null 2>&1
388463fi
@@ -395,6 +470,9 @@ if [ "${enable}" -eq 1 ]; then
395470 fi
396471 sleep 1
397472 select_config > /dev/null 2>&1
473+ sleep 1
474+ use_dns > /dev/null 2>&1
475+ sleep 1
398476
399477if [ " ${core} " -eq 1 ] && [ ! -f /etc/clash/clash ]; then
400478 if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
@@ -623,7 +701,7 @@ remove_mark(){
623701 ip rule del fwmark " $PROXY_FWMARK " table " $PROXY_ROUTE_TABLE " pref 789 > /dev/null 2>&1
624702 ip route del local default dev lo table " $PROXY_ROUTE_TABLE " > /dev/null 2>&1
625703
626- if [ " ${core} " -eq 3 ] || [ " ${core} " -eq 4 ] ; then
704+
627705
628706 TUN_DEVICE=$( egrep ' ^ {0,}device-url:' /etc/clash/config.yaml | grep device-url: | awk -F ' //' ' {print $2}' )
629707 if [ -z $TUN_DEVICE ]; then
@@ -639,10 +717,17 @@ remove_mark(){
639717 # ip rule del fwmark "$PROXY_FWMARK" table "$PROXY_ROUTE_TABLE" 2>/dev/null
640718 ip rule del fwmark " $PROXY_FWMARK " table " $PROXY_ROUTE_TABLE " pref 789 > /dev/null 2>&1
641719
642- iptables -t mangle -D OUTPUT -j clash 2> /dev/null
643- iptables -t mangle -D PREROUTING -m set ! --match-set localnetwork dst -j MARK --set-mark " $PROXY_FWMARK " 2> /dev/null
720+
721+ mangle=$( iptables -nvL OUTPUT -t mangle | sed 1,2d | sed -n ' /clash/=' | sort -r)
722+ for mangles in $mangle ; do
723+ iptables -t mangle -D OUTPUT $mangles 2> /dev/null
724+ done
725+
726+ pre=$( iptables -nvL PREROUTING -t mangle | sed 1,2d | sed -n ' /! match-set localnetwork dst MARK set 0x162/=' | sort -r)
727+ for prer in $pre ; do
728+ iptables -t mangle -D PREROUTING $prer 2> /dev/null
729+ done
644730
645- iptables -t nat -D OUTPUT -p udp --dport 53 -j CLASH_DNS
646731 iptables -t nat -D PREROUTING -p udp --dport 53 -j DNAT --to " 127.0.0.1:$dns_port "
647732
648733 if [ -f /usr/sbin/ip6tables ] && [ " ${ipv6} " -eq 1 ]; then
@@ -657,8 +742,8 @@ remove_mark(){
657742
658743 ipset destroy localnetwork 2> /dev/null
659744
660- fi
661- /etc/init.d/dnsmasq restart > /dev/null 2>&1
745+
746+ /etc/init.d/dnsmasq restart > /dev/null 2>&1
662747}
663748
664749stop (){
@@ -684,19 +769,24 @@ stop(){
684769 elif [ " ${lang} " == " zh_cn" ]; then
685770 echo " 删除clash iptables规则" > $REAL_LOG
686771 fi
687-
772+
773+ remove_mark > /dev/null 2>&1
774+
688775 nat_indexs=$( iptables -nvL PREROUTING -t nat | sed 1,2d | sed -n ' /clash/=' | sort -r)
689776 for nat_index in $nat_indexs ; do
690777 iptables -t nat -D PREROUTING $nat_index > /dev/null 2>&1
691- iptables -t nat -F clash > /dev/null 2>&1 && iptables -t nat -X clash > /dev/null 2>&1
692- iptables -t mangle -F clash > /dev/null 2>&1 && iptables -t mangle -D PREROUTING -p udp -j clash > /dev/null 2>&1 && iptables -t mangle -X clash > /dev/null 2>&1
778+ iptables -t nat -F clash > /dev/null 2>&1
779+ iptables -t nat -X clash > /dev/null 2>&1
780+ iptables -t mangle -F clash > /dev/null 2>&1
781+ iptables -t mangle -D PREROUTING -p udp -j clash > /dev/null 2>&1
782+ iptables -t mangle -X clash > /dev/null 2>&1
693783 done
694784
695- ip rule del fwmark 1 table 100 > /dev/null 2>&1
696- ip route del local default dev lo table 100 > /dev/null 2>&1
697-
698- iptables -t nat -D PREROUTING -p tcp --dport 53 -j ACCEPT > /dev/null 2>&1
699-
785+ nat= $( iptables -nvL PREROUTING -t nat | sed 1,2d | sed -n ' /tcp dpt:53/= ' | sort -r )
786+ for natx in $nat ; do
787+ iptables -t nat -D PREROUTING $natx > /dev/null 2>&1
788+ done
789+
700790 ip6tables -t mangle -F clash > /dev/null 2>&1
701791 ip6tables -t mangle -D PREROUTING -p udp -j clash > /dev/null 2>&1
702792 ip6tables -t mangle -X clash > /dev/null 2>&1
@@ -728,9 +818,9 @@ stop(){
728818 for fake2 in $fake2 ; do
729819 iptables -t nat -D OUTPUT $fake2 > /dev/null 2>&1
730820 done
731-
732- remove_mark > /dev/null 2>&1
733-
821+
822+ iptables -t nat -I PREROUTING -p tcp --dport 53 -j ACCEPT
823+
734824 bash /usr/share/clash/backup.sh > /dev/null 2>&1
735825
736826 if [ " ${lang} " == " en" ] || [ $lang == " auto" ]; then
0 commit comments