misc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. "encoding/json"
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "os"
  20. "strings"
  21. "zerotier/pkg/zerotier"
  22. )
  23. func apiGet(basePath, authToken, urlPath string, result interface{}) int64 {
  24. statusCode, clock, err := zerotier.APIGet(basePath, zerotier.APISocketName, authToken, urlPath, result)
  25. if err != nil {
  26. fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
  27. os.Exit(1)
  28. return 0
  29. }
  30. if statusCode != http.StatusOK {
  31. if statusCode == http.StatusUnauthorized {
  32. fmt.Printf("FATAL: API response code %d: unauthorized (authorization token incorrect)\n", statusCode)
  33. }
  34. fmt.Printf("FATAL: API response code %d\n", statusCode)
  35. os.Exit(1)
  36. return 0
  37. }
  38. return clock
  39. }
  40. func apiPost(basePath, authToken, urlPath string, post, result interface{}) int64 {
  41. statusCode, clock, err := zerotier.APIPost(basePath, zerotier.APISocketName, authToken, urlPath, post, result)
  42. if err != nil {
  43. fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
  44. os.Exit(1)
  45. return 0
  46. }
  47. if statusCode != http.StatusOK {
  48. if statusCode == http.StatusUnauthorized {
  49. fmt.Printf("FATAL: API response code %d: unauthorized (authorization token incorrect)\n", statusCode)
  50. }
  51. fmt.Printf("FATAL: API response code %d\n", statusCode)
  52. os.Exit(1)
  53. return 0
  54. }
  55. return clock
  56. }
  57. func apiDelete(basePath, authToken, urlPath string, result interface{}) int64 {
  58. statusCode, clock, err := zerotier.APIDelete(basePath, zerotier.APISocketName, authToken, urlPath, result)
  59. if err != nil {
  60. fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
  61. os.Exit(1)
  62. return 0
  63. }
  64. if statusCode != http.StatusOK {
  65. if statusCode == http.StatusUnauthorized {
  66. fmt.Printf("FATAL: API response code %d: unauthorized (authorization token incorrect)\n", statusCode)
  67. }
  68. fmt.Printf("FATAL: API response code %d\n", statusCode)
  69. os.Exit(1)
  70. return 0
  71. }
  72. return clock
  73. }
  74. func enabledDisabled(f bool) string {
  75. if f {
  76. return "enabled"
  77. }
  78. return "disabled"
  79. }
  80. func allowedBlocked(f bool) string {
  81. if f {
  82. return "allowed"
  83. }
  84. return "blocked"
  85. }
  86. // isTrueStringPrefixChars matches things like [Tt]rue, [Yy]es, 1, [Ee]nabled, and [Aa]llowed
  87. var isTrueStringPrefixChars = [9]uint8{'t', 'T', 'y', 'Y', '1', 'e', 'E', 'a', 'A'}
  88. func isTrue(s string) bool {
  89. if len(s) > 0 {
  90. f := s[0]
  91. for _, c := range isTrueStringPrefixChars {
  92. if c == f {
  93. return true
  94. }
  95. }
  96. }
  97. return false
  98. }
  99. func jsonDump(obj interface{}) string {
  100. j, _ := json.MarshalIndent(obj, "", "\t")
  101. return string(j)
  102. }
  103. // parseAddressFingerprintOrIdentity parses an argument as an address, fingerprint, or identity.
  104. // If it's an address, only that return variable is filled out. Fingerprints fill out both address and
  105. // fingerprint. Identity fills out all three.
  106. func parseAddressFingerprintOrIdentity(s string) (a zerotier.Address, fp *zerotier.Fingerprint, id *zerotier.Identity) {
  107. var err error
  108. s = strings.TrimSpace(s)
  109. hasColon := strings.ContainsRune(s, ':')
  110. hasDash := strings.ContainsRune(s, '-')
  111. if len(s) == zerotier.AddressStringLength && !hasColon && !hasDash {
  112. a, err = zerotier.NewAddressFromString(s)
  113. if err == nil {
  114. return
  115. }
  116. }
  117. if hasDash {
  118. fp, err = zerotier.NewFingerprintFromString(s)
  119. if err == nil {
  120. a = fp.Address
  121. return
  122. }
  123. }
  124. if hasColon {
  125. id, err = zerotier.NewIdentityFromString(s)
  126. if err == nil {
  127. a = id.Address()
  128. fp = id.Fingerprint()
  129. return
  130. }
  131. }
  132. a = zerotier.Address(0)
  133. return
  134. }
  135. func readIdentity(s string) *zerotier.Identity {
  136. if strings.ContainsRune(s, ':') {
  137. id, _ := zerotier.NewIdentityFromString(s)
  138. if id != nil {
  139. return id
  140. }
  141. }
  142. idData, err := ioutil.ReadFile(s)
  143. if err != nil {
  144. fmt.Printf("FATAL: identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
  145. os.Exit(1)
  146. }
  147. id, err := zerotier.NewIdentityFromString(string(idData))
  148. if err != nil {
  149. fmt.Printf("FATAL: identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
  150. os.Exit(1)
  151. }
  152. return id
  153. }
  154. func readLocator(s string) *zerotier.Locator {
  155. if strings.ContainsRune(s, '@') {
  156. loc, _ := zerotier.NewLocatorFromString(s)
  157. if loc != nil {
  158. return loc
  159. }
  160. }
  161. locData, err := ioutil.ReadFile(s)
  162. if err != nil {
  163. fmt.Printf("FATAL: locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
  164. os.Exit(1)
  165. }
  166. loc, err := zerotier.NewLocatorFromString(string(locData))
  167. if err != nil {
  168. fmt.Printf("FATAL: locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
  169. os.Exit(1)
  170. }
  171. return loc
  172. }
  173. func networkStatusStr(status int) string {
  174. switch status {
  175. case zerotier.NetworkStatusNotFound:
  176. return "not-found"
  177. case zerotier.NetworkStatusAccessDenied:
  178. return "access-denied"
  179. case zerotier.NetworkStatusRequestingConfiguration:
  180. return "updating"
  181. case zerotier.NetworkStatusOK:
  182. return "ok"
  183. }
  184. return "???"
  185. }