mirror of
https://github.com/Octops/quake-kube.git
synced 2026-04-11 12:20:33 +00:00
Initial commit
This commit is contained in:
25
internal/util/net/net.go
Normal file
25
internal/util/net/net.go
Normal file
@ -0,0 +1,25 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// DetectHostIPv4 attempts to determine the host IPv4 address by finding the
|
||||
// first non-loopback device with an assigned IPv4 address.
|
||||
func DetectHostIPv4() (string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() == nil {
|
||||
continue
|
||||
}
|
||||
return ipnet.IP.String(), nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("cannot detect host IPv4 address")
|
||||
}
|
||||
Reference in New Issue
Block a user