Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dynamic port allocation to avoid conflicts #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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