service.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 cliNameAddress(c *cli.Context) (name, address string, err error) {
  23. name = c.Args().Get(0)
  24. address = 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 address == "" && c.String("address") == "" {
  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("address") != "" {
  37. address = c.String("address")
  38. }
  39. return name, address, nil
  40. }
  41. func ServiceAdd() *cli.Command {
  42. return &cli.Command{
  43. Name: "service-add",
  44. Aliases: []string{"sa"},
  45. Usage: "Expose a service to the network without creating a VPN",
  46. Description: `Expose a local or a remote endpoint connection as a service in the VPN.
  47. The host will act as a proxy between the service and the connection`,
  48. UsageText: "edgevpn service-add unique-id ip:port",
  49. Flags: append(CommonFlags,
  50. &cli.StringFlag{
  51. Name: "name",
  52. Usage: `Unique name of the service to be server over the network.`,
  53. },
  54. &cli.StringFlag{
  55. Name: "address",
  56. Usage: `Remote address that the service is running to. That can be a remote webserver, a local SSH server, etc.
  57. For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
  58. },
  59. ),
  60. Action: func(c *cli.Context) error {
  61. name, address, err := cliNameAddress(c)
  62. if err != nil {
  63. return err
  64. }
  65. o, _, ll := cliToOpts(c)
  66. // Needed to unblock connections with low activity
  67. o = append(o,
  68. services.Alive(
  69. time.Duration(c.Int("aliveness-healthcheck-interval"))*time.Second,
  70. time.Duration(c.Int("aliveness-healthcheck-scrub-interval"))*time.Second,
  71. time.Duration(c.Int("aliveness-healthcheck-max-interval"))*time.Second)...)
  72. o = append(o, services.RegisterService(ll, time.Duration(c.Int("ledger-announce-interval"))*time.Second, name, address)...)
  73. e, err := node.New(o...)
  74. if err != nil {
  75. return err
  76. }
  77. displayStart(ll)
  78. go handleStopSignals()
  79. // Join the node to the network, using our ledger
  80. if err := e.Start(context.Background()); err != nil {
  81. return err
  82. }
  83. for {
  84. time.Sleep(2 * time.Second)
  85. }
  86. },
  87. }
  88. }
  89. func ServiceConnect() *cli.Command {
  90. return &cli.Command{
  91. Aliases: []string{"sc"},
  92. Usage: "Connects to a service in the network without creating a VPN",
  93. Name: "service-connect",
  94. Description: `Bind a local port to connect to a remote service in the network.
  95. Creates a local listener which connects over the service in the network without creating a VPN.
  96. `,
  97. UsageText: "edgevpn service-connect unique-id (ip):port",
  98. Flags: append(CommonFlags,
  99. &cli.StringFlag{
  100. Name: "name",
  101. Usage: `Unique name of the service in the network.`,
  102. },
  103. &cli.StringFlag{
  104. Name: "address",
  105. Usage: `Address where to bind locally. E.g. ':8080'. A proxy will be created
  106. to the service over the network`,
  107. },
  108. ),
  109. Action: func(c *cli.Context) error {
  110. name, address, err := cliNameAddress(c)
  111. if err != nil {
  112. return err
  113. }
  114. o, _, ll := cliToOpts(c)
  115. // Needed to unblock connections with low activity
  116. o = append(o,
  117. services.Alive(
  118. time.Duration(c.Int("aliveness-healthcheck-interval"))*time.Second,
  119. time.Duration(c.Int("aliveness-healthcheck-scrub-interval"))*time.Second,
  120. time.Duration(c.Int("aliveness-healthcheck-max-interval"))*time.Second)...)
  121. e, err := node.New(
  122. append(o,
  123. node.WithNetworkService(
  124. services.ConnectNetworkService(
  125. time.Duration(c.Int("ledger-announce-interval"))*time.Second,
  126. name,
  127. address,
  128. ),
  129. ),
  130. )...,
  131. )
  132. if err != nil {
  133. return err
  134. }
  135. displayStart(ll)
  136. go handleStopSignals()
  137. // starts the node
  138. return e.Start(context.Background())
  139. },
  140. }
  141. }