peers.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package cli
  14. import (
  15. "fmt"
  16. "os"
  17. "zerotier/pkg/zerotier"
  18. )
  19. // Peers CLI command
  20. func Peers(basePath, authToken string, args []string, jsonOutput bool) {
  21. var peers []zerotier.Peer
  22. apiGet(basePath, authToken, "/peer", &peers)
  23. if jsonOutput {
  24. fmt.Println(jsonDump(&peers))
  25. } else {
  26. fmt.Printf("<address> <ver> <role> <lat> <link> <lastTX> <lastRX> <path(s)>\n")
  27. for _, peer := range peers {
  28. role := "LEAF"
  29. link := "RELAY"
  30. lastTX, lastRX := int64(0), int64(0)
  31. address := ""
  32. if len(peer.Paths) > 0 {
  33. link = "DIRECT"
  34. lastTX, lastRX = peer.Clock-peer.Paths[0].LastSend, peer.Clock-peer.Paths[0].LastReceive
  35. if lastTX < 0 {
  36. lastTX = 0
  37. }
  38. if lastRX < 0 {
  39. lastRX = 0
  40. }
  41. address = fmt.Sprintf("%s/%d", peer.Paths[0].IP.String(), peer.Paths[0].Port)
  42. }
  43. fmt.Printf("%.10x %-7s %-6s %-5d %-6s %-8d %-8d %s\n",
  44. uint64(peer.Address),
  45. fmt.Sprintf("%d.%d.%d", peer.Version[0], peer.Version[1], peer.Version[2]),
  46. role,
  47. peer.Latency,
  48. link,
  49. lastTX,
  50. lastRX,
  51. address,
  52. )
  53. }
  54. }
  55. os.Exit(0)
  56. }