mirror of https://github.com/bol-van/zapret/
5 changed files with 396 additions and 576 deletions
@ -44,261 +44,109 @@ NFQWS_OPT_DESYNC_HTTPS6="${NFQWS_OPT_DESYNC_HTTPS6:-$NFQWS_OPT_DESYNC_HTTPS}" |
|||||
# we use low level function from network.sh to avoid this limitation |
# we use low level function from network.sh to avoid this limitation |
||||
# it can change theoretically and stop working |
# it can change theoretically and stop working |
||||
|
|
||||
network_find_wan_all() |
network_find_wan4_all() |
||||
{ |
{ |
||||
__network_ifstatus "$1" "" "[@.route[@.target='0.0.0.0' && [email protected]]].interface" "" 10 2>/dev/null && return |
__network_ifstatus "$1" "" "[@.route[@.target='0.0.0.0' && [email protected]]].interface" "" 10 2>/dev/null && return |
||||
network_find_wan $1 |
network_find_wan $1 |
||||
} |
} |
||||
|
network_find_wan_all() |
||||
|
{ |
||||
|
network_find_wan4_all "$@" |
||||
|
} |
||||
network_find_wan6_all() |
network_find_wan6_all() |
||||
{ |
{ |
||||
__network_ifstatus "$1" "" "[@.route[@.target='::' && [email protected]]].interface" "" 10 2>/dev/null && return |
__network_ifstatus "$1" "" "[@.route[@.target='::' && [email protected]]].interface" "" 10 2>/dev/null && return |
||||
network_find_wan6 $1 |
network_find_wan6 $1 |
||||
} |
} |
||||
|
|
||||
route_localnet() |
|
||||
{ |
|
||||
for lan in $OPENWRT_LAN; do |
|
||||
network_get_device DEVICE $lan |
|
||||
[ -n "$DEVICE" ] || continue |
|
||||
sysctl -qw net.ipv4.conf.$DEVICE.route_localnet=1 |
|
||||
done |
|
||||
} |
|
||||
|
|
||||
dnat6_target() |
dnat6_target() |
||||
{ |
{ |
||||
# $1 - lan network name |
# $1 - lan network name |
||||
# $2 - var to store target ip6 |
# $2 - var to store target ip6 |
||||
# get target ip address for DNAT. prefer link locals |
|
||||
# tpws should be as inaccessible from outside as possible |
|
||||
# link local address can appear not immediately after ifup |
|
||||
# DNAT6_TARGET=- means attempt was made but address was not found (to avoid multiple re-attempts) |
|
||||
|
|
||||
local DNAT6_TARGET DVAR=DNAT6_TARGET_$1 |
|
||||
DVAR=$(echo $DVAR | sed 's/[^a-zA-Z0-9_]/_/g') |
|
||||
eval DNAT6_TARGET="\$$DVAR" |
|
||||
[ -n "$2" ] && eval $2='' |
|
||||
|
|
||||
[ -n "$DNAT6_TARGET" ] || { |
|
||||
# no reason to query if its down |
|
||||
network_is_up $1 || return |
|
||||
|
|
||||
local DEVICE |
|
||||
network_get_device DEVICE $1 |
|
||||
|
|
||||
local ct=0 |
|
||||
while |
|
||||
DNAT6_TARGET=$(get_ipv6_linklocal $DEVICE) |
|
||||
[ -n "$DNAT6_TARGET" ] && break |
|
||||
[ "$ct" -ge "$LINKLOCAL_WAIT_SEC" ] && break |
|
||||
echo $DEVICE: waiting for the link local for another $(($LINKLOCAL_WAIT_SEC - $ct)) seconds ... |
|
||||
ct=$(($ct+1)) |
|
||||
sleep 1 |
|
||||
do :; done |
|
||||
|
|
||||
[ -n "$DNAT6_TARGET" ] || { |
|
||||
echo $DEVICE: no link local. getting global |
|
||||
DNAT6_TARGET=$(get_ipv6_global $DEVICE) |
|
||||
[ -n "$DNAT6_TARGET" ] || { |
|
||||
echo $DEVICE: could not get any address |
|
||||
DNAT6_TARGET=- |
|
||||
} |
|
||||
} |
|
||||
eval $DVAR="$DNAT6_TARGET" |
|
||||
} |
|
||||
[ -n "$2" ] && eval $2="$DNAT6_TARGET" |
|
||||
} |
|
||||
|
|
||||
|
|
||||
set_route_localnet() |
network_is_up $1 || { |
||||
{ |
[ -n "$2" ] && eval $2='' |
||||
# $1 - 1 = enable, 0 = disable |
return |
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
|
||||
local lan DEVICE |
|
||||
for lan in $OPENWRT_LAN; do |
|
||||
network_get_device DEVICE $lan |
|
||||
[ -n "$DEVICE" ] || continue |
|
||||
sysctl -q -w net.ipv4.conf.$DEVICE.route_localnet=$1 |
|
||||
done |
|
||||
} |
} |
||||
} |
|
||||
prepare_route_localnet() |
|
||||
{ |
|
||||
set_route_localnet 1 |
|
||||
} |
|
||||
unprepare_route_localnet() |
|
||||
{ |
|
||||
set_route_localnet 0 |
|
||||
} |
|
||||
prepare_tpws_fw4() |
|
||||
{ |
|
||||
# otherwise linux kernel will treat 127.0.0.0/8 as "martian" ip and refuse routing to it |
|
||||
# NOTE : kernels <3.6 do not have this feature. consider upgrading or change DNAT to REDIRECT and do not bind to 127.0.0.0/8 |
|
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
local DEVICE |
||||
iptables -N input_rule_zapret 2>/dev/null |
network_get_device DEVICE $1 |
||||
ipt input_rule_zapret -d $TPWS_LOCALHOST4 -j RETURN |
|
||||
ipta input_rule_zapret -d 127.0.0.0/8 -j DROP |
|
||||
ipt INPUT ! -i lo -j input_rule_zapret |
|
||||
|
|
||||
prepare_route_localnet |
_dnat6_target $DEVICE $2 |
||||
} |
|
||||
} |
} |
||||
unprepare_tpws_fw4() |
|
||||
{ |
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
|
||||
unprepare_route_localnet |
|
||||
|
|
||||
ipt_del INPUT ! -i lo -j input_rule_zapret |
set_route_localnet() |
||||
iptables -F input_rule_zapret 2>/dev/null |
|
||||
iptables -X input_rule_zapret 2>/dev/null |
|
||||
} |
|
||||
} |
|
||||
unprepare_tpws_fw() |
|
||||
{ |
{ |
||||
unprepare_tpws_fw4 |
# $1 - 1 = enable, 0 = disable |
||||
} |
|
||||
|
|
||||
fw_nfqws_pre4() |
local DLAN |
||||
{ |
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
||||
# $1 - 1 - add, 0 - del |
_set_route_localnet $1 $DLAN |
||||
# $2 - filter ipv4 |
} |
||||
# $3 - queue number |
|
||||
|
|
||||
local DEVICE wan_iface |
|
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
fw_nfqws_post_x() |
||||
network_find_wan_all wan_iface |
|
||||
for ext_iface in $wan_iface; do |
|
||||
network_get_device DEVICE $ext_iface |
|
||||
ipt_add_del $1 PREROUTING -t mangle -i $DEVICE -p tcp $2 $IPSET_EXCLUDE src -j NFQUEUE --queue-num $3 --queue-bypass |
|
||||
done |
|
||||
} |
|
||||
} |
|
||||
fw_nfqws_pre6() |
|
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
# $1 - 1 - add, 0 - del |
||||
# $2 - filter ipv6 |
# $2 - filter |
||||
# $3 - queue number |
# $3 - queue number |
||||
|
# $4 - ip version : 4 or 6 |
||||
|
|
||||
local DEVICE wan_iface |
network_find_wan${4}_all ifaces |
||||
|
call_for_multiple_items network_get_device DWAN "$ifaces" |
||||
[ "$DISABLE_IPV6" = "1" ] || { |
|
||||
network_find_wan6_all wan_iface |
|
||||
for ext_iface in $wan_iface; do |
|
||||
network_get_device DEVICE $ext_iface |
|
||||
ipt6_add_del $1 PREROUTING -t mangle -i $DEVICE -p tcp $2 $IPSET_EXCLUDE6 src -j NFQUEUE --queue-num $3 --queue-bypass |
|
||||
done |
|
||||
} |
|
||||
} |
|
||||
fw_nfqws_pre() |
|
||||
{ |
|
||||
# $1 - 1 - add, 0 - del |
|
||||
# $2 - filter ipv4 |
|
||||
# $3 - filter ipv6 |
|
||||
# $4 - queue number |
|
||||
|
|
||||
fw_nfqws_pre4 $1 "$2" $4 |
_fw_nfqws_post${4} $1 "$2" $3 "$(unique $DWAN)" |
||||
fw_nfqws_pre6 $1 "$3" $4 |
|
||||
} |
} |
||||
fw_nfqws_post4() |
fw_nfqws_post4() |
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
fw_nfqws_post_x $1 "$2" $3 4 |
||||
# $2 - filter ipv4 |
|
||||
# $3 - queue number |
|
||||
|
|
||||
local DEVICE wan_iface |
|
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
|
||||
network_find_wan_all wan_iface |
|
||||
for ext_iface in $wan_iface; do |
|
||||
network_get_device DEVICE $ext_iface |
|
||||
ipt_add_del $1 POSTROUTING -t mangle -o $DEVICE -p tcp $2 $IPSET_EXCLUDE dst -j NFQUEUE --queue-num $3 --queue-bypass |
|
||||
done |
|
||||
} |
|
||||
} |
} |
||||
fw_nfqws_post6() |
fw_nfqws_post6() |
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
fw_nfqws_post_x $1 "$2" $3 6 |
||||
# $2 - filter ipv6 |
|
||||
# $3 - queue number |
|
||||
|
|
||||
local DEVICE wan_iface |
|
||||
|
|
||||
[ "$DISABLE_IPV6" = "1" ] || { |
|
||||
network_find_wan6_all wan_iface |
|
||||
for ext_iface in $wan_iface; do |
|
||||
network_get_device DEVICE $ext_iface |
|
||||
ipt6_add_del $1 POSTROUTING -t mangle -o $DEVICE -p tcp $2 $IPSET_EXCLUDE6 dst -j NFQUEUE --queue-num $3 --queue-bypass |
|
||||
done |
|
||||
} |
|
||||
} |
} |
||||
fw_nfqws_post() |
fw_tpws_x() |
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
# $1 - 1 - add, 0 - del |
||||
# $2 - filter ipv4 |
# $2 - filter |
||||
# $3 - filter ipv6 |
# $3 - tpws port |
||||
# $4 - queue number |
# $4 - ip version : 4 or 6 |
||||
|
|
||||
fw_nfqws_post4 $1 "$2" $4 |
local script ifaces DLAN DWAN |
||||
fw_nfqws_post6 $1 "$3" $4 |
|
||||
} |
|
||||
|
|
||||
|
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
||||
|
|
||||
|
network_find_wan${4}_all ifaces |
||||
|
call_for_multiple_items network_get_device DWAN "$ifaces" |
||||
|
|
||||
IPT_OWNER="-m owner ! --uid-owner $WS_USER" |
_fw_tpws${4} $1 "$2" $3 "$DLAN" "$(unique $DWAN)" |
||||
|
} |
||||
fw_tpws4() |
fw_tpws4() |
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
fw_tpws_x $1 "$2" $3 4 |
||||
# $2 - filter ipv4 |
|
||||
# $3 - tpws port |
|
||||
|
|
||||
local lan DEVICE ext_iface wan_iface |
|
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
|
||||
network_find_wan_all wan_iface |
|
||||
for ext_iface in $wan_iface; do |
|
||||
network_get_device DEVICE $ext_iface |
|
||||
ipt_add_del $1 OUTPUT -t nat -o $DEVICE $IPT_OWNER -p tcp $2 $IPSET_EXCLUDE dst -j DNAT --to $TPWS_LOCALHOST4:$3 |
|
||||
done |
|
||||
[ "$1" = 1 ] && prepare_tpws_fw4 |
|
||||
for lan in $OPENWRT_LAN; do |
|
||||
network_get_device DEVICE $lan |
|
||||
[ -n "$DEVICE" ] || continue |
|
||||
ipt_add_del $1 PREROUTING -t nat -i $DEVICE -p tcp $2 $IPSET_EXCLUDE dst -j DNAT --to $TPWS_LOCALHOST4:$3 |
|
||||
done |
|
||||
} |
|
||||
} |
} |
||||
fw_tpws6() |
fw_tpws6() |
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
fw_tpws_x $1 "$2" $3 6 |
||||
# $2 - filter ipv6 |
} |
||||
# $3 - tpws port |
|
||||
|
|
||||
local lan DEVICE ext_iface wan_iface DNAT6 |
nft_fw_tpws4() |
||||
|
{ |
||||
[ "$DISABLE_IPV6" = "1" ] || { |
_nft_fw_tpws4 "$1" $2 always_apply_wan_filter |
||||
network_find_wan6_all wan_iface |
|
||||
for ext_iface in $wan_iface; do |
|
||||
network_get_device DEVICE $ext_iface |
|
||||
ipt6_add_del $1 OUTPUT -t nat -o $DEVICE $IPT_OWNER -p tcp $2 $IPSET_EXCLUDE6 dst -j DNAT --to [::1]:$3 |
|
||||
done |
|
||||
for lan in $OPENWRT_LAN; do |
|
||||
network_get_device DEVICE $lan |
|
||||
[ -n "$DEVICE" ] || continue |
|
||||
dnat6_target $lan DNAT6 |
|
||||
[ "$DNAT6" != '-' ] && ipt6_add_del $1 PREROUTING -t nat -i $DEVICE -p tcp $2 $IPSET_EXCLUDE6 dst -j DNAT --to [$DNAT6]:$3 |
|
||||
done |
|
||||
} |
|
||||
} |
} |
||||
fw_tpws() |
nft_fw_tpws6() |
||||
{ |
{ |
||||
# $1 - 1 - add, 0 - del |
local script ifaces DLAN |
||||
# $2 - filter ipv4 |
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
||||
# $3 - filter ipv6 |
_nft_fw_tpws6 "$1" $2 "$DLAN" always_apply_wan_filter |
||||
# $4 - tpws port |
} |
||||
|
nft_fw_nfqws_post4() |
||||
fw_tpws4 $1 "$2" $4 |
{ |
||||
fw_tpws6 $1 "$3" $4 |
_nft_fw_nfqws_post4 "$1" $2 always_apply_wan_filter |
||||
|
} |
||||
|
nft_fw_nfqws_post6() |
||||
|
{ |
||||
|
_nft_fw_nfqws_post6 "$1" $2 always_apply_wan_filter |
||||
} |
} |
||||
|
|
||||
|
|
||||
@ -384,13 +232,14 @@ flow_offloading_unexempt() |
|||||
} |
} |
||||
|
|
||||
|
|
||||
|
|
||||
nft_fill_ifsets_overload() |
nft_fill_ifsets_overload() |
||||
{ |
{ |
||||
local script ifaces DLAN DWAN DWAN6 |
local script ifaces DLAN DWAN DWAN6 |
||||
|
|
||||
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
||||
|
|
||||
network_find_wan_all ifaces |
network_find_wan4_all ifaces |
||||
call_for_multiple_items network_get_device DWAN "$ifaces" |
call_for_multiple_items network_get_device DWAN "$ifaces" |
||||
|
|
||||
network_find_wan6_all ifaces |
network_find_wan6_all ifaces |
||||
@ -399,75 +248,12 @@ nft_fill_ifsets_overload() |
|||||
nft_fill_ifsets "$DLAN" "$DWAN" "$DWAN6" |
nft_fill_ifsets "$DLAN" "$DWAN" "$DWAN6" |
||||
} |
} |
||||
|
|
||||
nft_fw_tpws4() |
|
||||
{ |
|
||||
# $1 - filter ipv4 |
|
||||
# $2 - tpws port |
|
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
|
||||
nft_add_rule dnat_output skuid != $WS_USER oifname @wanif meta l4proto tcp $1 ip daddr != @nozapret dnat ip to $TPWS_LOCALHOST4:$2 |
|
||||
nft_add_rule dnat_pre iifname @lanif meta l4proto tcp $1 ip daddr != @nozapret dnat ip to $TPWS_LOCALHOST4:$2 |
|
||||
prepare_route_localnet |
|
||||
} |
|
||||
} |
|
||||
nft_fw_tpws6() |
nft_fw_tpws6() |
||||
{ |
{ |
||||
# $1 - filter ipv6 |
# $1 - filter ipv6 |
||||
# $2 - tpws port |
# $2 - tpws port |
||||
|
|
||||
local lan DEVICE DNAT6 |
local DLAN |
||||
[ "$DISABLE_IPV6" = "1" ] || { |
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
||||
nft_add_rule dnat_output skuid != $WS_USER oifname @wanif6 meta l4proto tcp $1 ip6 daddr != @nozapret6 dnat ip6 to [::1]:$2 |
_nft_fw_tpws6 "$1" $2 "$DLAN" |
||||
for lan in $OPENWRT_LAN; do |
|
||||
network_get_device DEVICE $lan |
|
||||
[ -n "$DEVICE" ] || continue |
|
||||
dnat6_target $lan DNAT6 |
|
||||
[ "$DNAT6" != '-' ] && nft_add_rule dnat_pre iifname $DEVICE meta l4proto tcp $1 ip6 daddr != @nozapret6 dnat ip6 to [$DNAT6]:$2 |
|
||||
done |
|
||||
} |
|
||||
} |
|
||||
nft_fw_tpws() |
|
||||
{ |
|
||||
# $1 - filter ipv4 |
|
||||
# $2 - filter ipv6 |
|
||||
# $3 - tpws port |
|
||||
|
|
||||
nft_fw_tpws4 "$1" $3 |
|
||||
nft_fw_tpws6 "$2" $3 |
|
||||
} |
|
||||
|
|
||||
nft_fw_nfqws_post4() |
|
||||
{ |
|
||||
# $1 - filter ipv4 |
|
||||
# $2 - queue number |
|
||||
|
|
||||
local rule |
|
||||
|
|
||||
[ "$DISABLE_IPV4" = "1" ] || { |
|
||||
rule="oifname @wanif meta l4proto tcp $1 ip daddr != @nozapret" |
|
||||
nft_add_rule postrouting $rule queue num $2 bypass |
|
||||
nft_add_nfqws_flow_exempt_rule "$rule" |
|
||||
} |
|
||||
} |
|
||||
nft_fw_nfqws_post6() |
|
||||
{ |
|
||||
# $1 - filter ipv6 |
|
||||
# $2 - queue number |
|
||||
|
|
||||
local rule |
|
||||
|
|
||||
[ "$DISABLE_IPV6" = "1" ] || { |
|
||||
rule="oifname @wanif6 meta l4proto tcp $1 ip6 daddr != @nozapret6" |
|
||||
nft_add_rule postrouting $rule queue num $2 bypass |
|
||||
nft_add_nfqws_flow_exempt_rule "$rule" |
|
||||
} |
|
||||
} |
|
||||
nft_fw_nfqws_post() |
|
||||
{ |
|
||||
# $1 - filter ipv4 |
|
||||
# $2 - filter ipv6 |
|
||||
# $3 - queue number |
|
||||
|
|
||||
nft_fw_nfqws_post4 "$1" $3 |
|
||||
nft_fw_nfqws_post6 "$2" $3 |
|
||||
} |
} |
||||
|
Loading…
Reference in new issue