Skip to content

Commit

Permalink
Tidy functions and add support for comma-separated lists
Browse files Browse the repository at this point in the history
Required for compatibility with PodHostIPs, which uses downwards API to set the DEST_IPS list, but uses commas instead of spaces as a delimiter.

Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Nov 29, 2023
1 parent 63942df commit e25896a
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions entry
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,42 @@ trap exit TERM INT

BIN_DIR="/sbin"

info()
{
info() {
{ set +x; } 2> /dev/null
echo '[INFO] ' "$@"
set -x
}
fatal()
{

fatal() {
{ set +x; } 2> /dev/null
echo '[ERROR] ' "$@" >&2
set -x
exit 1
}

check_iptables_mode() {
set +e
lsmod | grep "nf_tables" 2> /dev/null
lsmod | grep -qF nf_tables 2> /dev/null
if [ $? = 0 ]; then
mode=nft
else
mode=legacy
fi
set -e

case "$mode" in
nft)
info "nft mode detected"
set_nft
;;
legacy)
info "legacy mode detected"
set_legacy
;;
*)
fatal "invalid iptables mode"
;;
esac
}

set_nft() {
Expand All @@ -39,15 +56,15 @@ set_legacy() {
}

start_proxy() {
for src_range in ${SRC_RANGES}; do
if echo ${src_range} | grep -Eq ":"; then
ip6tables -t filter -I FORWARD -s ${src_range} -p ${DEST_PROTO} --dport ${DEST_PORT} -j ACCEPT
else
iptables -t filter -I FORWARD -s ${src_range} -p ${DEST_PROTO} --dport ${DEST_PORT} -j ACCEPT
fi
for src_range in ${SRC_RANGES//,/ }; do
if echo ${src_range} | grep -Eq ":"; then
ip6tables -t filter -I FORWARD -s ${src_range} -p ${DEST_PROTO} --dport ${DEST_PORT} -j ACCEPT
else
iptables -t filter -I FORWARD -s ${src_range} -p ${DEST_PROTO} --dport ${DEST_PORT} -j ACCEPT
fi
done

for dest_ip in ${DEST_IPS}; do
for dest_ip in ${DEST_IPS//,/ }; do
if echo ${dest_ip} | grep -Eq ":"; then
[ $(cat /proc/sys/net/ipv6/conf/all/forwarding) == 1 ] || exit 1
ip6tables -t filter -A FORWARD -d ${dest_ip}/128 -p ${DEST_PROTO} --dport ${DEST_PORT} -j DROP
Expand All @@ -63,19 +80,6 @@ start_proxy() {
}

check_iptables_mode
case $mode in
nft)
info "nft mode detected"
set_nft
;;
legacy)
info "legacy mode detected"
set_legacy
;;
*)
fatal "invalid iptables mode"
;;
esac
start_proxy

if [ ! -e /pause ]; then
Expand Down

0 comments on commit e25896a

Please sign in to comment.