From 25d33469d45df63017045adc8d48542cd4a8fcc2 Mon Sep 17 00:00:00 2001 From: Hongjian Fan Date: Wed, 7 Aug 2024 13:20:42 -0500 Subject: [PATCH] fix: use net module instead of exe os command This removes the os dependency on host ip detection This method dials the public dns to see the host IP. If the host is in a complete private network, it might fail. In this case, the user could use the input argument to specify host IP --- services/services.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/services/services.go b/services/services.go index 70fae790..aa586d54 100644 --- a/services/services.go +++ b/services/services.go @@ -4,9 +4,9 @@ package services import ( "errors" "fmt" + "net" "net/http" "os" - "os/exec" "path/filepath" "strings" @@ -115,15 +115,17 @@ func FindWebUIDistPath(ctx context.Context) (*string, error) { func GetHostIp(ctx context.Context) string { logger := klog.FromContext(ctx) - cmd := exec.Command("hostname", "-I") - stdout, err := cmd.Output() + conn, err := net.Dial("udp", "8.8.8.8:80") if err != nil { logger.Error(err, ", [WEBUI] unable to retrive cfm-service's ip address") return "" } - output := string(stdout[:]) - addresses := strings.Split(output, " ") - return strings.TrimSpace(string(addresses[0])) + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + logger.V(2).Info("[WEBUI] found webui service", "ip addr", localAddr.IP) + + return fmt.Sprintf("%s", localAddr.IP[:]) // deep copy } // UpdateBasePath: Replace the base address in the webui distro file