From 3622498a2889c0afbce2602b38980f227b51088b Mon Sep 17 00:00:00 2001 From: Hongjian Fan <86060531+HJ-Fan@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:59:19 -0500 Subject: [PATCH] fix: use net module instead of exe os command (#23) 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 70fae79..aa586d5 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