feat: add remote config and host metadata

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-02 14:27:39 +08:00
parent 1e7efd9c3b
commit 1e33bfa4bc
4 changed files with 99 additions and 3 deletions
+55
View File
@@ -0,0 +1,55 @@
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
}
+19
View File
@@ -0,0 +1,19 @@
package hostmeta
import "testing"
func TestHostnameNonEmpty(t *testing.T) {
h, err := Hostname()
if err != nil || h == "" {
t.Fatalf("hostname: %q err=%v", h, err)
}
}
func TestIPsSkipsLoopback(t *testing.T) {
ips := IPs()
for _, ip := range ips {
if ip == "127.0.0.1" || ip == "::1" {
t.Fatalf("loopback in ips: %v", ips)
}
}
}