join.go 2.0 KB

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