locator.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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: 2025-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. "fmt"
  16. "zerotier/pkg/zerotier"
  17. )
  18. func Locator(args []string) int {
  19. if len(args) < 1 {
  20. Help()
  21. return 1
  22. }
  23. switch args[0] {
  24. case "new":
  25. if len(args) < 3 {
  26. Help()
  27. return 1
  28. }
  29. id := cliGetIdentityOrFatal(args[1])
  30. if !id.HasPrivate() {
  31. pErr("identity must include secret key to sign locator")
  32. return 1
  33. }
  34. var eps []*zerotier.Endpoint
  35. for i:=2;i<len(args);i++ {
  36. ep, err := zerotier.NewEndpointFromString(args[i])
  37. if err != nil {
  38. pErr("invalid endpoint: %s (%s)", args[i], err.Error())
  39. return 1
  40. }
  41. eps = append(eps, ep)
  42. }
  43. loc, err := zerotier.NewLocator(zerotier.TimeMs(), eps, id)
  44. if err != nil {
  45. pErr("error creating or signing locator: %s", err.Error())
  46. return 1
  47. }
  48. fmt.Println(loc.String())
  49. case "verify":
  50. if len(args) != 3 {
  51. Help()
  52. return 1
  53. }
  54. id := cliGetIdentityOrFatal(args[1])
  55. loc := cliGetLocatorOrFatal(args[2])
  56. if !loc.Validate(id) {
  57. fmt.Println("FAILED")
  58. return 1
  59. }
  60. fmt.Println("OK")
  61. case "show":
  62. if len(args) != 2 {
  63. Help()
  64. return 1
  65. }
  66. loc := cliGetLocatorOrFatal(args[1])
  67. fmt.Printf("%s fingerprint %s\n",loc.Fingerprint.Address.String(),loc.Fingerprint.String())
  68. for _, e := range loc.Endpoints {
  69. fmt.Printf("\tendpoint %s type %s\n",e.String(),e.TypeString())
  70. }
  71. }
  72. return 0
  73. }