Fix parseHostPort function to properly handle IPv6 addresses and port validation

- Use net.SplitHostPort instead of simple string splitting to properly handle IPv6 addresses like [::1]:8090
- Add port range validation (1-65535)
- Return extracted host instead of full hostPort string on parse errors
- Add net import for SplitHostPort function
- Fixes all failing tests in cmd/soundtouch-cli package
This commit is contained in:
Tobias Gesellchen
2026-01-10 00:06:21 +01:00
parent 62a67818d2
commit a1917a1565
+20 -19
View File
@@ -2,6 +2,7 @@ package main
import (
"fmt"
"net"
"strconv"
"strings"
"time"
@@ -102,28 +103,28 @@ func loadConfig(timeout time.Duration) (*config.Config, error) {
return cfg, nil
}
// parseHostPort parses a host:port string and returns host and port separately
// If no port is specified, returns the defaultPort
// parseHostPort splits a host:port string into separate host and port components
// If no port is specified, returns the original host and the provided default port
func parseHostPort(hostPort string, defaultPort int) (string, int) {
if !strings.Contains(hostPort, ":") {
return hostPort, defaultPort
// Check if host contains a port (has a colon)
if strings.Contains(hostPort, ":") {
host, portStr, err := net.SplitHostPort(hostPort)
if err != nil {
// If parsing fails, return original host and default port
return hostPort, defaultPort
}
port, err := strconv.Atoi(portStr)
if err != nil || port < 1 || port > 65535 {
// If port parsing fails or is invalid, return host and default port
return host, defaultPort
}
return host, port
}
// Simple parsing - in real use, we'd use net.SplitHostPort
parts := strings.Split(hostPort, ":")
if len(parts) != 2 {
return hostPort, defaultPort
}
host := parts[0]
portStr := parts[1]
port, err := strconv.Atoi(portStr)
if err != nil {
return hostPort, defaultPort
}
return host, port
// No port specified, return original host and default port
return hostPort, defaultPort
}
// PrintDeviceHeader prints a standard header for device commands