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

397 lines
12 KiB
Text
Raw Normal View History

2018-05-15 15:20:49 +00:00
#!/bin/sh /etc/rc.common
# Copyright (C) 2018 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
2018-05-15 15:20:49 +00:00
2018-07-10 09:04:23 +00:00
. /usr/lib/unbound/iptools.sh
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
#logger -t "omr-bypass" "Get IPs of $domain..."
2018-10-30 09:39:16 +00:00
if [ -z $BOOT ]; then
resolve=$(dig a +timeout=3 +nocmd +noall +answer $domain | awk '{print $5}')
for ip in $resolve; do
_bypass_ip $ip $intf
done
resolve=$(dig aaaa +timeout=3 +nocmd +noall +answer $domain | awk '{print $5}')
for ip in $resolve; do
_bypass_ip $ip $intf
done
fi
uci -q add_list dhcp.@dnsmasq[0].ipset="/$domain/omr_dst_bypass_$intf,omr6_dst_bypass_$intf"
#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
iptables-restore --wait=60 --noflush <<-EOF
*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
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
-A omr-bypass6 -m mac --mac-source $mac -j MARK --set-mark 0x6539
2018-11-02 19:19:50 +00:00
COMMIT
EOF
else
iptables-restore --wait=60 --noflush <<-EOF
*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
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
-A omr-bypass6 -m mac --mac-source $mac -j MARK --set-mark 0x6539$intfid
2018-11-02 19:19:50 +00:00
COMMIT
EOF
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
if [ "$intf" = "all" ]; then
iptables-restore --wait=60 --noflush <<-EOF
*mangle
2018-12-02 07:02:18 +00:00
-A omr-bypass -s $ip -j MARK --set-mark 0x539
2018-11-15 14:35:02 +00:00
COMMIT
EOF
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
-A omr-bypass6 -s $ip -j MARK --set-mark 0x6539
2018-11-15 14:35:02 +00:00
COMMIT
EOF
else
iptables-restore --wait=60 --noflush <<-EOF
*mangle
2018-12-02 07:02:18 +00:00
-A omr-bypass -s $ip -j MARK --set-mark 0x539$intfid
2018-11-15 14:35:02 +00:00
COMMIT
EOF
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
-A omr-bypass6 -s $ip -j MARK --set-mark 0x6539$intfid
2018-11-15 14:35:02 +00:00
COMMIT
EOF
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
2018-10-20 17:20:51 +00:00
iptables-restore --wait=60 --noflush <<-EOF
*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
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
-A omr-bypass6-dpi -m ndpi --proto $proto -j MARK --set-mark 0x6539
2018-10-20 17:20:51 +00:00
COMMIT
EOF
else
2018-10-20 17:20:51 +00:00
iptables-restore --wait=60 --noflush <<-EOF
*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
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
-A omr-bypass6-dpi -m ndpi --proto $proto -j MARK --set-mark 0x6539$intfid
2018-10-20 17:20:51 +00:00
COMMIT
EOF
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 $proto: | sed -e "s/$proto://" -e 's/*//' -e 's/,/ /g')"
if [ -n "$domains" ]; then
for domain in $domains; do
2019-01-27 09:15:33 +00:00
if [ -n "$domain" ] && [ "$(uci -q show omr-bypass | grep $domain)" = "" ]; then
2018-11-28 12:22:13 +00:00
_bypass_domain $domain $intf
fi
done
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
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
EOF
2019-02-03 18:19:21 +00:00
if [ "$(uci -q get openmptcprouter.settings.uci_rules)" = "1" ]; then
2019-01-20 19:42:36 +00:00
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
if [ "$(iptables -w 40 -t mangle -L | grep omr_dst_bypass_$intf)" = "" ]; then
iptables-restore --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
if [ "$(iptables -w 40 -t nat -L | grep ss_rules_pre_src)" != "" ] && [ "$(iptables -w 40 -t nat -L | grep omr_dst_bypass_$intf)" = "" ]; then
2018-10-18 12:53:42 +00:00
iptables-restore --wait=60 --noflush <<-EOF
*nat
-I ss_rules_dst 1 -m set --match-set omr_dst_bypass_$intf dst -j RETURN
-I ss_rules_local_out 1 -m set --match-set omr_dst_bypass_$intf dst -j RETURN
2018-10-20 17:20:51 +00:00
-I ss_rules_local_out 2 -m mark --mark 0x539$count -j RETURN
-I ss_rules_pre_src 1 -m set --match-set omr_dst_bypass_$intf dst -j MARK --set-mark 0x539$count
2018-12-02 07:02:18 +00:00
-I ss_rules_pre_src 2 -m mark --mark 0x539$count -j RETURN
COMMIT
EOF
fi
if [ "$(ip6tables -w 40 -t mangle -L | grep omr6_dst_bypass_$intf)" = "" ]; then
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
-I omr-bypass6 1 -m set --match-set omr6_dst_bypass_$intf dst -j MARK --set-mark 0x6539$count
2018-10-18 12:53:42 +00:00
COMMIT
EOF
fi
if [ "$(ip6tables -w 40 -t nat -L | grep ss_rules6_pre_src)" != "" ] && [ "$(ip6tables -w 40 -t nat -L | grep omr6_dst_bypass_$intf)" = "" ]; then
2018-10-18 12:53:42 +00:00
ip6tables-restore --wait=60 --noflush <<-EOF
*nat
-I ss_rules6_dst 1 -m set --match-set omr6_dst_bypass_$intf dst -j RETURN
-I ss_rules6_local_out 1 -m set --match-set omr6_dst_bypass_$intf dst -j RETURN
2019-01-13 10:08:51 +00:00
-I ss_rules6_local_out 2 -m mark --mark 0x6539$count -j RETURN
-I ss_rules6_pre_src 1 -m set --match-set omr6_dst_bypass_$intf dst -j MARK --set-mark 0x6539$count
2019-01-13 10:08:51 +00:00
-I ss_rules6_pre_src 2 -m mark --mark 0x6539$count -j RETURN
COMMIT
EOF
fi
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
}
_bypass_omr_server() {
local ip
config_get ip $1 ip
_bypass_ip $ip
}
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..."
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
2018-12-02 07:02:18 +00:00
iptables-save --counters | grep -v omr-bypass | iptables-restore --counters
iptables-restore --wait=60 --noflush <<-EOF
*mangle
:omr-bypass -
-I PREROUTING 1 -m addrtype ! --dst-type LOCAL -j omr-bypass
COMMIT
EOF
2019-02-09 20:15:57 +00:00
ip6tables-save --counters | grep -v omr-bypass6 | ip6tables-restore --counters
2018-12-02 07:02:18 +00:00
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
:omr-bypass6 -
-I PREROUTING 1 -m addrtype ! --dst-type LOCAL -j omr-bypass6
2018-12-02 07:02:18 +00:00
COMMIT
EOF
config_load network
config_foreach _intf_rule interface
local ndpi_rules=""
config_load openmptcprouter
config_foreach _bypass_omr_server server
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-01-03 19:41:43 +00:00
config_foreach _bypass_asn asns
2019-02-03 18:19:21 +00:00
uci -q delete dhcp.@dnsmasq[0].ipset
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
if [ "$(iptables -w 40 -t mangle -L | grep 'match-set omr_dst_bypass_all dst MARK set')" = "" ]; then
iptables-restore --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
COMMIT
EOF
fi
if [ "$(iptables -w 40 -t nat -L | grep ss_rules_pre_src)" != "" ] && [ "$(iptables -w 40 -t nat -L | grep omr_dst_bypass_all)" = "" ]; then
2018-10-23 22:29:33 +00:00
iptables-restore --wait=60 --noflush <<-EOF
*nat
-I ss_rules_dst 1 -m set --match-set omr_dst_bypass_all dst -j RETURN
-I ss_rules_local_out 1 -m set --match-set omr_dst_bypass_all dst -j RETURN
2018-10-23 22:29:33 +00:00
-I ss_rules_local_out 2 -m mark --mark 0x539 -j RETURN
-I ss_rules_pre_src 1 -m set --match-set omr_dst_bypass_all dst -j MARK --set-mark 0x539
2018-12-02 07:02:18 +00:00
-I ss_rules_pre_src 2 -m mark --mark 0x539 -j RETURN
2018-10-23 22:29:33 +00:00
COMMIT
EOF
fi
if [ "$(ip6tables -w 40 -t mangle -L | grep 'match-set omr6_dst_bypass_all dst MARK set')" = "" ]; then
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
-A omr-bypass6 -m set --match-set omr6_dst_bypass_all dst -j MARK --set-mark 0x6539
2018-10-23 22:29:33 +00:00
COMMIT
EOF
fi
if [ "$(ip6tables -w 40 -t nat -L | grep ss_rules6_pre_src)" != "" ] && [ "$(ip6tables -w 40 -t nat -L | grep omr6_dst_bypass_all)" = "" ]; then
2018-10-23 22:29:33 +00:00
ip6tables-restore --wait=60 --noflush <<-EOF
*nat
-I ss_rules6_dst 1 -m set --match-set omr6_dst_bypass_all dst -j RETURN
-I ss_rules6_local_out 1 -m set --match-set omr6_dst_bypass_all dst -j RETURN
2018-10-23 22:29:33 +00:00
-I ss_rules6_local_out 2 -m mark --mark 0x6539 -j RETURN
-I ss_rules6_pre_src 1 -m set --match-set omr6_dst_bypass_all dst -j MARK --set-mark 0x6539
2018-12-02 07:02:18 +00:00
-I ss_rules6_pre_src 2 -m mark --mark 0x6539 -j RETURN
COMMIT
EOF
fi
2018-10-23 13:07:23 +00:00
2018-06-22 08:50:17 +00:00
iptables-save --counters | grep -v omr-bypass-dpi | iptables-restore --counters
2018-10-22 13:44:36 +00:00
iptables-restore --wait=60 --noflush <<-EOF
*mangle
:omr-bypass-dpi -
-A PREROUTING -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
2019-02-09 20:15:57 +00:00
ip6tables-save --counters | grep -v omr-bypass6-dpi | ip6tables-restore --counters
2018-10-20 17:20:51 +00:00
ip6tables-restore --wait=60 --noflush <<-EOF
*mangle
2019-02-09 20:15:57 +00:00
:omr-bypass6-dpi -
-A PREROUTING -m addrtype ! --dst-type LOCAL -j omr-bypass6-dpi
-A POSTROUTING -m addrtype --dst-type LOCAL -j omr-bypass6-dpi
2018-10-20 17:20:51 +00:00
COMMIT
EOF
config_load omr-bypass
config_foreach _bypass_proto dpis
2018-10-23 13:07:23 +00:00
2018-11-28 12:22:13 +00:00
/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() {
2018-10-30 09:39:16 +00:00
iptables-save --counters | grep -v omr-bypass | iptables-restore --counters
iptables-save --counters | grep -v omr_dst | iptables-restore --counters
2019-02-09 20:15:57 +00:00
ip6tables-save --counters | grep -v omr-bypass6 | ip6tables-restore --counters
ip6tables-save --counters | grep -v omr6_dst | ip6tables-restore --counters
2018-10-30 09:39:16 +00:00
for setname in $(ipset -n list | grep "omr_"); do
ipset destroy "$setname" 2>/dev/null || true
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() {
2018-12-02 07:02:18 +00:00
[ "$( ipset -n list | grep omr_ )" = "" ] && return 0
2018-10-30 09:39:16 +00:00
start
2018-05-15 15:20:49 +00:00
}