file.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "context"
  16. "errors"
  17. "time"
  18. "github.com/mudler/edgevpn/pkg/node"
  19. "github.com/mudler/edgevpn/pkg/services"
  20. "github.com/urfave/cli/v2"
  21. )
  22. func cliNamePath(c *cli.Context) (name, path string, err error) {
  23. name = c.Args().Get(0)
  24. path = c.Args().Get(1)
  25. if name == "" && c.String("name") == "" {
  26. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  27. return
  28. }
  29. if path == "" && c.String("path") == "" {
  30. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  31. return
  32. }
  33. if c.String("name") != "" {
  34. name = c.String("name")
  35. }
  36. if c.String("path") != "" {
  37. path = c.String("path")
  38. }
  39. return name, path, nil
  40. }
  41. func FileSend() *cli.Command {
  42. return &cli.Command{
  43. Name: "file-send",
  44. Aliases: []string{"fs"},
  45. Usage: "Serve a file to the network",
  46. Description: `Serve a file to the network without connecting over VPN`,
  47. UsageText: "edgevpn file-send unique-id /src/path",
  48. Flags: append(CommonFlags,
  49. &cli.StringFlag{
  50. Name: "name",
  51. Required: true,
  52. Usage: `Unique name of the file to be served over the network.
  53. This is also the ID used to refer when receiving it.`,
  54. },
  55. &cli.StringFlag{
  56. Name: "path",
  57. Usage: `File to serve`,
  58. Required: true,
  59. },
  60. ),
  61. Action: func(c *cli.Context) error {
  62. name, path, err := cliNamePath(c)
  63. if err != nil {
  64. return err
  65. }
  66. o, _, ll := cliToOpts(c)
  67. // Needed to unblock connections with low activity
  68. o = append(o,
  69. services.Alive(
  70. time.Duration(c.Int("aliveness-healthcheck-interval"))*time.Second,
  71. time.Duration(c.Int("aliveness-healthcheck-scrub-interval"))*time.Second,
  72. time.Duration(c.Int("aliveness-healthcheck-max-interval"))*time.Second)...)
  73. opts, err := services.ShareFile(ll, time.Duration(c.Int("ledger-announce-interval"))*time.Second, name, path)
  74. if err != nil {
  75. return err
  76. }
  77. o = append(o, opts...)
  78. e, err := node.New(o...)
  79. if err != nil {
  80. return err
  81. }
  82. displayStart(ll)
  83. go handleStopSignals()
  84. // Start the node to the network, using our ledger
  85. if err := e.Start(context.Background()); err != nil {
  86. return err
  87. }
  88. for {
  89. time.Sleep(2 * time.Second)
  90. }
  91. },
  92. }
  93. }
  94. func FileReceive() *cli.Command {
  95. return &cli.Command{
  96. Name: "file-receive",
  97. Aliases: []string{"fr"},
  98. Usage: "Receive a file which is served from the network",
  99. Description: `Receive a file from the network without connecting over VPN`,
  100. UsageText: "edgevpn file-receive unique-id /dst/path",
  101. Flags: append(CommonFlags,
  102. &cli.StringFlag{
  103. Name: "name",
  104. Usage: `Unique name of the file to be received over the network.`,
  105. },
  106. &cli.StringFlag{
  107. Name: "path",
  108. Usage: `Destination where to save the file`,
  109. },
  110. ),
  111. Action: func(c *cli.Context) error {
  112. name, path, err := cliNamePath(c)
  113. if err != nil {
  114. return err
  115. }
  116. o, _, ll := cliToOpts(c)
  117. // Needed to unblock connections with low activity
  118. o = append(o,
  119. services.Alive(
  120. time.Duration(c.Int("aliveness-healthcheck-interval"))*time.Second,
  121. time.Duration(c.Int("aliveness-healthcheck-scrub-interval"))*time.Second,
  122. time.Duration(c.Int("aliveness-healthcheck-max-interval"))*time.Second)...)
  123. e, err := node.New(o...)
  124. if err != nil {
  125. return err
  126. }
  127. displayStart(ll)
  128. go handleStopSignals()
  129. // Start the node to the network, using our ledger
  130. if err := e.Start(context.Background()); err != nil {
  131. return err
  132. }
  133. ledger, _ := e.Ledger()
  134. return services.ReceiveFile(context.Background(), ledger, e, ll, time.Duration(c.Int("ledger-announce-interval"))*time.Second, name, path)
  135. },
  136. }
  137. }