diff --git a/src/network.sh b/src/network.sh index 5b347e18..6668dc16 100644 --- a/src/network.sh +++ b/src/network.sh @@ -210,8 +210,9 @@ compat() { getHostPorts() { - local list="$1" + local list="${HOST_PORTS:-}" list=$(echo "${list// /}" | sed 's/,*$//g') + list="${list//,,/,}" if [[ "${DISPLAY,,}" == "web" ]]; then [ -z "$list" ] && list="$WSS_PORT" || list+=",$WSS_PORT" @@ -228,21 +229,62 @@ getHostPorts() { [ -z "$list" ] && list="$WSD_PORT" || list+=",$WSD_PORT" fi + # Remove duplicates + list=$(echo "$list," | awk 'BEGIN{RS=ORS=","} !seen[$0]++' | sed 's/,*$//g') + echo "$list" return 0 } getUserPorts() { - local args="" - local list=$1 + local list="${USER_PORTS:-}" list=$(echo "${list// /}" | sed 's/,*$//g') local ssh="22" [[ "${BOOT_MODE:-}" == "windows"* ]] && ssh="3389" [ -z "$list" ] && list="$ssh" || list+=",$ssh" + + list="${list//,,/,}" + list="${list//,/ }" + list="${list## }" + list="${list%% }" - echo "$list" + local exclude + exclude=$(getHostPorts) + exclude="${exclude//,/ }" + exclude="${exclude## }" + exclude="${exclude%% }" + + local ports="" + + for userport in $list; do + + local num="${userport///tcp}" + num="${num///udp}" + [ -z "$num" ] && continue + + for hostport in $exclude; do + + local val="${hostport///tcp}" + + if [[ "$num" == "${val///udp}" ]]; then + num="" + warn "Could not assign port ${val///udp} to \"USER_PORTS\" because it is already in \"HOST_PORTS\"!" + fi + + done + + if [ -n "$num" ]; then + [ -z "$ports" ] && ports="$userport" || ports+=",$userport" + fi + + done + + # Remove duplicates + ports=$(echo "$ports," | awk 'BEGIN{RS=ORS=","} !seen[$0]++' | sed 's/,*$//g') + + echo "$ports" return 0 } @@ -251,15 +293,15 @@ getSlirp() { local args="" local list="" - list=$(getUserPorts "${USER_PORTS:-}") + list=$(getUserPorts) list="${list//,/ }" list="${list## }" list="${list%% }" for port in $list; do - proto="tcp" - num="${port%/tcp}" + local proto="tcp" + local num="${port%/tcp}" if [[ "$port" == *"/udp" ]]; then proto="udp" @@ -273,6 +315,8 @@ getSlirp() { args+="hostfwd=$proto::$num-$VM_NET_IP:$num," done + args=$(echo "$args" | sed 's/,*$//g') + echo "${args%?}" return 0 } @@ -341,7 +385,7 @@ configurePasst() { [ -n "$PASST_MTU" ] && PASST_OPTS+=" -m $PASST_MTU" local forward="" - forward=$(getUserPorts "${USER_PORTS:-}") + forward=$(getUserPorts) forward="${forward///tcp}" forward="${forward///udp}" @@ -492,7 +536,7 @@ configureNAT() { update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null fi - exclude=$(getHostPorts "$HOST_PORTS") + exclude=$(getHostPorts) if [ -n "$exclude" ]; then if [[ "$exclude" != *","* ]]; then