Skip to content

Commit

Permalink
fix: Port forwarding (#525)
Browse files Browse the repository at this point in the history
* fix: Port forwarding
  • Loading branch information
kroese committed Dec 29, 2023
1 parent 5ae4f59 commit 0862cad
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions src/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -Eeuo pipefail
# Docker environment variables

: ${DHCP:='N'}
: ${HOST_PORTS:='7100'}
: ${HOST_PORTS:=''}
: ${MAC:='02:11:32:AA:BB:CC'}

: ${VM_NET_DEV:=''}
Expand Down Expand Up @@ -91,6 +91,40 @@ configureDNS() {
return 0
}

getPorts() {

local list=$1
local args=""
local vnc="5900"

list="${list//,/ }"
list="${list## }"
list="${list%% }"

if [[ "${DISPLAY,,}" == "vnc" ]] && [[ "$list" != *"$vnc"* ]]; then
if [ -z "$list" ]; then
list="$vnc"
else
list="$list $vnc"
fi
fi

if [ -n "$list" ]; then
if [[ "$list" != *" "* ]]; then
args=" ! --dport $list"
else
args=" -m multiport ! --dports "
for port in $list; do
args="${args}${port},"
done
args="${args%?}"
fi
fi

echo "$args"
return 0
}

configureNAT() {

# Create the necessary file structure for /dev/net/tun
Expand Down Expand Up @@ -145,18 +179,11 @@ configureNAT() {
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null

if [[ "${DISPLAY,,}" == "vnc" ]] && [[ "$HOST_PORTS" != *"5900"* ]]; then
HOST_PORTS="$HOST_PORTS 5900"
fi

local PORT_ARGS=""
for PORT in $HOST_PORTS; do
PORT_ARGS="$PORT_ARGS ! --dport $PORT"
done
exclude="$(getPorts "$HOST_PORTS")"

iptables -t nat -A POSTROUTING -o "$VM_NET_DEV" -j MASQUERADE
# shellcheck disable=SC2086
iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -p tcp $PORT_ARGS -j DNAT --to "$VM_NET_IP"
iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -p tcp${exclude} -j DNAT --to "$VM_NET_IP"
iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -p udp -j DNAT --to "$VM_NET_IP"

if (( KERNEL > 4 )); then
Expand Down Expand Up @@ -231,10 +258,6 @@ getInfo() {
IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
echo "$IP" > /run/qemu.ip

if [[ "$DEBUG" == [Yy1]* ]]; then
info "Container IP is $IP with gateway $GATEWAY on interface $VM_NET_DEV" && echo
fi

return 0
}

Expand All @@ -252,6 +275,10 @@ fi

getInfo

if [[ "$DEBUG" == [Yy1]* ]]; then
info "Container IP is $IP with gateway $GATEWAY on interface $VM_NET_DEV" && echo
fi

if [[ "$DHCP" == [Yy1]* ]]; then

if [[ "$GATEWAY" == "172."* ]]; then
Expand Down

0 comments on commit 0862cad

Please sign in to comment.