mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-08-24 16:05:07 +03:00
Update peer handling to extract and display NodeInfo names in CLI and admin responses. Enhance debug logging for NodeInfo processing in various components.
This commit is contained in:
parent
1f8f36860f
commit
795cc506fd
4 changed files with 57 additions and 17 deletions
|
@ -186,9 +186,9 @@ func run() int {
|
||||||
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
if err := json.Unmarshal(recv.Response, &resp); err != nil {
|
||||||
panic(err)
|
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 {
|
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 {
|
if !peer.Up {
|
||||||
state, lasterr = "Down", fmt.Sprintf("%s ago: %s", peer.LastErrorTime.Round(time.Second), peer.LastError)
|
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 {
|
} else if rttms := float64(peer.Latency.Microseconds()) / 1000; rttms > 0 {
|
||||||
|
@ -197,6 +197,31 @@ func run() int {
|
||||||
if peer.Inbound {
|
if peer.Inbound {
|
||||||
dir = "In"
|
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
|
uristring := peer.URI
|
||||||
if uri, err := url.Parse(peer.URI); err == nil {
|
if uri, err := url.Parse(peer.URI); err == nil {
|
||||||
uri.RawQuery = ""
|
uri.RawQuery = ""
|
||||||
|
@ -213,6 +238,7 @@ func run() int {
|
||||||
uristring,
|
uristring,
|
||||||
state,
|
state,
|
||||||
dir,
|
dir,
|
||||||
|
name,
|
||||||
peer.IPAddress,
|
peer.IPAddress,
|
||||||
(time.Duration(peer.Uptime) * time.Second).String(),
|
(time.Duration(peer.Uptime) * time.Second).String(),
|
||||||
rtt,
|
rtt,
|
||||||
|
|
|
@ -2,11 +2,13 @@ package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetPeersRequest struct {
|
type GetPeersRequest struct {
|
||||||
|
@ -64,10 +66,13 @@ func (a *AdminSocket) getPeersHandler(_ *GetPeersRequest, res *GetPeersResponse)
|
||||||
peer.LastError = p.LastError.Error()
|
peer.LastError = p.LastError.Error()
|
||||||
peer.LastErrorTime = time.Since(p.LastErrorTime)
|
peer.LastErrorTime = time.Since(p.LastErrorTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add NodeInfo if available
|
// Add NodeInfo if available
|
||||||
if len(p.NodeInfo) > 0 {
|
if len(p.NodeInfo) > 0 {
|
||||||
peer.NodeInfo = string(p.NodeInfo)
|
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)
|
res.Peers = append(res.Peers, peer)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package core
|
||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
@ -97,6 +98,9 @@ func (c *Core) GetPeers() []PeerInfo {
|
||||||
if len(state._nodeInfo) > 0 {
|
if len(state._nodeInfo) > 0 {
|
||||||
peerinfo.NodeInfo = make([]byte, len(state._nodeInfo))
|
peerinfo.NodeInfo = make([]byte, len(state._nodeInfo))
|
||||||
copy(peerinfo.NodeInfo, 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 {
|
if p, ok := conns[conn]; ok {
|
||||||
|
|
|
@ -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
|
// Give the connection to the handler. The handler will block
|
||||||
// for the lifetime of the connection.
|
// 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 err == nil:
|
||||||
case errors.Is(err, io.EOF):
|
case errors.Is(err, io.EOF):
|
||||||
case errors.Is(err, net.ErrClosed):
|
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
|
// Give the connection to the handler. The handler will block
|
||||||
// for the lifetime of the connection.
|
// 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 err == nil:
|
||||||
case errors.Is(err, io.EOF):
|
case errors.Is(err, io.EOF):
|
||||||
case errors.Is(err, net.ErrClosed):
|
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)
|
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 := version_getBaseMetadata()
|
||||||
meta.publicKey = l.core.public
|
meta.publicKey = l.core.public
|
||||||
meta.priority = options.priority
|
meta.priority = options.priority
|
||||||
|
@ -615,6 +615,9 @@ func (l *links) handler(linkType linkType, options linkOptions, conn net.Conn, s
|
||||||
if len(nodeInfo) > 0 {
|
if len(nodeInfo) > 0 {
|
||||||
meta.nodeInfo = make([]byte, len(nodeInfo))
|
meta.nodeInfo = make([]byte, len(nodeInfo))
|
||||||
copy(meta.nodeInfo, 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
|
// Store the received NodeInfo in the link state
|
||||||
if len(meta.nodeInfo) > 0 {
|
if len(meta.nodeInfo) > 0 {
|
||||||
phony.Block(l, func() {
|
fmt.Printf("[DEBUG] Link: Received NodeInfo from peer: %s\n", string(meta.nodeInfo))
|
||||||
// Find the link state for this connection
|
if linkState != nil {
|
||||||
for _, state := range l._links {
|
phony.Block(l, func() {
|
||||||
if state._conn != nil && state._conn.Conn == conn {
|
linkState._nodeInfo = make([]byte, len(meta.nodeInfo))
|
||||||
state._nodeInfo = make([]byte, len(meta.nodeInfo))
|
copy(linkState._nodeInfo, meta.nodeInfo)
|
||||||
copy(state._nodeInfo, meta.nodeInfo)
|
fmt.Printf("[DEBUG] Link: Stored NodeInfo in link state\n")
|
||||||
break
|
})
|
||||||
}
|
} 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"
|
dir := "outbound"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue