join.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "flag"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "zerotier/pkg/zerotier"
  21. )
  22. func Join(basePath, authToken string, args []string) int {
  23. joinOpts := flag.NewFlagSet("join", flag.ContinueOnError)
  24. controllerAuthToken := joinOpts.String("a", "", "")
  25. controllerFingerprint := joinOpts.String("c", "", "")
  26. err := joinOpts.Parse(os.Args[1:])
  27. if err != nil {
  28. Help()
  29. return 1
  30. }
  31. args = joinOpts.Args()
  32. if len(args) < 1 {
  33. Help()
  34. return 1
  35. }
  36. if len(args[0]) != zerotier.NetworkIDStringLength {
  37. fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
  38. return 1
  39. }
  40. _ = *controllerAuthToken // TODO: not implemented yet
  41. var fp *zerotier.Fingerprint
  42. if len(*controllerFingerprint) > 0 {
  43. if strings.ContainsRune(*controllerFingerprint, '-') {
  44. fp, err = zerotier.NewFingerprintFromString(*controllerFingerprint)
  45. if err != nil {
  46. fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", *controllerFingerprint)
  47. return 1
  48. }
  49. } else {
  50. id, err := zerotier.NewIdentityFromString(*controllerFingerprint)
  51. if err != nil {
  52. fmt.Printf("ERROR: invalid network controller identity: %s\n", *controllerFingerprint)
  53. return 1
  54. }
  55. fp = id.Fingerprint()
  56. }
  57. }
  58. nwid, err := strconv.ParseUint(args[0], 16, 64)
  59. if err != nil {
  60. fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
  61. return 1
  62. }
  63. nwids := fmt.Sprintf("%.16x", nwid)
  64. var network zerotier.APINetwork
  65. network.ID = zerotier.NetworkID(nwid)
  66. network.ControllerFingerprint = fp
  67. if apiPost(basePath, authToken, "/network/"+nwids, &network, nil) <= 0 {
  68. fmt.Printf("FAILED\n")
  69. } else {
  70. if fp == nil {
  71. fmt.Printf("OK %s\n", nwids)
  72. } else {
  73. fmt.Printf("OK %s %s\n", nwids, fp.String())
  74. }
  75. }
  76. return 0
  77. }