locator.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "os"
  19. "strings"
  20. "zerotier/pkg/zerotier"
  21. )
  22. func locatorNew(args []string) {
  23. if len(args) < 2 {
  24. Help()
  25. os.Exit(1)
  26. }
  27. identity := readIdentity(args[0])
  28. if !identity.HasPrivate() {
  29. fmt.Println("FATAL: identity does not contain a secret key (required to sign locator)")
  30. os.Exit(1)
  31. }
  32. var virt []*zerotier.Identity
  33. var phys []*zerotier.InetAddress
  34. for i := 1; i < len(args); i++ {
  35. if strings.Contains(args[i], "/") {
  36. a := zerotier.NewInetAddressFromString(args[i])
  37. if a == nil {
  38. fmt.Printf("FATAL: IP/port address '%s' is not valid\n", args[i])
  39. os.Exit(1)
  40. }
  41. phys = append(phys, a)
  42. } else {
  43. a, err := zerotier.NewIdentityFromString(args[i])
  44. if err != nil {
  45. fmt.Printf("FATAL: identity (virtual address) '%s' is not valid: %s\n", args[i], err.Error())
  46. os.Exit(1)
  47. }
  48. virt = append(virt, a)
  49. }
  50. }
  51. loc, err := zerotier.NewLocator(identity, virt, phys)
  52. if err != nil {
  53. fmt.Printf("FATAL: internal error creating locator: %s\n", err.Error())
  54. os.Exit(1)
  55. }
  56. fmt.Println(jsonDump(loc))
  57. os.Exit(0)
  58. }
  59. func locatorNewDNSKey(args []string) {
  60. if len(args) != 0 {
  61. Help()
  62. os.Exit(0)
  63. }
  64. sk, err := zerotier.NewLocatorDNSSigningKey()
  65. if err != nil {
  66. fmt.Printf("FATAL: error creating secure DNS signing key: %s", err.Error())
  67. os.Exit(1)
  68. }
  69. fmt.Println(jsonDump(sk))
  70. os.Exit(0)
  71. }
  72. func locatorGetDNS(args []string) {
  73. if len(args) < 2 {
  74. Help()
  75. os.Exit(1)
  76. }
  77. keyData, err := ioutil.ReadFile(args[0])
  78. if err != nil {
  79. fmt.Printf("FATAL: unable to read secure DNS key file: %s\n", err.Error())
  80. os.Exit(1)
  81. }
  82. var sk zerotier.LocatorDNSSigningKey
  83. err = json.Unmarshal(keyData, &sk)
  84. if err != nil {
  85. fmt.Printf("FATAL: DNS key file invalid: %s", err.Error())
  86. os.Exit(1)
  87. }
  88. locData, err := ioutil.ReadFile(args[1])
  89. if err != nil {
  90. fmt.Printf("FATAL: unable to read locator: %s\n", err.Error())
  91. os.Exit(1)
  92. }
  93. var loc zerotier.Locator
  94. err = json.Unmarshal(locData, &loc)
  95. if err != nil {
  96. fmt.Printf("FATAL: locator invalid: %s", err.Error())
  97. os.Exit(1)
  98. }
  99. txt, err := loc.MakeTXTRecords(&sk)
  100. if err != nil {
  101. fmt.Printf("FATAL: error creating TXT records: %s\n", err.Error())
  102. os.Exit(1)
  103. }
  104. for _, t := range txt {
  105. fmt.Println(t)
  106. }
  107. os.Exit(0)
  108. }
  109. // Locator CLI command
  110. func Locator(args []string) {
  111. if len(args) > 0 {
  112. switch args[0] {
  113. case "new":
  114. locatorNew(args[1:])
  115. case "newdnskey":
  116. locatorNewDNSKey(args[1:])
  117. case "getdns":
  118. locatorGetDNS(args[1:])
  119. }
  120. }
  121. Help()
  122. os.Exit(1)
  123. }