1e33bfa4bc
Co-authored-by: Cursor <cursoragent@cursor.com>
56 lines
876 B
Go
56 lines
876 B
Go
package hostmeta
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"sort"
|
|
)
|
|
|
|
func Hostname() (string, error) {
|
|
return os.Hostname()
|
|
}
|
|
|
|
func IPs() []string {
|
|
seen := make(map[string]struct{})
|
|
var ips []string
|
|
|
|
ifaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
for _, iface := range ifaces {
|
|
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
|
continue
|
|
}
|
|
addrs, err := iface.Addrs()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, addr := range addrs {
|
|
var ip net.IP
|
|
switch v := addr.(type) {
|
|
case *net.IPNet:
|
|
ip = v.IP
|
|
case *net.IPAddr:
|
|
ip = v.IP
|
|
default:
|
|
continue
|
|
}
|
|
if ip == nil || ip.IsLoopback() {
|
|
continue
|
|
}
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
ip = ip4
|
|
}
|
|
s := ip.String()
|
|
if _, ok := seen[s]; ok {
|
|
continue
|
|
}
|
|
seen[s] = struct{}{}
|
|
ips = append(ips, s)
|
|
}
|
|
}
|
|
sort.Strings(ips)
|
|
return ips
|
|
}
|