Skip to content

Commit

Permalink
fix: use net module instead of exe os command
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HJ-Fan committed Aug 1, 2024
1 parent beb92b1 commit 83edc63
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package services
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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 string(localAddr.IP)
}

// UpdateBasePath: Replace the base address in the webui distro file
Expand Down

0 comments on commit 83edc63

Please sign in to comment.