From 2c7fcb56512ff86cd6212269aec8f7963c3a446d Mon Sep 17 00:00:00 2001 From: MarBeanAI Date: Fri, 1 Nov 2024 20:08:05 -0500 Subject: [PATCH] Implement dynamic port allocation to avoid conflicts Fixes #147 Implement dynamic port allocation to avoid conflicts with reserved ports. * **computer-use-demo/image/x11vnc_startup.sh** - Add functions to check port availability and find an available port. - Dynamically assign the VNC server port instead of hardcoding port 5900. - Update the `netstat` command to use the dynamically assigned port. * **computer-use-demo/image/novnc_startup.sh** - Add functions to check port availability and find an available port. - Dynamically assign the VNC server port instead of hardcoding port 5900. - Modify the `--vnc` option to use the dynamic port. * **computer-use-demo/image/port_check.sh** - Create a script to check port availability. - Use `lsof` to check if a port is in use. - Return an available port if the specified one is in use. * **computer-use-demo/.ports** - Maintain a list of reserved ports. - Include common ports used by macOS and other systems. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/anthropics/anthropic-quickstarts/issues/147?shareId=XXXX-XXXX-XXXX-XXXX). --- computer-use-demo/.ports | 9 +++++++ computer-use-demo/image/novnc_startup.sh | 24 +++++++++++++++++- computer-use-demo/image/port_check.sh | 31 +++++++++++++++++++++++ computer-use-demo/image/x11vnc_startup.sh | 26 +++++++++++++++++-- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 computer-use-demo/.ports mode change 100755 => 100644 computer-use-demo/image/novnc_startup.sh create mode 100644 computer-use-demo/image/port_check.sh mode change 100755 => 100644 computer-use-demo/image/x11vnc_startup.sh 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