Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions rdp-ssh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ elif [[ $COMMAND == "start" ]]; then
else
LAST_DISPLAY=$(ssh $HOST ls /tmp/.X11-unix/ | sort -V | tail -n 1 | cut -c 2-)
REMOTE_DISPLAY=$(($LAST_DISPLAY + 1))

# Find an available port by checking both X11 sockets and Docker port usage
REMOTE_PORT=$(($BASE_PORT + $REMOTE_DISPLAY))
USED_PORTS=$(ssh $HOST "docker ps --format '{{.Ports}}' | grep -oP '0\.0\.0\.0:\K[0-9]+' | sort -n")

# Keep incrementing until we find a free port
while echo "$USED_PORTS" | grep -q "^$REMOTE_PORT$"; do
REMOTE_DISPLAY=$(($REMOTE_DISPLAY + 1))
REMOTE_PORT=$(($BASE_PORT + $REMOTE_DISPLAY))
done

NAME=${NAME:=desktop-$REMOTE_DISPLAY}

echo $LAST_DISPLAY $REMOTE_DISPLAY
Expand All @@ -57,7 +67,7 @@ elif [[ $COMMAND == "start" ]]; then

echo "Started \"$NAME\"!"
echo "Open http://localhost:$LOCAL_PORT in browser"
ssh -t -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST DISPLAY=:$REMOTE_DISPLAY bash -l
ssh -t -o ExitOnForwardFailure=no -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST DISPLAY=:$REMOTE_DISPLAY bash -l

elif [[ $COMMAND == "connect" ]]; then
if ! $RUNNING ; then
Expand All @@ -68,7 +78,7 @@ elif [[ $COMMAND == "connect" ]]; then
echo "Open http://localhost:$LOCAL_PORT in browser"
REMOTE_PORT=$(ssh $HOST "docker port $NAME | head -n 1 | awk -F: '{print \$NF}'")
REMOTE_DISPLAY=$(($REMOTE_PORT - $BASE_PORT))
ssh -t -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST DISPLAY=:$REMOTE_DISPLAY bash -l
ssh -t -o ExitOnForwardFailure=no -L $LOCAL_PORT:localhost:$REMOTE_PORT $HOST DISPLAY=:$REMOTE_DISPLAY bash -l


elif [[ $COMMAND == "stop" ]]; then
Expand Down