network.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "strconv"
  17. "strings"
  18. "zerotier/pkg/zerotier"
  19. )
  20. func listNetworks(basePath, authToken string, jsonOutput bool) int {
  21. var networks []zerotier.APINetwork
  22. apiGet(basePath, authToken, "/network", &networks)
  23. if jsonOutput {
  24. fmt.Println(jsonDump(networks))
  25. } else {
  26. fmt.Printf("%-16s %-24s %-17s %-8s <type> <device> <managed IP(s)>\n", "<id>", "<name>", "<mac>", "<status>")
  27. for _, nw := range networks {
  28. t := "PRIVATE"
  29. if nw.Config.Type == zerotier.NetworkTypePublic {
  30. t = "PUBLIC"
  31. }
  32. fmt.Printf("%.16x %-24s %-17s %-16s %-7s %-16s ", uint64(nw.ID), nw.Config.Name, nw.Config.MAC.String(), networkStatusStr(nw.Config.Status), t, nw.PortName)
  33. for i, ip := range nw.Config.AssignedAddresses {
  34. if i > 0 {
  35. fmt.Print(",")
  36. }
  37. fmt.Print(ip.String())
  38. }
  39. fmt.Print("\n")
  40. }
  41. }
  42. return 0
  43. }
  44. func showNetwork(nwids string, network *zerotier.APINetwork, jsonOutput bool) {
  45. if jsonOutput {
  46. fmt.Println(jsonDump(&network))
  47. } else {
  48. fmt.Printf("%s\t%s\n", nwids, network.Config.Name)
  49. fmt.Printf("\tstatus:\t%s\n", networkStatusStr(network.Config.Status))
  50. enabled := "no"
  51. if network.PortEnabled {
  52. enabled = "yes"
  53. }
  54. bridge := "no"
  55. if network.Config.Bridge {
  56. bridge = "yes"
  57. }
  58. broadcast := "off"
  59. if network.Config.BroadcastEnabled {
  60. broadcast = "on"
  61. }
  62. fmt.Printf("\tport:\t%s dev %s type %s mtu %d enabled %s bridge %s broadcast %s\n", network.Config.MAC.String(), network.PortName, network.PortType, network.Config.MTU, enabled, bridge, broadcast)
  63. fmt.Printf("\tmanaged addresses:\t")
  64. for i, a := range network.Config.AssignedAddresses {
  65. if i > 0 {
  66. fmt.Print(" ")
  67. }
  68. fmt.Print(a.String())
  69. }
  70. fmt.Printf("\n\tmanaged routes:\t")
  71. for i, r := range network.Config.Routes {
  72. if i > 0 {
  73. fmt.Print(" ")
  74. }
  75. fmt.Print(r.Target.String())
  76. if r.Via == nil {
  77. fmt.Print("->LAN")
  78. } else {
  79. fmt.Printf("->%s", r.Via.String())
  80. }
  81. }
  82. managedIPs := "blocked"
  83. if network.Settings.AllowManagedIPs {
  84. managedIPs = "allowed"
  85. }
  86. managedIPsGlobal := "blocked"
  87. if network.Settings.AllowGlobalIPs {
  88. managedIPsGlobal = "allowed"
  89. }
  90. fmt.Printf("\n\tmanaged address local permissions:\t%s global %s\n", managedIPs, managedIPsGlobal)
  91. managedRoutes := "blocked"
  92. if network.Settings.AllowManagedRoutes {
  93. managedRoutes = "allowed"
  94. }
  95. managedGlobalRoutes := "blocked"
  96. if network.Settings.AllowGlobalRoutes {
  97. managedGlobalRoutes = "allowed"
  98. }
  99. managedDefaultRoute := "blocked"
  100. if network.Settings.AllowDefaultRouteOverride {
  101. managedDefaultRoute = "allowed"
  102. }
  103. fmt.Printf("\tmanaged route local permissions:\t%s global %s default %s\n", managedRoutes, managedGlobalRoutes, managedDefaultRoute)
  104. }
  105. }
  106. func Network(basePath string, authTokenGenerator func() string, args []string, jsonOutput bool) int {
  107. if len(args) < 1 {
  108. Help()
  109. return 1
  110. }
  111. authToken := authTokenGenerator()
  112. if len(args) == 1 && args[0] == "list" {
  113. return listNetworks(basePath, authToken, jsonOutput)
  114. }
  115. if len(args[0]) != zerotier.NetworkIDStringLength {
  116. pErr("ERROR: invalid network ID: %s", args[0])
  117. return 1
  118. }
  119. nwid, err := strconv.ParseUint(args[0], 16, 64)
  120. if err != nil {
  121. pErr("ERROR: invalid network ID: %s", args[0])
  122. return 1
  123. }
  124. nwids := fmt.Sprintf("%.16x", nwid)
  125. var network zerotier.APINetwork
  126. apiGet(basePath, authToken, "/network/"+nwids, &network)
  127. if len(args) == 1 {
  128. showNetwork(nwids, &network, jsonOutput)
  129. } else {
  130. switch args[1] {
  131. case "show", "info":
  132. showNetwork(nwids, &network, jsonOutput)
  133. case "set":
  134. if len(args) > 3 {
  135. Help()
  136. return 1
  137. } else if len(args) > 2 {
  138. fieldName := strings.ToLower(strings.TrimSpace(args[2]))
  139. var field *bool
  140. switch fieldName {
  141. case "managedips":
  142. field = &network.Settings.AllowManagedIPs
  143. case "managedroutes":
  144. field = &network.Settings.AllowGlobalRoutes
  145. case "globalips":
  146. field = &network.Settings.AllowGlobalIPs
  147. case "globalroutes":
  148. field = &network.Settings.AllowGlobalRoutes
  149. case "defaultroute":
  150. field = &network.Settings.AllowDefaultRouteOverride
  151. default:
  152. Help()
  153. return 1
  154. }
  155. if len(args) == 3 {
  156. *field = isTrue(args[2])
  157. }
  158. fmt.Printf("%s\t%t\n", fieldName, allowedBlocked(*field))
  159. } else {
  160. fmt.Printf("manageips\t%s\n", allowedBlocked(network.Settings.AllowManagedIPs))
  161. fmt.Printf("manageroutes\t%s\n", allowedBlocked(network.Settings.AllowManagedRoutes))
  162. fmt.Printf("globalips\t%s\n", allowedBlocked(network.Settings.AllowGlobalIPs))
  163. fmt.Printf("globalroutes\t%s\n", allowedBlocked(network.Settings.AllowGlobalRoutes))
  164. fmt.Printf("defaultroute\t%s\n", allowedBlocked(network.Settings.AllowDefaultRouteOverride))
  165. }
  166. }
  167. }
  168. return 0
  169. }