1
0
Fork 0
mirror of https://github.com/Ysurac/openmptcprouter-feeds.git synced 2025-02-13 19:11:51 +00:00
openmptcprouter-feeds/luci-app-omr-bypass/root/etc/init.d/omr-bypass

641 lines
19 KiB
Text
Raw Normal View History

2018-05-15 15:20:49 +00:00
#!/bin/sh /etc/rc.common
2020-09-04 19:53:16 +00:00
# Copyright (C) 2018-2020 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
2018-05-15 15:20:49 +00:00
2018-06-22 08:50:17 +00:00
START=99
STOP=10
USE_PROCD=1
2019-11-01 08:27:38 +00:00
EXTRA_COMMANDS="reload_rules bypass_asn"
2018-05-15 15:20:49 +00:00
2018-07-10 09:04:23 +00:00
. /usr/lib/unbound/iptools.sh
_add_proto() {
protoname=$1
2020-01-15 18:52:18 +00:00
if [ "$(dd if=/proc/net/xt_ndpi/proto bs=4096 2> /dev/null | grep $protoname)" = "" ]; then
2019-09-11 06:37:38 +00:00
echo "add_custom $protoname" >/proc/net/xt_ndpi/proto
fi
hosts="$( uci -q get omr-bypass.$protoname.url )"
for url in $hosts; do
echo "$protoname:$url" >/proc/net/xt_ndpi/host_proto
done
ip="$( uci -q get omr-bypass.$protoname.ip )"
for ip in $ips; do
echo "$protoname:$ip" >/proc/net/xt_ndpi/ip_proto
done
}
2018-05-15 15:20:49 +00:00
_bypass_ip() {
local ip=$1
local type=$2
[ -z "$type" ] && type="all"
2018-06-22 08:50:17 +00:00
valid_ip4=$( valid_subnet4 $ip)
valid_ip6=$( valid_subnet6 $ip)
if [ "$valid_ip4" = "ok" ]; then
ipset -q add omr_dst_bypass_$type $ip
2018-06-22 08:50:17 +00:00
elif [ "$valid_ip6" = "ok" ]; then
ipset -q add omr6_dst_bypass_$type $ip
2018-06-22 08:50:17 +00:00
fi
}
2018-11-28 12:22:13 +00:00
_bypass_domains() {
local domain
local intf
config_get domain $1 name
config_get intf $1 interface
2018-11-28 12:22:13 +00:00
_bypass_domain $domain $intf
}
_bypass_domain() {
local domain=$1
local intf=$2
[ -z "$intf" ] && intf="all"
if [ -n "$domain" ]; then
2019-03-13 17:32:09 +00:00
domain=$(echo $domain | sed 's:^\.::')
#logger -t "omr-bypass" "Get IPs of $domain..."
if [ -z $RELOAD ]; then
2019-08-02 20:39:02 +00:00
resolve=$(dig a +timeout=1 +nocmd +noall +answer $domain | awk '{print $5}')
2018-10-30 09:39:16 +00:00
for ip in $resolve; do
_bypass_ip $ip $intf
done
if [ "$disableipv6" != "1" ]; then
resolve=$(dig aaaa +timeout=1 +nocmd +noall +answer $domain | awk '{print $5}')
for ip in $resolve; do
_bypass_ip $ip $intf
done
fi
2018-10-30 09:39:16 +00:00
fi
if [ "$(uci -q get dhcp.@dnsmasq[0].ipset | grep /$domain/)" = "" ]; then
uci -q add_list dhcp.@dnsmasq[0].ipset="/$domain/omr_dst_bypass_$intf,omr6_dst_bypass_$intf"
else
dnsmasqipset=$(uci -q get dhcp.@dnsmasq[0].ipset | sed 's/ /\n/g')
for dnsipset in $dnsmasqipset; do
if [ "$(echo $dnsipset | cut -d/ -f2)" = "$domain" ]; then
uci -q del_list dhcp.@dnsmasq[0].ipset=$dnsipset
uci -q add_list dhcp.@dnsmasq[0].ipset="$dnsipset,omr_dst_bypass_$intf,omr6_dst_bypass_$intf"
fi
done
fi
#logger -t "omr-bypass" "Get IPs of $domain... Done"
fi
}
2018-11-02 19:19:50 +00:00
_bypass_mac() {
local mac
local intf
config_get mac $1 mac
config_get intf $1 interface
local intfid="$(uci -q get omr-bypass.$intf.id)"
[ -z "$intf" ] && intf="all"
[ -z "$mac" ] && return
if [ "$intf" = "all" ]; then
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2018-11-02 19:19:50 +00:00
*mangle
2018-12-02 07:02:18 +00:00
-A omr-bypass -m mac --mac-source $mac -j MARK --set-mark 0x539
2018-11-02 19:19:50 +00:00
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 -m mac --mac-source $mac -j MARK --set-mark 0x6539
COMMIT
EOF
fi
2018-11-02 19:19:50 +00:00
else
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2018-11-02 19:19:50 +00:00
*mangle
2018-12-02 07:02:18 +00:00
-A omr-bypass -m mac --mac-source $mac -j MARK --set-mark 0x539$intfid
2018-11-02 19:19:50 +00:00
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 -m mac --mac-source $mac -j MARK --set-mark 0x6539$intfid
COMMIT
EOF
fi
2018-11-02 19:19:50 +00:00
fi
}
2018-11-15 14:35:02 +00:00
_bypass_lan_ip() {
local ip
local intf
config_get ip $1 ip
config_get intf $1 interface
local intfid="$(uci -q get omr-bypass.$intf.id)"
[ -z "$intf" ] && intf="all"
[ -z "$ip" ] && return
2019-08-17 18:21:31 +00:00
valid_ip4=$(valid_subnet4 $ip)
valid_ip6=$(valid_subnet6 $ip)
2018-11-15 14:35:02 +00:00
if [ "$intf" = "all" ]; then
2019-07-26 18:10:53 +00:00
if [ "$valid_ip4" = "ok" ]; then
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2019-07-26 18:10:53 +00:00
*mangle
-A omr-bypass -s $ip -j MARK --set-mark 0x539
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local -s $ip -j MARK --set-mark 0x539
COMMIT
EOF
2019-07-26 18:10:53 +00:00
elif [ "$valid_ip6" = "ok" ]; then
2019-12-30 08:15:08 +00:00
ip6tables-restore -w --wait=60 --noflush <<-EOF
2019-07-26 18:10:53 +00:00
*mangle
-A omr-bypass6 -s $ip -j MARK --set-mark 0x6539
COMMIT
EOF
fi
2018-11-15 14:35:02 +00:00
else
2019-07-26 18:10:53 +00:00
if [ "$valid_ip4" = "ok" ]; then
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2019-07-26 18:10:53 +00:00
*mangle
-A omr-bypass -s $ip -j MARK --set-mark 0x539$intfid
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local -s $ip -j MARK --set-mark 0x539$intfid
COMMIT
EOF
2019-07-26 18:10:53 +00:00
elif [ "$valid_ip6" = "ok" ]; then
2019-12-30 08:15:08 +00:00
ip6tables-restore -w --wait=60 --noflush <<-EOF
2019-07-26 18:10:53 +00:00
*mangle
-A omr-bypass6 -s $ip -j MARK --set-mark 0x6539$intfid
COMMIT
EOF
fi
2018-11-15 14:35:02 +00:00
fi
}
2019-10-26 18:16:31 +00:00
_bypass_dest_port() {
local intf
config_get dport $1 dport
config_get proto $1 proto
config_get intf $1 interface
local intfid="$(uci -q get omr-bypass.$intf.id)"
[ -z "$intf" ] && intf="all"
2019-12-07 19:13:54 +00:00
[ -z "$dport" ] && return
dport="$(echo $dport | sed 's/-/:/')"
2019-12-07 19:13:54 +00:00
[ -z "$proto" ] && return
2019-10-26 18:16:31 +00:00
if [ "$intf" = "all" ]; then
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2019-12-07 19:13:54 +00:00
*mangle
-A omr-bypass --protocol $proto --destination-port $dport -j MARK --set-mark 0x539
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local --protocol $proto --destination-port $dport -j MARK --set-mark 0x539
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 --protocol $proto --destination-port $dport -j MARK --set-mark 0x6539
COMMIT
EOF
fi
2019-10-26 18:16:31 +00:00
else
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2019-12-07 19:13:54 +00:00
*mangle
-A omr-bypass --protocol $proto --destination-port $dport -j MARK --set-mark 0x539$intfid
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local --protocol $proto --destination-port $dport -j MARK --set-mark 0x539$intfid
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 --protocol $proto --destination-port $dport -j MARK --set-mark 0x6539$intfid
COMMIT
EOF
fi
2019-10-26 18:16:31 +00:00
fi
}
2020-05-13 19:46:35 +00:00
_bypass_src_port() {
local intf
config_get sport $1 sport
config_get proto $1 proto
config_get intf $1 interface
local intfid="$(uci -q get omr-bypass.$intf.id)"
[ -z "$intf" ] && intf="all"
[ -z "$sport" ] && return
sport="$(echo $sport | sed 's/-/:/')"
[ -z "$proto" ] && return
if [ "$intf" = "all" ]; then
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass --protocol $proto --source-port $sport -j MARK --set-mark 0x539
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local --protocol $proto --source-port $sport -j MARK --set-mark 0x539
COMMIT
EOF
2020-05-13 19:46:35 +00:00
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 --protocol $proto --source-port $sport -j MARK --set-mark 0x6539
COMMIT
EOF
fi
else
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass --protocol $proto --source-port $sport -j MARK --set-mark 0x539$intfid
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local --protocol $proto --source-port $sport -j MARK --set-mark 0x539$intfid
COMMIT
EOF
2020-05-13 19:46:35 +00:00
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 --protocol $proto --source-port $sport -j MARK --set-mark 0x6539$intfid
COMMIT
EOF
fi
fi
}
2018-06-22 08:50:17 +00:00
_bypass_proto() {
local proto
local intf
config_get proto $1 proto
config_get intf $1 interface
local intfid="$(uci -q get omr-bypass.$intf.id)"
[ -z "$intf" ] && intf="all"
2018-07-10 08:56:43 +00:00
[ -z "$proto" ] && return
if [ "$intf" = "all" ]; then
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2018-10-20 17:20:51 +00:00
*mangle
2018-11-27 14:21:10 +00:00
-A omr-bypass-dpi -m ndpi --proto $proto -j MARK --set-mark 0x539
2018-10-20 17:20:51 +00:00
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6-dpi -m ndpi --proto $proto -j MARK --set-mark 0x6539
COMMIT
EOF
fi
else
2019-12-30 08:15:08 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
2018-10-20 17:20:51 +00:00
*mangle
2018-11-27 14:21:10 +00:00
-A omr-bypass-dpi -m ndpi --proto $proto -j MARK --set-mark 0x539$intfid
2018-10-20 17:20:51 +00:00
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6-dpi -m ndpi --proto $proto -j MARK --set-mark 0x6539$intfid
COMMIT
EOF
fi
fi
2018-11-28 12:22:13 +00:00
# Use dnsmasq ipset to bypass domains of the proto
local domains
domains="$(cat /proc/net/xt_ndpi/host_proto | grep -i $proto: | sed -e "s/$proto://" -e 's/*//' -e 's/,/ /g')"
2018-11-28 12:22:13 +00:00
if [ -n "$domains" ]; then
for domain in $domains; do
2020-09-02 08:27:44 +00:00
if [ -n "$domain" ]; then
2018-11-28 12:22:13 +00:00
_bypass_domain $domain $intf
fi
done
fi
}
2020-06-29 13:11:35 +00:00
_intf_rule_ss_rules() {
rule_name=$1
2020-08-21 08:00:52 +00:00
[ "$rule_name" = "ss_rules" ] && rule_name="def"
2020-08-24 14:43:59 +00:00
if [ "$(iptables --wait=40 -t nat -L -n | grep ssr_${rule_name}_pre_src)" != "" ] && [ "$(iptables --wait=40 -t nat -L -n | grep omr_dst_bypass_$intf)" = "" ]; then
2020-06-29 13:11:35 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*nat
2020-08-24 14:53:36 +00:00
-I ssr_${rule_name}_dst 1 -m set --match-set omr_dst_bypass_$intf dst -j MARK --set-mark 0x539$count
2020-09-04 19:53:16 +00:00
-I ssr_${rule_name}_dst 2 -m mark --mark 0x539$count -j RETURN
2020-08-24 14:53:36 +00:00
-I ssr_${rule_name}_local_out 1 -m set --match-set omr_dst_bypass_$intf dst -j MARK --set-mark 0x539$count
2020-08-24 14:43:59 +00:00
-I ssr_${rule_name}_local_out 2 -m mark --mark 0x539$count -j RETURN
-I ssr_${rule_name}_pre_src 1 -m set --match-set omr_dst_bypass_$intf dst -j MARK --set-mark 0x539$count
-I ssr_${rule_name}_pre_src 2 -m mark --mark 0x539$count -j RETURN
2020-06-29 13:11:35 +00:00
COMMIT
EOF
fi
if [ "$disableipv6" != "1" ]; then
2020-09-10 19:03:44 +00:00
if [ "$(ip6tables --wait=40 -t mangle -L -n | grep omr6_dst_bypass_$intf)" = "" ]; then
2020-06-29 13:11:35 +00:00
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-I omr-bypass6 1 -m set --match-set omr6_dst_bypass_$intf dst -j MARK --set-mark 0x6539$count
COMMIT
EOF
fi
2020-09-10 19:03:44 +00:00
if [ "$(ip6tables --wait=40 -t nat -L -n | grep ssr6_${rule_name}_pre_src)" != "" ] && [ "$(ip6tables --wait=40 -t nat -L -n | grep omr6_dst_bypass_$intf)" = "" ]; then
2020-06-29 13:11:35 +00:00
ip6tables-restore -w --wait=60 --noflush <<-EOF
*nat
2020-08-24 14:53:36 +00:00
-I ssr6_${rule_name}_dst 1 -m set --match-set omr6_dst_bypass_$intf dst -j MARK --set-mark 0x6539$count
2020-09-04 19:53:16 +00:00
-I ssr6_${rule_name}_dst 2 -m mark --mark 0x6539$count -j RETURN
2020-08-24 14:53:36 +00:00
-I ssr6_${rule_name}_local_out 1 -m set --match-set omr6_dst_bypass_$intf dst -j MARK --set-mark 0x6539$count
2020-08-24 14:43:59 +00:00
-I ssr6_${rule_name}_local_out 2 -m mark --mark 0x6539$count -j RETURN
-I ssr6_${rule_name}_pre_src 1 -m set --match-set omr6_dst_bypass_$intf dst -j MARK --set-mark 0x6539$count
-I ssr6_${rule_name}_pre_src 2 -m mark --mark 0x6539$count -j RETURN
2020-06-29 13:11:35 +00:00
COMMIT
EOF
fi
fi
}
_intf_rule() {
2018-10-13 21:33:12 +00:00
local intf
2018-10-14 17:15:26 +00:00
config_get intf $1 ifname
2019-01-27 09:15:33 +00:00
[ -z "$intf" ] && intf=$(ifstatus "$1" | jsonfilter -q -e '@["l3_device"]')
2018-12-21 14:42:14 +00:00
#count=$((count+1))
config_get count $1 metric
2018-10-13 21:33:12 +00:00
local mode
#config_get mode $1 multipath "off"
#[ "$mode" = "off" ] && return
2019-01-27 09:15:33 +00:00
[ -z "$count" ] && return
[ -z "$intf" ] && return
2018-10-15 13:11:04 +00:00
[ "$(echo $1 | grep _dev)" != "" ] && return
[ -z "$RELOAD" ] || [ "$(ipset --list | grep omr_dst_bypass_$intf)" = "" ] && {
unset RELOAD
ipset -q flush omr_dst_bypass_$intf > /dev/null 2>&1
ipset -q flush omr6_dst_bypass_$intf > /dev/null 2>&1
ipset -q --exist restore <<-EOF
create omr_dst_bypass_$intf hash:net hashsize 64
create omr6_dst_bypass_$intf hash:net family inet6 hashsize 64
2019-01-20 19:42:36 +00:00
EOF
if [ "$(uci -q get openmptcprouter.settings.uci_rules)" = "1" ]; then
uci -q batch <<-EOF >/dev/null
delete network.${1}_fw_rule=rule
set network.${1}_fw_rule=rule
set network.${1}_fw_rule.priority=1
set network.${1}_fw_rule.mark=0x539${count}
set network.${1}_fw_rule.lookup=${count}
delete network.${1}_fw_rule6=rule6
set network.${1}_fw_rule6=rule6
set network.${1}_fw_rule6.priority=1
set network.${1}_fw_rule6.mark=0x6539${count}
set network.${1}_fw_rule6.lookup=${count}
commit network
EOF
else
ip rule add prio 1 fwmark 0x539$count lookup $count pref 1 > /dev/null 2>&1
ip -6 rule add prio 1 fwmark 0x6539$count lookup 6$count > /dev/null 2>&1
fi
}
2019-12-30 08:15:08 +00:00
if [ "$(iptables --wait=40 -t mangle -L -n | grep omr_dst_bypass_$intf)" = "" ]; then
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-I omr-bypass 1 -m set --match-set omr_dst_bypass_$intf dst -j MARK --set-mark 0x539$count
2018-10-18 12:53:42 +00:00
COMMIT
EOF
fi
2020-06-29 13:11:35 +00:00
config_load shadowsocks-libev
config_foreach _intf_rule_ss_rules ss_rules
uci -q set omr-bypass.$intf=interface
uci -q set omr-bypass.$intf.id=$count
}
_bypass_ip_set() {
local ip
local interface
config_get ip $1 ip
config_get interface $1 interface
_bypass_ip $ip $interface
2018-05-15 15:20:49 +00:00
}
2019-01-03 19:41:43 +00:00
_bypass_asn() {
local asn
local interface
config_get asn $1 asn
config_get interface $1 interface
local asnips
asnips=`curl --max-time 4 -s -k https://stat.ripe.net/data/announced-prefixes/data.json?resource=${asn} | jsonfilter -q -e '@.data.prefixes.*.prefix'`
for ip in $asnips; do
_bypass_ip $ip $interface
done
}
2019-11-01 08:27:38 +00:00
bypass_asn() {
config_load omr-bypass
config_foreach _bypass_asn asns
}
_bypass_omr_server() {
local ip
config_get ip $1 ip
_bypass_ip $ip
}
2020-06-29 13:11:35 +00:00
_ss_rules_config() {
rule_name=$1
2020-08-21 08:00:52 +00:00
[ "$rule_name" = "ss_rules" ] && rule_name="def"
2020-08-24 14:43:59 +00:00
if [ "$(iptables --wait=40 -t nat -L -n | grep ssr_${rule_name}_pre_src)" != "" ] && [ "$(iptables --wait=40 -t nat -L -n | grep omr_dst_bypass_all)" = "" ]; then
2020-06-29 13:11:35 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*nat
2020-08-24 14:53:36 +00:00
-I ssr_${rule_name}_dst 1 -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
-I ssr_${rule_name}_dst 1 -m mark --mark 0x539 -j RETURN
-I ssr_${rule_name}_local_out 1 -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
2020-08-24 14:43:59 +00:00
-I ssr_${rule_name}_local_out 2 -m mark --mark 0x539 -j RETURN
-I ssr_${rule_name}_pre_src 1 -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
-I ssr_${rule_name}_pre_src 2 -m mark --mark 0x539 -j RETURN
2020-06-29 13:11:35 +00:00
COMMIT
EOF
fi
if [ "$disableipv6" != "1" ]; then
2020-09-10 19:03:44 +00:00
if [ "$(ip6tables --wait=40 -t mangle -L -n | grep 'match-set omr6_dst_bypass_all dst MARK set')" = "" ]; then
2020-06-29 13:11:35 +00:00
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 -m set --match-set omr6_dst_bypass_all dst -j MARK --set-mark 0x6539
COMMIT
EOF
fi
2020-09-10 19:03:44 +00:00
if [ "$(ip6tables --wait=40 -t nat -L -n | grep ssr6_${rule_name}_pre_src)" != "" ] && [ "$(ip6tables --wait=40 -t nat -L -n | grep omr6_dst_bypass_all)" = "" ]; then
2020-06-29 13:11:35 +00:00
ip6tables-restore -w --wait=60 --noflush <<-EOF
*nat
2020-08-24 14:53:36 +00:00
-I ssr6_${rule_name}_dst 1 -m set --match-set omr6_dst_bypass_all dst -j MARK --set-mark 0x6539
-I ssr6_${rule_name}_dst 1 -m mark --mark 0x6539 -j RETURN
-I ssr6_${rule_name}_local_out 1 -m set --match-set omr6_dst_bypass_all dst -j MARK --set-mark 0x6539
2020-08-24 14:43:59 +00:00
-I ssr6_${rule_name}_local_out 2 -m mark --mark 0x6539 -j RETURN
-I ssr6_${rule_name}_pre_src 1 -m set --match-set omr6_dst_bypass_all dst -j MARK --set-mark 0x6539
-I ssr6_${rule_name}_pre_src 2 -m mark --mark 0x6539 -j RETURN
2020-06-29 13:11:35 +00:00
COMMIT
EOF
fi
fi
}
2018-10-30 09:39:16 +00:00
boot() {
BOOT=1
start "$@"
}
2018-05-15 15:20:49 +00:00
start_service() {
2018-12-21 14:42:14 +00:00
#local count
2018-10-13 19:40:42 +00:00
logger -t "omr-bypass" "Starting OMR-ByPass..."
config_load omr-bypass
config_foreach _add_proto proto
disableipv6="$(uci -q get openmptcprouter.settings.disable_ipv6)"
2019-06-18 16:44:39 +00:00
[ -n "$RELOAD" ] && [ "$(ipset --list | grep omr_dst_bypass_all)" = "" ] && {
unset RELOAD
}
[ -z "$RELOAD" ] && {
ipset -q flush omr_dst_bypass_all > /dev/null 2>&1
ipset -q flush omr6_dst_bypass_all > /dev/null 2>&1
ipset -q --exist restore <<-EOF
create omr_dst_bypass_all hash:net hashsize 64
create omr6_dst_bypass_all hash:net family inet6 hashsize 64
EOF
}
2019-12-30 08:15:08 +00:00
iptables-save --counters | grep -v omr-bypass | iptables-restore -w --counters
iptables-restore -w --wait=60 --noflush <<-EOF
2018-12-02 07:02:18 +00:00
*mangle
:omr-bypass -
2020-07-29 08:57:04 +00:00
-I PREROUTING -m addrtype ! --dst-type LOCAL -j omr-bypass
2020-08-24 14:53:36 +00:00
COMMIT
EOF
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
:omr-bypass-local -
-I OUTPUT -m addrtype ! --dst-type LOCAL -j omr-bypass-local
2018-12-02 07:02:18 +00:00
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-save --counters | grep -v omr-bypass6 | ip6tables-restore -w --counters
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
:omr-bypass6 -
2020-07-29 08:57:04 +00:00
-I PREROUTING -m addrtype ! --dst-type LOCAL -j omr-bypass6
COMMIT
EOF
fi
2019-07-26 18:10:53 +00:00
config_load network
config_foreach _intf_rule interface
local ndpi_rules=""
2020-09-04 19:53:16 +00:00
if [ "$(uci -q get openmptcprouter.settings.bypass_servers)" = "1" ]; then
config_load openmptcprouter
config_foreach _bypass_omr_server server
fi
config_load omr-bypass
config_foreach _bypass_ip_set ips
2018-11-02 19:19:50 +00:00
config_foreach _bypass_mac macs
2018-11-15 14:35:02 +00:00
config_foreach _bypass_lan_ip lan_ip
2019-10-26 18:16:31 +00:00
config_foreach _bypass_dest_port dest_port
2020-05-13 19:46:35 +00:00
config_foreach _bypass_src_port src_port
2019-01-03 19:41:43 +00:00
config_foreach _bypass_asn asns
2019-04-15 20:13:41 +00:00
dnsmasqipset=$(uci -q get dhcp.@dnsmasq[0].ipset | sed 's/ /\n/g' | grep -v dst_bypass)
2019-02-03 18:19:21 +00:00
uci -q delete dhcp.@dnsmasq[0].ipset
2019-04-15 20:13:41 +00:00
if [ -n "$dnsmasqipset" ]; then
for dnsipset in $dnsmasqipset; do
ipsets=""
allipsets=$(echo $dnsipset | cut -d/ -f3 | sed 's/,/\n/g')
for ipset in $allipsets; do
[ "$(echo $ipset | grep -v dst_bypass)" != "" ] && {
[ "$ipsets" != "" ] && ipsets="$ipsets,$ipset"
[ "$ipsets" = "" ] && ipsets="$ipset"
}
done
if [ "$ipsets" != "" ]; then
resultipset="/$(echo $dnsipset | cut -d/ -f2)/$ipsets"
uci -q add_list dhcp.@dnsmasq[0].ipset=$resultipset
fi
2019-04-15 20:13:41 +00:00
done
fi
2018-11-28 12:22:13 +00:00
config_foreach _bypass_domains domains
uci -q commit dhcp
ip rule add prio 1 fwmark 0x539 lookup 991337 > /dev/null 2>&1
2018-10-26 14:50:27 +00:00
ip -6 rule add prio 1 fwmark 0x6539 lookup 6991337 > /dev/null 2>&1
2018-06-22 08:50:17 +00:00
2019-12-30 08:15:08 +00:00
if [ "$(iptables --wait=40 -t mangle -L -n | grep 'match-set omr_dst_bypass_all dst MARK set')" = "" ]; then
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
COMMIT
EOF
2020-08-24 14:53:36 +00:00
iptables-restore -w --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass-local -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
COMMIT
EOF
fi
2020-06-29 13:11:35 +00:00
config_load shadowsocks-libev
config_foreach _ss_rules_config
2018-10-23 13:07:23 +00:00
2019-12-30 08:15:08 +00:00
iptables-save --counters | grep -v omr-bypass-dpi | iptables-restore -w --counters
iptables-restore -w --wait=60 --noflush <<-EOF
2018-10-22 13:44:36 +00:00
*mangle
:omr-bypass-dpi -
-A PREROUTING -m addrtype ! --dst-type LOCAL -j omr-bypass-dpi
2020-08-21 08:00:52 +00:00
-A OUTPUT -m addrtype ! --dst-type LOCAL -j omr-bypass-dpi
2018-10-23 13:07:23 +00:00
-A POSTROUTING -m addrtype --dst-type LOCAL -j omr-bypass-dpi
2018-10-22 13:44:36 +00:00
COMMIT
EOF
if [ "$disableipv6" != "1" ]; then
ip6tables-save --counters | grep -v omr-bypass6-dpi | ip6tables-restore -w --counters
ip6tables-restore -w --wait=60 --noflush <<-EOF
*mangle
:omr-bypass6-dpi -
-A PREROUTING -m addrtype ! --dst-type LOCAL -j omr-bypass6-dpi
2020-08-21 08:00:52 +00:00
-A OUTPUT -m addrtype ! --dst-type LOCAL -j omr-bypass6-dpi
-A POSTROUTING -m addrtype --dst-type LOCAL -j omr-bypass6-dpi
COMMIT
EOF
fi
2018-10-20 17:20:51 +00:00
config_load omr-bypass
config_foreach _bypass_proto dpis
2018-10-23 13:07:23 +00:00
[ -z "$RELOAD" ] && {
logger -t "omr-bypass" "Restart dnsmasq..."
/etc/init.d/dnsmasq restart
2019-07-26 17:56:07 +00:00
}
[ -n "$RELOAD" ] && {
logger -t "omr-bypass" "Reload dnsmasq..."
/etc/init.d/dnsmasq reload
}
2018-10-13 19:40:42 +00:00
logger -t "omr-bypass" "OMR-ByPass is running"
2018-05-15 15:20:49 +00:00
}
2018-12-02 07:02:18 +00:00
stop_service() {
2019-12-30 08:15:08 +00:00
iptables-save --counters | grep -v omr-bypass | iptables-restore -w --counters
iptables-save --counters | grep -v omr_dst | iptables-restore -w --counters
ip6tables-save --counters | grep -v omr-bypass6 | ip6tables-restore -w --counters
ip6tables-save --counters | grep -v omr6_dst | ip6tables-restore -w --counters
2018-10-30 09:39:16 +00:00
for setname in $(ipset -n list | grep "omr_"); do
2020-07-16 13:59:15 +00:00
ipset -q destroy "$setname" 2>/dev/null || true
2018-10-30 09:39:16 +00:00
done
}
2018-05-15 15:20:49 +00:00
service_triggers() {
2018-10-18 12:53:42 +00:00
PROCD_RELOAD_DELAY=1000
2018-10-20 17:20:51 +00:00
procd_add_reload_trigger omr-bypass
2018-10-18 12:53:42 +00:00
procd_add_raw_trigger "interface.*" 2000 /etc/init.d/omr-bypass restart
2018-05-15 15:20:49 +00:00
}
reload_service() {
start
}
reload_rules() {
2018-12-02 07:02:18 +00:00
[ "$( ipset -n list | grep omr_ )" = "" ] && return 0
RELOAD=1
2018-10-30 09:39:16 +00:00
start
2018-05-15 15:20:49 +00:00
}