Improve port mapping to prefer same local port
Enhanced port forwarding logic to be more intuitive: - Try to map remote port to same local port when possible - Fallback to random available port if same port unavailable - Clear user feedback showing port mapping (same vs different) - Enhanced forwarding view with access URLs and instructions - Added --test-port command to test port mapping logic Examples: - Remote port 3000 -> localhost:3000 (if available) - Remote port 80 -> localhost:random (if 80 unavailable) - Shows 'same port' or 'port X was unavailable' messages This makes port forwarding much more intuitive - users can access localhost:3000 when forwarding remote port 3000. Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
43
main.go
43
main.go
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -21,6 +22,12 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// Check for port mapping test mode
|
||||
if len(os.Args) > 2 && os.Args[1] == "--test-port" {
|
||||
testPortMapping(os.Args[2])
|
||||
return
|
||||
}
|
||||
|
||||
// Initialize the application
|
||||
app := NewApp()
|
||||
if err := app.Run(); err != nil {
|
||||
@@ -62,6 +69,7 @@ func testMode() {
|
||||
fmt.Println("Note: TUI requires a proper terminal environment")
|
||||
fmt.Println("")
|
||||
fmt.Println("To test connection to a specific host: ./kport --test-connect <hostname>")
|
||||
fmt.Println("To test port mapping logic: ./kport --test-port <port>")
|
||||
}
|
||||
|
||||
// testConnection tests connecting to a specific host
|
||||
@@ -182,4 +190,39 @@ func expandShellVars(value string) string {
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// testPortMapping tests the port mapping logic
|
||||
func testPortMapping(portStr string) {
|
||||
fmt.Printf("Testing port mapping for port: %s\n", portStr)
|
||||
fmt.Println("=====================================")
|
||||
|
||||
remotePort, err := strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Invalid port number: %s\n", portStr)
|
||||
return
|
||||
}
|
||||
|
||||
if remotePort <= 0 || remotePort > 65535 {
|
||||
fmt.Printf("❌ Port number must be between 1 and 65535\n")
|
||||
return
|
||||
}
|
||||
|
||||
// Test the port mapping logic
|
||||
localPort, samePort, err := findPreferredLocalPort(remotePort)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Failed to find available port: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
if samePort {
|
||||
fmt.Printf("✅ Port %d is available locally - using same port\n", localPort)
|
||||
fmt.Printf(" Mapping: localhost:%d -> remote:%d\n", localPort, remotePort)
|
||||
} else {
|
||||
fmt.Printf("⚠️ Port %d is unavailable locally - using alternative port %d\n", remotePort, localPort)
|
||||
fmt.Printf(" Mapping: localhost:%d -> remote:%d\n", localPort, remotePort)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
fmt.Println("This is how kport will map the ports when forwarding.")
|
||||
}
|
Reference in New Issue
Block a user