From 1e33bfa4bc2281a6d7ff2693ee5cde8d10cf450e Mon Sep 17 00:00:00 2001 From: laily Date: Tue, 2 Jun 2026 14:27:39 +0800 Subject: [PATCH] feat: add remote config and host metadata Co-authored-by: Cursor --- internal/config/config.go | 14 +++++++- internal/config/config_test.go | 14 ++++++-- internal/hostmeta/hostmeta.go | 55 ++++++++++++++++++++++++++++++ internal/hostmeta/hostmeta_test.go | 19 +++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 internal/hostmeta/hostmeta.go create mode 100644 internal/hostmeta/hostmeta_test.go diff --git a/internal/config/config.go b/internal/config/config.go index 4e05eae..a153a94 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,6 +12,7 @@ type Config struct { Events Events `toml:"events"` Notify Notify `toml:"notify"` Inbox Inbox `toml:"inbox"` + Remote Remote `toml:"remote"` } type Events struct { @@ -38,6 +39,13 @@ type Inbox struct { TimeoutMS int `toml:"timeout_ms"` } +type Remote struct { + Enabled bool `toml:"enabled"` + URL string `toml:"url"` + Token string `toml:"token"` + TimeoutMS int `toml:"timeout_ms"` +} + func Default() Config { return Config{ Events: Events{Stop: true, Response: true, Idle: false, Tool: false}, @@ -49,13 +57,17 @@ func Default() Config { BodyTool: "工具执行完成", }, Inbox: Inbox{ - Enabled: true, + Enabled: false, Socket: DefaultInboxSocket(), RemoteSocket: DefaultInboxRemoteSocket(), Addr: "127.0.0.1:17777", FallbackLocal: true, TimeoutMS: 500, }, + Remote: Remote{ + Enabled: false, + TimeoutMS: 2000, + }, } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 15e704a..b517999 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -18,8 +18,8 @@ func TestDefaultConfig(t *testing.T) { if cfg.Notify.Protocol != "osc777" { t.Fatalf("expected osc777, got %q", cfg.Notify.Protocol) } - if !cfg.Inbox.Enabled { - t.Fatal("expected inbox enabled by default") + if cfg.Inbox.Enabled { + t.Fatal("expected inbox disabled by default") } if cfg.Inbox.Addr != "127.0.0.1:17777" { t.Fatalf("expected default inbox addr, got %q", cfg.Inbox.Addr) @@ -104,6 +104,16 @@ func TestDefaultInboxSocketPrefersXDGRuntimeDir(t *testing.T) { } } +func TestDefaultRemoteDisabledInbox(t *testing.T) { + cfg := Default() + if cfg.Inbox.Enabled { + t.Fatal("inbox should default disabled") + } + if cfg.Remote.TimeoutMS != 2000 { + t.Fatalf("remote timeout: got %d", cfg.Remote.TimeoutMS) + } +} + func TestEventEnabled(t *testing.T) { cfg := Default() if !cfg.EventEnabled("stop") { diff --git a/internal/hostmeta/hostmeta.go b/internal/hostmeta/hostmeta.go new file mode 100644 index 0000000..2e99704 --- /dev/null +++ b/internal/hostmeta/hostmeta.go @@ -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 +} diff --git a/internal/hostmeta/hostmeta_test.go b/internal/hostmeta/hostmeta_test.go new file mode 100644 index 0000000..0077440 --- /dev/null +++ b/internal/hostmeta/hostmeta_test.go @@ -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) + } + } +}