peers.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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. "strings"
  18. "zerotier/pkg/zerotier"
  19. )
  20. func Peers(basePath, authToken string, args []string, jsonOutput bool, rootsOnly bool) {
  21. var peers []zerotier.Peer
  22. apiGet(basePath, authToken, "/peer", &peers)
  23. if rootsOnly {
  24. roots := make([]zerotier.Peer, 0, len(peers))
  25. for i := range peers {
  26. if peers[i].Root {
  27. roots = append(roots, peers[i])
  28. }
  29. }
  30. peers = roots
  31. }
  32. if jsonOutput {
  33. fmt.Println(jsonDump(&peers))
  34. } else {
  35. fmt.Printf("<address> <ver> <root> <lat(ms)> <path(s)>\n")
  36. for _, peer := range peers {
  37. root := ""
  38. if peer.Root {
  39. root = " *"
  40. }
  41. var paths strings.Builder
  42. if len(peer.Paths) > 0 {
  43. if paths.Len() > 0 {
  44. paths.WriteRune(' ')
  45. }
  46. paths.WriteString(peer.Paths[0].Endpoint.String())
  47. } else {
  48. paths.WriteString("(relayed)")
  49. }
  50. fmt.Printf("%.10x %-7s %-6s %-9d %s\n",
  51. uint64(peer.Address),
  52. fmt.Sprintf("%d.%d.%d", peer.Version[0], peer.Version[1], peer.Version[2]),
  53. root,
  54. peer.Latency,
  55. paths.String())
  56. }
  57. }
  58. os.Exit(0)
  59. }