proxy.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. "time"
  19. "github.com/mudler/edgevpn/api"
  20. "github.com/mudler/edgevpn/pkg/node"
  21. "github.com/mudler/edgevpn/pkg/services"
  22. "github.com/urfave/cli"
  23. )
  24. func Proxy() cli.Command {
  25. return cli.Command{
  26. Name: "proxy",
  27. Usage: "Starts a local http proxy server to egress nodes",
  28. Description: `Start a proxy locally, providing an ingress point for the network.`,
  29. UsageText: "edgevpn proxy",
  30. Flags: append(CommonFlags,
  31. &cli.StringFlag{
  32. Name: "listen",
  33. Value: ":8080",
  34. Usage: "Listening address",
  35. EnvVar: "PROXYLISTEN",
  36. },
  37. &cli.BoolFlag{
  38. Name: "debug",
  39. },
  40. &cli.IntFlag{
  41. Name: "interval",
  42. Usage: "proxy announce time interval",
  43. EnvVar: "PROXYINTERVAL",
  44. Value: 120,
  45. },
  46. &cli.IntFlag{
  47. Name: "dead-interval",
  48. Usage: "interval (in seconds) wether detect egress nodes offline",
  49. EnvVar: "PROXYDEADINTERVAL",
  50. Value: 600,
  51. },
  52. ),
  53. Action: func(c *cli.Context) error {
  54. o, _, ll := cliToOpts(c)
  55. o = append(o, services.Proxy(
  56. time.Duration(c.Int("interval"))*time.Second,
  57. time.Duration(c.Int("dead-interval"))*time.Second,
  58. c.String("listen"))...)
  59. e, err := node.New(o...)
  60. if err != nil {
  61. return err
  62. }
  63. displayStart(ll)
  64. ctx := context.Background()
  65. // Start the node to the network, using our ledger
  66. if err := e.Start(ctx); err != nil {
  67. return err
  68. }
  69. return api.API(ctx, c.String("listen"), 5*time.Second, 20*time.Second, e, c.Bool("debug"))
  70. },
  71. }
  72. }