service.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package cmd
  16. import (
  17. "context"
  18. "errors"
  19. "time"
  20. "github.com/mudler/edgevpn/pkg/node"
  21. "github.com/mudler/edgevpn/pkg/services"
  22. "github.com/urfave/cli"
  23. )
  24. func cliNameAddress(c *cli.Context) (name, address string, err error) {
  25. name = c.Args().Get(0)
  26. address = c.Args().Get(1)
  27. if name == "" && c.String("name") == "" {
  28. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  29. return
  30. }
  31. if address == "" && c.String("address") == "" {
  32. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  33. return
  34. }
  35. if c.String("name") != "" {
  36. name = c.String("name")
  37. }
  38. if c.String("address") != "" {
  39. address = c.String("address")
  40. }
  41. return name, address, nil
  42. }
  43. func ServiceAdd() cli.Command {
  44. return cli.Command{
  45. Name: "service-add",
  46. Aliases: []string{"sa"},
  47. Usage: "Expose a service to the network without creating a VPN",
  48. Description: `Expose a local or a remote endpoint connection as a service in the VPN.
  49. The host will act as a proxy between the service and the connection`,
  50. UsageText: "edgevpn service-add unique-id ip:port",
  51. Flags: append(CommonFlags,
  52. cli.StringFlag{
  53. Name: "name",
  54. Usage: `Unique name of the service to be server over the network.`,
  55. },
  56. cli.StringFlag{
  57. Name: "address",
  58. Usage: `Remote address that the service is running to. That can be a remote webserver, a local SSH server, etc.
  59. For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
  60. },
  61. ),
  62. Action: func(c *cli.Context) error {
  63. name, address, err := cliNameAddress(c)
  64. if err != nil {
  65. return err
  66. }
  67. o, _, ll := cliToOpts(c)
  68. o = append(o, services.RegisterService(ll, time.Duration(c.Int("ledger-announce-interval"))*time.Second, name, address)...)
  69. e, err := node.New(o...)
  70. if err != nil {
  71. return err
  72. }
  73. displayStart(ll)
  74. // Join the node to the network, using our ledger
  75. if err := e.Start(context.Background()); err != nil {
  76. return err
  77. }
  78. for {
  79. time.Sleep(2 * time.Second)
  80. }
  81. },
  82. }
  83. }
  84. func ServiceConnect() cli.Command {
  85. return cli.Command{
  86. Aliases: []string{"sc"},
  87. Usage: "Connects to a service in the network without creating a VPN",
  88. Name: "service-connect",
  89. Description: `Bind a local port to connect to a remote service in the network.
  90. Creates a local listener which connects over the service in the network without creating a VPN.
  91. `,
  92. UsageText: "edgevpn service-connect unique-id (ip):port",
  93. Flags: append(CommonFlags,
  94. cli.StringFlag{
  95. Name: "name",
  96. Usage: `Unique name of the service in the network.`,
  97. },
  98. cli.StringFlag{
  99. Name: "address",
  100. Usage: `Address where to bind locally. E.g. ':8080'. A proxy will be created
  101. to the service over the network`,
  102. },
  103. ),
  104. Action: func(c *cli.Context) error {
  105. name, address, err := cliNameAddress(c)
  106. if err != nil {
  107. return err
  108. }
  109. o, _, ll := cliToOpts(c)
  110. e, err := node.New(
  111. append(o,
  112. node.WithNetworkService(
  113. services.ConnectNetworkService(
  114. time.Duration(c.Int("ledger-announce-interval"))*time.Second,
  115. name,
  116. address,
  117. ),
  118. ),
  119. )...,
  120. )
  121. if err != nil {
  122. return err
  123. }
  124. displayStart(ll)
  125. // starts the node
  126. return e.Start(context.Background())
  127. },
  128. }
  129. }