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

[process-compose] Improve port selection #1835

Merged
merged 5 commits into from
Feb 23, 2024
Merged
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
24 changes: 3 additions & 21 deletions internal/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,9 @@ import (

const (
processComposeLogfile = ".devbox/compose.log"
startingPort = 8260
maxPortTries = 10
fileLockTimeout = 5 * time.Second
)

func getAvailablePort(config *globalProcessComposeConfig) (int, bool) {
for i := 0; i < maxPortTries; i++ {
port := startingPort + i
available := true
for _, instance := range config.Instances {
if instance.Port == port {
available = false
}
}
if available {
return port, true
}
}
return 0, false
}

type instance struct {
Pid int `json:"pid"`
Port int `json:"port"`
Expand Down Expand Up @@ -143,9 +125,9 @@ func StartProcessManager(
config.File = configFile

// Get the port to use for this project
port, available := getAvailablePort(config)
if !available {
return fmt.Errorf("no available ports to start process-compose. You should run `devbox services stop` in your projects to free up ports")
port, err := getAvailablePort()
if err != nil {
return err
}

// Start building the process-compose command
Expand Down
65 changes: 65 additions & 0 deletions internal/services/ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package services

import (
"net"

"github.com/pkg/errors"
)

var disallowedPorts = map[int]string{
// Anything <= 1024
1433: "MS-SQL (Microsoft SQL Server database management system)",
1434: "MS-SQL (Microsoft SQL Server database management system)",
1521: "Oracle SQL",
1701: "L2TP (Layer 2 Tunneling Protocol)",
1723: "PPTP (Point-to-Point Tunneling Protocol)",
2049: "NFS (Network File System)",
3000: "Node.js (Server-side JavaScript environment)",
3001: "Node.js (Server-side JavaScript environment)",
3306: "MySQL (Database system)",
3389: "RDP (Remote Desktop Protocol)",
5060: "SIP (Session Initiation Protocol)",
5145: "RSH (Remote Shell)",
5353: "mDNS (Multicast DNS)",
5432: "PostgreSQL (Database system)",
5900: "VNC (Virtual Network Computing)",
6379: "Redis (Database system)",
8000: "HTTP Alternate (http_alt)",
8080: "HTTP Alternate (http_alt)",
8082: "PHP FPM",
8443: "HTTPS Alternate (https_alt)",
9443: "Redis Enterprise (Database system)",
}

func getAvailablePort() (int, error) {
get := func() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, errors.WithStack(err)
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, errors.WithStack(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

for range 1000 {
port, err := get()
if err != nil {
return 0, errors.WithStack(err)
}

if isAllowed(port) {
return port, nil
}
}

return 0, errors.New("no available port")
}

func isAllowed(port int) bool {
return port > 1024 && disallowedPorts[port] == ""
}
Loading