peer.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: 2025-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. "strings"
  17. "zerotier/pkg/zerotier"
  18. )
  19. func listPeers(basePath, authToken string, jsonOutput bool, rootsOnly bool) int {
  20. var peers []zerotier.Peer
  21. apiGet(basePath, authToken, "/peer", &peers)
  22. if rootsOnly {
  23. roots := make([]zerotier.Peer, 0, len(peers))
  24. for i := range peers {
  25. if peers[i].Root {
  26. roots = append(roots, peers[i])
  27. }
  28. }
  29. peers = roots
  30. }
  31. if jsonOutput {
  32. fmt.Println(jsonDump(&peers))
  33. } else {
  34. fmt.Printf("<address> <ver> <root> <lat(ms)> <path(s)>\n")
  35. for _, peer := range peers {
  36. root := ""
  37. if peer.Root {
  38. root = " *"
  39. }
  40. var paths strings.Builder
  41. if len(peer.Paths) > 0 {
  42. if paths.Len() > 0 {
  43. paths.WriteRune(' ')
  44. }
  45. paths.WriteString(peer.Paths[0].Endpoint.String())
  46. } else {
  47. paths.WriteString("(relayed)")
  48. }
  49. fmt.Printf("%.10x %-7s %-6s %-9d %s\n",
  50. uint64(peer.Address),
  51. fmt.Sprintf("%d.%d.%d", peer.Version[0], peer.Version[1], peer.Version[2]),
  52. root,
  53. peer.Latency,
  54. paths.String())
  55. }
  56. }
  57. return 0
  58. }
  59. func Peer(basePath string, authTokenGenerator func() string, args []string, jsonOutput bool) int {
  60. if len(args) < 1 {
  61. Help()
  62. return 1
  63. }
  64. authToken := authTokenGenerator()
  65. //var addr zerotier.Address
  66. if isValidAddress(args[0]) {
  67. //addr, _ = zerotier.NewAddressFromString(args[0])
  68. args = args[1:]
  69. if len(args) < 1 {
  70. Help()
  71. return 1
  72. }
  73. }
  74. switch args[0] {
  75. case "list":
  76. return listPeers(basePath, authToken, jsonOutput, false)
  77. case "listroots":
  78. return listPeers(basePath, authToken, jsonOutput, true)
  79. case "show":
  80. case "try":
  81. }
  82. return 0
  83. }