Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status error nodes #57

Merged
merged 8 commits into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions src/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"net/netip"
"strings"
"log"

"github.com/fatih/color"
"github.com/m1gwings/treedrawer/tree"
Expand Down Expand Up @@ -54,6 +53,7 @@ func (c statusCmdConfig) Run() {
relayConfig peer.Config
e2eeConfig peer.Config
children []*Node
error string
}

var err error
Expand All @@ -75,12 +75,16 @@ func (c statusCmdConfig) Run() {
// Get map of all nodes for building tree.
// Key on public key of relay interfaces.
nodes := make(map[string]Node)
e2ee_peer_list := clientConfigE2EE.GetPeers()
var errorNodes []Node
e2ee_peer_list := client.e2eeConfig.GetPeers()
for _, ep := range e2ee_peer_list {
relayConfig, e2eeConfig, err := api.ServerInfo(netip.AddrPortFrom(ep.GetApiAddr(), uint16(ApiPort)))
if err != nil {
message := "failed to fetch node's configuration as peer"
log.Printf("%s: %v", message, err)
errorNodes = append(errorNodes, Node{
Aptimex marked this conversation as resolved.
Show resolved Hide resolved
peerConfig: ep,
error: err.Error(),
})

} else {
nodes[relayConfig.GetPublicKey()] = Node{
peerConfig: ep,
Expand Down Expand Up @@ -127,7 +131,7 @@ func (c statusCmdConfig) Run() {
ips := []string{}
var api string
for j, a := range c.peerConfig.GetAllowedIPs() {
if j == len(c.peerConfig.GetAllowedIPs())-1 {
if j == len(c.peerConfig.GetAllowedIPs()) - 1 {
api = a.IP.String()
} else {
ips = append(ips, a.String())
Expand All @@ -149,6 +153,56 @@ func (c statusCmdConfig) Run() {
check("could not build tree", err)
treeTraversal(&client, child)

fmt.Fprintln(color.Output)
fmt.Println()
fmt.Fprintln(color.Output, WhiteBold(t))
fmt.Println()

if len(errorNodes) > 0 {
// Display known peers that we had issues connecting to
fmt.Fprintln(color.Output, WhiteBold("Peers with Errors:"))
fmt.Println()

for _, node := range errorNodes {
ips := []string{}
var api string
for j, a := range node.peerConfig.GetAllowedIPs() {
if j == len(node.peerConfig.GetAllowedIPs()) - 1 {
api = a.IP.String()
} else {
ips = append(ips, a.String())
}
}

t = tree.NewTree(tree.NodeString(fmt.Sprintf(`server

nickname: %v
e2ee: %v...
api: %v
routes: %v

error: %v`, node.peerConfig.GetNickname(), node.peerConfig.GetPublicKey().String()[:8], api, strings.Join(ips, ","), errorWrap(node.error, 80))))
fmt.Fprintln(color.Output, WhiteBold(t))
}
}
}

func errorWrap(text string, lineWidth int) string {
words := strings.Fields(strings.TrimSpace(text))
if len(words) == 0 {
return text
}
wrapped := words[0]
spaceLeft := lineWidth - len(wrapped)
indent := len(" error: ")
for _, word := range words[1:] {
if len(word)+1 > spaceLeft {
wrapped += " \n" + strings.Repeat(" ", indent) + word
spaceLeft = lineWidth - len(word)
} else {
wrapped += " " + word
spaceLeft -= 1 + len(word)
}
}

return wrapped
}
Loading