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