Improve client config validation

This commit is contained in:
Gouryella
2025-12-03 10:28:57 +08:00
parent dd54e79ad7
commit fbd910b3a2
2 changed files with 37 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package cli
import (
"bufio"
"fmt"
"net"
"os"
"strings"
@@ -202,14 +203,19 @@ func runConfigValidate(cmd *cobra.Command, args []string) error {
return err
}
serverValid := cfg.Server != ""
serverValid, serverMsg := validateServerAddress(cfg.Server)
tokenSet := cfg.Token != ""
tlsEnabled := cfg.TLS
fmt.Println(ui.RenderConfigValidation(serverValid, tokenSet, tlsEnabled))
tokenMsg := "Token is set"
if !tokenSet {
tokenMsg = "Token is not set (authentication may fail)"
}
fmt.Println(ui.RenderConfigValidation(serverValid, serverMsg, tokenSet, tokenMsg, tlsEnabled))
if !serverValid {
return fmt.Errorf("invalid configuration")
return fmt.Errorf("invalid configuration: %s", serverMsg)
}
return nil
@@ -221,3 +227,25 @@ func enabledDisabled(value bool) string {
}
return "disabled"
}
func validateServerAddress(addr string) (bool, string) {
addr = strings.TrimSpace(addr)
if addr == "" {
return false, "Server address is not set"
}
host, port, err := net.SplitHostPort(addr)
if err != nil {
return false, fmt.Sprintf("Server address must include host and port (e.g., tunnel.example.com:443): %v", err)
}
if host == "" {
return false, "Server host is empty"
}
if port == "" {
return false, "Server port is empty"
}
return true, fmt.Sprintf("Server address is valid (%s:%s)", host, port)
}

View File

@@ -69,19 +69,19 @@ func RenderConfigDeleted() string {
}
// RenderConfigValidation renders config validation results
func RenderConfigValidation(serverValid, tokenSet, tlsEnabled bool) string {
func RenderConfigValidation(serverValid bool, serverMsg string, tokenSet bool, tokenMsg string, tlsEnabled bool) string {
lines := []string{}
if serverValid {
lines = append(lines, Success("Server address is valid"))
lines = append(lines, Success(serverMsg))
} else {
lines = append(lines, Error("Server address is not set"))
lines = append(lines, Error(serverMsg))
}
if tokenSet {
lines = append(lines, Success("Token is set"))
lines = append(lines, Success(tokenMsg))
} else {
lines = append(lines, Warning("Token is not set (authentication may fail)"))
lines = append(lines, Warning(tokenMsg))
}
if tlsEnabled {
@@ -111,7 +111,7 @@ func RenderDaemonStarted(tunnelType string, port int, pid int, logPath string) s
Cyan(fmt.Sprintf(" drip attach %s %d", tunnelType, port)) + Muted(" View logs"),
Cyan(fmt.Sprintf(" drip stop %s %d", tunnelType, port)) + Muted(" Stop tunnel"),
"",
Muted("Logs: ")+mutedStyle.Render(logPath),
Muted("Logs: ") + mutedStyle.Render(logPath),
}
return SuccessBox("Tunnel Started in Background", lines...)
}