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 |
|||
# 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_find_wan $1 |
|||
} |
|||
network_find_wan_all() |
|||
{ |
|||
network_find_wan4_all "$@" |
|||
} |
|||
network_find_wan6_all() |
|||
{ |
|||
__network_ifstatus "$1" "" "[@.route[@.target='::' && [email protected]]].interface" "" 10 2>/dev/null && return |
|||
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() |
|||
{ |
|||
# $1 - lan network name |
|||
# $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() |
|||
{ |
|||
# $1 - 1 = enable, 0 = disable |
|||
|
|||
[ "$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 |
|||
network_is_up $1 || { |
|||
[ -n "$2" ] && eval $2='' |
|||
return |
|||
} |
|||
} |
|||
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" ] || { |
|||
iptables -N input_rule_zapret 2>/dev/null |
|||
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 |
|||
local DEVICE |
|||
network_get_device DEVICE $1 |
|||
|
|||
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 |
|||
iptables -F input_rule_zapret 2>/dev/null |
|||
iptables -X input_rule_zapret 2>/dev/null |
|||
} |
|||
} |
|||
unprepare_tpws_fw() |
|||
set_route_localnet() |
|||
{ |
|||
unprepare_tpws_fw4 |
|||
} |
|||
# $1 - 1 = enable, 0 = disable |
|||
|
|||
fw_nfqws_pre4() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $2 - filter ipv4 |
|||
# $3 - queue number |
|||
local DLAN |
|||
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
|||
_set_route_localnet $1 $DLAN |
|||
} |
|||
|
|||
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 PREROUTING -t mangle -i $DEVICE -p tcp $2 $IPSET_EXCLUDE src -j NFQUEUE --queue-num $3 --queue-bypass |
|||
done |
|||
} |
|||
} |
|||
fw_nfqws_pre6() |
|||
fw_nfqws_post_x() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $2 - filter ipv6 |
|||
# $2 - filter |
|||
# $3 - queue number |
|||
# $4 - ip version : 4 or 6 |
|||
|
|||
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 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 |
|||
network_find_wan${4}_all ifaces |
|||
call_for_multiple_items network_get_device DWAN "$ifaces" |
|||
|
|||
fw_nfqws_pre4 $1 "$2" $4 |
|||
fw_nfqws_pre6 $1 "$3" $4 |
|||
_fw_nfqws_post${4} $1 "$2" $3 "$(unique $DWAN)" |
|||
} |
|||
fw_nfqws_post4() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $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_post_x $1 "$2" $3 4 |
|||
} |
|||
fw_nfqws_post6() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $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_x $1 "$2" $3 6 |
|||
} |
|||
fw_nfqws_post() |
|||
fw_tpws_x() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $2 - filter ipv4 |
|||
# $3 - filter ipv6 |
|||
# $4 - queue number |
|||
# $2 - filter |
|||
# $3 - tpws port |
|||
# $4 - ip version : 4 or 6 |
|||
|
|||
fw_nfqws_post4 $1 "$2" $4 |
|||
fw_nfqws_post6 $1 "$3" $4 |
|||
} |
|||
local script ifaces DLAN DWAN |
|||
|
|||
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() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $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_tpws_x $1 "$2" $3 4 |
|||
} |
|||
fw_tpws6() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $2 - filter ipv6 |
|||
# $3 - tpws port |
|||
fw_tpws_x $1 "$2" $3 6 |
|||
} |
|||
|
|||
local lan DEVICE ext_iface wan_iface DNAT6 |
|||
|
|||
[ "$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 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 |
|||
} |
|||
nft_fw_tpws4() |
|||
{ |
|||
_nft_fw_tpws4 "$1" $2 always_apply_wan_filter |
|||
} |
|||
fw_tpws() |
|||
nft_fw_tpws6() |
|||
{ |
|||
# $1 - 1 - add, 0 - del |
|||
# $2 - filter ipv4 |
|||
# $3 - filter ipv6 |
|||
# $4 - tpws port |
|||
|
|||
fw_tpws4 $1 "$2" $4 |
|||
fw_tpws6 $1 "$3" $4 |
|||
local script ifaces DLAN |
|||
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
|||
_nft_fw_tpws6 "$1" $2 "$DLAN" always_apply_wan_filter |
|||
} |
|||
nft_fw_nfqws_post4() |
|||
{ |
|||
_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() |
|||
{ |
|||
local script ifaces DLAN DWAN DWAN6 |
|||
|
|||
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" |
|||
|
|||
network_find_wan6_all ifaces |
|||
@ -399,75 +248,12 @@ nft_fill_ifsets_overload() |
|||
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() |
|||
{ |
|||
# $1 - filter ipv6 |
|||
# $2 - tpws port |
|||
|
|||
local lan DEVICE DNAT6 |
|||
[ "$DISABLE_IPV6" = "1" ] || { |
|||
nft_add_rule dnat_output skuid != $WS_USER oifname @wanif6 meta l4proto tcp $1 ip6 daddr != @nozapret6 dnat ip6 to [::1]:$2 |
|||
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 |
|||
local DLAN |
|||
call_for_multiple_items network_get_device DLAN "$OPENWRT_LAN" |
|||
_nft_fw_tpws6 "$1" $2 "$DLAN" |
|||
} |
|||
|
Loading…
Reference in new issue