network.go 4.1 KB

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