Skip to content

Commit

Permalink
Implement dynamic port allocation to avoid conflicts
Browse files Browse the repository at this point in the history
Fixes anthropics#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).
  • Loading branch information
Stevo-G-Swag committed Nov 2, 2024
1 parent bbff506 commit 2c7fcb5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
9 changes: 9 additions & 0 deletions computer-use-demo/.ports
Original file line number Diff line number Diff line change
@@ -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
24 changes: 23 additions & 1 deletion computer-use-demo/image/novnc_startup.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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 &
Expand Down
31 changes: 31 additions & 0 deletions computer-use-demo/image/port_check.sh
Original file line number Diff line number Diff line change
@@ -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 <start_port>"
exit 1
fi

start_port=$1
available_port=$(find_available_port $start_port)
echo $available_port
26 changes: 24 additions & 2 deletions computer-use-demo/image/x11vnc_startup.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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) &

Expand All @@ -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
Expand Down

0 comments on commit 2c7fcb5

Please sign in to comment.