misc.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 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{}) {
  24. statusCode, 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
  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
  37. }
  38. }
  39. func apiPost(basePath, authToken, urlPath string, post, result interface{}) {
  40. statusCode, err := zerotier.APIPost(basePath, zerotier.APISocketName, authToken, urlPath, post, result)
  41. if err != nil {
  42. fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
  43. os.Exit(1)
  44. return
  45. }
  46. if statusCode != http.StatusOK {
  47. if statusCode == http.StatusUnauthorized {
  48. fmt.Printf("FATAL: API response code %d: unauthorized (authorization token incorrect)\n", statusCode)
  49. }
  50. fmt.Printf("FATAL: API response code %d\n", statusCode)
  51. os.Exit(1)
  52. return
  53. }
  54. }
  55. func apiDelete(basePath, authToken, urlPath string, result interface{}) {
  56. statusCode, err := zerotier.APIDelete(basePath, zerotier.APISocketName, authToken, urlPath, result)
  57. if err != nil {
  58. fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
  59. os.Exit(1)
  60. return
  61. }
  62. if statusCode != http.StatusOK {
  63. if statusCode == http.StatusUnauthorized {
  64. fmt.Printf("FATAL: API response code %d: unauthorized (authorization token incorrect)\n", statusCode)
  65. }
  66. fmt.Printf("FATAL: API response code %d\n", statusCode)
  67. os.Exit(1)
  68. return
  69. }
  70. }
  71. func enabledDisabled(f bool) string {
  72. if f {
  73. return "ENABLED"
  74. }
  75. return "DISABLED"
  76. }
  77. func jsonDump(obj interface{}) string {
  78. j, _ := json.MarshalIndent(obj, "", " ")
  79. return string(j)
  80. }
  81. func readIdentity(s string) *zerotier.Identity {
  82. if strings.ContainsRune(s, ':') {
  83. id, _ := zerotier.NewIdentityFromString(s)
  84. if id != nil {
  85. return id
  86. }
  87. }
  88. idData, err := ioutil.ReadFile(s)
  89. if err != nil {
  90. fmt.Printf("FATAL: identity '%s' cannot be resolved as file or literal identity: %s", s, err.Error())
  91. os.Exit(1)
  92. }
  93. id, err := zerotier.NewIdentityFromString(string(idData))
  94. if err != nil {
  95. fmt.Printf("FATAL: identity '%s' cannot be resolved as file or literal identity: %s", s, err.Error())
  96. os.Exit(1)
  97. }
  98. return id
  99. }
  100. func networkStatusStr(status int) string {
  101. switch status {
  102. case zerotier.NetworkStatusNotFound:
  103. return "NOTFOUND"
  104. case zerotier.NetworkStatusAccessDenied:
  105. return "DENIED"
  106. case zerotier.NetworkStatusRequestConfiguration:
  107. return "UPDATING"
  108. case zerotier.NetworkStatusOK:
  109. return "OK"
  110. }
  111. return "???"
  112. }