2020-07-17 17:40:34 +08:00
|
|
|
|
#!/bin/sh /etc/rc.common
|
|
|
|
|
# Example script
|
|
|
|
|
# Copyright (C) 2007 OpenWrt.org
|
|
|
|
|
|
|
|
|
|
USE_PROCD=1
|
|
|
|
|
START=99
|
2020-07-29 23:18:10 +08:00
|
|
|
|
|
|
|
|
|
getconfig(){
|
2020-07-31 00:36:08 +08:00
|
|
|
|
cpath=/etc/clash
|
|
|
|
|
ccfg=$cpath/mark
|
2020-07-29 23:18:10 +08:00
|
|
|
|
if [ ! -f "$ccfg" ]; then
|
|
|
|
|
echo mark文件不存在,默认以Redir模式运行!
|
|
|
|
|
cat >$ccfg<<EOF
|
|
|
|
|
#标识clash运行状态的文件,请勿改动!
|
|
|
|
|
EOF
|
|
|
|
|
redir_mod=redir模式
|
|
|
|
|
fi
|
|
|
|
|
source $ccfg
|
|
|
|
|
}
|
|
|
|
|
mark_time(){
|
|
|
|
|
start_time=`date +%s`
|
|
|
|
|
sed -i '/start_time*/'d $ccfg
|
|
|
|
|
sed -i "3i\start_time=$start_time" $ccfg
|
|
|
|
|
}
|
2020-07-26 21:21:47 +08:00
|
|
|
|
start_redir(){
|
2020-07-24 20:38:40 +08:00
|
|
|
|
#修改iptables规则使流量进入clash
|
2020-07-23 12:32:13 +08:00
|
|
|
|
iptables -t nat -N clash
|
2020-07-24 14:41:21 +08:00
|
|
|
|
iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
|
|
|
|
|
iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
|
2020-07-23 09:59:11 +08:00
|
|
|
|
iptables -t nat -A clash -p tcp -j REDIRECT --to-ports 7892
|
|
|
|
|
iptables -t nat -A PREROUTING -p tcp -j clash
|
2020-07-23 12:32:13 +08:00
|
|
|
|
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
|
2020-07-30 17:40:14 +08:00
|
|
|
|
ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
|
2020-07-23 12:32:13 +08:00
|
|
|
|
}
|
2020-07-26 21:21:47 +08:00
|
|
|
|
stop_redir(){
|
2020-07-23 12:32:13 +08:00
|
|
|
|
#重置iptables规则
|
2020-07-26 21:21:47 +08:00
|
|
|
|
iptables -t nat -D PREROUTING -p tcp -j clash > /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 -D PREROUTING -p udp --dport 53 -j REDIRECT --to 1053 > /dev/null 2>&1
|
2020-07-31 00:36:08 +08:00
|
|
|
|
ip6tables -t nat -D PREROUTING -p udp --dport 53 -j REDIRECT --to 1053 > /dev/null 2>&1
|
2020-07-26 21:21:47 +08:00
|
|
|
|
}
|
|
|
|
|
start_tun(){
|
|
|
|
|
#修改iptables规则使流量进入clash
|
|
|
|
|
iptables -I FORWARD -o utun -j ACCEPT
|
2020-07-31 00:36:08 +08:00
|
|
|
|
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
|
2020-07-30 17:40:14 +08:00
|
|
|
|
ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
|
2020-07-26 21:21:47 +08:00
|
|
|
|
}
|
|
|
|
|
stop_tun(){
|
|
|
|
|
iptables -t nat -D PREROUTING -p udp --dport 53 -j REDIRECT --to 1053 > /dev/null 2>&1
|
2020-07-30 17:40:14 +08:00
|
|
|
|
ip6tables -t nat -D PREROUTING -p udp --dport 53 -j REDIRECT --to 1053 > /dev/null 2>&1
|
2020-07-26 21:21:47 +08:00
|
|
|
|
}
|
|
|
|
|
start_service() {
|
2020-07-31 00:36:08 +08:00
|
|
|
|
getconfig
|
2020-07-26 21:21:47 +08:00
|
|
|
|
#创建clash后台进程
|
|
|
|
|
procd_open_instance
|
|
|
|
|
procd_set_param respawn
|
|
|
|
|
procd_set_param stderr 1
|
|
|
|
|
procd_set_param stdout 1
|
2020-07-31 00:36:08 +08:00
|
|
|
|
procd_set_param command $cpath/clash -d $cpath
|
2020-07-26 21:21:47 +08:00
|
|
|
|
procd_close_instance
|
|
|
|
|
#修改iptables规则使流量进入clash
|
2020-07-29 23:18:10 +08:00
|
|
|
|
if [[ "$redir_mod" = "Tun模式" ]];then
|
2020-07-26 21:21:47 +08:00
|
|
|
|
stop_tun
|
|
|
|
|
start_tun
|
2020-07-29 23:18:10 +08:00
|
|
|
|
mark_time
|
2020-07-26 21:21:47 +08:00
|
|
|
|
else
|
|
|
|
|
stop_redir
|
|
|
|
|
start_redir
|
2020-07-29 23:18:10 +08:00
|
|
|
|
mark_time
|
2020-07-26 21:21:47 +08:00
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
stop_service() {
|
2020-07-29 23:18:10 +08:00
|
|
|
|
getconfig
|
|
|
|
|
if [[ "$redir_mod" = "Tun模式" ]];then
|
2020-07-26 21:21:47 +08:00
|
|
|
|
stop_tun
|
|
|
|
|
else
|
|
|
|
|
stop_redir
|
|
|
|
|
fi
|
2020-07-17 17:40:34 +08:00
|
|
|
|
}
|