diff --git a/computer-use-demo/.ports b/computer-use-demo/.ports new file mode 100644 index 00000000..c146210f --- /dev/null +++ b/computer-use-demo/.ports @@ -0,0 +1,9 @@ +# Reserved Ports List +# This file contains a list of ports that are reserved and should not be used by the application. + +# Common ports used by macOS and other systems +5900 # VNC +22 # SSH +80 # HTTP +443 # HTTPS +3389 # RDP diff --git a/computer-use-demo/image/novnc_startup.sh b/computer-use-demo/image/novnc_startup.sh old mode 100755 new mode 100644 index da56816c..5bcf1277 --- a/computer-use-demo/image/novnc_startup.sh +++ b/computer-use-demo/image/novnc_startup.sh @@ -1,9 +1,31 @@ #!/bin/bash echo "starting noVNC" +# Function to check port availability +check_port() { + local port=$1 + if lsof -i :$port >/dev/null 2>&1; then + return 1 # Port is in use + else + return 0 # Port is available + fi +} + +# Find an available port starting from 5900 +find_available_port() { + local port=5900 + while ! check_port $port; do + ((port++)) + done + echo $port +} + +# Dynamically assign the VNC server port +VNC_PORT=$(find_available_port) + # Start noVNC with explicit websocket settings /opt/noVNC/utils/novnc_proxy \ - --vnc localhost:5900 \ + --vnc localhost:$VNC_PORT \ --listen 6080 \ --web /opt/noVNC \ > /tmp/novnc.log 2>&1 & diff --git a/computer-use-demo/image/port_check.sh b/computer-use-demo/image/port_check.sh new file mode 100644 index 00000000..d4746120 --- /dev/null +++ b/computer-use-demo/image/port_check.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Function to check if a port is in use +check_port() { + local port=$1 + if lsof -i :$port >/dev/null 2>&1; then + return 1 # Port is in use + else + return 0 # Port is available + fi +} + +# Function to find an available port starting from a given port +find_available_port() { + local start_port=$1 + local port=$start_port + while ! check_port $port; do + ((port++)) + done + echo $port +} + +# Main script +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +start_port=$1 +available_port=$(find_available_port $start_port) +echo $available_port diff --git a/computer-use-demo/image/x11vnc_startup.sh b/computer-use-demo/image/x11vnc_startup.sh old mode 100755 new mode 100644 index ad4b352c..e649eaa1 --- a/computer-use-demo/image/x11vnc_startup.sh +++ b/computer-use-demo/image/x11vnc_startup.sh @@ -1,11 +1,33 @@ #!/bin/bash echo "starting vnc" +# Function to check port availability +check_port() { + local port=$1 + if lsof -i :$port >/dev/null 2>&1; then + return 1 # Port is in use + else + return 0 # Port is available + fi +} + +# Find an available port starting from 5900 +find_available_port() { + local port=5900 + while ! check_port $port; do + ((port++)) + done + echo $port +} + +# Dynamically assign the VNC server port +VNC_PORT=$(find_available_port) + (x11vnc -display $DISPLAY \ -forever \ -shared \ -wait 50 \ - -rfbport 5900 \ + -rfbport $VNC_PORT \ -nopw \ 2>/tmp/x11vnc_stderr.log) & @@ -14,7 +36,7 @@ x11vnc_pid=$! # Wait for x11vnc to start timeout=10 while [ $timeout -gt 0 ]; do - if netstat -tuln | grep -q ":5900 "; then + if netstat -tuln | grep -q ":$VNC_PORT "; then break fi sleep 1