Skip to content

Commit

Permalink
Merge pull request #1321 from nerdalert/fix-nexctl-peer-list-panic
Browse files Browse the repository at this point in the history
Resolve the nexctl peer list panic
  • Loading branch information
mergify[bot] authored Aug 21, 2023
2 parents c9efe3e + 50b8188 commit 20d07f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
70 changes: 35 additions & 35 deletions cmd/nexctl/peers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"

"encoding/json"

"github.com/nexodus-io/nexodus/internal/util"
"github.com/urfave/cli/v2"
)
Expand All @@ -21,38 +21,7 @@ type WgListPeers struct {
Healthy bool
}

func peerTableFields(cCtx *cli.Context) []TableField {
var fields []TableField
fields = append(fields, TableField{Header: "PUBLIC KEY", Field: "PublicKey"})
full := cCtx.Bool("full")
if full {
fields = append(fields, TableField{Header: "ENDPOINT", Field: "Endpoint"})
}
fields = append(fields, TableField{Header: "ALLOWED IPS", Field: "AllowedIPs"})
if full {
fields = append(fields, TableField{
Header: "LATEST HANDSHAKE",
Formatter: func(item interface{}) string {
peer := item.(WgListPeers)
handshakeTime, err := util.ParseTime(peer.LatestHandshake)
if err != nil {
log.Printf("Unable to parse LatestHandshake to time: %v", err)
}
handshake := "None"
if !handshakeTime.IsZero() {
secondsAgo := time.Now().UTC().Sub(handshakeTime).Seconds()
handshake = fmt.Sprintf("%.0f seconds ago", secondsAgo)
}
return handshake
},
})
fields = append(fields, TableField{Header: "TRANSMITTED", Field: "Tx"})
fields = append(fields, TableField{Header: "RECEIVED", Field: "Rx"})
}
fields = append(fields, TableField{Header: "HEALTHY", Field: ""})
return fields
}

// cmdListPeers get peer listings from nexd
func cmdListPeers(cCtx *cli.Context, encodeOut string) error {
var err error
var peers map[string]WgListPeers
Expand All @@ -70,6 +39,37 @@ func cmdListPeers(cCtx *cli.Context, encodeOut string) error {
return fmt.Errorf("Failed to marshall peer results: %w\n", err)
}

showOutput(cCtx, peerTableFields(cCtx), peers)
if encodeOut == encodeColumn || encodeOut == encodeNoHeader {
w := newTabWriter()
fs := "%s\t%s\t%s\t%s\t%s\t%s\t%s\n"
if encodeOut != encodeNoHeader {
fmt.Fprintf(w, fs, "PUBLIC KEY", "ENDPOINT", "ALLOWED IPS", "LATEST HANDSHAKE", "TRANSMITTED", "RECEIVED", "HEALTHY")
}

for _, peer := range peers {
tx := strconv.FormatInt(peer.Tx, 10)
rx := strconv.FormatInt(peer.Rx, 10)
handshakeTime, err := util.ParseTime(peer.LatestHandshake)
if err != nil {
log.Printf("Unable to parse LatestHandshake to time: %v", err)
}
handshake := "None"
if !handshakeTime.IsZero() {
secondsAgo := time.Now().UTC().Sub(handshakeTime).Seconds()
handshake = fmt.Sprintf("%.0f seconds ago", secondsAgo)
}
fmt.Fprintf(w, fs, peer.PublicKey, peer.Endpoint, peer.AllowedIPs, handshake, tx, rx, strconv.FormatBool(peer.Healthy))
}

w.Flush()

return nil
}

err = FormatOutput(encodeOut, peers)
if err != nil {
log.Fatalf("Failed to print output: %v", err)
}

return nil
}
15 changes: 12 additions & 3 deletions integration-tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/nexodus-io/nexodus/internal/state"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net"
"testing"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/nexodus-io/nexodus/internal/models"
"github.com/nexodus-io/nexodus/internal/state"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

func TestBasicConnectivity(t *testing.T) {
Expand Down Expand Up @@ -825,7 +825,7 @@ func TestNexctl(t *testing.T) {
require.NotEmpty(organizations[0].IpCidrV6)
require.NotEmpty(organizations[0].Description)

// verify nexd peers list
// validate nexctl nexd peers list does not throw any errors with no peers present
err = helper.peerListNexdDevices(ctx, node1)
require.NoError(err)

Expand Down Expand Up @@ -863,6 +863,15 @@ func TestNexctl(t *testing.T) {
"device", "list",
)
require.NoErrorf(err, "nexctl device list error: %v\n", err)

// validate nexctl nexd peers list does not throw any errors with peers present
pListOut, err := helper.containerExec(ctx, node1, []string{"/bin/nexctl", "nexd", "peers", "list"})
require.NoError(err)
node2Eth0, err := node2.ContainerIP(ctx)
require.NoError(err)
require.Contains(pListOut, node2Eth0)
helper.Logf("nexctl nexd peer list output: %s", pListOut)

var devices []models.Device
err = json.Unmarshal([]byte(allDevices), &devices)
require.NoErrorf(err, "nexctl device Unmarshal error: %v\n", err)
Expand Down

0 comments on commit 20d07f5

Please sign in to comment.