service.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package cmd
  2. import (
  3. "errors"
  4. "github.com/mudler/edgevpn/pkg/edgevpn"
  5. "github.com/urfave/cli"
  6. )
  7. func cliNameAddress(c *cli.Context) (name, address string, err error) {
  8. name = c.Args().Get(0)
  9. address = c.Args().Get(1)
  10. if name == "" && c.String("name") == "" {
  11. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  12. return
  13. }
  14. if address == "" && c.String("address") == "" {
  15. err = errors.New("Either a file UUID as first argument or with --name needs to be provided")
  16. return
  17. }
  18. if c.String("name") != "" {
  19. name = c.String("name")
  20. }
  21. if c.String("address") != "" {
  22. address = c.String("address")
  23. }
  24. return name, address, nil
  25. }
  26. func ServiceAdd() cli.Command {
  27. return cli.Command{
  28. Name: "service-add",
  29. Aliases: []string{"sa"},
  30. Usage: "Expose a service to the network without creating a VPN",
  31. Description: `Expose a local or a remote endpoint connection as a service in the VPN.
  32. The host will act as a proxy between the service and the connection`,
  33. UsageText: "edgevpn service-add unique-id ip:port",
  34. Flags: append(CommonFlags,
  35. cli.StringFlag{
  36. Name: "virtual-ip",
  37. Usage: `VirtualIP for the service inside the VPN network. For example 10.1.0.10:90`,
  38. },
  39. cli.StringFlag{
  40. Name: "name",
  41. Usage: `Unique name of the service to be server over the network.`,
  42. },
  43. cli.StringFlag{
  44. Name: "address",
  45. Usage: `Remote address that the service is running to. That can be a remote webserver, a local SSH server, etc.
  46. For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
  47. },
  48. ),
  49. Action: func(c *cli.Context) error {
  50. name, address, err := cliNameAddress(c)
  51. if err != nil {
  52. return err
  53. }
  54. e := edgevpn.New(cliToOpts(c)...)
  55. displayStart(e)
  56. ledger, err := e.Ledger()
  57. if err != nil {
  58. return err
  59. }
  60. // Join the node to the network, using our ledger
  61. e.ExposeService(ledger, name, c.String("virtual-ip"), address)
  62. // Join the node to the network, using our ledger
  63. if err := e.Join(); err != nil {
  64. return err
  65. }
  66. for {
  67. }
  68. },
  69. }
  70. }
  71. func ServiceConnect() cli.Command {
  72. return cli.Command{
  73. Aliases: []string{"sc"},
  74. Usage: "Connects to a service in the network without creating a VPN",
  75. Name: "service-connect",
  76. Description: `Bind a local port to connect to a remote service in the network.
  77. Creates a local listener which connects over the service in the network without creating a VPN.
  78. `,
  79. UsageText: "edgevpn service-connect unique-id (ip):port",
  80. Flags: append(CommonFlags,
  81. cli.StringFlag{
  82. Name: "name",
  83. Usage: `Unique name of the service in the network.`,
  84. },
  85. cli.StringFlag{
  86. Name: "address",
  87. Usage: `Address where to bind locally. E.g. ':8080'. A proxy will be created
  88. to the service over the network`,
  89. },
  90. ),
  91. Action: func(c *cli.Context) error {
  92. name, address, err := cliNameAddress(c)
  93. if err != nil {
  94. return err
  95. }
  96. e := edgevpn.New(cliToOpts(c)...)
  97. displayStart(e)
  98. // Join the node to the network, using our ledger
  99. if err := e.Join(); err != nil {
  100. return err
  101. }
  102. ledger, _ := e.Ledger()
  103. return e.ConnectToService(ledger, name, address)
  104. },
  105. }
  106. }