From 795cc506fd24144fc12cc7edb1a8f5b50219b917 Mon Sep 17 00:00:00 2001 From: Andy Oknen Date: Fri, 15 Aug 2025 15:38:27 +0000 Subject: [PATCH] Update peer handling to extract and display NodeInfo names in CLI and admin responses. Enhance debug logging for NodeInfo processing in various components. --- cmd/yggdrasilctl/main.go | 30 ++++++++++++++++++++++++++++-- src/admin/getpeers.go | 9 +++++++-- src/core/api.go | 4 ++++ src/core/link.go | 31 ++++++++++++++++++------------- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/cmd/yggdrasilctl/main.go b/cmd/yggdrasilctl/main.go index b63826d6..e06d497d 100644 --- a/cmd/yggdrasilctl/main.go +++ b/cmd/yggdrasilctl/main.go @@ -186,9 +186,9 @@ func run() int { if err := json.Unmarshal(recv.Response, &resp); err != nil { panic(err) } - table.SetHeader([]string{"URI", "State", "Dir", "IP Address", "Uptime", "RTT", "RX", "TX", "Down", "Up", "Pr", "Cost", "Last Error"}) + table.SetHeader([]string{"URI", "State", "Dir", "Name", "IP Address", "Uptime", "RTT", "RX", "TX", "Down", "Up", "Pr", "Cost", "Last Error"}) for _, peer := range resp.Peers { - state, lasterr, dir, rtt, rxr, txr := "Up", "-", "Out", "-", "-", "-" + state, lasterr, dir, rtt, rxr, txr, name := "Up", "-", "Out", "-", "-", "-", "-" if !peer.Up { state, lasterr = "Down", fmt.Sprintf("%s ago: %s", peer.LastErrorTime.Round(time.Second), peer.LastError) } else if rttms := float64(peer.Latency.Microseconds()) / 1000; rttms > 0 { @@ -197,6 +197,31 @@ func run() int { if peer.Inbound { dir = "In" } + + // Extract name from NodeInfo if available + if peer.NodeInfo != "" { + fmt.Printf("[DEBUG] Peer %s has NodeInfo: %s\n", peer.IPAddress, peer.NodeInfo) + var nodeInfo map[string]interface{} + if err := json.Unmarshal([]byte(peer.NodeInfo), &nodeInfo); err == nil { + fmt.Printf("[DEBUG] Parsed NodeInfo for %s: %+v\n", peer.IPAddress, nodeInfo) + if nameValue, ok := nodeInfo["name"]; ok { + fmt.Printf("[DEBUG] Found name field for %s: %v (type: %T)\n", peer.IPAddress, nameValue, nameValue) + if nameStr, ok := nameValue.(string); ok && nameStr != "" { + name = nameStr + fmt.Printf("[DEBUG] Set name for %s: %s\n", peer.IPAddress, name) + } else { + fmt.Printf("[DEBUG] Name field for %s is not a non-empty string\n", peer.IPAddress) + } + } else { + fmt.Printf("[DEBUG] No 'name' field found in NodeInfo for %s\n", peer.IPAddress) + } + } else { + fmt.Printf("[DEBUG] Failed to parse NodeInfo JSON for %s: %v\n", peer.IPAddress, err) + } + } else { + fmt.Printf("[DEBUG] Peer %s has empty NodeInfo\n", peer.IPAddress) + } + uristring := peer.URI if uri, err := url.Parse(peer.URI); err == nil { uri.RawQuery = "" @@ -213,6 +238,7 @@ func run() int { uristring, state, dir, + name, peer.IPAddress, (time.Duration(peer.Uptime) * time.Second).String(), rtt, diff --git a/src/admin/getpeers.go b/src/admin/getpeers.go index a013d12a..f54decdf 100644 --- a/src/admin/getpeers.go +++ b/src/admin/getpeers.go @@ -2,11 +2,13 @@ package admin import ( "encoding/hex" - "github.com/yggdrasil-network/yggdrasil-go/src/address" + "fmt" "net" "slices" "strings" "time" + + "github.com/yggdrasil-network/yggdrasil-go/src/address" ) type GetPeersRequest struct { @@ -64,10 +66,13 @@ func (a *AdminSocket) getPeersHandler(_ *GetPeersRequest, res *GetPeersResponse) peer.LastError = p.LastError.Error() peer.LastErrorTime = time.Since(p.LastErrorTime) } - + // Add NodeInfo if available if len(p.NodeInfo) > 0 { peer.NodeInfo = string(p.NodeInfo) + fmt.Printf("[DEBUG] Admin: Added NodeInfo for peer %s: %s\n", peer.IPAddress, peer.NodeInfo) + } else { + fmt.Printf("[DEBUG] Admin: No NodeInfo for peer %s\n", peer.IPAddress) } res.Peers = append(res.Peers, peer) diff --git a/src/core/api.go b/src/core/api.go index d4b96391..beaeb025 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -3,6 +3,7 @@ package core import ( "crypto/ed25519" "encoding/json" + "fmt" "net" "net/url" "sync/atomic" @@ -97,6 +98,9 @@ func (c *Core) GetPeers() []PeerInfo { if len(state._nodeInfo) > 0 { peerinfo.NodeInfo = make([]byte, len(state._nodeInfo)) copy(peerinfo.NodeInfo, state._nodeInfo) + fmt.Printf("[DEBUG] Core: Added NodeInfo from handshake for link state: %s\n", string(state._nodeInfo)) + } else { + fmt.Printf("[DEBUG] Core: No NodeInfo in link state\n") } } if p, ok := conns[conn]; ok { diff --git a/src/core/link.go b/src/core/link.go index 7645aa3d..97cadcd2 100644 --- a/src/core/link.go +++ b/src/core/link.go @@ -367,7 +367,7 @@ func (l *links) add(u *url.URL, sintf string, linkType linkType) error { // Give the connection to the handler. The handler will block // for the lifetime of the connection. - switch err = l.handler(linkType, options, lc, resetBackoff, false); { + switch err = l.handler(linkType, options, lc, resetBackoff, false, state); { case err == nil: case errors.Is(err, io.EOF): case errors.Is(err, net.ErrClosed): @@ -563,7 +563,7 @@ func (l *links) listen(u *url.URL, sintf string, local bool) (*Listener, error) // Give the connection to the handler. The handler will block // for the lifetime of the connection. - switch err = l.handler(linkTypeIncoming, options, lc, nil, local); { + switch err = l.handler(linkTypeIncoming, options, lc, nil, local, state); { case err == nil: case errors.Is(err, io.EOF): case errors.Is(err, net.ErrClosed): @@ -604,7 +604,7 @@ func (l *links) connect(ctx context.Context, u *url.URL, info linkInfo, options return dialer.dial(ctx, u, info, options) } -func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, success func(), local bool) error { +func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, success func(), local bool, linkState *link) error { meta := version_getBaseMetadata() meta.publicKey = l.core.public meta.priority = options.priority @@ -615,6 +615,9 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s if len(nodeInfo) > 0 { meta.nodeInfo = make([]byte, len(nodeInfo)) copy(meta.nodeInfo, nodeInfo) + fmt.Printf("[DEBUG] Link: Adding our NodeInfo to handshake: %s\n", string(nodeInfo)) + } else { + fmt.Printf("[DEBUG] Link: No NodeInfo to add to handshake\n") } }) @@ -676,16 +679,18 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s // Store the received NodeInfo in the link state if len(meta.nodeInfo) > 0 { - phony.Block(l, func() { - // Find the link state for this connection - for _, state := range l._links { - if state._conn != nil && state._conn.Conn == conn { - state._nodeInfo = make([]byte, len(meta.nodeInfo)) - copy(state._nodeInfo, meta.nodeInfo) - break - } - } - }) + fmt.Printf("[DEBUG] Link: Received NodeInfo from peer: %s\n", string(meta.nodeInfo)) + if linkState != nil { + phony.Block(l, func() { + linkState._nodeInfo = make([]byte, len(meta.nodeInfo)) + copy(linkState._nodeInfo, meta.nodeInfo) + fmt.Printf("[DEBUG] Link: Stored NodeInfo in link state\n") + }) + } else { + fmt.Printf("[DEBUG] Link: linkState is nil, cannot store NodeInfo\n") + } + } else { + fmt.Printf("[DEBUG] Link: No NodeInfo received from peer\n") } dir := "outbound"