zerotier.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 main
  14. import (
  15. "flag"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "runtime"
  21. "strings"
  22. "zerotier/cmd/zerotier/cli"
  23. "zerotier/pkg/zerotier"
  24. )
  25. var copyrightText = fmt.Sprintf(`ZeroTier Network Virtualization Service Version %d.%d.%d
  26. (c)2019 ZeroTier, Inc.
  27. Licensed under the ZeroTier BSL (see LICENSE.txt)`, zerotier.CoreVersionMajor, zerotier.CoreVersionMinor, zerotier.CoreVersionRevision)
  28. func printHelp() {
  29. fmt.Println(copyrightText + `
  30. Usage: zerotier [-options] <command> [-options] [command args]
  31. Global Options
  32. -j Output raw JSON where applicable
  33. -p <path> Connect to service running at this path
  34. -t <authtoken.secret path> Use secret auth token from this file
  35. Commands:
  36. help Show this help
  37. version Print version
  38. service [path] Start in system service mode
  39. status Show ZeroTier service status and config
  40. peers Show VL1 peers
  41. roots Show VL1 root servers
  42. addroot <type> [options] Add a VL1 root
  43. static <identity> <ip/port> [...] Add a root with a set identity and IPs
  44. dynamic <name> [default locator] Add a dynamic root fetched by name
  45. removeroot <type> [options] Remove a VL1 root
  46. static <identity> Remove a root with a set identity
  47. dynamic <name> Remove a dynamic root fetched by name
  48. networks Show joined VL2 virtual networks
  49. join <network ID> Join a virtual network
  50. leave <network ID> Leave a virtual network
  51. show <network ID> Show verbose network info
  52. set <network ID> <option> <value> Set a network local config option
  53. manageips <boolean> Is IP management allowed?
  54. manageroutes <boolean> Is route management allowed?
  55. globalips <boolean> Can IPs in global IP space be managed?
  56. globalroutes <boolean> Can global IP space routes be set?
  57. defaultroute <boolean> Can default route be overridden?
  58. set <local config option> <value> Set a local configuration option
  59. phy <IP/bits> blacklist <boolean> Set or clear blacklist for CIDR
  60. phy <IP/bits> trust <path ID/0> Set or clear trusted path ID for CIDR
  61. virt <address> try <IP/port> [...] Set explicit IPs for reaching a peer
  62. port <port> Set primary local port for VL1 P2P
  63. secondaryport <port/0> Set or disable secondary VL1 P2P port
  64. tertiaryport <port/0> Set or disable tertiary VL1 P2P port
  65. portsearch <boolean> Set or disable port search on startup
  66. portmapping <boolean> Set or disable use of uPnP and NAT-PMP
  67. explicitaddresses <IP/port> [...] Set explicit external IPs to advertise
  68. Most commands require a secret token to permit control of a running ZeroTier
  69. service. The CLI will automatically try to read this token from the
  70. authtoken.secret file in the service's working directory and then from a
  71. file called .zerotierauth in the user's home directory. The -t option can be
  72. used to explicitly specify a location.
  73. `)
  74. }
  75. func readAuthToken(basePath string) string {
  76. data, _ := ioutil.ReadFile(path.Join(basePath, "authtoken.secret"))
  77. if len(data) > 0 {
  78. return string(data)
  79. }
  80. userHome, _ := os.UserHomeDir()
  81. if len(userHome) > 0 {
  82. if runtime.GOOS == "darwin" {
  83. data, _ = ioutil.ReadFile(userHome + "/Library/Application Support/ZeroTier/authtoken.secret")
  84. if len(data) > 0 {
  85. return string(data)
  86. }
  87. data, _ = ioutil.ReadFile(userHome + "/Library/Application Support/ZeroTier/One/authtoken.secret")
  88. if len(data) > 0 {
  89. return string(data)
  90. }
  91. }
  92. data, _ = ioutil.ReadFile(path.Join(userHome, ".zerotierauth"))
  93. if len(data) > 0 {
  94. return string(data)
  95. }
  96. data, _ = ioutil.ReadFile(path.Join(userHome, ".zeroTierOneAuthToken"))
  97. if len(data) > 0 {
  98. return string(data)
  99. }
  100. }
  101. return ""
  102. }
  103. func main() {
  104. globalOpts := flag.NewFlagSet("global", flag.ContinueOnError)
  105. hflag := globalOpts.Bool("h", false, "") // support -h to be canonical with other Unix utilities
  106. jflag := globalOpts.Bool("j", false, "")
  107. pflag := globalOpts.String("p", "", "")
  108. tflag := globalOpts.String("t", "", "")
  109. err := globalOpts.Parse(os.Args[1:])
  110. if err != nil {
  111. printHelp()
  112. os.Exit(1)
  113. return
  114. }
  115. args := globalOpts.Args()
  116. if len(args) < 1 || *hflag {
  117. printHelp()
  118. os.Exit(0)
  119. return
  120. }
  121. var cmdArgs []string
  122. if len(args) > 1 {
  123. cmdArgs = args[1:]
  124. }
  125. basePath := zerotier.PlatformDefaultHomePath
  126. if len(*pflag) > 0 {
  127. basePath = *pflag
  128. }
  129. var authToken string
  130. if len(*tflag) > 0 {
  131. authToken = *tflag
  132. } else {
  133. authToken = readAuthToken(basePath)
  134. }
  135. if len(authToken) == 0 {
  136. fmt.Println("FATAL: unable to read API authorization token from service path or user home ('sudo' may be needed)")
  137. os.Exit(1)
  138. }
  139. authToken = strings.TrimSpace(authToken)
  140. switch args[0] {
  141. case "help":
  142. printHelp()
  143. os.Exit(0)
  144. case "version":
  145. fmt.Printf("%d.%d.%d\n", zerotier.CoreVersionMajor, zerotier.CoreVersionMinor, zerotier.CoreVersionRevision)
  146. os.Exit(0)
  147. case "service":
  148. cli.Service(basePath, authToken, cmdArgs)
  149. case "status":
  150. cli.Status(basePath, authToken, cmdArgs, *jflag)
  151. case "peers":
  152. cli.Peers(basePath, authToken, cmdArgs)
  153. case "roots":
  154. cli.Roots(basePath, authToken, cmdArgs)
  155. case "addroot":
  156. cli.AddRoot(basePath, authToken, cmdArgs)
  157. case "removeroot":
  158. cli.RemoveRoot(basePath, authToken, cmdArgs)
  159. case "networks":
  160. cli.Networks(basePath, authToken, cmdArgs)
  161. case "join":
  162. cli.Join(basePath, authToken, cmdArgs)
  163. case "leave":
  164. cli.Leave(basePath, authToken, cmdArgs)
  165. case "show":
  166. cli.Show(basePath, authToken, cmdArgs)
  167. case "set":
  168. cli.Set(basePath, authToken, cmdArgs)
  169. }
  170. printHelp()
  171. os.Exit(1)
  172. }