#!/bin/sh /etc/rc.common # Example script # Copyright (C) 2007 OpenWrt.org USE_PROCD=1 START=92 getconfig(){ #开机加载环境变量保证找到文件路径 source /etc/profile > /dev/null 2>&1 ccfg=$clashdir/mark if [ ! -f "$ccfg" ]; then echo mark文件不存在,默认以Redir模式运行! cat >$ccfg< /dev/null 2>&1 iptables -t nat -D PREROUTING -p udp -j clash_dns > /dev/null 2>&1 iptables -t nat -F clash > /dev/null 2>&1 iptables -t nat -X clash > /dev/null 2>&1 iptables -t nat -F clash_dns > /dev/null 2>&1 iptables -t nat -X clash_dns > /dev/null 2>&1 #重置ipv6规则 ip6tables -t nat -D PREROUTING -p tcp -j clash > /dev/null 2>&1 ip6tables -t nat -D PREROUTING -p udp -j clash_dns > /dev/null 2>&1 ip6tables -t nat -F clash > /dev/null 2>&1 ip6tables -t nat -X clash > /dev/null 2>&1 ip6tables -t nat -F clash_dns > /dev/null 2>&1 ip6tables -t nat -X clash_dns > /dev/null 2>&1 } start_dns(){ #允许tun网卡接受流量 iptables -I FORWARD -o utun -j ACCEPT ip6tables -I FORWARD -o utun -j ACCEPT #设置dns转发 iptables -t nat -N clash_dns for mac in $(cat $clashdir/mac); do iptables -t nat -A clash_dns -m mac --mac-source $mac -j RETURN done iptables -t nat -A clash_dns -p udp --dport 53 -j REDIRECT --to 1053 iptables -t nat -A PREROUTING -p udp -j clash_dns #ipv6DNS ip6tables -t nat -N clash_dns for mac in $(cat $clashdir/mac); do ip6tables -t nat -A clash_dns -m mac --mac-source $mac -j RETURN done ip6tables -t nat -A clash_dns -p udp --dport 53 -j REDIRECT --to 1053 ip6tables -t nat -A PREROUTING -p udp -j clash_dns } start_service() { getconfig #使用内置规则强行覆盖config配置文件 if [ "$modify_yaml" != "已开启" ];then modify_yaml fi #创建clash后台进程 procd_open_instance procd_set_param respawn procd_set_param stderr 1 procd_set_param stdout 1 procd_set_param command $clashdir/clash -d $clashdir procd_close_instance #修改iptables规则使流量进入clash stop_iptables start_dns if [ "$redir_mod" != "Tun模式" ];then start_redir fi mark_time } stop_service() { stop_iptables }