zerotier.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 main
  14. import "C"
  15. import (
  16. "flag"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "runtime"
  22. "runtime/debug"
  23. "strings"
  24. "zerotier/cmd/zerotier/cli"
  25. "zerotier/pkg/zerotier"
  26. )
  27. func getAuthTokenPaths(basePath string) (p []string) {
  28. p = append(p, path.Join(basePath, "authtoken.secret"))
  29. userHome, _ := os.UserHomeDir()
  30. if len(userHome) > 0 {
  31. if runtime.GOOS == "darwin" {
  32. p = append(p, path.Join(userHome, "Library", "Application Support", "ZeroTier", "authtoken.secret"))
  33. p = append(p, path.Join(userHome, "Library", "Application Support", "ZeroTier", "One", "authtoken.secret"))
  34. }
  35. p = append(p, path.Join(userHome, ".zerotierauth"))
  36. p = append(p, path.Join(userHome, ".zeroTierOneAuthToken"))
  37. }
  38. return p
  39. }
  40. func authTokenRequired(basePath, tflag, tTflag string) string {
  41. authTokenPaths := getAuthTokenPaths(basePath)
  42. var authToken string
  43. if len(tflag) > 0 {
  44. at, err := ioutil.ReadFile(tflag)
  45. if err != nil || len(at) == 0 {
  46. fmt.Println("FATAL: unable to read local service API authorization token from " + tflag)
  47. os.Exit(1)
  48. }
  49. authToken = string(at)
  50. } else if len(tTflag) > 0 {
  51. authToken = tTflag
  52. } else {
  53. for _, p := range authTokenPaths {
  54. tmp, _ := ioutil.ReadFile(p)
  55. if len(tmp) > 0 {
  56. authToken = string(tmp)
  57. break
  58. }
  59. }
  60. if len(authToken) == 0 {
  61. fmt.Println("FATAL: unable to read local service API authorization token from any of:")
  62. for _, p := range authTokenPaths {
  63. fmt.Println(" " + p)
  64. }
  65. os.Exit(1)
  66. }
  67. }
  68. authToken = strings.TrimSpace(authToken)
  69. if len(authToken) == 0 {
  70. fmt.Println("FATAL: unable to read API authorization token from command line or any filesystem location.")
  71. os.Exit(1)
  72. }
  73. return authToken
  74. }
  75. func main() {
  76. // Reduce Go's thread and memory footprint. This would slow things down if the Go code
  77. // were doing a lot, but it's not. It just manages the core and is not directly involved
  78. // in pushing a lot of packets around. If that ever changes this should be adjusted.
  79. runtime.GOMAXPROCS(1)
  80. debug.SetGCPercent(15)
  81. globalOpts := flag.NewFlagSet("global", flag.ContinueOnError)
  82. hflag := globalOpts.Bool("h", false, "") // support -h to be canonical with other Unix utilities
  83. jflag := globalOpts.Bool("j", false, "")
  84. pflag := globalOpts.String("p", "", "")
  85. tflag := globalOpts.String("t", "", "")
  86. tTflag := globalOpts.String("T", "", "")
  87. err := globalOpts.Parse(os.Args[1:])
  88. if err != nil {
  89. cli.Help()
  90. os.Exit(1)
  91. return
  92. }
  93. args := globalOpts.Args()
  94. if len(args) < 1 || *hflag {
  95. cli.Help()
  96. os.Exit(0)
  97. return
  98. }
  99. var cmdArgs []string
  100. if len(args) > 1 {
  101. cmdArgs = args[1:]
  102. }
  103. basePath := zerotier.PlatformDefaultHomePath
  104. if len(*pflag) > 0 {
  105. basePath = *pflag
  106. }
  107. switch args[0] {
  108. default:
  109. //case "help":
  110. cli.Help()
  111. case "version":
  112. fmt.Printf("%d.%d.%d\n", zerotier.CoreVersionMajor, zerotier.CoreVersionMinor, zerotier.CoreVersionRevision)
  113. case "service":
  114. cli.Service(basePath, cmdArgs)
  115. case "status", "info":
  116. cli.Status(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag)
  117. case "join":
  118. cli.Join(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs)
  119. case "leave":
  120. cli.Leave(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs)
  121. case "networks", "listnetworks":
  122. cli.Networks(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag)
  123. case "network":
  124. cli.Network(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag)
  125. case "peers", "listpeers", "lspeers":
  126. cli.Peers(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag, false)
  127. case "peer":
  128. authTokenRequired(basePath, *tflag, *tTflag)
  129. case "roots":
  130. cli.Peers(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag, true)
  131. case "root":
  132. cli.Root(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag)
  133. case "set":
  134. cli.Set(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs)
  135. case "controller":
  136. cli.Controller(basePath, authTokenRequired(basePath, *tflag, *tTflag), cmdArgs, *jflag)
  137. case "identity":
  138. cli.Identity(cmdArgs)
  139. }
  140. os.Exit(0)
  141. }